Tag: bookshelf.js knex.js

Nodejs + Knex + Postgres:未处理的拒绝错误:池被破坏

我和我的团队使用knex和postgres来处理我们所有的数据库工作。 不时,我们会得到以下错误: Knex:Error Pool2 – error: too many connections for role "<rolename>" 随着每个请求的这个错误: app[web.2]: Unhandled rejection Error: Pool is destroyed app[web.2]: at Pool.acquire (/app/node_modules/knex/node_modules/pool2/lib/pool.js:163:12) app[web.2]: at /app/node_modules/knex/lib/client.js:204:19 app[web.2]: at tryCatcher (/app/node_modules/knex/node_modules/bluebird/js/main/util.js:26:23) app[web.2]: at Promise._resolveFromResolver (/app/node_modules/knex/node_modules/bluebird/js/main/promise.js:480:31) app[web.2]: at new Promise (/app/node_modules/knex/node_modules/bluebird/js/main/promise.js:70:37) app[web.2]: at Client.acquireConnection (/app/node_modules/knex/lib/client.js:200:12) app[web.2]: at /app/node_modules/knex/lib/runner.js:138:49 app[web.2]: at tryCatcher (/app/node_modules/knex/node_modules/bluebird/js/main/util.js:26:23) app[web.2]: at Function.Promise.attempt.Promise.try (/app/node_modules/knex/node_modules/bluebird/js/main/method.js:31:24) […]

书架(knex) – belongsToMany关系不工作

我一直在尝试使用belongsToMany关系(Bookshelf)build立post和标签之间的关系。 这是我的代码: db.js const Post = bookshelf.Model.extend({ tableName: 'posts', hasTimestamps: true, tags: function(){ return this.belongsToMany(Tag) } }) const Tag = bookshelf.Model.extend({ tableName: 'tags', posts: function(){ return this.belongsToMany(Post) } }) // Pivot table const PostTag = bookshelf.Model.extend({ tableName: 'posts_tags', post: function(){ return this.belongsTo(Post) }, tag: function(){ return this.belongsTo(Tag) } }) 获取路线是: .get('/:id', (req, res, next) => […]

有书架的Node.js; 使用ID数组设置多对多关系的值

我正在使用Bookshelf的Node.js来设置一个API。 请求中的JSON看起来像这样 { conversation: { user_ids: [1, 2, 3, 4] } } 会话对象如下所示: var Conversation = storeManager.bookshelf.Model.extend({ tableName: 'conversations', users: function() { this.belongsToMany(User) } }) 用户看起来像这样: var User = storeManager.bookshelf.Model.extend({ tableName: 'users', conversations: function() { return this.belongsToMany(Conversation) } }) 数据库有一个会话和用户表,以及一个具有user_id和conversation_id字段的conversations_users表。 所以我一直在阅读文档,我正在努力弄清楚如何创build一个与现有用户具有给定ID的关系的对话对象。 这是我的控制器迄今为止 ConversationController.prototype.create = function(req, res, next) { // Create the conversation Conversation.forge().set('users', req.body['conversation']['user_ids']).save().then(function(model) { […]

如何使用knex / Bookshelfselect数据库expression式作为值

我试图执行下面的查询使用knex.js和MySql SELECT m.id, TIME(date_created) AS `timestamp`, u.username, m.`message` FROM `messages` AS m INNER JOIN users AS u ON u.id = m.user_id WHERE m.game_id IS NULL AND m.`date_created` > DATE_SUB( CURRENT_TIMESTAMP (), INTERVAL 12 HOUR ) ORDER BY m.`date_created` ASC LIMIT 50 为了正确的处理expression式,在DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 12 HOUR)闭包中DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 12 HOUR)根据文档,在knex中有whereRow()方法。 我试图使用select()方法 select('messages.id', 'TIME(date_created) AS timestamp', 'users.username', 'messages.message') […]

如何查询与书架有很多关系的数据?

所以,我有以下几点: var Device = services.bookshelf.Model.extend({ tableName: 'device', calendar: function() { return this.belongsToMany(Calendar, 'calendar_device', 'deviceId', 'calendarId'); } }); var Calendar = module.exports = services.bookshelf.Model.extend({ tableName: 'calendar', device: function() { return this.belongsToMany(Device, 'calendar_device', 'calendarId', 'deviceId'); }, schedule: function() { return this.hasMany(Schedule); } }); var Schedule = module.exports = services.bookshelf.Model.extend({ tableName: 'schedule', calendar: function() { return this.belongsTo(Calendar); }, […]

Bookshelf.js / Knex.js上数据库模式加载的常见做法

ActiveRecord(来自Rails)和Sequelize(另一个node.js ORM)提供了一种初始化数据库的方法,从模型定义中创build表结构。 Rails通过rails db:schema:load命令来实现,而Sequelize具有相同的sync()方法。 通过使用它,我们不需要运行应用程序的整个迁移堆栈来启动一个新的数据库,也不需要在项目存储库上保存SQL转储。 来自这个背景,我期待Bookshelf.js或Knex.js有一些类似的function,但我找不到这两个项目的文档。 然后我决定看看使用Bookshelf的Ghost博客引擎的源代码,并且发现他们在自己的代码库中处理数据库初始化: https://github.com/TryGhost/Ghost/blob/e40290a/core/server/data/schema/schema.js https://github.com/TryGhost/Ghost/blob/e40290a/core/server/data/migration/populate.js https://github.com/TryGhost/Ghost/blob/e40290a/core/server/data/schema/commands.js 我想避免写我自己的代码来处理像这样的事情,因为像Sequelize这样的其他选项提供了这个开箱即用。 是否有任何常见的做法,插件或库build议在书架上加载数据库模式?

这是可能的使用bookshelf.js迁移?

我正在尝试使用knex和bookshelf进行迁移,到目前为止,这是我的代码,它是书架文档中的一个示例: exports.up = function(knex, Promise) { return knex.schema.createTable('books', function(table) { table.increments('id').primary(); table.string('name'); }).createTable('summaries', function(table) { table.increments('id').primary(); table.string('details'); table.integer('book_id').unique().references('books.id'); }); }; 我试了跑: knex migrate:make my_migration_name knex migrate:latest knex migrate:rollback 但是在我的数据库中没有一个变化。 任何想法如何能得到它的工作?

Knex迁移种子完成,错误

我从一个看起来像这样的数组播种我的数据库,单词和定义是在多对多的关系 var seeds = [ { "word": "Click", "definitions": ["Computer", "Mouse", "Tasto", "Pulsante", "Selezionare"] }, { "word": "Galoppo", "definitions": ["Cavallo", "Andatura", "Trotto", "Ippica", "Passo"] }, { "word": "Raggio", "definitions": ["Sole", "Bicicletta", "Diametro", "Luce", "Laser"] }, { . . .goes on for 1089 objects 这是我的尝试 exports.seed = function (knex, Promise) { var promises = seeds.map(function (seed) […]

销毁书架/ knex连接池

我有一个使用Bookshelf / Knex的数据库API。 我有多个Bookshelf模型,看起来像这样: import bookshelf from '../bookshelf'; var Product = bookshelf.Model.extend({ tableName: 'product' // … }); export default Product; 书架包括如下: import dbConfig from './dbConfig'; const knex = require('knex')(dbConfig); const bookshelf = require('bookshelf')(knex); module.exports = bookshelf; 我也有多个api动作调用,看起来像这样: import Product from '../../Models/Product'; export default function getProducts(bookshelf) { return new Promise((resolve) => { Product.collection().fetch().then((products) => { resolve({ […]

在bookshelf.js中手动设置时间戳记值

我有一个表设置了时间戳和书架configuration使用它们。 通常情况下,所有事情都会发生,因为它应该和书架照顾的时间戳,但我有一个情况下,我想指定他们,但是当我尝试这样做的值被忽略,并使用当前date。 我试图简化我的用例到基本要点: var Author = Bookshelf.Model.extend({ tableName: 'authors', hasTimestamps: ['created_at', 'updated_at'], bookAuthors: function(){ return this.hasMany(require('.book_authors')); }, associateBookWithAuthor(bookId,timestamp) { return self.related('bookAuthors').create({ book_id: bookId, updated_at: timestamp, // ignored by bookshelf created_at: timestamp // also ignored }) } } 我仍然想要保持为常规用例configuration的hasTimestamps 。 是否有可能让Bookshelf让我重写时间戳行为?