Tag: bookshelf.js knex.js

Bookshelf js fetch withRelated选项返回空的关系对象

我有一个图像模型和一个位置模型。 图像模型包含外键到位置。 要获取我使用的结果: fetch({withRelated: ['location']}; 我收到以下结果: { "id": 24, "created_by": 1, "location_id": 202, "location": {} } 但是我想要的东西是: { "id": 24, "created_by": 1, "location": {….} } 我的图像模型: objectProperties = { tableName: 'images', location: function () { return this.hasOne(location, 'id'); } }; classProperties = {}; imageModel = bookshelf.Model.extend(objectProperties, classProperties); 和我的位置模型: objectProperties = { tableName: 'locations', images: function […]

没有knex的书架 – 执行查询

我有一个functionnode.js应用程序与日志数据到MySQL数据库。 (不使用knex.js) 现在,我想添加function来查询我的数据库表。 我的问题是我现在需要knex.js? 有没有可能执行查询knex? 我无法清楚地find这个例子。

在Node.js中设置模型的正确方法

我正在构build一个小型的Node / Express应用程序,并且已经使用postgresql适配器设置了Knex。 这一切工作。 我知道它的工作原理,因为我可以做一个SQL查询,结果是我想要的 – 特定表中的行。 但是我试图build立模型,以便我可以引入相关数据并进行validation。 我为此使用书架。 我已经build立了这样的模型: base.js var knex = require('../../config/db'); var bookshelf = require('bookshelf')(knex); bookshelf.plugin('registry'); module.exports = bookshelf; food.js var bookshelf = require('./base'); var Meal = require('./meal'); var Food = bookshelf.Model.extend({ tableName: 'foods', meal: function() { return this.belongsTo(Meal); } }); module.exports = bookshelf.model('Food', Food); meal.js var bookshelf = require('./base'); var […]

我如何能够实时查看在Postgres上执行的查询?

我在Node, Bookshelf和Knex上构build了一个应用程序,出于学习和性能方面的考虑,我希望看到在Postgres上正在执行哪些查询。 我该怎么做? 如果这意味着查询将存储在日志文件中,我并不是特别感兴趣的分析器。 我只想看看现在正在发生的事情。 可能的解决scheme: 像SQL Server分析器这样的应用程序,只要它显示实时查询即可 使书架或Knex将查询输出到控制台的一种方法 不幸的是,我search谷歌无济于事。

Bookshelf.js – 更新logging后,初始页面加载/查询显示旧logging。 刷新数据正确显示后

当我使用书架将列设置为空,并redirect到显示该数据的页面时,数据将显示未更新的logging,直到我刷新。 代码如下: Table.where({id: req.param.id}).fetch() .catch(function (err) { console.log(err); }) .then(function (results) { results.set('other_column', null); results.save(); results.refresh(); // Redirect to orders page res.redirect('/landing-page'); }); 着陆页查询是这样的: Table.where({id: req.param.id}).fetch() .catch(function (err) { console.log(err); }) .then(function (results) { data.results = results.attributes; res.render('display-page', data); }); 有谁知道我如何获取更新的logging,或让我知道如果我更新logging不正确? 任何帮助解决这个问题将不胜感激,因为我不能用旧的数据渲染页面后,用户刚刚更新…

bookhelfjs中的多个orderBy()列

如何在书架上实现多个orderBy 我可以添加尽可能多的orderBy模型,但在API中可以有任何sorting选项,如example.com/users?sort=-name,status ,状态,它不需要硬编码。 下面的答案似乎是合法的要求 Knex.js多个orderBy()列 但如何在书架上实现多个orderBy? 车型/ Users.js var Bookshelf = require('../../dbConfig').bookshelf; var User = Bookshelf.Model.extend({ tableName: 'user_table' }); var Users = Bookshelf.Collection.extend({ model: User }); module.exports = { User: User, Users: Users }; services.js var Model = require('./../models/Users'); var express = require('express'); var listAllContentProviders = function (query_params, callback) { Model.Users .forge() .orderBy("name") .orderBy("-status") .fetch() […]

在Bookshelf模型上限制Knex查询以仅返回n个logging

我有以下代码,我正在使用splice函数只传递第一个10 / JSON对象到JADE模板。 app.get('/index', function(req, res) { new models.Condos() .query('orderBy', 'age', 'asc') .fetch() .then(function(names) { var name = names.splice(0,10); res.render('index', { names: name.toJSON() }); }); }); }; 有什么办法可以限制查询本身只返回前10条logging,而不是拼接数组来做到这一点(使用偏移量和限制参数)?

ORM工具NodeJS MySQL

您好,我对NodeJS很新。 我使用mongoose模块与MongoDB合作。 现在我正在为当前项目使用MySQL,并试图找出使用MySQL的ORM的最佳select。 我遇到了BookShelf.JS(与Knex.JS)node-orm2和Squel.js有没有人使用任何这些或其他的NodeJS MySQL / PostgreSQL / SQLite3?

Knex和MySQLdate时间字段精度

在节点应用程序中使用knex时,是否有任何方法将MySQL datetime列设置为datetime(3)或datetime(6),或者更好地为所有date时间列(包括.timestamps)设置默认值? 现在看起来像我插入数据库,然后检索时得到的毫秒数截断。 谢谢!

使用Bookshelf.js在透视模型上设置时间戳

我有两个Bookshelf模型在多对多的关系,我想附加或分离一些关系时更新时间戳。 这是我的模型: var Video = Bookshelf.Model.extend({ tableName: 'video', program: function(){ return this.belongsToMany(Bookshelf.model('Program'), 'programvideo', 'videoId', 'programId'); } }); var Program = Bookshelf.Model.extend({ tableName: 'program', videos: function(){ return this.belongsToMany(Bookshelf.model('Video'), 'programvideo', 'programId', 'videoId'); } }); 一切正常,当我使用 prgm.videos().attach(videos); 但是有什么办法给这个关系添加时间戳吗? 我是否需要在Bookshelf中定义枢纽模型? 谢谢