Database (Drizzle)

Centralized Drizzle ORM schemas, pooled database clients, and migration tooling.

Database Package (`@repo/db`)

Morax centralizes database models and connections into packages/db powered by Drizzle ORM. This allows both your server backend and Next.js server actions to share the exact same database schema and types.

Schema Definitions

Schemas are defined with full TypeScript inference:

packages/db/src/schema/users.ts
// packages/db/src/schema/users.ts
import { pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: uuid('id').defaultRandom().primaryKey(),
  name: text('name').notNull(),
  email: text('email').notNull().unique(),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

export type User = typeof users.$inferSelect;
export type NewUser = typeof users.$inferInsert;

Pooled Database Client

A singleton connection client is exported from the package index:

packages/db/src/index.ts
// packages/db/src/index.ts
import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
import * as schema from './schema';
import { env } from '@repo/env';

const pool = new Pool({
  connectionString: env.DATABASE_URL,
});

export const db = drizzle(pool, { schema });
export * from './schema';

Migrations & Drizzle Studio

Run migrations or launch the visual database GUI directly from the root workspace:

Database Commands
# Generate migration files
pnpm --filter @repo/db generate

# Push schema directly in development
pnpm --filter @repo/db push

# Open Drizzle Studio in browser
pnpm --filter @repo/db studio

Multi-App Access

Both apps/server and apps/web can query the database with zero duplication of models:

Unified Query
import { db, users } from '@repo/db';

const allUsers = await db.select().from(users);