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