bookshelf.js

How to add simple Where-clause with bookshelf-pagemaker

痴心易碎 提交于 2019-12-02 07:29:15
Using the bookshelf-pagemaker NodeJS Module: https://www.npmjs.com/package/bookshelf-pagemaker https://github.com/bhoriuchi/bookshelf-pagemaker .. I was able to get basic paginate working where it selects all rows from a table. But I am not seeing how to add a WHERE clause to the query. QUESTION: Can someone please share a small example of using bookshelf-pagemaker with search criteria - e.g. SELECT * FROM user WHERE id > 10 ? var Bookshelf = require('bookshelf').mysqlAuth; var pagemaker = require('bookshelf-pagemaker')(Bookshelf); var UserDAO = Bookshelf.Model.extend({ tableName: 'user' });

Tracking DB querying time - Bookshelf/knex

ぃ、小莉子 提交于 2019-12-01 12:38:35
I would like to monitor the time taken by a query on my API's db. I so created the following function, using bookshelf-signals, a Bookshelf plugin. : bookshelf.on('fetching', () => { server.app.fetching = new Date().valueOf(); }); bookshelf.on('counting', () => { server.app.fetching = new Date().valueOf(); }); bookshelf.on('fetched', () => { server.statsd.gauge('db_query', new Date().valueOf() - server.app.fetching); }); ... so that I can retrieve the time just before and just after a fetch/count; I did the same with deleting-deleted and saving-saved. What I think I fail to understand is when

make async call inside forEach

我怕爱的太早我们不能终老 提交于 2019-12-01 07:15:23
I am trying to iterate thru array of objects and add some stuff inside these objects using async function in Node.js. So far my code looks like: var channel = channels.related('channels'); channel.forEach(function (entry) { knex('albums') .select(knex.raw('count(id) as album_count')) .where('channel_id', entry.id) .then(function (terms) { var count = terms[0].album_count; entry.attributes["totalAlbums"] = count; }); }); //console.log("I want this to be printed once the foreach is finished"); //res.json({error: false, status: 200, data: channel}); How can I achieve such a thing in JavaScript?

How to resolve promises when using app with REPL

北城余情 提交于 2019-12-01 04:41:09
I've got a basic Node webserver (Koa.js + a ORM). I like to start it with a REPL meaning I can use my app like a CLI-tool. All my queries return Promises but I don't know how I can resolve them in the REPL. How can I resolve them? For example the following code (fetch() queries the database and returns a promise) gives only this output Promise {_bitField: 4325376, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined …} Transaction.where('reference', '1').fetch().then((res) => return res) Update: Node.js now does this by default and resolves promises Old answer: You can't resolve them

make async call inside forEach

让人想犯罪 __ 提交于 2019-12-01 04:30:28
问题 I am trying to iterate thru array of objects and add some stuff inside these objects using async function in Node.js. So far my code looks like: var channel = channels.related('channels'); channel.forEach(function (entry) { knex('albums') .select(knex.raw('count(id) as album_count')) .where('channel_id', entry.id) .then(function (terms) { var count = terms[0].album_count; entry.attributes["totalAlbums"] = count; }); }); //console.log("I want this to be printed once the foreach is finished");

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"); } } ]})

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