Adam
Adam
Explore posts from servers
DTDrizzle Team
Created by Adam on 12/21/2023 in #help
Drizzle with MySQL - most optimal way to insert row and immediately retrieve inserted row
Hey, thanks for helping me out :) I'm currently using Drizzle in a serverless application with PlanetScale. Here is some code where I insert a row and immediately after retrieve it:
create: privateProcedure
.mutation(async ({ ctx, input }) => {
const inserted = await ctx.db.insert(posts).values({
content: input.content,
authorId: ctx.currentUser,
});

return ctx.db
.select()
.from(posts)
.where(eq(posts.id, Number(inserted.insertId)));
}),
create: privateProcedure
.mutation(async ({ ctx, input }) => {
const inserted = await ctx.db.insert(posts).values({
content: input.content,
authorId: ctx.currentUser,
});

return ctx.db
.select()
.from(posts)
.where(eq(posts.id, Number(inserted.insertId)));
}),
My question is whether this approach is the most optimal way to use Drizzle for such a pattern, or if there's a recommended best practice or more performent way for insertions followed by immediate retrieval in this context. Thanks!
6 replies