writing a transaction that calls functions
Hey everyone, I like what I've got going here but before I did this for my whole code base I just wanted to make sure I was on the right path
11 Replies
at first I was calling each function from another file, so they are exported but after realizing tha I want them to fail if another function fails, I learned about transactions
I still wanna keep it as functions instead of having the code for all the functions as queries within the transaction callback function
but ya, if anyone can point out anything that isn't good practice, I rather know now, thanks
Hey 👋
Generally that's a reasonable pattern.
You could make the code less specific to transactions, so you could use all these functions outside of that context if you needed to (e.g. running on main instance, running on a test instance, running on a single connection instance, etc). Since a
Transaction
extends Kysely
it would work.
I just realized that my SvelteAuth adapter file calls these functions individually, so I need to be usable outside of
trx
as you are mentioning
I oped for this
but wait a minuteAgain, that's too specific to transactions
what is the difference between this
executor: Kysely<Database> = db
and by default it uses this
db
instance, but you can "inject" anything that matches Kysely<DB>
type.. including trx
, or a test instance.
it makes the code more testable and reusable
this would work of course but, when we pass in
trx
, it would not automatically assign it the type of Transaction<DB>
which is a problem. -- if I am on the right train of thought, to get around that, whatever get's passed to executor
will have the type of Kysely<Database>
and since Transaction extends from this class, you're saying it's okay for it to have that type
but does that mean, it will still be a little limiting for trx to have that type instead of the Transaction<DB>
type, since whatever it extends will not be included in Kysely<DB>
?Unless you're doing something specific to transactions in those functions, which you're not, this is completely legit
got it
thank you so much for showing me that!
Glad to share this, as I've used this in production in the past.
Unknown User•15mo ago
Message Not Public
Sign In & Join Server To View