Tag: bookshelf.js

Bookshelf.js中的多重关系

我试图在node.js应用程序中使用bookshelf.js作为ORM到Postgresql来实现基于angular色的访问控制。 我有以下架构: USERS <- USERS_ROLES -> ROLES <- ROLES_RIGHTS -> RIGHTS 我希望能够获得用户拥有的所有权利。 在SQL中,我只是这样做: SELECT rights.* FROM rights INNER JOIN roles_rights ON roles_rights.right_id=rights.id INNER JOIN users_roles ON users_roles.role_id = roles_rights.role_id WHERE users_roles.user_id=1 GROUP BY rights.id 对于Bookshelf.js来说,这是一个相对较新的问题,但是在设置关系时遇到了一些麻烦。 这是我第一次尝试: const User = bookshelf.Model.extend({ tableName: 'users', roles: function() { return this.belongsToMany(Role, 'users_roles', 'user_id', 'role_id') }, rights: function() { return […]

使用Bookshelf.js执行更新后提取数据

我正在使用save()方法(Bookshelf.js)来更新PostgreSQL中的数据。 我想在更新后获取完整的模型(所有列),我该怎么做? 在我看来, save()方法只返回id和更新的列。 我可能会做错了,所以任何指导将不胜感激。 谢谢。

如何与Bookshelfjs保存1到多个关系

所以我试了这个 let Book = bookshelf.Model.extend({tableName: 'book'}) let User = bookshelf.Model.extend({ tableName: 'user', books: function() { return this.belongsToMany(Book) // i have table book_user with user_id and book_id fk to respective tables } }) return User 然后,我试图保存使用此片段 models.User.forge(myUserObject).save() .then(function (user) { return user.books().attach(myBookArray) }) 但它没有工作,因为它期望的关系对象(表book_user ) 我该如何解决?

model.fetch与相关模型bookhelfjs

我有以下模型 company.js var Company = DB.Model.extend({ tableName: 'company', hasTimestamps: true, hasTimestamps: ['created_at', 'updated_at'] }); user.js的 var User = DB.Model.extend({ tableName: 'user', hasTimestamps: true, hasTimestamps: ['created_at', 'updated_at'], companies: function() { return this.belongsToMany(Company); } }); Company和User之间通过数据库中的下表进行many-to-many关系。 user_company.js var UserCompany = DB.Model.extend({ tableName: 'user_company', hasTimestamps: true, hasTimestamps: ['created_at', 'updated_at'], users: function() { return this.belongsToMany(User); }, companies: function() { […]

当使用bookshelf的时候,得到“不能读取属性'toString'的undefined”

我有一套看起来像这样的Bookshelf模型: const User = bookshelf.Model.extend({ tableName: 'users', hasTimestamps: true, posts: function() { return this.hasMany(Posts); }, }); const Posts = bookshelf.Model.extend({ tableName: 'posts', hasTimestamps: true, author: function() { return this.belongsTo(User); }, }); 使用knex,我像这样设置模式: Promise.all([ knex.schema.createTableIfNotExists('users', (tbl) => { tbl.increments('id').primary(); tbl.string('name'); tbl.string('username'); tbl.string('email'); tbl.timestamps(); }), knex.schema.createTableIfNotExists('posts', (tbl) => { tbl.increments(); tbl.string('title'); tbl.string('body'); tbl.integer('author').references('users.id'); tbl.timestamps(); }) ]); 当我然后运行下面的服务器路线devise来获取一个职位: […]

Bookshelf里面的JS函数extend()

我正在使用http://bookshelfjs.org/ 我在里面添加了一个函数 var User = Bookshelf.Model.extend({ … // verify the password verifyPassword: function (password, hash, done) { // Load hash from your password DB. bcrypt.compare(password, hash.replace('$2y$', '$2a$'), function(err, result) { return done(err, result); }); } … module.exports = User; 从我的用户控制器,我称之为: var User = require('../models/user'); User.verifyPassword(req.body.password, user.password, function (err, result) { 但我没有得到no method 'verifyPassword 。

如何在Node.js的Bookshelfjs中连接两个表

我在MySQl DB中有两个表格: 顾客: cust_ID(PK) CUST_NAME trans_ID(FK) 交易 trans_id(PK) trans_amount 在Node.js中,我为这两个表创build了两个模型,现在我想在这两个基于trans_id的表上进行Inner Join。 我没有得到如何做的想法。 var Transaction = bookshelf.Model.extend({ tableName: 'Transaction' }); var Customer = bookshelf.Model.extend({ tableName: 'Customer' });

与FetchAll相关的BookshelfJS限制不起作用

我有一个hasMany belongsTo boxes和box_states之间的关系。 我想抓住他们最新的状态,所以这是我的查询: new Box() .orderBy('id', 'ASC') .fetchAll({ withRelated: [{ 'states' : function(qb) { qb.limit(1) .orderBy('id', 'DESC'); } }] }) .then(function (boxes) { res.json(boxes); }); 这将返回所有的框,但只有最后一个框有一个关系; 一切都没有关系。 帮助将不胜感激。

Node.js等待callback或数据库响应

我看到很多非常类似的问题,但是并没有直接回答真正的等待。 我有一个基于Bookshelf.js的脚本; var Product = bookshelf.Model.extend({ tableName: 'amazon_products', hasTimestamps: true, virtuals: { //https://github.com/bookshelf/bookshelf/wiki/Plugin:-Virtuals hasSiteProduct: function() { var product_references = this.related('siteProductsReferences').fetch() return product_references.length; } } }); 我已经设置了一个虚拟属性,它将简单地计算模型的OneToMany关系并返回计数。 但在这种情况下,它看起来像this.related('siteProductsReferences').fetch()需要一段时间才返回一个响应。 this.related('siteProductsReferences').fetch()也有一个承诺,可以看起来像这样; this.related('siteProductsReferences').fetch().then(function(result) { // … }); 我正在构build这个通过API返回。 如果我只是添加return this.related('siteProductsReferences').fetch()的方法,我得到一个完整的得到; ,"hasSiteProduct":{"isFulfilled":false,"isRejected":false} 所以显然这个callback还没有完成。

如何在bookshelf.js中同时插入父表和子表

我得到了一个名为companies的表,另一个名为companies_posts ,其中company_posts.company_id是companies.id的外键 假设我有以下的json : { "name" : "Teste", "username" : "teste1", "password" : "teste1", "email" : "teste2@teste", "photo" : "teste", "description" : "teste", "company_posts" : [ {"text" : "oi"}, {"text" : "olá"} ] } 在bookshelf.js有没有办法一次插入它? 就像mongodb,我想。 或者我需要insert company ,得到lastInsertId ,然后insert每个company_posts后? 谢谢。 编辑:(顺便说一句,我刚刚学会了如何select使用withRelated和hasMany 。现在我必须学习如何insert / update )