Skip to content

Database

TSKit uses PostgreSQL with Drizzle ORM for database access. The database layer handles schema definitions, migrations, and seeding.

The Drizzle client is created in database/index.ts and reads the DATABASE_URL environment variable. All services import the client from here.

Table definitions live in database/schemas/, organized by domain:

FileTables
auth.tsusers, sessions, accounts, verifications, twoFactors, organizations, members, invitations
billing.tsplans, planPrices, customers, subscriptions, usage, webhookEvents
settings.tsuserSettings
audit.tsauditLogs

The auth schema is generated by Better Auth CLI. If you modify the auth config (add plugins, change fields), regenerate it with:

Terminal window
bun run auth:generate

For other schemas, you define tables using Drizzle’s schema builder:

export const plans = pgTable('plans', {
id: uuid('id').default(sql`gen_random_uuid()`).primaryKey(),
slug: text('slug').notNull().unique(),
name: text('name').notNull(),
price: integer('price').default(0).notNull(),
entitlements: jsonb('entitlements')
.$type<Record<string, boolean | number>>()
.default({})
.notNull(),
active: boolean('active').default(true).notNull(),
createdAt: timestamp('created_at').defaultNow().notNull(),
})

After changing a schema file, generate a migration:

Terminal window
bun run db:generate

This creates a new SQL migration file in database/migrations/. Review the generated SQL, then apply it:

Terminal window
bun run db:migrate

Migrations run in order based on their filename. Never edit an already-applied migration - create a new one instead.

The seed script at database/seed.ts populates the database with sample data for development. Run it with:

Terminal window
bun run db:seed

This creates sample users, teams, plans, and subscriptions so you can test features without setting up everything manually.

FilePurpose
database/index.tsDrizzle client
database/schemas/auth.tsAuth tables (generated by Better Auth CLI)
database/schemas/billing.tsBilling tables (plans, subscriptions, usage)
database/schemas/settings.tsUser settings
database/schemas/audit.tsAudit log table
database/seed.tsDevelopment seed script
database/migrations/SQL migration files