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