# Drizzle Schema

> Database schema patterns using Drizzle ORM with PostgreSQL

- Skill: `majiayu000/drizzle-schema` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add majiayu000/drizzle-schema`
- Raw SKILL.md: https://api.skillmd.com/api/skills/majiayu000/drizzle-schema/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: majiayu000 (https://skillmd.com/u/majiayu000)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/majiayu000/drizzle-schema

---


# Drizzle ORM Schema Patterns

## Schema Files

- Main schema: `shared/schema.ts`
- MLOps schema: `shared/mlops-schema.ts`

## Core Tables

### Tenants (Multi-tenant)
All data is scoped to a tenant. Every query must include tenant filtering.

### Users
- Roles: `admin`, `operator`, `viewer`, `platform_admin`
- Linked to tenant via `tenantId`
- Password hashed with bcryptjs

### Sessions
- PostgreSQL-backed session store
- Used with `connect-pg-simple`

## Patterns

### Define a table:
```typescript
import { pgTable, text, serial, integer, timestamp } from "drizzle-orm/pg-core";

export const myTable = pgTable("my_table", {
  id: serial("id").primaryKey(),
  tenantId: integer("tenant_id").notNull().references(() => tenants.id),
  name: text("name").notNull(),
  createdAt: timestamp("created_at").defaultNow(),
});
```

### Create Zod schemas:
```typescript
import { createInsertSchema, createSelectSchema } from "drizzle-zod";

export const insertMyTableSchema = createInsertSchema(myTable);
export const selectMyTableSchema = createSelectSchema(myTable);
```

### Query with tenant isolation:
```typescript
import { eq, and } from "drizzle-orm";

const results = await db
  .select()
  .from(myTable)
  .where(and(eq(myTable.tenantId, tenantId), eq(myTable.id, id)));
```

## Configuration

- `drizzle.config.ts` — migration config
- Push schema: `npm run db:push`
- Connection via `DATABASE_URL` env variable

