DATABASE WORKFLOW - nself-org/cli GitHub Wiki
Complete guide to the nself database workflow: from blank folder to production.
nself aims for 3 commands to working local, 5 to production:
# Local Development (3 commands)
mkdir myapp && cd myapp
nself init
nself build
nself start
# To Production (add 2 more)
nself env create prod production
nself deploy prodFor most projects, start by designing your database schema:
# Create a project
nself init
nself build
# Create a starter schema (choose a template)
nself db schema scaffold basic # Users, profiles, posts
nself db schema scaffold ecommerce # Products, orders, cart
nself db schema scaffold saas # Organizations, members, projects
nself db schema scaffold blog # Posts, categories, commentsThis creates schema.dbml - a human-readable schema definition.
Edit schema.dbml or use dbdiagram.io to design visually:
// schema.dbml
Table users {
id serial [pk]
email varchar(255) [not null, unique]
display_name varchar(100)
role varchar(20) [default: 'user']
created_at timestamptz [default: `NOW()`]
}
Table posts {
id serial [pk]
user_id integer [not null]
title varchar(255) [not null]
content text
published boolean [default: false]
created_at timestamptz [default: `NOW()`]
}
Ref: posts.user_id > users.id
nself db schema apply schema.dbmlThis single command:
- Imports DBML โ Creates SQL migration files
- Runs migrations โ Creates tables in database
- Generates mock data โ Populates tables with test data
- Seeds users โ Creates sample accounts you can log in with
Your database is ready with:
- Schema from your DBML
- Mock data for testing
- Sample users to log in:
-
[email protected](admin role) -
[email protected](user role) -
[email protected](viewer role)
-
If you need more control over each step:
# Option A: Use a template
nself db schema scaffold saas
# Option B: Design at dbdiagram.io, download DBML
# Option C: Create schema.dbml manuallynself db schema import schema.dbmlCreates migration files:
nself/migrations/
โโโ 20260122143000_imported_schema.up.sql
โโโ 20260122143000_imported_schema.down.sql
# Review the generated SQL
cat nself/migrations/*_imported_schema.up.sql
# Run migrations
nself db migrate up
# Check status
nself db migrate status# Auto-generate based on schema
nself db mock auto
# Or with specific seed (reproducible)
nself db mock auto --seed 12345# Environment-aware user seeding
nself db seed usersAll database commands adapt to your environment:
# Everything enabled
nself db mock auto # โ
Generates mock data
nself db seed users # โ
Creates test accounts
nself db migrate fresh # โ
Allowed
nself db reset # โ
AllowedSample users created:
- 20 mock users with simple passwords
- Test accounts:
[email protected],[email protected],[email protected]
# Most operations allowed with warnings
nself db mock auto # โ
Generates mock data
nself db seed users # โ
Creates QA accounts
nself db migrate fresh # โ ๏ธ Requires confirmation
nself db reset # โ ๏ธ Requires confirmationSample users created:
- 100 mock users for load testing
- QA accounts with stronger test passwords
# Destructive operations blocked
nself db mock auto # โ Blocked
nself db migrate fresh # โ Blocked
nself db reset # โ Blocked
# Safe operations allowed
nself db migrate up # โ
Allowed
nself db seed users # โ
Only creates explicit users
nself db backup # โ
AllowedProduction users come from explicit configuration:
# Environment variable
NSELF_PROD_USERS='[email protected]:Admin:admin,[email protected]:Support:moderator'DBML (Database Markup Language) is a readable format for database schemas:
Table table_name {
column_name type [constraints]
}
| DBML Type | PostgreSQL |
|---|---|
int, integer
|
INTEGER |
bigint |
BIGINT |
serial |
SERIAL |
varchar(n) |
VARCHAR(n) |
text |
TEXT |
boolean |
BOOLEAN |
timestamp, timestamptz
|
TIMESTAMPTZ |
date |
DATE |
uuid |
UUID |
json, jsonb
|
JSONB |
decimal(p,s) |
DECIMAL(p,s) |
Table users {
id serial [pk] // Primary key
email varchar(255) [not null, unique] // Not null + unique
role varchar(20) [default: 'user'] // Default value
created_at timestamptz [default: `NOW()`] // SQL default
}
// One-to-many
Ref: posts.user_id > users.id
// Many-to-many (via junction table)
Ref: post_tags.post_id > posts.id
Ref: post_tags.tag_id > tags.id
Users, profiles, and posts - good for social apps, blogs, forums.
nself db schema scaffold basicTables: users, profiles, posts
Products, orders, and cart - good for online stores.
nself db schema scaffold ecommerceTables: users, products, categories, orders, order_items, cart_items
Organizations and teams - good for B2B applications.
nself db schema scaffold saasTables: organizations, users, organization_members, invitations, projects, api_keys
Content management - good for blogs, CMS, documentation.
nself db schema scaffold blogTables: users, posts, categories, tags, post_categories, post_tags, comments, media
- Edit
schema.dbml:
Table comments {
id serial [pk]
post_id integer [not null]
content text [not null]
created_at timestamptz [default: `NOW()`]
}
Ref: comments.post_id > posts.id
- Create migration from changes:
nself db schema import schema.dbml --name add_comments- Run migration:
nself db migrate upFor modifications, create a manual migration:
nself db migrate create add_user_avatarEdit the generated file:
-- UP
ALTER TABLE users ADD COLUMN avatar_url TEXT;
-- DOWN
ALTER TABLE users DROP COLUMN avatar_url;Run it:
nself db migrate upExport your existing database to DBML:
nself db schema diagram > current-schema.dbmlThis is useful for:
- Documenting existing databases
- Visualizing structure at dbdiagram.io
- Migrating to DBML workflow
# 1. Create and initialize
mkdir my-saas-app && cd my-saas-app
nself init
nself build
# 2. Start services
nself start
# 3. Create and apply schema
nself db schema scaffold saas
# Edit schema.dbml if needed
nself db schema apply schema.dbml
# 4. Verify
nself db inspect # Database overview
nself urls # Get API URLs
# 5. Generate types for your frontend
nself db types typescript # Creates types/db.tsYour app now has:
- Running services (Hasura, Auth, etc.)
- Database with your schema
- Mock data for testing
- Sample users to authenticate with
- TypeScript types for your frontend
# After local development is working...
# 1. Create production environment
nself env create prod production
# 2. Configure production users
export NSELF_PROD_USERS='[email protected]:Admin:admin'
# 3. Configure server (edit .environments/prod/server.json)
# {
# "host": "your-server.example.com",
# "user": "root",
# "key": "~/.ssh/id_ed25519"
# }
# 4. Deploy
nself deploy prod
# 5. Run migrations on production
ENV=production nself db migrate up
# 6. Seed production users only
ENV=production nself db seed usersProblem: Import fails with parse error
Solution: Check DBML syntax:
- Column names must be valid identifiers
- Types must be supported
- Brackets must be balanced
# Validate at dbdiagram.io before importingProblem: nself db mock auto creates no data
Solution: Ensure tables exist:
nself db migrate status # Check migrations ran
nself db schema show # Verify tables existProblem: Migration already applied error
Solution: Check status and repair if needed:
nself db migrate status
nself db migrate repair # Fix tracking table- Design schema first - Use dbdiagram.io to visualize before coding
- Use templates - Start from a scaffold, customize from there
- Keep DBML in sync - Re-export after manual migrations
- Use seeds for required data - Reference data, admin users
- Use mock for test data - Generated, reproducible, disposable
- Always test migrations - Run locally before staging/production
-
Backup before migrate -
nself db backup && nself db migrate up