68 |
69 |
Get All Users
70 |
71 |
77 |
78 |
79 |
Id
80 |
Name
81 |
Email
82 |
83 |
84 | {fetchAllUsers.data &&
85 | fetchAllUsers.data.map((user) => (
86 |
90 |
{user.id}
91 |
{user.name}
92 |
{user.email}
93 |
94 | ))}
95 |
96 | {/* Get one user UI */}
97 |
98 |
99 |
Get One User
100 |
101 | setUserId(String(e.target.value))}
106 | />
107 |
113 |
114 | {fetchOneUser.data && (
115 |
116 |
Name: {fetchOneUser.data.name}
117 |
Email: {fetchOneUser.data.email}
118 |
119 | )}
120 |
121 |
122 | {/* Create User */}
123 |
147 |
148 | {/* Update User */}
149 |
178 |
179 | {/* Delete User */}
180 |
181 |
182 |
Delete User
183 | setUserIdToDelete(e.target.value)}
188 | />
189 |
195 |
196 |
197 | );
198 | }
199 |
--------------------------------------------------------------------------------
/src/server/api/root.ts:
--------------------------------------------------------------------------------
1 | import { exampleRouter } from "~/server/api/routers/example";
2 | import { createTRPCRouter } from "~/server/api/trpc";
3 |
4 | /**
5 | * This is the primary router for your server.
6 | *
7 | * All routers added in /api/routers should be manually added here.
8 | */
9 | export const appRouter = createTRPCRouter({
10 | example: exampleRouter,
11 | });
12 |
13 | // export type definition of API
14 | export type AppRouter = typeof appRouter;
15 |
--------------------------------------------------------------------------------
/src/server/api/routers/example.ts:
--------------------------------------------------------------------------------
1 | import { z } from "zod";
2 | import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
3 |
4 | const idSchema = z.object({ id: z.string() });
5 |
6 | const userSchema = z.object({
7 | name: z.string(),
8 | email: z.string(),
9 | });
10 |
11 | const userUpdateSchema = z.object({
12 | id: z.string(),
13 | name: z.string(),
14 | email: z.string(),
15 | });
16 |
17 | export const exampleRouter = createTRPCRouter({
18 | //get all users
19 | getAll: publicProcedure.query(({ ctx }) => {
20 | return ctx.prisma.user.findMany();
21 | }),
22 |
23 | //get user by id
24 | getOne: publicProcedure
25 | .input(idSchema)
26 | .query(({ input, ctx }) => {
27 | return ctx.prisma.user.findUnique({
28 | where: idSchema.parse(input),
29 | });
30 | }),
31 |
32 | //create user
33 | createUser: publicProcedure
34 | .input(userSchema)
35 | .mutation(({ input, ctx }) => {
36 | return ctx.prisma.user.create({
37 | data: userSchema.parse(input),
38 | });
39 | }),
40 |
41 | //update user
42 | updateUser: publicProcedure
43 | .input(userUpdateSchema)
44 | .mutation(({ input, ctx }) => {
45 | return ctx.prisma.user.update({
46 | where: {
47 | id: input.id.toString(),
48 | },
49 | data: userUpdateSchema.parse(input),
50 | });
51 | }),
52 |
53 | //delete user
54 | deleteUser: publicProcedure
55 | .input(idSchema)
56 | .mutation(({ input, ctx }) => {
57 | return ctx.prisma.user.delete({
58 | where: idSchema.parse(input),
59 | });
60 | }),
61 | });
62 |
--------------------------------------------------------------------------------
/src/server/api/trpc.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * YOU PROBABLY DON'T NEED TO EDIT THIS FILE, UNLESS:
3 | * 1. You want to modify request context (see Part 1).
4 | * 2. You want to create a new middleware or type of procedure (see Part 3).
5 | *
6 | * TL;DR - This is where all the tRPC server stuff is created and plugged in. The pieces you will
7 | * need to use are documented accordingly near the end.
8 | */
9 | import { initTRPC } from "@trpc/server";
10 | import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
11 | import superjson from "superjson";
12 | import { ZodError } from "zod";
13 | import { prisma } from "~/server/db";
14 |
15 | /**
16 | * 1. CONTEXT
17 | *
18 | * This section defines the "contexts" that are available in the backend API.
19 | *
20 | * These allow you to access things when processing a request, like the database, the session, etc.
21 | */
22 |
23 | type CreateContextOptions = Record