| 193 | User.hasOne(Address, { sourceKey: "id" }); |
| 194 | |
| 195 | async function doStuffWithUser() { |
| 196 | const newUser = await User.create({ |
| 197 | name: "Johnny", |
| 198 | preferredName: "John", |
| 199 | }); |
| 200 | console.log(newUser.id, newUser.name, newUser.preferredName); |
| 201 | |
| 202 | const project = await newUser.createProject({ |
| 203 | name: "first!", |
| 204 | ownerId: 123, |
| 205 | }); |
| 206 | |
| 207 | const ourUser = await User.findByPk(1, { |
| 208 | include: [User.associations.projects], |
| 209 | rejectOnEmpty: true, // Specifying true here removes `null` from the return type! |
| 210 | }); |
| 211 | |
| 212 | // Note the `!` null assertion since TS can't know if we included |
| 213 | // the model or not |
| 214 | console.log(ourUser.projects![0].name); |
| 215 | } |