From a07a676bea2d27b3000988f05ed104ce24a38d40 Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Mon, 11 Sep 2023 23:02:29 +0200 Subject: [PATCH] drizzle-trpc-auth: query fixes --- .../src/server/api/routers/post/with-auth-drizzle.ts | 11 +++++++---- .../src/server/api/routers/post/with-drizzle.ts | 8 +++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cli/template/extras/src/server/api/routers/post/with-auth-drizzle.ts b/cli/template/extras/src/server/api/routers/post/with-auth-drizzle.ts index 60448ff2df..7671971c08 100644 --- a/cli/template/extras/src/server/api/routers/post/with-auth-drizzle.ts +++ b/cli/template/extras/src/server/api/routers/post/with-auth-drizzle.ts @@ -7,13 +7,16 @@ import { } from "~/server/api/trpc"; import { posts } from "~/server/db/schema"; -export const createPost = publicProcedure +export const createPost = protectedProcedure .input(z.object({ text: z.string().min(1) })) .mutation(async ({ ctx, input }) => { // simulate a slow db call await new Promise((resolve) => setTimeout(resolve, 1000)); - await ctx.db.insert(posts).values({ text: input.text }); + await ctx.db.insert(posts).values({ + text: input.text, + createdById: ctx.session.user.id, + }); }); export const postRouter = createTRPCRouter({ @@ -28,8 +31,8 @@ export const postRouter = createTRPCRouter({ create: createPost, getLatest: publicProcedure.query(({ ctx }) => { - return ctx.db.query.post.findFirst({ - orderBy: { createdAt: "desc" }, + return ctx.db.query.posts.findFirst({ + orderBy: (posts, { desc }) => [desc(posts.createdAt)], }); }), diff --git a/cli/template/extras/src/server/api/routers/post/with-drizzle.ts b/cli/template/extras/src/server/api/routers/post/with-drizzle.ts index 4d8cc1b639..ec92d827dd 100644 --- a/cli/template/extras/src/server/api/routers/post/with-drizzle.ts +++ b/cli/template/extras/src/server/api/routers/post/with-drizzle.ts @@ -9,7 +9,9 @@ export const createPost = publicProcedure // simulate a slow db call await new Promise((resolve) => setTimeout(resolve, 1000)); - await ctx.db.insert(posts).values({ text: input.text }); + await ctx.db.insert(posts).values({ + text: input.text, + }); }); export const postRouter = createTRPCRouter({ @@ -24,8 +26,8 @@ export const postRouter = createTRPCRouter({ create: createPost, getLatest: publicProcedure.query(({ ctx }) => { - return ctx.db.query.post.findFirst({ - orderBy: { createdAt: "desc" }, + return ctx.db.query.posts.findFirst({ + orderBy: (posts, { desc }) => [desc(posts.createdAt)], }); }), });