DrX
DrX
DTDrizzle Team
Created by DrX on 10/20/2023 in #help
Slow api request using a transaction
Hi I wondered if I can remove async out of the child transactions inside the transaction function, to increase performacne or run promise.allSettled Use case is I have some code like this which works, but it is slow.
const user = db.transaction(async (transaction) => {
const invite = await transaction
.update(invites)
.set({ claimed: true })
.where(eq(invites.email, data.email))
.returning();
const userData = !userExists
? await transaction
.insert(users)
.values({
...dataObject,
wasPreviouslyWaitlisted: false,
numberOfInvites: 3,
invitedBy: invite[0].createdBy
})
.returning()
: await transaction
.update(users)
.set({
...dataObject,
wasPreviouslyWaitlisted: true,
numberOfInvites: 3,
invitedBy: invite[0].createdBy,
approvalStatus: invite[0].inviteType === 'admin' ? 'approved' : 'pending'
})
.where(eq(users.email, data.email))
.returning();

await transaction.insert(coverageOptions).values(coverageItems).onConflictDoNothing();
await transaction.insert(tickers).values(tickersOptions).onConflictDoNothing();
await transaction
.insert(userCoverageOptions)
.values(coverageItems.map((item) => ({ userId: userData[0].id, coverageOptionId: item.id })))
.onConflictDoNothing();
await transaction
.insert(userTickers)
.values(tickersOptions.map((item) => ({ userId: userData[0].id, tickerId: item.id })))
.onConflictDoNothing();

return userData;
});
const user = db.transaction(async (transaction) => {
const invite = await transaction
.update(invites)
.set({ claimed: true })
.where(eq(invites.email, data.email))
.returning();
const userData = !userExists
? await transaction
.insert(users)
.values({
...dataObject,
wasPreviouslyWaitlisted: false,
numberOfInvites: 3,
invitedBy: invite[0].createdBy
})
.returning()
: await transaction
.update(users)
.set({
...dataObject,
wasPreviouslyWaitlisted: true,
numberOfInvites: 3,
invitedBy: invite[0].createdBy,
approvalStatus: invite[0].inviteType === 'admin' ? 'approved' : 'pending'
})
.where(eq(users.email, data.email))
.returning();

await transaction.insert(coverageOptions).values(coverageItems).onConflictDoNothing();
await transaction.insert(tickers).values(tickersOptions).onConflictDoNothing();
await transaction
.insert(userCoverageOptions)
.values(coverageItems.map((item) => ({ userId: userData[0].id, coverageOptionId: item.id })))
.onConflictDoNothing();
await transaction
.insert(userTickers)
.values(tickersOptions.map((item) => ({ userId: userData[0].id, tickerId: item.id })))
.onConflictDoNothing();

return userData;
});
3 replies
DTDrizzle Team
Created by DrX on 10/18/2023 in #help
creating external types based on drizzle types
hey I'm trying to abstract some of my drizzle calls into seperate functions to refactor my code. How would I go about getting the type that my .values() enforces on the current table and replacing my data: any with that?
export async function createInvite(data: any) {
const invite = await db
.insert(data)
.values({
...data
})
.returning({ inviteId: data.id });

return invite;
}
export async function createInvite(data: any) {
const invite = await db
.insert(data)
.values({
...data
})
.returning({ inviteId: data.id });

return invite;
}
3 replies