drizzle-team / drizzle-trpc-zod

70 stars 3 forks source link

Drizzle ORM + Neon + tRPC + Zod

This is an example repo to showcase Neon database + tRPC + zod and drizzle-orm native integration.

We've implemented a native Zod module for Drizzle ORM so you can rapidly implement APIs with Zod validations

import { pgTable, serial, text } from "drizzle-orm-pg";
import { createInsertSchema } from "drizzle-zod/pg";
import { z } from "zod";

export const users = pgTable("users", {
  id: serial("id").primaryKey(),
  name: text("name").notNull(),
  email: text("email"),
  projectRole: text<"admin" | "user">("role"),
});

export const apiUser = createInsertSchema(users, {
  projectRole: z.enum(["admin", "user"]),
});

// zod schema for API user creation
export const apiCreateUser = apiUser.omit({ id: true })

To run example let's install node_modules

npm i

Prepare you Neon database and get all the needed credentials and put them to .env

## see https://neon.tech/docs/guides/node/
PGHOST='<endpoint_hostname>:<port>'
PGDATABASE='<dbname>'
PGUSER='<username>'
PGPASSWORD='<password>'
ENDPOINT_ID='<endpoint_id>'

then just

npm run start:server
npm run start:client

You can also alter src/schema.ts and generate new SQL migrations automatically with drizzle-kit just by running. Give it a try, it's very useful.

npm run generate

Help us grow!