project setup
This commit is contained in:
5
src/server/api/root.ts
Normal file
5
src/server/api/root.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { createTRPCRouter } from "~/server/api/trpc";
|
||||
|
||||
export const appRouter = createTRPCRouter({});
|
||||
|
||||
export type AppRouter = typeof appRouter;
|
||||
48
src/server/api/trpc.ts
Normal file
48
src/server/api/trpc.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { initTRPC } from "@trpc/server";
|
||||
import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
|
||||
import superjson from "superjson";
|
||||
import { ZodError } from "zod";
|
||||
|
||||
import { db } from "~/server/db";
|
||||
|
||||
/**
|
||||
* 1. CONTEXT
|
||||
*/
|
||||
|
||||
type CreateContextOptions = Record<string, never>;
|
||||
|
||||
const createInnerTRPCContext = (_opts: CreateContextOptions) => {
|
||||
return {
|
||||
db,
|
||||
};
|
||||
};
|
||||
|
||||
export const createTRPCContext = (_opts: CreateNextContextOptions) => {
|
||||
return createInnerTRPCContext({});
|
||||
};
|
||||
|
||||
/**
|
||||
* 2. INITIALIZATION
|
||||
*/
|
||||
|
||||
const t = initTRPC.context<typeof createTRPCContext>().create({
|
||||
transformer: superjson,
|
||||
errorFormatter({ shape, error }) {
|
||||
return {
|
||||
...shape,
|
||||
data: {
|
||||
...shape.data,
|
||||
zodError:
|
||||
error.cause instanceof ZodError ? error.cause.flatten() : null,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* 3. ROUTER & PROCEDURE (THE IMPORTANT BIT)
|
||||
*/
|
||||
|
||||
export const createTRPCRouter = t.router;
|
||||
|
||||
export const publicProcedure = t.procedure;
|
||||
12
src/server/db/index.ts
Normal file
12
src/server/db/index.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Client } from "@planetscale/database";
|
||||
import { drizzle } from "drizzle-orm/planetscale-serverless";
|
||||
|
||||
import { env } from "~/env";
|
||||
import * as schema from "./schema";
|
||||
|
||||
export const db = drizzle(
|
||||
new Client({
|
||||
url: env.DATABASE_URL,
|
||||
}).connection(),
|
||||
{ schema },
|
||||
);
|
||||
3
src/server/db/schema.ts
Normal file
3
src/server/db/schema.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { mysqlTableCreator } from "drizzle-orm/mysql-core";
|
||||
|
||||
export const mysqlTable = mysqlTableCreator(name => `me_${name}`);
|
||||
Reference in New Issue
Block a user