mattiskristensen
mattiskristensen
DTDrizzle Team
Created by mattiskristensen on 11/26/2024 in #help
Passing transaction to functions
Hey everyone. Quite new to the drizzle sphere. Coming from Go, i am quite used to passing db transactions from function to function, to be able to orchestrate a bigger function from smaller ones. As an example, say i have some music track. each track can have multiple genres, and each genre could be associated with different tracks - a many-to-many relationship. Essentially, i would love to be able to create this chain of functions:
const getCompleteTrackById(id: number): Promise<CompleteTrack> => {
await db.transaction(async (tx) => {
const baseEvent = await getTrackById(tx, id);
const genres = await getTrackGenres(tx, id);
return {
...baseEvent,
genres,
}
})
const getCompleteTrackById(id: number): Promise<CompleteTrack> => {
await db.transaction(async (tx) => {
const baseEvent = await getTrackById(tx, id);
const genres = await getTrackGenres(tx, id);
return {
...baseEvent,
genres,
}
})
This, to make me able to use the getTrackGenres function independently for other types of queries, aswell as make code more readable. Is this possible somehow with TypeScript? Thanks in advance!
6 replies