|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Trying to get everything in the database camel cased, columns lowercase, tables uppercase. |
| 4 | +// The defaults documented on getting started documentation do uppercase foreign keys, an |
| 5 | +// lowercase non-foreign keys. It's a mess. |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const path = require('path'); |
| 9 | + |
| 10 | +const { Sequelize, DataTypes } = require('sequelize'); |
| 11 | + |
| 12 | +const sequelize = new Sequelize({ |
| 13 | + dialect: 'sqlite', |
| 14 | + storage: 'tmp.' + path.basename(__filename) + '.sqlite', |
| 15 | + define: { |
| 16 | + timestamps: false |
| 17 | + }, |
| 18 | +}); |
| 19 | + |
| 20 | +(async () => { |
| 21 | + |
| 22 | +// Create the tables. |
| 23 | +const User = sequelize.define('User', { |
| 24 | + name: { type: DataTypes.STRING }, |
| 25 | +}); |
| 26 | +const Post = sequelize.define('Post', { |
| 27 | + body: { type: DataTypes.STRING }, |
| 28 | +}); |
| 29 | +const UserFollowsUser = sequelize.define('UserFollowsUser', { |
| 30 | + userId: { |
| 31 | + type: DataTypes.INTEGER, |
| 32 | + references: { |
| 33 | + model: User, |
| 34 | + key: 'id' |
| 35 | + } |
| 36 | + }, |
| 37 | + followId: { |
| 38 | + type: DataTypes.INTEGER, |
| 39 | + references: { |
| 40 | + model: User, |
| 41 | + key: 'id' |
| 42 | + } |
| 43 | + }, |
| 44 | +}); |
| 45 | +User.belongsToMany(User, { |
| 46 | + through: UserFollowsUser, |
| 47 | + as: 'follows', |
| 48 | + foreignKey: 'userId', |
| 49 | + otherKey: 'followId', |
| 50 | +}); |
| 51 | +User.hasMany(Post, {foreignKey: 'authorId'}); |
| 52 | +Post.belongsTo(User, {foreignKey: 'authorId'}); |
| 53 | +await sequelize.sync({force: true}); |
| 54 | + |
| 55 | +// Create data. |
| 56 | +const users = await User.bulkCreate([ |
| 57 | + {name: 'user0'}, |
| 58 | + {name: 'user1'}, |
| 59 | + {name: 'user2'}, |
| 60 | + {name: 'user3'}, |
| 61 | +]) |
| 62 | + |
| 63 | +const posts = await Post.bulkCreate([ |
| 64 | + {body: 'body00', authorId: users[0].id}, |
| 65 | + {body: 'body01', authorId: users[0].id}, |
| 66 | + {body: 'body10', authorId: users[1].id}, |
| 67 | + {body: 'body11', authorId: users[1].id}, |
| 68 | + {body: 'body20', authorId: users[2].id}, |
| 69 | + {body: 'body21', authorId: users[2].id}, |
| 70 | + {body: 'body30', authorId: users[3].id}, |
| 71 | + {body: 'body31', authorId: users[3].id}, |
| 72 | +]) |
| 73 | +await users[0].addFollows([users[1], users[2]]) |
| 74 | + |
| 75 | +// Check case of auto-getters. |
| 76 | + |
| 77 | +const user0Posts = await users[0].getPosts({order: [['body', 'ASC']]}) |
| 78 | +assert(user0Posts[0].body === 'body00') |
| 79 | +assert(user0Posts[1].body === 'body01') |
| 80 | +assert(user0Posts.length === 2) |
| 81 | + |
| 82 | +const user1Posts = await users[1].getPosts({order: [['body', 'ASC']]}) |
| 83 | +assert(user1Posts[0].body === 'body10') |
| 84 | +assert(user1Posts[1].body === 'body11') |
| 85 | +assert(user1Posts.length === 2) |
| 86 | + |
| 87 | +const post00User = await posts[0].getUser() |
| 88 | +assert(post00User.name === 'user0') |
| 89 | + |
| 90 | +const post01User = await posts[1].getUser() |
| 91 | +assert(post01User.name === 'user0') |
| 92 | + |
| 93 | +const post10User = await posts[2].getUser() |
| 94 | +assert(post10User.name === 'user1') |
| 95 | + |
| 96 | +const post11User = await posts[3].getUser() |
| 97 | +assert(post11User.name === 'user1') |
| 98 | + |
| 99 | +// Check case of as; |
| 100 | +{ |
| 101 | + const user0Follows = (await User.findByPk(users[0].id, { |
| 102 | + include: [ |
| 103 | + { |
| 104 | + model: User, |
| 105 | + as: 'follows', |
| 106 | + include: [ |
| 107 | + { |
| 108 | + model: Post, |
| 109 | + } |
| 110 | + ], |
| 111 | + }, |
| 112 | + ], |
| 113 | + })).follows |
| 114 | + const postsFound = [] |
| 115 | + for (const followedUser of user0Follows) { |
| 116 | + postsFound.push(...followedUser.Posts) |
| 117 | + } |
| 118 | + postsFound.sort((x, y) => { return x.body < y.body ? -1 : x.body > y.body ? 1 : 0 }) |
| 119 | + assert(postsFound[0].body === 'body10') |
| 120 | + assert(postsFound[1].body === 'body11') |
| 121 | + assert(postsFound[2].body === 'body20') |
| 122 | + assert(postsFound[3].body === 'body21') |
| 123 | + assert(postsFound.length === 4) |
| 124 | +} |
| 125 | + |
| 126 | +await sequelize.close(); |
| 127 | +})(); |
0 commit comments