System Information
-
Strapi Version: v4.2.0
-
Operating System: Ubuntu 20.04.4
-
Database: Postgres
-
Node Version: v12.13.1
-
NPM Version: 6.12.1
-
Yarn Version: NA
Hello,
We are using Strapi as one of the API services in a micro-services architecture-based application. Most of the front end is made in Angular. There are node services that help orchestrate data routing and data aggregation. My question is particularly about the data consistency of the content maintained by Strapi.
Suppose, there are two collections in Strapi; Customers and Orders. The Orders collection has a field called CustomerId, which happens to be the ID field from the Customers collection. Now, if the user deletes the Customer that had the ID used in one of the Orders, my data becomes inconsistent. What are the best practices in the context of Strapi to maintain this consistency?
Thanks for any help you can provide.
beforeDelete: Customer → Check if customerId is in any Order → throw error / do whatever?
Note: But be careful there is also a beforeDeleteMany that you also need to take into account!
1 Like
I like what @lolafkok said here, if this is multiple pods / servers etc, it’s better to keep it in one place with strapi.
Else you would need to make a multi tenancy, (I did make a plugin for V3 but never finished to release it) and it’s not updated to V4. But the idea is to have a internal API that just forwards the request on the different events like update, create delete. And the next one does the same thing.
Though as @lolafkok did suggest that is a great solution on how to keep it consitent.
Thanks. I also think it would be best to use the lifecycle hooks as suggested by @lolafkok.
@lolafkok How can I use beforeDeleteMany hook, I want to get access to the field from the list of users who are about to be deleted and send them an email before the action is completed. But seems like I am not getting access to the users id for me to achiev this.
I am currently not using an updated Strapi Environment.
You can check out this documentation:
For your problem:
Create beforeDelete
-Hook & beforeDeleteMany
-Hook and console.log
your deleted User
here. (regarding User
→ there most likely is a differentation between collectionType-Lifecycle-Hooks and User
, you might need to check this also)