使用 Sequelize QueryChainer 时遇到一些困难。通常我不需要这样做,但我很早就决定处理自己的关联,而不是使用关联管理中内置的模块(结果证明这是相当痛苦的)。
我正在编写我的 REST 例程,并尝试根据已获取或需要获取的其他记录删除一些记录。我决定使用 QueryChainer 和串行运行,而不是使用 4-5 个嵌套回调。
在这个例子中,我正在寻找一个用户,如果找到一个用户,我想将一行查询拼接在一起来处理特定关联的删除。我遇到的问题主要是理解链接器的语法,而不仅仅是使用模型 EventEmitting 方法。
// Find user, destroy it and associations
db.Models.user.find({ where: {STUID: id} })
.success(function (user) {
if (!user) {
res.locals.err = {
status: 'FAILED',
message: 'Could not delete user',
reason: 'No user resource found'
}
} else {
chain = new db._sqlize.Utils.QueryChainer();
chain.add(user.destroy())
// Check and destroy author association
if (user.AUTHOR) {
// Would like to fetch the specific author model
// db.models.author.find({})
// ... and destroy it too,
// author.destroy()
// without having to nest
}
}
});
sequelize querychain语法指定执行如下操作:
.add(Model, 'function', [param1, param2])
但我不太确定这到底意味着什么。我可以在这里定义内联函数吗?函数只是一个寻找其他地方已定义的字符串的字符串,还是我要“添加”的模型的函数?文档在这里感觉有点仓促。
最佳答案
queryChain 所需的语法是:
.add(sequelize, 'sync', [ {force: true} ])
.add(Partner, 'create', [ {name: 'foo'} ])
我犯了同样的错误,正如你在这里看到的:https://github.com/sdepold/sequelize/issues/385#issuecomment-11690183
关于node.js - Sequelize QueryChainer 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14001389/