Documentation
Database

Developer

Database development guide

This guide covers the project's database architecture and development conventions. The project uses Drizzle ORM as the database tool.

Database Architecture

All schema files are located in the src/db/ directory:

FileDescription
auth.schema.tsUsers, sessions, accounts, verification codes
subscription.schema.tsSubscription management
payment.schema.tsPayment records
order.schema.tsOrder management
credit.schema.tsCredit system
rbac.schema.tsAccess control (roles, permissions)
config.schema.tsSystem configuration

Using the Database

Import the db instance and schema from @/db:

import { db, user } from "@/db"
import { eq } from "drizzle-orm"

// Query
const users = await db.select().from(user)

// Conditional query
const result = await db.select().from(user).where(eq(user.id, "xxx"))

// Insert
await db.insert(user).values({ id: "xxx", name: "test", email: "[email protected]" })

// Update
await db.update(user).set({ name: "new name" }).where(eq(user.id, "xxx"))

// Delete
await db.delete(user).where(eq(user.id, "xxx"))

Type Inference

Database table types should be inferred from the schema in src/shared/types/, not manually defined:

import type { user } from "@/db/auth.schema"

// Infer complete type from table
export type User = typeof user.$inferSelect

// Infer type from enum
import type { paymentStatusEnum } from "@/db/payment.schema"
export type PaymentStatus = (typeof paymentStatusEnum.enumValues)[number]

Common Commands

# Generate migration files
pnpm db:generate

# Run migrations
pnpm db:migrate

# Push schema changes (development)
pnpm db:push

# Pull schema from database
pnpm db:pull

# Open Drizzle Studio
pnpm db:studio

Adding a New Table

Create Schema File

Create a new schema file in the src/db/ directory:

src/db/example.schema.ts
import { pgTable, text, timestamp } from "drizzle-orm/pg-core"

export const example = pgTable("example", {
  id: text("id").primaryKey(),
  name: text("name").notNull(),
  createdAt: timestamp("created_at").defaultNow().notNull(),
})

Export Schema

Export it in src/db/index.ts:

export * from "./example.schema"
import * as exampleSchema from "./example.schema"

const schema = {
  // ... other schemas
  ...exampleSchema,
}

Generate Migration

Run the command to generate the migration file:

pnpm db:generate

On this page