Basically this allows someone to write a bunch of business logic that might span several different function calls etc. while having all the ORM database calls automatically use a transaction that was started earlier in the callstack, without having to pass an object like session around and manually provide it to each ORM call.
I think this would be a pretty powerful alternative interface for transactions
Motivation
We are writing an API where we have a use-case that requires us to start a transaction at the beginning of every request and then use it for each DB operation that occurs within that request's business logic, before committing the transaction at the end.
Currently that requires us to find our own way to pass the session object around, eg:
async function requestHandler() {
await connection.withTransaction(async (session) => {
await someBusinessLogic(session)
}
}
async function someBusinessLogic(session) {
// need to explicitly provide session
MyModel.findOneAndUpdate({}, {}, {session})
}
but with this proposal you wouldn't need to provide the session:
async function requestHandler() {
await connection.withTransactionContext(async (session) => {
await someBusinessLogic()
}
}
async function someBusinessLogic() {
// automatically uses transaction by finding it via AsyncLocalStorage context provided by `withTransactionContext` wrapper
MyModel.findOneAndUpdate({}, {})
}
Prerequisites
🚀 Feature Proposal
A few other ORMs use the NodeJS
AsyncLocalStorage
API to provide a more ergonomic way of using transactions. One example is Sequelize: https://sequelize.org/docs/v7/querying/transactions/#disabling-clsBasically this allows someone to write a bunch of business logic that might span several different function calls etc. while having all the ORM database calls automatically use a transaction that was started earlier in the callstack, without having to pass an object like
session
around and manually provide it to each ORM call.I think this would be a pretty powerful alternative interface for transactions
Motivation
We are writing an API where we have a use-case that requires us to start a transaction at the beginning of every request and then use it for each DB operation that occurs within that request's business logic, before committing the transaction at the end.
Currently that requires us to find our own way to pass the session object around, eg:
but with this proposal you wouldn't need to provide the session:
Example
No response