-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SRI - Introduce Prisma transactions #741
Comments
NestJS Scope hierarchy Imagine the following dependency graph: CatsController <- CatsService <- CatsRepository. If CatsService is request-scoped (and the others are default singletons), the CatsController will become request-scoped as it is dependent on the injected service. The CatsRepository, which is not dependent, would remain singleton-scoped. Transient-scoped dependencies don't follow that pattern. If a singleton-scoped DogsService injects a transient LoggerService provider, it will receive a fresh instance of it. However, DogsService will stay singleton-scoped, so injecting it anywhere would not resolve to a new instance of DogsService. In case it's desired behavior, DogsService must be explicitly marked as TRANSIENT as well. |
More Resources: |
What is the issue if the controller ends up request scoped because an agent injects a dbContext which is also request scoped? Could we just use the default SINGLETON scope for the dbContext? How would it behave when there are multiple concurrent requests? |
For a request-scoped controller, a new instance is created for each inbound request, and garbage-collected when the request has completed processing.
Its a good question. With prisma transaction implementation we will probably avoid race condition-like problem with accessing and altering stuff in the DB, I guess there is no guarantee of the order of the concurrent requests. hopefully that is the worst of it. I suppose it doesn't do much to hurt our current implementation, but would inevitably need a Queue-ish tool. Only one way to find out. |
Exactly, so the only issue could be that your memory footprint is larger in high-load scenarios, right? This is not something we need to worry about right now in my eyes |
Update on this issue:
|
@Kasshern is there any other place where we need to apply the agreed approach or this one can be closed? |
Overview
We are currently not working with transactions, meaning that a failed execution of a command could leave the db in a incomplete state. In order to allow for transactions, we need to refactor the current approach we do persistence in a way that all db updates are collected and commited and the end of the execution of a command handler.
This is one approach we can use : https://www.prisma.io/docs/guides/performance-and-optimization/prisma-client-transactions-guide#scenario-pre-computed-ids-and-the-transaction-api
We precompute Ids, collect all storage actions from the relevant storage agents and then execute a single prisma transaction at the end of the command handler. Best way to achieve this is to have a provider called i.e dbContext, which is scoped as REQUEST
that is injected in every agent and serves as the place where we collect all the db actions created by storage agents which are invoked by regular agents. This dbContext is in the end passed to prisma.transaction call, so that db actions are executed in order
as part of a single transaction.
Reference
Given above
Questions
Acceptance
Tasks
The text was updated successfully, but these errors were encountered: