more sequelize will it ever end

This commit is contained in:
Ciro Santilli
2021-06-17 12:06:44 +01:00
parent 8c86e0aa4d
commit fa0766e88f
6 changed files with 358 additions and 8 deletions

View File

@@ -49,6 +49,8 @@ const post2 = await Post.create({body: 'post2'});
// Make user0 like post0
await user0.addPost(post0)
// Also works.
//await user0.addPost(post0.id)
// Make user0 and user2 like post1
await post1.addUsers([user0, user2])
@@ -68,6 +70,20 @@ const user2Likes = await user2.getPosts({order: [['body', 'ASC']]})
assert(user2Likes[0].body === 'post1');
assert(user2Likes.length === 1);
// Same as get* but with the user ID instead of the model object.
{
const user0Likes = await Post.findAll({
include: [{
model: User,
where: {id: user0.id},
}],
order: [['body', 'ASC']],
})
assert(user0Likes[0].body === 'post0');
assert(user0Likes[1].body === 'post1');
assert(user0Likes.length === 2);
}
// Get users that liked a given likes.
const post0Likers = await post0.getUsers({order: [['name', 'ASC']]})
@@ -88,6 +104,7 @@ assert(post1Likers.length === 2);
// Check if user likes post.
assert( await user0.hasPost(post0))
assert( await user0.hasPost(post0.id)) // same
assert( await user0.hasPost(post1))
assert(!await user0.hasPost(post2))
@@ -104,7 +121,7 @@ assert(!await user0.hasPosts([post0, post1, post2]))
assert(await user0.countPosts() === 2)
assert(await post0.countUsers() === 1)
// Autogenerated remove* method
// Autogenerated remove* methods
// user0 doesn't like post0 anymore.
await user0.removePost(post0)