From f7fcb4d630bddf28cda00c8dbdf7bc043ff0d52e Mon Sep 17 00:00:00 2001 From: Data Salaryman Date: Tue, 16 Jul 2024 00:43:25 +0800 Subject: [PATCH] fix(cli/template): add code attempting to resolve #1943 --- .../src/server/api/routers/post/with-auth-drizzle.ts | 6 +++++- .../src/server/api/routers/post/with-auth-prisma.ts | 8 ++++++-- .../extras/src/server/api/routers/post/with-drizzle.ts | 6 +++++- .../extras/src/server/api/routers/post/with-prisma.ts | 8 +++++++- 4 files changed, 23 insertions(+), 5 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 1fc4a3c486..138e47a394 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 @@ -29,9 +29,13 @@ export const postRouter = createTRPCRouter({ }), getLatest: publicProcedure.query(({ ctx }) => { - return ctx.db.query.posts.findFirst({ + const post = ctx.db.query.posts.findFirst({ orderBy: (posts, { desc }) => [desc(posts.createdAt)], }); + if (post === undefined) { + return null; + } + return post; }), getSecretMessage: protectedProcedure.query(() => { diff --git a/cli/template/extras/src/server/api/routers/post/with-auth-prisma.ts b/cli/template/extras/src/server/api/routers/post/with-auth-prisma.ts index 3994691ed1..088d0dc13d 100644 --- a/cli/template/extras/src/server/api/routers/post/with-auth-prisma.ts +++ b/cli/template/extras/src/server/api/routers/post/with-auth-prisma.ts @@ -29,11 +29,15 @@ export const postRouter = createTRPCRouter({ }); }), - getLatest: protectedProcedure.query(({ ctx }) => { - return ctx.db.post.findFirst({ + getLatest: publicProcedure.query(async ({ ctx }) => { + const post = ctx.db.post.findFirst({ orderBy: { createdAt: "desc" }, where: { createdBy: { id: ctx.session.user.id } }, }); + if (post === undefined) { + return null; + } + return post; }), getSecretMessage: protectedProcedure.query(() => { 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 b8e95d2939..ada814a119 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 @@ -24,8 +24,12 @@ export const postRouter = createTRPCRouter({ }), getLatest: publicProcedure.query(({ ctx }) => { - return ctx.db.query.posts.findFirst({ + const post = ctx.db.query.posts.findFirst({ orderBy: (posts, { desc }) => [desc(posts.createdAt)], }); + if (post === undefined) { + return null; + } + return post; }), }); diff --git a/cli/template/extras/src/server/api/routers/post/with-prisma.ts b/cli/template/extras/src/server/api/routers/post/with-prisma.ts index 68367a35b6..d9e46a1e6c 100644 --- a/cli/template/extras/src/server/api/routers/post/with-prisma.ts +++ b/cli/template/extras/src/server/api/routers/post/with-prisma.ts @@ -25,8 +25,14 @@ export const postRouter = createTRPCRouter({ }), getLatest: publicProcedure.query(({ ctx }) => { - return ctx.db.post.findFirst({ + const post = ctx.db.post.findFirst({ orderBy: { createdAt: "desc" }, }); + + if (post === undefined) { + return null; + } + + return post; }), });