Tag: bookshelf.js knex.js

Knex:错误池2 – 错误:连接ECONNREFUSED

我正在使用Node.js,Express.js,Bookshelf.js和Knex.js 我得到以下错误与默认knex池定义。 Knex:Error Pool2 – Error: connect ECONNREFUSED 这是以下knex定义(默认pool.min值是2) var knex = require('knex')({ client: 'mysql', connection: config.connection, }); 我得到错误 Knex:Error Pool2 – Error: connect ECONNREFUSED Knex:Error Pool2 – Error: connect ECONNREFUSED 在以下定义中,它可以正常工作。 var knex = require('knex')({ client: 'mysql', connection: config.connection, pool: { min: 0, max: 10 } }); 我注意到错误是打印n次,其中n在pool.min值。 有人可以告诉我为什么发生这种情况。 虽然,错误已经解决,但由于我是新手,我无法理解为什么会发生这种情况。

Bookshelf.js或Knex.js中的2个表的外连接

我是Bookshelf.js和knex的新手。 我需要在Bookshelf / knex中写一个相当于这个的查询 SELECT Inv.*, Comp.* FROM Inv, Comp WHERE Inv.uId =2 AND Comp.cId = Inv.cId; 发表表格有: Id | 主键,整数不为空 col1 | string数据 cId | 整数,外键引用C表 uId | 整数外键引用U表 比较表有: cId | 主键,整数不为空 col3 | string数据

Bookshelf.js,在相关表上查询

我有一个类似于以下的模型: var ScholarlyPaper = Bookshelf.Model.extend({ tableName: 'papers', paragraphs: function() { return this.hasMany(Paragraph).through(Section); }, sections: function() { return this.hasMany(Section); } }); var Section = Bookshelf.Model.extend({ tableName: 'sections', paragraphs: function() { return this.hasMany(Paragraph); } scholarlyPaper: function() { return this.belongsTo(ScholarlyPaper); } }); var Paragraph = Bookshelf.Model.extend({ tableName: 'paragraphs', section: function() { return this.belongsTo(Section); }, scholarlyPaper: function() { return […]

始终从Bookshelf.js中的相关模型中获取

我希望baffle.where({id: 1}).fetch()总是将typeName属性作为baffle模型的一部分,而不是baffleType明确地从baffleType获取它。 下面的工作对我来说,但似乎与withRelated将加载关系,如果baffle模型直接提取,而不是通过关系: let baffle = bookshelf.Model.extend({ constructor: function() { bookshelf.Model.apply(this, arguments); this.on('fetching', function(model, attrs, options) { options.withRelated = options.withRelated || []; options.withRelated.push('type'); }); }, virtuals: { typeName: { get: function () { return this.related('type').attributes.typeName; } } }, type: function () { return this.belongsTo(baffleType, 'type_id'); } }); let baffleType = bookshelf.Model.extend({}); 什么是正确的方法来做到这一点?

bookshelf.js时间戳不工作

我现在正在试用bookshelf.js,并使用以下knex迁移创build了一个样本表: exports.up = function(knex, Promise) { return knex.schema.createTable( "users", function( table ) { table.increments(); table.timestamps(); table.string( "email" ); } ); }; 然后我定义了一个bookshelf.js模型: var User = bookshelf.Model.extend( { tableName: "users" } ); 并试图保存它: var u = new User( { email: "john.doe@example.com" } ); u.save(); 一切似乎工作,当我看着数据库,新的用户真的被保存,但时间戳列是NULL 。 在调用u.timestamp()之前调用u.save()似乎没有任何作用。 我在这里做错了什么?

使用Bookshelf.js和knex.js进行unit testing

我对Node相对比较陌生,正在使用knex和bookshelf开展一个项目。 我有一些unit testing我的代码有点麻烦,我不知道我做错了什么。 基本上我有一个模型(称为VorcuProduct),看起来像这样: var VorcuProduct = bs.Model.extend({ tableName: 'vorcu_products' }); module.exports.VorcuProduct = VorcuProduct 还有一个function可以将VorcuProduct保存在数据库中。 非常简单。 这样做的function如下所示: function subscribeToUpdates(productInformation, callback) { model.VorcuProduct .where({product_id: productInformation.product_id, store_id: productInformation.store_id}) .fetch() .then(function(existing_model) { if (existing_model == undefined) { new model.VorcuProduct(productInformation) .save() .then(function(new_model) { callback(null, new_model)}) .catch(callback); } else { callback(null, existing_model) } }) } 哪种方法可以正确testing而不碰到数据库? 我需要模拟fetch返回一个模型或未定义(取决于testing),然后做同样的save ? 我应该使用rewire吗? 正如你可以看到我有点失落,所以任何帮助将不胜感激。 […]