|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const { Sequelize, DataTypes } = require('sequelize'); |
| 7 | + |
| 8 | +const sequelize = new Sequelize({ |
| 9 | + dialect: 'sqlite', |
| 10 | + storage: 'tmp.' + path.basename(__filename) + '.sqlite', |
| 11 | +}); |
| 12 | +(async () => { |
| 13 | +const Comment = sequelize.define('Comment', { |
| 14 | + body: { type: DataTypes.STRING }, |
| 15 | +}, {}); |
| 16 | +const User = sequelize.define('User', { |
| 17 | + name: { type: DataTypes.STRING }, |
| 18 | +}, {}); |
| 19 | +User.hasMany(Comment) |
| 20 | +Comment.belongsTo(User) |
| 21 | +await sequelize.sync({force: true}); |
| 22 | +const u0 = await User.create({name: 'u0'}) |
| 23 | +const u1 = await User.create({name: 'u1'}) |
| 24 | +await Comment.create({body: 'u0c0', UserId: u0.id}); |
| 25 | +await Comment.create({body: 'u0c1', UserId: u0.id}); |
| 26 | +await Comment.create({body: 'u1c0', UserId: u1.id}); |
| 27 | + |
| 28 | +// Direct way. |
| 29 | +{ |
| 30 | + const u0Comments = await Comment.findAll({ |
| 31 | + where: { UserId: u0.id }, |
| 32 | + order: [['id', 'ASC']], |
| 33 | + }); |
| 34 | + assert(u0Comments[0].body === 'u0c0'); |
| 35 | + assert(u0Comments[1].body === 'u0c1'); |
| 36 | + assert(u0Comments[0].UserId === u0.id); |
| 37 | + assert(u0Comments[1].UserId === u0.id); |
| 38 | + // Not added as an object by default. Would require extra query. |
| 39 | + assert(u0Comments[0].User === undefined); |
| 40 | + assert(u0Comments[1].User === undefined); |
| 41 | +} |
| 42 | + |
| 43 | +// Include data from the other side of the association in the query. |
| 44 | +{ |
| 45 | + const u0Comments = await Comment.findAll({ |
| 46 | + where: { UserId: u0.id }, |
| 47 | + order: [['id', 'ASC']], |
| 48 | + include: [{ model: User }], |
| 49 | + }); |
| 50 | + assert(u0Comments[0].body === 'u0c0'); |
| 51 | + assert(u0Comments[1].body === 'u0c1'); |
| 52 | + assert(u0Comments[0].UserId === u0.id); |
| 53 | + assert(u0Comments[1].UserId === u0.id); |
| 54 | + // These did get added now. |
| 55 | + assert(u0Comments[0].User.name === 'u0'); |
| 56 | + assert(u0Comments[1].User.name === 'u0'); |
| 57 | +} |
| 58 | + |
| 59 | +// Nicer higher level way. |
| 60 | +{ |
| 61 | + const u0Comments = await u0.getComments({ |
| 62 | + include: [{ model: User }], |
| 63 | + }); |
| 64 | + assert(u0Comments[0].body === 'u0c0'); |
| 65 | + assert(u0Comments[1].body === 'u0c1'); |
| 66 | + assert(u0Comments[0].User.name === 'u0'); |
| 67 | + assert(u0Comments[1].User.name === 'u0'); |
| 68 | +} |
| 69 | + |
| 70 | +// No way to create new item with association without explicit foreign key?? |
| 71 | +// https://stackoverflow.com/questions/34059081/how-do-i-reference-an-association-when-creating-a-row-in-sequelize-without-assum |
| 72 | +// This does not work as we would like: |
| 73 | +{ |
| 74 | + await Comment.create({body: 'u0c2', User: u0}); |
| 75 | + // We'd want 3 here. |
| 76 | + assert((await Comment.findAll({ |
| 77 | + where: { UserId: u0.id }, |
| 78 | + })).length === 2); |
| 79 | +} |
| 80 | + |
| 81 | +// Removal auto-cascades. |
| 82 | +const u0id = u0.id |
| 83 | +await u0.destroy() |
| 84 | +assert((await Comment.findAll({ |
| 85 | + where: { UserId: u0id }, |
| 86 | +})).length === 0); |
| 87 | +assert((await Comment.findAll({ |
| 88 | + where: { UserId: u1.id }, |
| 89 | +})).length === 1); |
| 90 | + |
| 91 | +await sequelize.close(); |
| 92 | +})(); |
0 commit comments