Replies: 2 comments
-
I just tested my edited code and this does not work. The problem is with
The functions db.task and db.tx require a callback function. How can I solve this? |
Beta Was this translation helpful? Give feedback.
0 replies
-
It is well within the library's protocol, to create a task and then a transaction from it, if needed: await db.task(async t => {
if(/* a transaction is needed */) {
await t.tx(async t1 => {
// transaction queries against t1 context
});
}
}); For more flexibility, the library has methods taskIf and txIf. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm using express as a framework for my node.js backend. Currently, a route looks like this (simplified):
I have routes for selecting data, inserting, updating, ... but I also have a lot of routes where data is first selected, and then inserted/updated/deleted (the select query is for a check if the user can do the insert/update/delete operation).
As you can see, currently, my "setup" is just starting a transaction in the beginning of the route and then adding ALL the necessary code in the then-block of the transaction, even if I only have select-queries.
First of all: I know I should change the select queries to tasks instead of transactions. But what if I have select-queries and then insert-queries? Should I do something like this:
This code is really simplified, but I think you get my point.
The second code block, is this the way to go?
For routes with only 1 select-query, I'm planning to use the db-connection directly (without task).
For routes with only 1 insert-query, I probably also need to use the transaction stuff?
Beta Was this translation helpful? Give feedback.
All reactions