knex.js

Cloud SQL instance connection working locally, but not on App Engine

眉间皱痕 提交于 2019-12-01 08:11:52
I am trying to host an API implemented as a Node.js app on Google Cloud App Engine. The database that the API utilizes is a PostgreSQL 9.6 database which is hosted with a Cloud SQL instance. The database is connected to the Node.js API through Knex . When hosted on App Engine, the API endpoints that do not require any contact with the database work fine. However, when the database needs to be contacted to complete the API call, the following error appears in the logs: Unhandled rejection TimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a

Order Bookshelf.js fetch by related column value

左心房为你撑大大i 提交于 2019-11-30 16:15:42
问题 I'm using Bookshelf.js/Knex.js, fetching a model (call it user) with a related child model (call it company). Can I order by a field on the child model - company.name ? Also, if that's possible, can I multi sort, say company.name descending then lastName ascending Here's my current code, which only works on root model fields. qb.orderBy('company.name', 'desc') doesn't work. users.query(function(qb) { qb.orderBy('lastName', 'asc'); }) .fetch({withRelated: ['company']}) .then(success, error);

Order Bookshelf.js fetch by related column value

只愿长相守 提交于 2019-11-30 16:06:47
I'm using Bookshelf.js/Knex.js, fetching a model (call it user) with a related child model (call it company). Can I order by a field on the child model - company.name ? Also, if that's possible, can I multi sort, say company.name descending then lastName ascending Here's my current code, which only works on root model fields. qb.orderBy('company.name', 'desc') doesn't work. users.query(function(qb) { qb.orderBy('lastName', 'asc'); }) .fetch({withRelated: ['company']}) .then(success, error); Try the following: users .fetch({withRelated: [ { 'company': function(qb) { qb.orderBy("name"); } } ]})

Commit/rollback a knex transaction using async/await

独自空忆成欢 提交于 2019-11-30 14:09:36
I'm test driving the ES7 async/await proposal using this module to emulate it. I'm trying to make knex.js transactions play well with them, as a starting point. Example code: async function transaction() { return new Promise(function(resolve, reject){ knex.transaction(function(err, result){ if (err) { reject(err); } else { resolve(result); } }); }); } // Start transaction from this call insert: async (function(db, data) { const trx = await(transaction()); const idUser = await(user.insertData(trx, data)); return { idCidUserstomer: idUser } }) How can I commit() or rollback() if a transaction

Can I conditionally add a where() clause to my knex query?

最后都变了- 提交于 2019-11-30 11:20:03
问题 I want to add a where() clause in my query, but conditionally . Specifically, I want it added only if a sepecific querystring parameter is passed in the URL. Is this possible, and if so, how would I go about doing it? router.get('/questions', function (req, res) { knex('questions') .select('question', 'correct', 'incorrect') .limit(50) .where('somecolumn', req.query.param) // <-- only if param exists .then(function (results) { res.send(results); }); }); 回答1: You can store your query in a

Can I conditionally add a where() clause to my knex query?

╄→гoц情女王★ 提交于 2019-11-30 02:58:32
I want to add a where() clause in my query, but conditionally . Specifically, I want it added only if a sepecific querystring parameter is passed in the URL. Is this possible, and if so, how would I go about doing it? router.get('/questions', function (req, res) { knex('questions') .select('question', 'correct', 'incorrect') .limit(50) .where('somecolumn', req.query.param) // <-- only if param exists .then(function (results) { res.send(results); }); }); You can store your query in a variable, apply your conditional where clause and then execute it, like this : router.get('/questions', function

Commit/rollback a knex transaction using async/await

孤者浪人 提交于 2019-11-29 20:27:01
问题 I'm test driving the ES7 async/await proposal using this module to emulate it. I'm trying to make knex.js transactions play well with them, as a starting point. Example code: async function transaction() { return new Promise(function(resolve, reject){ knex.transaction(function(err, result){ if (err) { reject(err); } else { resolve(result); } }); }); } // Start transaction from this call insert: async (function(db, data) { const trx = await(transaction()); const idUser = await(user.insertData

Batch update in knex

巧了我就是萌 提交于 2019-11-29 10:05:55
I'd like to perform a batch update using Knex.js For example: 'UPDATE foo SET [theValues] WHERE idFoo = 1' 'UPDATE foo SET [theValues] WHERE idFoo = 2' with values: { name: "FooName1", checked: true } // to `idFoo = 1` { name: "FooName2", checked: false } // to `idFoo = 2` I was using node-mysql previously, which allowed multiple-statements. While using that I simply built a mulitple-statement query string and just send that through the wire in a single run. I'm not sure how to achieve the same with Knex. I can see batchInsert as an API method I can use, but nothing as far as batchUpdate is

Unit testing with Bookshelf.js and knex.js

余生颓废 提交于 2019-11-29 00:12:51
问题 I'm relatively new to Node and am working on a project using knex and bookshelf. I'm having a little bit of trouble unit testing my code and I'm not sure what I'm doing wrong. Basically I have a model (called VorcuProduct) that looks like this: var VorcuProduct = bs.Model.extend({ tableName: 'vorcu_products' }); module.exports.VorcuProduct = VorcuProduct And a function that saves a VorcuProduct if it does not exist on the DB. Quite simple. The function doing this looks like this: function

Get Knex.js transactions working with ES7 async/await

别来无恙 提交于 2019-11-28 21:24:14
I'm trying to couple ES7's async/await with knex.js transactions. Although I can easily play around with non-transactional code, I'm struggling to get transactions working properly using the aforementioned async/await structure. I'm using this module to simulate async/await Here's what I currently have: Non-transactional version: works fine but is not transactional app.js // assume `db` is a knex instance app.post("/user", async((req, res) => { const data = { idUser: 1, name: "FooBar" } try { const result = await(user.insert(db, data)); res.json(result); } catch (err) { res.status(500).json