您将需要包含(SQL 中的联接)来连接所有这些表。
像这样的东西,但首先你需要将你的模型(数据库表)交织在一起,看起来像这样:
User.belongsToMany(Profile, { through: Grant });
Profile.belongsToMany(User, { through: Grant });
User.hasMany(Grant);
Grant.belongsTo(User);
Profile.hasMany(Grant);
Grant.belongsTo(Profile);
参考:
https://sequelize.org/master/manual/advanced-many-to-many.html
现在,一旦您完成了连接,您需要在 findAll(或 findAllAndCount 根据要求)中使用 include 关键字来使用这些连接:
一些基本代码(你需要尝试和调整,这是粗略的代码):
S3Files.findAll({
include: [
{
model: user,
attributes: ['some columns']
include: {
model: userPlaylist,
attributes: ['some columns'],
required: false
},
where: {[Op.and]: Sequelize.where(Sequelize.col('userPlaylist.userId'), 'is not' null)},
attributes: ['some columns']
]
});
最后,对于偏移量和限制,您需要LIMIT 和 OFFSET 的基本 SQL 逻辑
来自官方文档的示例 sn-p 用于偏移和限制:
Project
.findAndCountAll({
where: {
title: {
[Op.like]: 'foo%'
}
},
offset: 10,
limit: 2
})
相同的参考:
https://sequelize.org/v5/manual/models-usage.html