Postgresql Sequelize:根据关联的belongToMany记录查找记录——但仍然返回所有关联的记录?
我有两种型号:Postgresql Sequelize:根据关联的belongToMany记录查找记录——但仍然返回所有关联的记录?,postgresql,sequelize.js,Postgresql,Sequelize.js,我有两种型号: const apptsModel = db.define('Appts', { id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true}, [.....] }); const UserDataModel = db.define('UserData', { id: {type: Sequelize.STRING, primaryKey: true}, gender: {
const apptsModel = db.define('Appts', {
id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
[.....]
});
const UserDataModel = db.define('UserData', {
id: {type: Sequelize.STRING, primaryKey: true},
gender: {type: Sequelize.STRING},
name_title: {type: Sequelize.STRING},
name_first: {type: Sequelize.STRING},
name_last: {type: Sequelize.STRING},
[.....]
});
apptsModel.belongsToMany(UserDataModel, {through: 'ApptsToUsers'});
UserDataModel.belongsToMany(apptsModel, {through: 'ApptsToUsers'});
我想进行以下搜索:
1) 查找至少一个关联用户具有特定用户id的所有约会
2) 返回该约会的所有关联用户
我有一个正在运行的sequelize代码,它可以(1):
…但它仅返回指定用户的关联用户数据。如何返回每个约会的所有关联用户的数据?关于如何返回的文档似乎还不多。这是有效的,所以我将在这里发布这篇文章以供参考
getAllApptsForCurrentUser(_, args) {
return Promise.resolve()
.then(() => {
//find all appointments and find those for which at least one
//participating user is the one specified in originatingUserID
var appts = connectors.Appts.findAll({
include: [{
model: connectors.UserData,
where: {id: args.originatingUserID}
}],
}).then((res) => res.map((item) => item.dataValues));
return appts;
})
.then(appts => {
//iterate returned appointments and perform a subquery on each,
//finding the other participating users
var arrayOfPromises = [];
appts.forEach(function (appt) {
arrayOfPromises.push(
connectors.Appts.findOne({where: {id: appt.id}, order: [['apptDateTime']], include: [ connectors.UserData ] })
);
});
//Promise.all returns true when all promises passed to it have
//returned true, or when one of them returns false
return Promise.all(arrayOfPromises);
})
.then(apptsWithJoinedData => {
//console.log(apptsWithJoinedData);
return apptsWithJoinedData;
})
.catch((err)=> {
console.log(err);
});
}
如果有更好的方法,请告诉我。关于如何做到这一点,似乎还没有很多文档。这是有效的,所以我将在这里发布这篇文章以供参考
getAllApptsForCurrentUser(_, args) {
return Promise.resolve()
.then(() => {
//find all appointments and find those for which at least one
//participating user is the one specified in originatingUserID
var appts = connectors.Appts.findAll({
include: [{
model: connectors.UserData,
where: {id: args.originatingUserID}
}],
}).then((res) => res.map((item) => item.dataValues));
return appts;
})
.then(appts => {
//iterate returned appointments and perform a subquery on each,
//finding the other participating users
var arrayOfPromises = [];
appts.forEach(function (appt) {
arrayOfPromises.push(
connectors.Appts.findOne({where: {id: appt.id}, order: [['apptDateTime']], include: [ connectors.UserData ] })
);
});
//Promise.all returns true when all promises passed to it have
//returned true, or when one of them returns false
return Promise.all(arrayOfPromises);
})
.then(apptsWithJoinedData => {
//console.log(apptsWithJoinedData);
return apptsWithJoinedData;
})
.catch((err)=> {
console.log(err);
});
}
如果有更好的方法,请告诉我。我还想知道是否有更简单的方法可以在单个查询中而不是在多个查询中执行此操作。我还想知道是否有更简单的方法可以在单个查询而不是多个查询中执行此操作。