12. Two requests read views = 10 at the same time, both add 1 in JavaScript and save. What is the stored value afterwards?
- 11, because one update overwrites the other
- 12, because databases serialise all writes
- An error, because the ORM detects the conflict
- 10, because neither save succeeds
Show answer
Answer: 11, because one update overwrites the other
This is a lost update. Both write 11. An atomic increment (SET views = views + 1) or optimistic locking prevents it.
Lesson: Updating, Deleting and Upserting
13. In TypeORM's find(), how do you express 'city is Pune OR city is Indore'?
- where: { city: 'Pune', city: 'Indore' }
- where: { OR: ['Pune', 'Indore'] }
- where: { city: { [Op.or]: ['Pune', 'Indore'] } }
- where: [{ city: 'Pune' }, { city: 'Indore' }] or where: { city: In(['Pune', 'Indore']) }
Show answer
Answer: where: [{ city: 'Pune' }, { city: 'Indore' }] or where: { city: In(['Pune', 'Indore']) }
TypeORM treats an array of where objects as OR, and In() generates an IN list. Op.or is Sequelize syntax, and OR is Prisma syntax.
Lesson: Filtering, Sorting and Pagination
14. In TypeORM, you save a new User whose posts array contains two new Post entities, but no posts are inserted. What is the most likely cause?
- TypeORM never saves related entities
- The relation does not have cascade enabled
- Posts must be saved before users in every case
- The DataSource has logging enabled
Show answer
Answer: The relation does not have cascade enabled
Without cascade on the OneToMany relation, TypeORM does not insert related entities when saving the parent. Enable cascade for inserts or save the posts separately.
Lesson: Nested Writes: Creating Related Records Together