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)));
  }),

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!
Was this page helpful?