System Information
- Strapi Version: 4.9.0
- Operating System: Ubuntu 22.04
- Database: Postgres
- Node Version: v16.19.0
- NPM Version: 8.19.3
- Yarn Version: 1.22.19
I’m creating a custom api endpoint for my website users to update tags on video content. In my controller, I have the following code.
const { tag, vod: vodId } = ctx.request.body.data;
const vod = await strapi.entityService.findOne('api::vod.vod', vodId, {
populate: { tags: true }
})
const existingTags = vod.attributes.tags
const updatedTags = vod.attributes.tags.concat([tag])
await strapi.entityService.update('api::vod.vod', vodId, {
data: {
tags: updatedTags
}
})
This code gets the list of existing vod tags, add the new tag from the user, and updates the vod record with the new tags list.
I see a potential issue here. Because Strapi’s entity service is asyncronous, I think it is possible for updates from two or more users updating tags on the same vod at the same time to interfere with each other.
Here is the potential issue, illustrated.
- entityService.findOne for user1 retrieves tags: [‘cats’, ‘dogs’]
- entityService.findOne for user2 retrieves tags: [‘cats’, ‘dogs’]
- entityService.update for user 1 sets tags: [‘cats’, ‘dogs’, ‘turtles’]
- entityService.update for user 2 sets tags: [‘cats’, ‘dogs’, ‘parrots’]
In this example, user 1’s tag addition, ‘turtles’ was overwritten, because the order of the operations was not atomic.
Is there a way to avoid this scenario in strapi?