Skip to main content

kysera db

Database management utilities.

Commands

CommandDescription
seedRun database seeders
resetTruncate all tables
tablesList database tables
dumpExport database schema and data
restoreRestore database from dump
introspectAnalyze database schema
consoleInteractive database console

seed

Run database seeders.

kysera db seed

Options:

-f, --file <path>         Specific seed file to run
-d, --directory <path> Seed directory (default: ./seeds)
--fresh Truncate tables before seeding
--dry-run Show what would execute
--transaction Run in single transaction
-v, --verbose Detailed output

Examples:

# Run all seeds
kysera db seed

# Run specific seed file
kysera db seed --file seeds/users.ts

# Fresh seed (truncate first)
kysera db seed --fresh

# Preview
kysera db seed --dry-run

reset

Truncate all tables.

kysera db reset

Options:

--force                   Skip confirmation
--exclude <tables> Tables to exclude

tables

List database tables.

kysera db tables

Options:

--json                    Output as JSON
-v, --verbose Show column details

Output:

Database Tables
===============

users (4 columns)
id serial PRIMARY KEY
email varchar(255) NOT NULL UNIQUE
name varchar(100) NOT NULL
created_at timestamp NOT NULL

posts (5 columns)
id serial PRIMARY KEY
user_id integer REFERENCES users(id)
title varchar(255) NOT NULL
content text
created_at timestamp NOT NULL

Total: 2 tables

dump

Export database schema and data.

kysera db dump

Options:

-o, --output <path>       Output file path
--schema-only Export schema only
--data-only Export data only
--format <type> Format: sql, json
--tables <list> Specific tables

Examples:

# Full dump
kysera db dump -o backup.sql

# Schema only
kysera db dump --schema-only -o schema.sql

# Data only as JSON
kysera db dump --data-only --format json -o data.json

restore

Restore database from dump.

kysera db restore <file>

Options:

--force                   Skip confirmation prompt
-c, --config <path> Path to configuration file

Examples:

# Restore from SQL dump
kysera db restore backup.sql

# Restore from JSON dump
kysera db restore data.json

# Restore without confirmation
kysera db restore backup.sql --force

introspect

Introspect database schema and generate TypeScript types.

kysera db introspect [table]

Options:

--json                    Output as JSON
--detailed Show detailed information with TypeScript types
-c, --config <path> Path to configuration file

Examples:

# List all tables with summary
kysera db introspect

# Introspect specific table
kysera db introspect users

# Show detailed information with TypeScript types
kysera db introspect users --detailed

# Output as JSON
kysera db introspect --json

console

Interactive database console (REPL).

kysera db console

Options:

-q, --query <sql>         Execute SQL query and exit
-c, --config <path> Path to configuration file

Console Commands:

.help, .h                 Show help
.exit, .quit, .q Exit the console
.tables, .t List all tables
.describe, .d <table> Describe table structure
.indexes, .i <table> Show table indexes
.count, .c <table> Count rows in table
.clear, .cls Clear the screen

Examples:

# Open interactive console
kysera db console

# Execute single query
kysera db console -q "SELECT * FROM users LIMIT 5"

Seed File Structure

// seeds/users.ts
import { Kysely } from 'kysely'

export async function seed(db: Kysely<any>): Promise<void> {
await db
.insertInto('users')
.values([
{ email: 'admin@example.com', name: 'Admin' },
{ email: 'user@example.com', name: 'User' }
])
.execute()
}

Best Practices

1. Use Transactions for Seeds

kysera db seed --transaction

2. Order Seed Files

seeds/
├── 01_users.ts
├── 02_posts.ts
└── 03_comments.ts

3. Backup Before Reset

kysera db dump -o backup.sql
kysera db reset --force