bookshelf.js

Proper way to define two way relations between two tables

家住魔仙堡 提交于 2019-12-11 23:37:41
问题 I have three tables: Employee (emp_id,name) Projects (pid,proj_status) Project_Allocation (pid,emp_id) To find the people belonging to my project: 1. I have my name, I query my emp_id. 2. Using my emp_id, I query (withRelated) for all my projects from Project_Allocation (completed ones as well) with a belongsTo relation defined on Projects 3. Then I pick the pid of the project having status as 'LIVE' 4. Now I pick all the models having this pid in Project_allocation. The issue I'm

Function is undefined, Bookshelf.js model function is not being recognized as a function

寵の児 提交于 2019-12-11 04:07:07
问题 I am using Bookshelf.js to handle a user registration API end-point, designed with NodeJS and ExpressJS. But upon POSTing to the register url, I keep hitting an error at one of the User model functions. Here is routes/index.js var User = require(./models/User); router.post('/register', function(req, res, next){ if(!req.body.username || !req.body.password){ return res.status(400).json({message: 'Please fill out all fields'}); } try { var hash = User.createPassword(req.body.password); console

How to fix nested structure in bookshelfjs transaction [duplicate]

a 夏天 提交于 2019-12-10 15:08:12
问题 This question already has answers here : How do I access previous promise results in a .then() chain? (17 answers) Closed 4 years ago . I want to update a number of database tables in a single bookshelf transaction. I could use some help refactoring my code. I'm new to node and don't have a good understanding of promises, but the nested structure below is not very pretty, and I'm hoping there is a cleaner way. Any help would be appreciated. function insertUser(user, cb) { bookshelf

Accessing nested relations in Bookshelf.js

谁说我不能喝 提交于 2019-12-10 09:51:59
问题 I guess you could say I'm building a reddit style app. So I have a topic, and that topic has comments, and those comments have parent comments, etc. Here is my Comment model: var Comment = bookshelf.Model.extend({ tableName: 'comments', topic: function() { return this.belongsTo(Topic, 'topic_id'); }, children: function() { return this.hasMany(Comment, 'parent_id') } }); And so in my .get('/topic') page, i load my comments like this: new Comment() .query({where: {topic_id: topic_id}}) .query(

perform a where in query in bookshelf.js

僤鯓⒐⒋嵵緔 提交于 2019-12-10 06:13:27
问题 I want to perform a WHERE - IN query/operation but normal where gives error. I want this select * from `calendar_event_rsvp` where `event_id` in ('1', '2', '3') But below code leads to select * from `calendar_event_rsvp` where `event_id in` = '1', '2', '3' Code CalendarEventRSVP.forge() .where({ "event_id": event_ids }) How do i do this in bookshelf.js 回答1: Try to add the operator: CalendarEventRSVP.forge() .where('event_id', 'in', event_ids) Or use knex's whereIn : CalendarEventRSVP.forge()

Sort Bookshelf.js results with .orderBy()

一笑奈何 提交于 2019-12-09 05:21:50
问题 I am working on a personal project to learn Node.js + express + Bookshelf.js. Where do I build queries? In particular, how do I simply set an 'ORDER BY' or 'WHERE' in the following code? var Accounts = require('../collections/accounts').collection; new Accounts().fetch({ withRelated: ['folders'] }).then(function(collection) { // process results }); I would like to learn Bookshelf.js because it seems to offer the features I am used to with Laravel's Elequent (PHP), such as polymorphic

app.use inside a promise (bookshelf.js & express-basic-auth)

廉价感情. 提交于 2019-12-08 19:16:29
Api.fetchAll({columns: ['username','password']}) .then(function(employee) { return employee.toJSON(); }) .then(function(employee){ app.use(basicAuth({ users: {employee} })); }); I need my middleware (app.use) to run before my node starts so it registers. It doesn't, so when I start my node, my basic auth never registers. I'm using express-basic-auth to do the basic authentication for my api, and bookshelf.js for retrieving the values in the database. Okay so here is how I solved it. async function runServerAuth (){ let employee = await Api.fetchAll({columns: ['username','password']}); employee

Select object created between two dates by Bookshelf, MySQL, Knex on node.js

喜你入骨 提交于 2019-12-08 03:29:55
问题 I need to get a list of an object created between two times; I am able to get data by writing SQL Query; But I need to write this query by Bookshelf; My simple query: router.route('/locations') // fetch all locations .get(function (req, res) { Locations.forge() .fetch() .then(function (collection) { res.json({error: false, data: collection.toJSON()}); }) .catch(function (err) { res.status(500).json({error: true, data: {message: err.message}}); }); }) how can I get location between two dates

Bookshelf.js set attribute not in database

ⅰ亾dé卋堺 提交于 2019-12-07 04:51:38
问题 I have a Bookshelf.js model. I want to be able to set and get attributes for this model that are not persistent in the database. For instance lets say I have a model that looks like this: var Domain = bookshelf.Model.extend({ tableName: 'domains', initialize: function() { this.on('creating', this.setDomainName); }, setDomainName: function() { this.set('name', getDomainFromUrl(this.url)); } }); With a schema that looks like this: knex.schema.createTable('domains', function (table) { table

Select object created between two dates by Bookshelf, MySQL, Knex on node.js

不羁的心 提交于 2019-12-07 03:51:27
I need to get a list of an object created between two times; I am able to get data by writing SQL Query; But I need to write this query by Bookshelf; My simple query: router.route('/locations') // fetch all locations .get(function (req, res) { Locations.forge() .fetch() .then(function (collection) { res.json({error: false, data: collection.toJSON()}); }) .catch(function (err) { res.status(500).json({error: true, data: {message: err.message}}); }); }) how can I get location between two dates like above query? Try adding a query() call to the chain, like Locations .forge() .query(function(qb) {