Tag: orm

更新sailsjs模型afterCreate挂钩中的undefined

在我的sailsjs应用程序中,我试图添加一个默认的头像为我的用户创build用户后,所以我在models/User.js这个afterCreate挂钩: var User = { // … attributes: { avatar: { model: 'Image' }, // … }, afterCreate: function(user, next){ var url = sails.config.s3 + "/" + sails.config.default_avatar; sails.log.info(user); // This displays the proper user var data = { url: url, isAvatar: true, user: user.id }; Image.create(data) .exec( function (err, image) { sails.log.info(image); // This […]

水线:访问模型中的填充值

我正在使用sails.js来开发我的第一个应用程序。 我有一个水线模型如下所示。 //ModelA.js module.exports = { attributes: { //more attributes userId: { model: 'user' }, //more attributes } }; 我在我的一个控制器中使用模型,如下所示。 ModelA.find(options) .populate('userId') .exec(function (err, modelA) { //some logic //modelA.userId is undefined here res.json(modelA); //userId is populated in the JSON output }); 如何访问模型中的填充值?

Sails.js数据与水线航行电梯缺less

我正在使用waterline orm,它默认使用sails.js。 我最近意识到我的数据刚刚在我的MySQL数据库中失踪,当我做帆升降机。 我的模型configuration是改变。 有没有人遇到类似的问题? 这是可怕的。

如何在sails中使用存储过程调用?

目前我正在使用sailsjs构build一个项目。 我想在sailsjs控制器中使用存储过程调用。 任何build议,我怎样才能使用它?

续集多对多关联 – 未find方法

我有两个n:m sequelize模型,如下所示 // Organization Model module.exports = { attributes: { id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, name: { type: Sequelize.STRING, required: true }, }, associations: function() { Organization.belongsToMany(Contact, { through : OrganizationContact, foreignKey: { name: 'organizationId', allowNull: false } }); } }; // OrganizationContact Model module.exports = { attributes: { id: { […]

使用Sequelize.jsjoin特定的列

我正在尝试使用Sequelize.js在特定列上join一些表格。 到目前为止,我的代码是这样的: table_1.findall({ include: [{ model: table_2 attributes: ['id', 'another_id'] include: [{ model: table_3 required: true attributes: ['time'] }] }] }) 每个表的主键都是“id”。 这似乎等同于下面的SQL(为了简洁起见,我显示了SELECT *,因为这不是这个问题的焦点): SELECT * FROM table_1 as t1 LEFT OUTER JOIN table_2 as t2 ON t1.id = t2.t1_id INNER JOIN table_3 as t3 ON t2.id = t3.t2_id 我想有这样的东西: SELECT * FROM table_1 as […]

使用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中定义枢纽模型? 谢谢

BookshelfJS关系 – hasMany

我正在使用BookshelfJS作为我的ORM。 我有3个模型。 TripHistory user_id route_id Route id name Users id name 从我的用户模型,我有一个关系 trips() { return this.hasMay(TripHistory) } 和TripHistory有类似的关系 route() { return this.belongsTo(Route) } 这里的问题是,当我尝试获取行程历史logging时,我只想得到路线名称。 运行 withRelated: ['trips.route'] 回报 "trips": [ { "id": 1, "user_id": 1, "route_id": 1, "driver_id": 1, "trip_id": 1, "created_at": "2017-08-09T16:46:34.000Z", "updated_at": "2017-08-09T16:46:34.000Z", "route": { "id": 1, "pickup": "Fadeyi", "destination": "Jibowu", "departure_time": "6:00", […]

我怎样才能编写一个需要操作mySQL数据库的testing模块?

我正在写一个nodejs npm模块,需要一个mySQL数据库来操作。 我想写一个连接到“假”数据库的testing模块,并对其进行一些操作。 我已经在我的开发机器中本地安装了我的testing数据库,但是我希望这个testing可以在任何机器上运行。 编写依赖于操作mySQL数据库的集成testing模块的最佳做法是什么? 是否存在networking中的任何公共服务,我可以得到一个临时的MySQL用户/密码,我可以在有限的时间/大小做一些操作?

Sequelizejs中.save和.create有什么区别?

我是新来的Sequelize,努力去理解这个非常陌生的ORM新世界是如何工作的。 曾经我似乎无法理解的东西是Sequelizejs中“.create”和“.save”之间的区别。 我用两种语言编写了testing函数,除了语法略有不同外,他们似乎也完全一样。 这是使用“.save”方法 models.User.build({ username: req.body.username, password: req.body.password, first_name: req.body.firstName, last_name: req.body.lastName }) .save() .then(function(task){ // some function… }) .catch(function(error){ // some function… }); 这是使用“.create”方法 models.User.create({ username: req.body.username, password: req.body.password, first_name: req.body.firstName, last_name: req.body.lastName }).then(function(data) { // some function… }); 我在这里没有看到什么?