javascript - How to get bookshelf model from multiple tables at once -
i'm developing node.js web service looks auction. i'm using bookshelf.js models , mysql db. please consider following db structure:
users: id | login | password | users_roles: id | user_id | role_id | roles: id | name | auctions: id | user_id | rating_id | rating: id | speed | quality | communication |
currently have implemented user model follows:
var bookshelf = require('./bookshelf_model'), role = require("./role"); var user = bookshelf.model.extend({ tablename: 'users', role: function () { return this.belongstomany(role, 'users_roles', 'user_id', 'role_id'); } }); module.exports = user;
i can user details
user.forge({id: 1}).fetch().then( function(userdetails){ ....... } );
this returns users' details users table. user role have yet db call, like:
user.forge({id: 1}).role().fetch().then( function(userroledetails){ ..... } );
is there method user info @ once using bookshelf.js (or using anythig else)? great @ once:
user={ id:..., login:..., password:..., role_id:...., rating:/*avg rating user's auctions*/
}
try this:
return user.forge( { id: 1 } ) .fetch( { require: true, //throws error if not found withrelated: [ 'role' ] } ) .then( function( usermodel ) { return usermodel.tojson(); //don't need step, depends want next } )
something that. load user_roles
object user, role
property. documentation here
Comments
Post a Comment