Drizzle
Drizzle is a headless Typescript ORM with relational↗ and SQL-like↗ query APIs. It can handle database migrations and schemas, and provides a type safe database client. It also comes with Drizzle-Kit↗, a set of companion tools that help with querying your database.
Drizzle Client
The Drizzle Client is located at
src/server/db/index.ts
src/server/db/index.ts
import { env } from "~/env";
import * as schema from "./schema";
import postgres from "postgres";
const conn = postgres(env.DATABASE_URL)
export const db = drizzle(conn, { schema });
We reccommend including the database client in your tRPC Context:
src/server/api/trpc.ts
import { db } from "~/server/db";
export const createTRPCContext = async (opts: { headers: Headers }) => {
const session = await auth();
return {
db,
session,
...opts,
};
};
Schema
The Drizzle schema file can be found at
src/server/db/schema.ts
When you select NextAuth.js in combination with Prisma, the schema file is generated and set up for you with the recommended values for the
User
Session
Account
VerificationToken
Drizzle Kit
Drizzle Kit is a collection of command line tools designed to help you manage your database. T3 Stack automatically includes drizzle kit when you select Drizzle as your ORM.
package.json
"scripts": {
...
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:push": "drizzle-kit push",
"db:studio": "drizzle-kit studio",
...
},
Script Explanations
db:generate
db:migrate
db:push
db:studio
Useful Resources
Resource | Link |
---|---|
Drizzle Docs | https://orm.drizzle.team/docs/overview↗ |
Drizzle GitHub | https://github.com/drizzle-team/drizzle-orm↗ |
Auth.JS Drizzle Adapter | https://authjs.dev/getting-started/adapters/drizzle↗ |
Drizzle Kit Migration Guide | https://orm.drizzle.team/docs/kit-overview↗ |