MrV*_*inz 5 sails.js waterline
那里,
在进入hacky/cutom方式之前,我想知道是否有内置的query方法来检查空/非空的多对多关系,因为我在谷歌和文档上都没有成功.
如果我采用doc中的示例,让我想象一下,只有当他有一个Pet或Retrive a Pet而没有任何所有者通过查询时,我才想要检索用户.
// A user may have many pets
var User = Waterline.Collection.extend({
identity: 'user',
connection: 'local-postgresql',
attributes: {
firstName: 'string',
lastName: 'string',
// Add a reference to Pet
pets: {
collection: 'pet',
via: 'owners',
dominant: true
}
}
});
// A pet may have many owners
var Pet = Waterline.Collection.extend({
identity: 'pet',
connection: 'local-postgresql',
attributes: {
breed: 'string',
type: 'string',
name: 'string',
// Add a reference to User
owners: {
collection: 'user',
via: 'pets'
}
}
});
Run Code Online (Sandbox Code Playgroud)
Ps我知道如何在查询执行后过滤结果,这不是我要问的:)
没有内置任何东西(又名 User.hasPet() )或类似的东西,所以简单的答案是否定的
如果我事先知道这些问题,我倾向于以查询速度快的方式编写数据库。IE:用户模式将有一个 hasPets 列。每当添加/删除宠物时,回调都会命中用户表以标记该字段是否有所有者。那么我就可以查询 User.findAll({hasPet:true})。
它有点多,但这取决于你需要速度的地方。