JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 18
  • Score
    100M100P100Q59170F

Pearl.js — The TypeScript framework that does it right.

Package Exports

  • @pearl-framework/database

Readme

@pearl-framework/database

Drizzle ORM integration, Model helpers, and migrations for Pearl.js

Installation

pnpm add @pearl-framework/database drizzle-orm
pnpm add -D drizzle-kit

# Add your driver
pnpm add pg        # PostgreSQL
pnpm add mysql2    # MySQL
pnpm add better-sqlite3  # SQLite

Usage

Define a schema

import { pgTable, serial, varchar, timestamp } from '@pearl-framework/database'

export const users = pgTable('users', {
  id:        serial('id').primaryKey(),
  name:      varchar('name', { length: 255 }).notNull(),
  email:     varchar('email', { length: 255 }).notNull().unique(),
  createdAt: timestamp('created_at').defaultNow(),
})

Define a model

import { Model } from '@pearl-framework/database'

export class User extends Model<typeof users> {
  static table = users
}

Query

const db = app.make(DatabaseManager).db

await User.all(db)
await User.find(db, 1)
await User.findOrFail(db, 1)
await User.create(db, { name: 'Sharvari', email: 'hi@pearl.dev' })
await User.update(db, 1, { name: 'Updated' })
await User.delete(db, 1)
await User.count(db)

// Raw Drizzle query
const admins = await db.select().from(users).where(eq(users.role, 'admin'))

DatabaseServiceProvider

export class AppDatabaseServiceProvider extends DatabaseServiceProvider {
  protected config = {
    connection: {
      driver:   'postgres' as const,
      host:     process.env.DB_HOST!,
      port:     Number(process.env.DB_PORT),
      user:     process.env.DB_USER!,
      password: process.env.DB_PASSWORD!,
      database: process.env.DB_NAME!,
    },
    migrationsFolder: './database/migrations',
    runMigrationsOnBoot: true,
  }
}

Migrations

# Generate migrations from schema changes
npx drizzle-kit generate:pg --schema=./src/schema

# Apply migrations
pnpm pearl migrate