mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
add orpc
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
{{#if (eq api "orpc")}}
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db";
|
||||
import { todo } from "../db/schema/todo";
|
||||
import { publicProcedure } from "../lib/orpc";
|
||||
|
||||
export const todoRouter = {
|
||||
getAll: publicProcedure.handler(async () => {
|
||||
return await db.select().from(todo);
|
||||
}),
|
||||
|
||||
create: publicProcedure
|
||||
.input(z.object({ text: z.string().min(1) }))
|
||||
.handler(async ({ input }) => {
|
||||
const result = await db
|
||||
.insert(todo)
|
||||
.values({
|
||||
text: input.text,
|
||||
})
|
||||
.returning();
|
||||
return result[0];
|
||||
}),
|
||||
|
||||
toggle: publicProcedure
|
||||
.input(z.object({ id: z.number(), completed: z.boolean() }))
|
||||
.handler(async ({ input }) => {
|
||||
await db
|
||||
.update(todo)
|
||||
.set({ completed: input.completed })
|
||||
.where(eq(todo.id, input.id));
|
||||
return { success: true };
|
||||
}),
|
||||
|
||||
delete: publicProcedure
|
||||
.input(z.object({ id: z.number() }))
|
||||
.handler(async ({ input }) => {
|
||||
await db.delete(todo).where(eq(todo.id, input.id));
|
||||
return { success: true };
|
||||
}),
|
||||
};
|
||||
{{/if}}
|
||||
|
||||
{{#if (eq api "trpc")}}
|
||||
import { z } from "zod";
|
||||
import { router, publicProcedure } from "../lib/trpc";
|
||||
import { todo } from "../db/schema/todo";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db";
|
||||
|
||||
export const todoRouter = router({
|
||||
getAll: publicProcedure.query(async () => {
|
||||
return await db.select().from(todo);
|
||||
}),
|
||||
|
||||
create: publicProcedure
|
||||
.input(z.object({ text: z.string().min(1) }))
|
||||
.mutation(async ({ input }) => {
|
||||
return await db.insert(todo).values({
|
||||
text: input.text,
|
||||
});
|
||||
}),
|
||||
|
||||
toggle: publicProcedure
|
||||
.input(z.object({ id: z.number(), completed: z.boolean() }))
|
||||
.mutation(async ({ input }) => {
|
||||
return await db
|
||||
.update(todo)
|
||||
.set({ completed: input.completed })
|
||||
.where(eq(todo.id, input.id));
|
||||
}),
|
||||
|
||||
delete: publicProcedure
|
||||
.input(z.object({ id: z.number() }))
|
||||
.mutation(async ({ input }) => {
|
||||
return await db.delete(todo).where(eq(todo.id, input.id));
|
||||
}),
|
||||
});
|
||||
{{/if}}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { mysqlTable, varchar, int, boolean } from "drizzle-orm/mysql-core";
|
||||
|
||||
export const todo = mysqlTable("todo", {
|
||||
id: int("id").primaryKey().autoincrement(),
|
||||
text: varchar("text", { length: 255 }).notNull(),
|
||||
completed: boolean("completed").default(false).notNull(),
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
import { pgTable, text, boolean, serial } from "drizzle-orm/pg-core";
|
||||
|
||||
export const todo = pgTable("todo", {
|
||||
id: serial("id").primaryKey(),
|
||||
text: text("text").notNull(),
|
||||
completed: boolean("completed").default(false).notNull()
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||
|
||||
export const todo = sqliteTable("todo", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
text: text("text").notNull(),
|
||||
completed: integer("completed", { mode: "boolean" }).default(false).notNull(),
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
import prisma from "../../prisma";
|
||||
import { publicProcedure, router } from "../lib/trpc";
|
||||
|
||||
export const todoRouter = router({
|
||||
getAll: publicProcedure.query(async () => {
|
||||
return await prisma.todo.findMany({
|
||||
orderBy: {
|
||||
id: "asc"
|
||||
}
|
||||
});
|
||||
}),
|
||||
|
||||
create: publicProcedure
|
||||
.input(z.object({ text: z.string().min(1) }))
|
||||
.mutation(async ({ input }) => {
|
||||
return await prisma.todo.create({
|
||||
data: {
|
||||
text: input.text,
|
||||
},
|
||||
});
|
||||
}),
|
||||
|
||||
toggle: publicProcedure
|
||||
.input(z.object({ id: z.number(), completed: z.boolean() }))
|
||||
.mutation(async ({ input }) => {
|
||||
try {
|
||||
return await prisma.todo.update({
|
||||
where: { id: input.id },
|
||||
data: { completed: input.completed },
|
||||
});
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Todo not found",
|
||||
});
|
||||
}
|
||||
}),
|
||||
|
||||
delete: publicProcedure
|
||||
.input(z.object({ id: z.number() }))
|
||||
.mutation(async ({ input }) => {
|
||||
try {
|
||||
return await prisma.todo.delete({
|
||||
where: { id: input.id },
|
||||
});
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Todo not found",
|
||||
});
|
||||
}
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
model Todo {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
text String
|
||||
completed Boolean @default(false)
|
||||
|
||||
@@map("todo")
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
model Todo {
|
||||
id Int @id @default(autoincrement())
|
||||
text String
|
||||
completed Boolean @default(false)
|
||||
|
||||
@@map("todo")
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
model Todo {
|
||||
id Int @id @default(autoincrement())
|
||||
text String
|
||||
completed Boolean @default(false)
|
||||
|
||||
@@map("todo")
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
model Todo {
|
||||
id Int @id @default(autoincrement())
|
||||
text String
|
||||
completed Boolean @default(false)
|
||||
|
||||
@@map("todo")
|
||||
}
|
||||
Reference in New Issue
Block a user