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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -