| 120 | User.hasOne(Address,{sourceKey: 'id'}); |
| 121 | |
| 122 | async function stuff() { |
| 123 | const newUser = await User.create({ |
| 124 | name: 'Johnny', |
| 125 | preferredName: 'John', |
| 126 | }); |
| 127 | console.log(newUser.id, newUser.name, newUser.preferredName); |
| 128 | |
| 129 | const project = await newUser.createProject({ |
| 130 | name: 'first!', |
| 131 | }); |
| 132 | |
| 133 | const ourUser = await User.findByPk(1, { |
| 134 | include: [User.associations.projects], |
| 135 | rejectOnEmpty: true, // Specifying true here removes `null` from the return type! |
| 136 | }); |
| 137 | console.log(ourUser.projects![0].name); // Note the `!` null assertion since TS can't know if we included |
| 138 | // the model or not |
| 139 | |
| 140 | const user = await sequelize.query('SELECT * FROM users WHERE name = :userName',{ |
| 141 | type: QueryTypes.SELECT, |
| 142 | replacements: { |
| 143 | userName: 'Johnny' |
| 144 | }, |
| 145 | mapToModel: true, |
| 146 | model: User |
| 147 | }) |
| 148 | } |
| 149 | |
| 150 | // Legacy models |
| 151 | |