mathiasgruber
mathiasgruber
DTDrizzle Team
Created by mathiasgruber on 6/18/2023 in #help
Running parallel queries
I thought I could be clever with something like this to speed up an endpoint with several queries:
return await ctx.drizzle.transaction(
async (tx) => {
const threadId = nanoid();
const [board, user] = await Promise.all([
fetchBoard(tx, input.board_id),
fetchUser(tx, ctx.userId),
tx.insert(forumThread).values({
id: threadId,
title: input.title,
boardId: input.board_id,
userId: ctx.userId,
}),
tx.insert(forumPost).values({
id: nanoid(),
content: input.content,
threadId: threadId,
userId: ctx.userId,
}),
tx
.update(forumBoard)
.set({ nThreads: sql`nThreads + 1` })
.where(eq(forumBoard.id, input.board_id)),
]);

if (user.isBanned) {
throw serverError("UNAUTHORIZED", "You are banned");
}
if (!board) {
throw serverError("UNAUTHORIZED", "Board does not exist");
}
return threadId;
},
{
isolationLevel: "read uncommitted",
accessMode: "read write",
}
);
return await ctx.drizzle.transaction(
async (tx) => {
const threadId = nanoid();
const [board, user] = await Promise.all([
fetchBoard(tx, input.board_id),
fetchUser(tx, ctx.userId),
tx.insert(forumThread).values({
id: threadId,
title: input.title,
boardId: input.board_id,
userId: ctx.userId,
}),
tx.insert(forumPost).values({
id: nanoid(),
content: input.content,
threadId: threadId,
userId: ctx.userId,
}),
tx
.update(forumBoard)
.set({ nThreads: sql`nThreads + 1` })
.where(eq(forumBoard.id, input.board_id)),
]);

if (user.isBanned) {
throw serverError("UNAUTHORIZED", "You are banned");
}
if (!board) {
throw serverError("UNAUTHORIZED", "Board does not exist");
}
return threadId;
},
{
isolationLevel: "read uncommitted",
accessMode: "read write",
}
);
However, unless I await on each query, this route will frequently fail in terms of nothing getting updated on the database, with no errors on my side. Is this to be expected?
1 replies