|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Find all posts by users that a given user follows. |
| 4 | +// https://stackoverflow.com/questions/42632943/sequelize-multiple-where-clause |
| 5 | + |
| 6 | +const assert = require('assert'); |
| 7 | +const path = require('path'); |
| 8 | + |
| 9 | +const { Sequelize, DataTypes } = require('sequelize'); |
| 10 | + |
| 11 | +const sequelize = new Sequelize({ |
| 12 | + dialect: 'sqlite', |
| 13 | + storage: 'tmp.' + path.basename(__filename) + '.sqlite', |
| 14 | +}); |
| 15 | + |
| 16 | +(async () => { |
| 17 | + |
| 18 | +// Create the tables. |
| 19 | +const User = sequelize.define('User', { |
| 20 | + name: { type: DataTypes.STRING }, |
| 21 | +}, {}); |
| 22 | +const Post = sequelize.define('Post', { |
| 23 | + body: { type: DataTypes.STRING }, |
| 24 | +}, {}); |
| 25 | +User.belongsToMany(User, {through: 'UserFollowUser', as: 'Follows'}); |
| 26 | +User.hasMany(Post); |
| 27 | +Post.belongsTo(User); |
| 28 | +await sequelize.sync({force: true}); |
| 29 | + |
| 30 | +// Create data. |
| 31 | +const users = await User.bulkCreate([ |
| 32 | + {name: 'user0'}, |
| 33 | + {name: 'user1'}, |
| 34 | + {name: 'user2'}, |
| 35 | + {name: 'user3'}, |
| 36 | +]) |
| 37 | + |
| 38 | +const posts = await Post.bulkCreate([ |
| 39 | + {body: 'body00', UserId: users[0].id}, |
| 40 | + {body: 'body11', UserId: users[0].id}, |
| 41 | + {body: 'body10', UserId: users[1].id}, |
| 42 | + {body: 'body11', UserId: users[1].id}, |
| 43 | + {body: 'body20', UserId: users[2].id}, |
| 44 | + {body: 'body21', UserId: users[2].id}, |
| 45 | + {body: 'body30', UserId: users[3].id}, |
| 46 | + {body: 'body31', UserId: users[3].id}, |
| 47 | +]) |
| 48 | + |
| 49 | +await users[0].addFollows([users[1], users[2]]) |
| 50 | + |
| 51 | +// Get all posts by authors that user0 follows. |
| 52 | +// The posts are placed inside their respetive authors under .Posts |
| 53 | +// so we loop to gather all of them. |
| 54 | +{ |
| 55 | + const user0Follows = (await User.findByPk(users[0].id, { |
| 56 | + include: [ |
| 57 | + { |
| 58 | + model: User, |
| 59 | + as: 'Follows', |
| 60 | + include: [ |
| 61 | + { |
| 62 | + model: Post, |
| 63 | + } |
| 64 | + ], |
| 65 | + }, |
| 66 | + ], |
| 67 | + })).Follows |
| 68 | + const postsFound = [] |
| 69 | + for (const followedUser of user0Follows) { |
| 70 | + postsFound.push(...followedUser.Posts) |
| 71 | + } |
| 72 | + postsFound.sort((x, y) => { return x.body < y.body ? -1 : x.body > y.body ? 1 : 0 }) |
| 73 | + assert(postsFound[0].body === 'body10') |
| 74 | + assert(postsFound[1].body === 'body11') |
| 75 | + assert(postsFound[2].body === 'body20') |
| 76 | + assert(postsFound[3].body === 'body21') |
| 77 | + assert(postsFound.length === 4) |
| 78 | +} |
| 79 | + |
| 80 | +// With ordering, offset and limit. |
| 81 | +// The posts are placed inside their respetive authors under .Posts |
| 82 | +// The only difference is that posts that we didn't select got removed. |
| 83 | + |
| 84 | +{ |
| 85 | + const user0Follows = (await User.findByPk(users[0].id, { |
| 86 | + offset: 1, |
| 87 | + limit: 2, |
| 88 | + // TODO why is this needed? It does try to make a subquery otherwise, and then it doesn't work. |
| 89 | + // https://selleo.com/til/posts/ddesmudzmi-offset-pagination-with-subquery-in-sequelize- |
| 90 | + subQuery: false, |
| 91 | + include: [ |
| 92 | + { |
| 93 | + model: User, |
| 94 | + as: 'Follows', |
| 95 | + include: [ |
| 96 | + { |
| 97 | + model: Post, |
| 98 | + } |
| 99 | + ], |
| 100 | + }, |
| 101 | + ], |
| 102 | + })).Follows |
| 103 | + assert(user0Follows[0].name === 'user1') |
| 104 | + assert(user0Follows[1].name === 'user2') |
| 105 | + assert(user0Follows.length === 2) |
| 106 | + const postsFound = [] |
| 107 | + for (const followedUser of user0Follows) { |
| 108 | + postsFound.push(...followedUser.Posts) |
| 109 | + } |
| 110 | + postsFound.sort((x, y) => { return x.body < y.body ? -1 : x.body > y.body ? 1 : 0 }) |
| 111 | + // Note that what happens is that some of the |
| 112 | + assert(postsFound[0].body === 'body11') |
| 113 | + assert(postsFound[1].body === 'body20') |
| 114 | + assert(postsFound.length === 2) |
| 115 | + |
| 116 | + // Same as above, but now with DESC ordering. |
| 117 | + { |
| 118 | + const user0Follows = (await User.findByPk(users[0].id, { |
| 119 | + order: [[ |
| 120 | + {model: User, as: 'Follows'}, |
| 121 | + Post, |
| 122 | + 'body', |
| 123 | + 'DESC' |
| 124 | + ]], |
| 125 | + offset: 1, |
| 126 | + limit: 2, |
| 127 | + subQuery: false, |
| 128 | + include: [ |
| 129 | + { |
| 130 | + model: User, |
| 131 | + as: 'Follows', |
| 132 | + include: [ |
| 133 | + { |
| 134 | + model: Post, |
| 135 | + } |
| 136 | + ], |
| 137 | + }, |
| 138 | + ], |
| 139 | + })).Follows |
| 140 | + // Note how user ordering is also reversed from an ASC. |
| 141 | + // it likely takes the use that has the first post. |
| 142 | + assert(user0Follows[0].name === 'user2') |
| 143 | + assert(user0Follows[1].name === 'user1') |
| 144 | + assert(user0Follows.length === 2) |
| 145 | + const postsFound = [] |
| 146 | + for (const followedUser of user0Follows) { |
| 147 | + postsFound.push(...followedUser.Posts) |
| 148 | + } |
| 149 | + // In this very specific data case, this would not be needed. |
| 150 | + // because user2 has the second post body and user1 has the first |
| 151 | + // alphabetically. |
| 152 | + postsFound.sort((x, y) => { return x.body < y.body ? 1 : x.body > y.body ? -1 : 0 }) |
| 153 | + // Note that what happens is that some of the |
| 154 | + assert(postsFound[0].body === 'body20') |
| 155 | + assert(postsFound[1].body === 'body11') |
| 156 | + assert(postsFound.length === 2) |
| 157 | + } |
| 158 | + |
| 159 | + // Here user2 would have no post hits due to the limit, |
| 160 | + // so it is entirely pruned from the user list as desired. |
| 161 | + // Otherwise we would fetch a lot of unwanted user data |
| 162 | + // in a large database. |
| 163 | + const user0FollowsLimit2 = (await User.findByPk(users[0].id, { |
| 164 | + limit: 2, |
| 165 | + subQuery: false, |
| 166 | + include: [ |
| 167 | + { |
| 168 | + model: User, |
| 169 | + as: 'Follows', |
| 170 | + include: [ { model: Post } ], |
| 171 | + }, |
| 172 | + ], |
| 173 | + })).Follows |
| 174 | + assert(user0FollowsLimit2[0].name === 'user1') |
| 175 | + assert(user0FollowsLimit2.length === 1) |
| 176 | + |
| 177 | + // Case in which our post-sorting is needed. |
| 178 | + // TODO: possible to get sequelize to do this for us by returning |
| 179 | + // a flat array directly? |
| 180 | + // It's not big deal since the LIMITed result should be small, |
| 181 | + // but feels wasteful. |
| 182 | + // https://stackoverflow.com/questions/41502699/return-flat-object-from-sequelize-with-association |
| 183 | + // https://github.com/sequelize/sequelize/issues/4419 |
| 184 | + { |
| 185 | + await Post.truncate({restartIdentity: true}) |
| 186 | + const posts = await Post.bulkCreate([ |
| 187 | + {body: 'body0', UserId: users[0].id}, |
| 188 | + {body: 'body1', UserId: users[1].id}, |
| 189 | + {body: 'body2', UserId: users[2].id}, |
| 190 | + {body: 'body3', UserId: users[3].id}, |
| 191 | + {body: 'body4', UserId: users[0].id}, |
| 192 | + {body: 'body5', UserId: users[1].id}, |
| 193 | + {body: 'body6', UserId: users[2].id}, |
| 194 | + {body: 'body7', UserId: users[3].id}, |
| 195 | + ]) |
| 196 | + const user0Follows = (await User.findByPk(users[0].id, { |
| 197 | + order: [[ |
| 198 | + {model: User, as: 'Follows'}, |
| 199 | + Post, |
| 200 | + 'body', |
| 201 | + 'DESC' |
| 202 | + ]], |
| 203 | + subQuery: false, |
| 204 | + include: [ |
| 205 | + { |
| 206 | + model: User, |
| 207 | + as: 'Follows', |
| 208 | + include: [ |
| 209 | + { |
| 210 | + model: Post, |
| 211 | + } |
| 212 | + ], |
| 213 | + }, |
| 214 | + ], |
| 215 | + })).Follows |
| 216 | + assert(user0Follows[0].name === 'user2') |
| 217 | + assert(user0Follows[1].name === 'user1') |
| 218 | + assert(user0Follows.length === 2) |
| 219 | + const postsFound = [] |
| 220 | + for (const followedUser of user0Follows) { |
| 221 | + postsFound.push(...followedUser.Posts) |
| 222 | + } |
| 223 | + // We need this here, otherwise we would get all user2 posts first: |
| 224 | + // body6, body2, body5, body1 |
| 225 | + postsFound.sort((x, y) => { return x.body < y.body ? 1 : x.body > y.body ? -1 : 0 }) |
| 226 | + assert(postsFound[0].body === 'body6') |
| 227 | + assert(postsFound[1].body === 'body5') |
| 228 | + assert(postsFound[2].body === 'body2') |
| 229 | + assert(postsFound[3].body === 'body1') |
| 230 | + assert(postsFound.length === 4) |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +await sequelize.close(); |
| 235 | +})(); |
0 commit comments