-
-
Notifications
You must be signed in to change notification settings - Fork 2
DATABASE WORKFLOW
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:
-
admin@example.com(admin role) -
user@example.com(user role) -
demo@example.com(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:
admin@example.com,user@example.com,demo@example.com
# 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='admin@company.com:Admin:admin,support@company.com: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='admin@company.com: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
ɳSelf CLI v1.0.9. MIT licensed. Docs CC BY 4.0.
GitHub · Issues · Discussions · nself.org · nself.org/docs
Getting Started
Commands
- Commands, Overview
- Lifecycle: cmd-init · cmd-build · cmd-start · cmd-stop · cmd-restart · cmd-dev
- Monitoring: cmd-status · cmd-logs · cmd-health · cmd-urls · cmd-doctor · cmd-monitor · cmd-alerts · cmd-sentry · cmd-watchdog
- Data: cmd-db · cmd-backup · cmd-dr · cmd-queue · cmd-webhooks
- Config: cmd-config · cmd-service · cmd-env · cmd-promote
- Networking: cmd-ssl · cmd-trust · cmd-dns-setup
- Security: cmd-access · cmd-security · cmd-secrets
- Tenancy: cmd-tenant · cmd-billing
- Plugins: cmd-plugin · cmd-license · cmd-dogfood (extracted, CLI-R11) · cmd-k8s (extracted, CLI-R11) · cmd-encryption (extracted, CLI-R11) · cmd-waf (extracted, CLI-R11) · cmd-federation (extracted, CLI-R11) · cmd-mail (extracted, CLI-R11) · cmd-dlq (extracted, CLI-R11)
- AI: cmd-ai · cmd-claw · cmd-model
- Templates: cmd-template
- Utilities: cmd-exec · cmd-clean · cmd-reset · cmd-update · cmd-upgrade · cmd-version · cmd-admin · cmd-migrate · cmd-migrate-firebase · cmd-migrate-supabase · cmd-completion
Features
- Features, Overview
- Feature-Auth
- Feature-Storage
- Feature-Search
- Feature-Functions
- Feature-Email
- Feature-Monitoring
- Feature-Plugins
- Feature-nClaw, AI Assistant
- Feature-nChat, Messaging
- Feature-nTV, Media Player
- Feature-nFamily, Family Social
- Feature-nCloud, Managed Hosting
- Feature-Memory-Rooms, Knowledge Organization
- Feature-Agent-Dashboard, Agent Metrics
- Feature-Image-Generation, AI Image Generation
Configuration
- Configuration, Overview
- Config-Env-Vars
- Config-Postgres
- Config-Hasura
- Config-Auth
- Config-Nginx
- Config-Optional-Services
- Config-Custom-Services
- Config-System
Plugins (87 + 10 monitoring)
Free (25)
- plugin-backup
- plugin-content-acquisition
- plugin-content-progress
- plugin-cron
- plugin-donorbox
- plugin-feature-flags
- plugin-github
- plugin-github-runner
- plugin-invitations
- plugin-jobs
- plugin-link-preview
- plugin-mdns
- plugin-mlflow
- plugin-monitoring
- plugin-notifications
- plugin-notify
- plugin-paypal
- plugin-search
- plugin-shopify
- plugin-stripe
- plugin-subtitle-manager
- plugin-tokens
- plugin-torrent-manager
- plugin-vpn
- plugin-webhooks
Pro (62)
- plugin-access-controls
- plugin-activity-feed
- plugin-admin-api
- plugin-nself-ai-gateway
- plugin-nself-ai-mcp
- plugin-nself-ai-mcp
- plugin-analytics
- plugin-auth
- plugin-backup-pro
- plugin-bots
- plugin-browser
- plugin-calendar
- plugin-cdn
- plugin-chat
- plugin-claw
- plugin-claw-budget
- plugin-claw-news
- plugin-claw-web
- plugin-cloudflare
- plugin-cms
- plugin-compliance
- plugin-cron-pro
- plugin-ddns
- plugin-devices
- plugin-documents
- plugin-donorbox-pro
- plugin-entitlements
- plugin-epg
- plugin-file-processing
- plugin-game-metadata
- plugin-geocoding
- plugin-geolocation
- plugin-google
- plugin-home
- plugin-idme
- plugin-knowledge-base
- plugin-linkedin
- plugin-livekit
- plugin-media-processing
- plugin-meetings
- plugin-moderation
- plugin-mux
- plugin-notify-pro
- plugin-object-storage
- plugin-observability
- plugin-paypal-pro
- plugin-photos
- plugin-podcast
- plugin-post
- plugin-realtime
- plugin-recording
- plugin-retro-gaming
- plugin-rom-discovery
- plugin-shopify-pro
- plugin-social
- plugin-sports
- plugin-stream-gateway
- plugin-streaming
- plugin-stripe-pro
- plugin-support
- plugin-tmdb
- plugin-voice
- plugin-web3
- plugin-workflows
Planned (26)
plugin-auditplugin-blogplugin-checkoutplugin-commerceplugin-drmplugin-exportplugin-flowplugin-importplugin-ldapplugin-mailgunplugin-mediaplugin-oauth-providersplugin-pagesplugin-postmarkplugin-rate-limitplugin-reportsplugin-samlplugin-schedulerplugin-sendgridplugin-ssoplugin-subscriptionplugin-thumbplugin-transcoderplugin-twilioplugin-wafplugin-watermark
Guides
- Guide-Production-Deployment
- Guide-SSL-Setup
- Guide-Multi-Tenancy
- Guide-Security-Hardening
- Guide-Monitoring-Setup
- Guide-Backup-Restore
- Guide-Custom-Services
- Guide-Migration-from-v1
Architecture
Reference
- API-Reference
- error-codes, Error Codes
Licensing
Security
Brand
Operations
- operations/release-cascade, Release Cascade
- operations/self-healing, Self-Healing Schema
- operations/redis-tuning, Redis Pool Tuning
- operations/meilisearch-warmup, MeiliSearch Warm-Up
- operations/jwt-rotation, JWT Key Rotation
- operations/windows-wsl2-setup, Windows / WSL2 Setup
- operations/gemini-oauth-reauth, Gemini OAuth Reauth
Contributing
Admin
- USER-ACTION-QUEUE, Pending Admin Actions
All commands (52)
- A: cmd-access · cmd-account · cmd-admin
- B: cmd-backup · cmd-build · cmd-bundle
- C: cmd-ci · cmd-clean · cmd-completion · cmd-config
- D: cmd-db · cmd-deploy · cmd-dev · cmd-doctor
- E: cmd-env · cmd-exec
- F: cmd-functions
- G: cmd-generate
- H: cmd-health · cmd-help-topics
- I: cmd-init · cmd-install
- L: cmd-license · cmd-login · cmd-logout · cmd-logs
- M: cmd-man · cmd-mcp · cmd-migrate
- O: cmd-oauth · cmd-ops
- P: cmd-plugin · cmd-promote
- R: cmd-remove · cmd-reset · cmd-restart · cmd-runner
- S: cmd-secrets · cmd-security · cmd-self-heal · cmd-server · cmd-service · cmd-start · cmd-status · cmd-stop
- T: cmd-telemetry · cmd-template · cmd-trust
- U: cmd-update · cmd-urls
- V: cmd-verify-sbom · cmd-version