Summary
To summarize, Strapi v4 doesn’t support transactions and to implement them you must manually write Knex queries or patch your app with one of the pull requests that implement transactions at a very low level, which defeats a lot of the usability of Strapi’s native entityManager and queryEngine APIs. This means that Strapi isn’t ACID compliant and data integrity is not guaranteed
Issues this causes
This raises a whole host of issues for apps and Strapi generally, primarily:
- If your app has a high number of users doing any things at once on the same data, e.g. using the same orders table, data may get corrupt or lost
- Mistakes and inconsistencies in your database can cost huge sums of money and damage customer relations for certain businesses (including the author’s), e.g. a customer’s order being wrong or not correctly charged
- Lack of transactions causes issues with multi-tenant cloud services, e.g. containers trying to access the same table may write at the same time to the same index causing inconsistencies
- Quoting this directly from stackoverflow “very few applications truly require ACID semantics everywhere. However, many applications will require them somewhere”. Missing transactions limits what applications can use Strapi and prevents adoption. Worse yet, this isn’t widely documented, not at all for the v3 → v4 migration, so it could take users a long time to realize this and cause them to really, really dislike Strapi when they find this out that could damage the likelihood users will stick with Strapi long term database - What Applications Don't Need ACID? - Stack Overflow
- Affects developer experience as instead of using simple APIs, they have to write low-level queries with Knex, a library Strapi developers may not use or want to use
History
According to the team, Strapi did support transactions in v3 when they were using bookshelf.js as Strapi’s ORM, but now they have their own implementation that doesn’t have transactions.
There have been a number of pull requests and issues related to this that seem to have fizzled out.
-
Ml folder/transactions by petersg83 · Pull Request #13350 · strapi/strapi · GitHub
-
[v4] Support Database Transactions with Knex, please · Issue #11792 · strapi/strapi · GitHub
-
Ml folder/transactions by petersg83 · Pull Request #13350 · strapi/strapi · GitHub
Temporary Solutions
As you can read in the above, Strapi’s team has provided some temporary solutions
- get your database connection, make the knex queries yourself and add the transactions to them
- patch your version of strapi with that of this pull request which may or may not break things https://github.com/strapi/strapi/pull/12715
- patch your version of strapi with a commit on the features branch that provides low level implementation (this may or may not have been merged, also may or may not break things) https://github.com/strapi/strapi/pull/13350
Reason for posting
I’m mainly posting this because I feel like this issue has got little traction, despite what a breaking issue this is for many applications in development if they were aware of this, and discussion on pull requests and issues has fizzled. This isn’t to denigrate the team in anyway, they have provided some temporary solutions and have reviewed pull requests and been involved in discussions. However, there are still two main problems with this
-
Writing Knex queries adds development time for crucial features, involves delicate code that using Strapi was supposed to avoid in the first place and allows will likely need to be replaced in v5
-
They have also said this issue isn’t a priority, despite that fact that I, and I feel like quite a few others, would argue it is and not just a minor feature requirement but a core feature that if we had know v4 didn’t have, wouldn’t have even used Strapi in the first place
Goals
I’m hoping that this post will raise awareness around this issue for those using Strapi who may have otherwise missed it, either coming from the v3 → v4 migration or those fresh to Strapi. More importantly, I’m hoping that this awareness can raise the priority of this issue and get it solved soon. If this issue affects you or will affect you, please vote for it on the strapi feedback Support Database Transactions with Knex | Developer Experience | Strapi. I don’t think this is exactly what we want but it’s a proxy for the same thing. Ideally, it would be good to just wrap all queries that need to be transacted in something akin to the v3 version found here Using Database Transactions to Write Queries in Strapi
Thanks to…
Finally worth mentioning that a lot of these issues were brought up by @willnode, @benderillo, and @tuxuuman and they actively have worked on these issues, I’m just summarising their arguments and adding a thing or two. Thank you also @alexandrebodin and @derrickmehaffy for the responses and solutions from Strapi so far
Mistakes
If I made any mistakes or missed anything, please let me know and I’ll amend the post