0
沿着接合的鋸齒ASSOCATION我有Posts
和Users
基本應用程序。我想,以確保用戶只提出對符合下列條件的職位:Sequelize.js與非混疊關聯
- 他們以前沒有
- 看到這個帖子,他們是不是這個帖子的作者
要爲此,我有這些協會:
User.hasMany(Post, {
foreignKey: 'user_id'
});
User.belongsToMany(Post, {
as: {
singular: 'ViewedPost',
plural: 'ViewedPosts'
},
through: UserPostView,
foreignKey: 'user_id',
otherKey: 'post_id'
});
Post.belongsToMany(User, {
as: {
singular: 'ViewUser',
plural: 'ViewUsers'
},
through: UserPostView,
foreignKey: 'post_id',
otherKey: 'user_id'
});
我能滿足每個條件有一個單獨的查詢,但是,我希望能夠重新使用單個查詢轉換符合我的條件的帖子。
這裏是我現在使用的獲取用戶以前沒有見過的職位查詢。我怎樣才能修改這個查詢來返回未由用戶創作的帖子?
function fetchFreshPost(user) {
return user.getViewedPosts({
attributes: ['id'],
joinTableAttributes: []
}).then(function(posts) {
var postIds = _.map(posts, 'id');
var query = {
limit: 1,
where: {
id: {
$notIn: postIds
}
}
};
return Post.findAll(query);
})
}
謝謝你的時間。
感謝@Keval - 不幸的是,這仍然需要兩個查詢,一個'getViewedPosts'來獲取用戶的瀏覽帖子,然後第二個與地方條款。我試圖把這個結合成一個查詢。 – lightyrs