CLI
Command-line interface for generating migrations and managing your database
CLI
The @deessejs/cli package provides a command-line interface for generating migrations and managing your database. It wraps drizzle-kit with automatic configuration detection.
Installation
Install the CLI package:
npm install @deessejs/cli --save-devOr install it globally:
npm install -g @deessejs/cliAvailable Commands
Prop
Type
Configuration
The CLI automatically detects your configuration from the following locations:
- config/database.ts or config/database.js
- drizzle.config.ts or drizzle.config.js
- Environment variable
DATABASE_URL
Example configuration:
// config/database.ts
import { defineConfig } from '@deessejs/collections'
import { users } from '../collections/users'
export const { collections, db } = defineConfig({
database: {
url: process.env.DATABASE_URL!
},
collections: [users]
})The CLI will automatically:
- Find your collections
- Generate the corresponding Drizzle schema
- Create migrations based on schema changes
Commands
generate
Generate database migrations based on your collection definitions:
npx @deessejs/cli generateThis command:
- Scans your collection files
- Generates Drizzle schema
- Compares with your current database schema
- Creates a new migration file in your migrations folder
Output:
✓ Generated migration: 20240110120000_initOptions:
--custom- Use a custom migrations folder--verbose- Show detailed output
npx @deessejs/cli generate --custom
npx @deessejs/cli generate --verbosemigrate
Apply pending migrations to your database:
npx @deessejs/cli migrateThis command:
- Reads migration files from your migrations folder
- Applies them in order to your database
- Tracks which migrations have been applied
Output:
✓ Applied migration: 20240110120000_init
✓ Database is up to dateOptions:
--force- Force re-run migrations (use with caution)
npx @deessejs/cli migrate --forcepush
Push schema changes directly to the database without creating migration files. Use only in development!
npx @deessejs/cli pushThis command:
- Compares your collections with the database
- Applies changes directly
- Does not create migration files
Warning: This command bypasses migration files. Use it only for prototyping or development, never in production.
Options:
--force- Push changes even if they could cause data loss
npx @deessejs/cli push --forcestudio
Open Drizzle Studio to visualize and manage your database:
npx @deessejs/cli studioThis opens a web interface at http://localhost:4983 where you can:
- Browse your database tables
- View and edit data
- Run queries
- Inspect relationships
Options:
--port- Specify a custom port
npx @deessejs/cli studio --port 3000Environment Variables
The CLI respects these environment variables:
Prop
Type
Example .env file:
# PostgreSQL
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
# MySQL
DATABASE_URL=mysql://user:password@localhost:3306/mydb
# SQLite
DATABASE_URL=file:./local.dbWorkflow
Development Workflow
During development, use push for quick iterations:
# Make changes to your collections
# Push changes directly to database
npx @deessejs/cli push
# View changes in Studio
npx @deessejs/cli studioProduction Workflow
For production, always use migrations:
# 1. Make changes to your collections
# 2. Generate a migration
npx @deessejs/cli generate
# 3. Review the generated migration file
# 4. Commit the migration to version control
# 5. Apply migration to database
npx @deessejs/cli migrateTeam Workflow
When working with a team:
- Pull latest changes - Get new migrations from your team
- Apply migrations - Run
migrateto update your local database - Make changes - Modify your collections
- Generate migration - Create a new migration file
- Commit everything - Include both collection changes and migration files
- Push to team - Your team will run
migrateto get your changes
Migration Files
Generated migration files are stored in the migrations/ folder by default:
migrations/
├── meta/
│ ├── 0000_snapshot.json
│ └── journal.json
└── 20240110120000_init.sqlMigration file structure:
-- migrations/20240110120000_init.sql
CREATE TABLE IF NOT EXISTS "users" (
"id" SERIAL PRIMARY KEY,
"name" TEXT NOT NULL,
"email" TEXT NOT NULL UNIQUE
);
ALTER TABLE "users" ADD COLUMN "created_at" TIMESTAMP DEFAULT NOW();Snapshot files:
Snapshot files (meta/0000_snapshot.json) store the complete schema state. They're used by Drizzle to detect changes and generate correct migrations.
Troubleshooting
"Configuration file not found"
Make sure you have a configuration file in one of these locations:
config/database.tsdrizzle.config.ts- Or set
DATABASE_URLenvironment variable
"No database connection"
Check your DATABASE_URL environment variable:
echo $DATABASE_URL"Migration failed"
If a migration fails:
- Check the migration file for errors
- Verify your database is accessible
- Ensure you have necessary permissions
- Use
--verboseflag for more details
npx @deessejs/cli migrate --verbose"Schema out of sync"
If your schema is out of sync with the database:
- Development: Use
pushto resync (⚠️ causes data loss) - Production: Manually create a migration to fix the schema
Best Practices
- Always use migrations in production - Never use
pushin production - Review migration files - Check generated SQL before committing
- Commit migration files - Always include migrations in version control
- Test migrations - Try migrations on a staging database first
- Backup before migrating - Always backup production databases
- Use descriptive names - Name migrations to describe what they do
Migration Naming
The CLI generates migration names automatically, but you can rename them for clarity:
20240110120000_init.sql → 0001_init.sql
20240110123000_add_users_table.sql → 0002_add_users_table.sql
20240110150000_add_email_index.sql → 0003_add_email_index.sqlNext Steps
- Getting Started - Installation and first collection
- Core Concepts - Configuration and collections
- Field Types - Available field types