Skip to content

Commit

Permalink
drizzle-trpc-auth: query fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge committed Sep 11, 2023
1 parent 21af68b commit a07a676
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)],
});
}),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)],
});
}),
});

0 comments on commit a07a676

Please sign in to comment.