Data Export
Flash ORM provides powerful export capabilities to extract your database data in various formats for backup, migration, analysis, or sharing.
Table of Contents
- Export Formats
- Basic Export
- Advanced Export
- Filtering & Querying
- Performance Considerations
- Use Cases
- Troubleshooting
Export Formats
JSON Format
flash export --format jsonCreates a structured JSON file:
{
"timestamp": "2024-01-15T10:30:00Z",
"version": "1.0",
"tables": {
"users": [
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"created_at": "2024-01-01T00:00:00Z"
}
],
"posts": [
{
"id": 1,
"user_id": 1,
"title": "My First Post",
"content": "Hello world!",
"created_at": "2024-01-02T00:00:00Z"
}
]
}
}CSV Format
flash export --format csvCreates separate CSV files for each table:
users.csv
posts.csv
comments.csvEach CSV includes headers and properly escaped data.
SQLite Format
flash export --format sqliteCreates a new SQLite database file with the same schema and data. Perfect for:
- Creating portable database copies
- Testing with SQLite locally
- Sharing data without requiring PostgreSQL/MySQL
Basic Export
Export All Data
# Export to default location (db/export/)
flash export
# Export to specific directory
flash export --output ./backups/
# Export with custom filename
flash export --output ./backup.jsonExport Specific Tables
# Export only users and posts tables
flash export --tables users,posts
# Export all except certain tables
flash export --exclude _flash_migrations,sessionsExport Options
flash export \
--format json \
--output ./data/ \
--tables users,posts \
--compress # Create .gz compressed filesAdvanced Export
Schema-Only Export
# Export schema without data
flash export --schema-only
# Useful for creating empty database templates
flash export --schema-only --format sqlite --output template.dbData-Only Export
# Export data without schema
flash export --data-only
# Useful for seeding databases
flash export --data-only --format json --output seeds/Incremental Export
# Export only records modified after a date
flash export --since "2024-01-01"
# Export only records modified in the last 24 hours
flash export --since "24 hours ago"Parallel Export
For large databases, Flash ORM automatically uses parallel processing:
- Concurrent Table Export: Multiple tables exported simultaneously
- Batch Processing: Large tables processed in chunks
- Memory Management: Controlled memory usage for large exports
Filtering & Querying
Custom WHERE Clauses
# Export only active users
flash export --where "users.is_active = true"
# Export posts from specific user
flash export --where "posts.user_id = 123"
# Export recent data
flash export --where "created_at > '2024-01-01'"JOIN-Based Filtering
# Export users who have posts
flash export \
--query "SELECT DISTINCT u.* FROM users u JOIN posts p ON u.id = p.user_id"
# Export with related data
flash export \
--query "SELECT u.*, COUNT(p.id) as post_count FROM users u LEFT JOIN posts p ON u.id = p.user_id GROUP BY u.id"Limit & Offset
# Export first 1000 records from each table
flash export --limit 1000
# Export records 1000-2000 from each table
flash export --limit 1000 --offset 1000Performance Considerations
Large Database Export
For databases with millions of records:
# Use parallel processing (automatic)
flash export --parallel 4
# Export in batches
flash export --batch-size 10000
# Compress output
flash export --compressMemory Optimization
# Control memory usage
flash export --memory-limit 512MB
# Use streaming for large exports
flash export --streamIndex Usage
Flash ORM automatically uses appropriate indexes for filtering:
- Date Filters: Uses date indexes when available
- ID Filters: Uses primary key indexes
- Foreign Key Filters: Uses foreign key indexes
Use Cases
Database Backup
# Daily backup
flash export --format json --compress --output ./backups/daily/
# Schema backup
flash export --schema-only --format sql --output schema.sqlData Migration
# Export from production
flash export --format json --output migration_data.json
# Import to staging
flash import --file migration_data.jsonDevelopment Seeding
# Export sample data
flash export --limit 100 --format json --output seeds/
# Create development fixtures
flash export --where "environment = 'development'" --output fixtures/Analytics & Reporting
# Export user behavior data
flash export \
--tables users,sessions,events \
--where "created_at >= '2024-01-01'" \
--format csv \
--output analytics/
# Export for BI tools
flash export --format json --compress --output bi_data.jsonTesting
# Create test fixtures
flash export --limit 50 --format json --output tests/fixtures/
# Export test database state
flash export --format sqlite --output tests/test.dbExport Configuration
flash.config.json
{
"export": {
"default_format": "json",
"output_dir": "db/export",
"compress": true,
"parallel": true,
"batch_size": 1000,
"exclude_tables": ["_flash_migrations", "sessions"]
}
}Environment Variables
# Override config with environment
EXPORT_FORMAT=json
EXPORT_COMPRESS=true
EXPORT_PARALLEL=4Import Functionality
While primarily an export tool, Flash ORM also supports basic import:
# Import JSON data
flash import --file backup.json
# Import CSV data
flash import --file data.csv --table users
# Import SQLite database
flash import --file backup.dbMonitoring & Logging
Export Progress
# Verbose output
flash export --verbose
# Progress bar (default)
flash export
# Quiet mode
flash export --quietExport Logs
# Log export operations
flash export --log export.log
# Log errors only
flash export --log errors.log --log-level errorPerformance Metrics
# Show timing information
flash export --metrics
# Output:
# Export completed in 45.2 seconds
# Tables processed: 15
# Total records: 1,234,567
# Average speed: 27,300 records/secondTroubleshooting
Common Issues
Export fails with "permission denied"
# Check output directory permissions
ls -la db/export/
# Create directory if needed
mkdir -p db/export/
# Change permissions
chmod 755 db/export/Memory errors on large exports
# Reduce batch size
flash export --batch-size 1000
# Use streaming
flash export --stream
# Increase memory limit
flash export --memory-limit 1GBSlow exports
# Check database indexes
flash status --indexes
# Add missing indexes
CREATE INDEX CONCURRENTLY idx_users_created_at ON users(created_at);
# Use parallel processing
flash export --parallel 8Incomplete exports
# Check export logs
cat export.log
# Verify table counts
flash export --count-only
# Resume interrupted export
flash export --resumeValidation
# Validate export file
flash export --validate backup.json
# Check data integrity
flash export --checksum backup.jsonRecovery
# List available backups
flash export --list
# Restore from backup
flash import --file backup.json
# Compare with current database
flash export --diff backup.jsonBest Practices
Backup Strategy
- Regular Backups: Schedule daily/weekly backups
- Multiple Formats: Keep JSON for flexibility, SQLite for testing
- Compression: Always compress large exports
- Verification: Regularly test backup restoration
Security
- Encrypt Sensitive Data: Encrypt exports containing PII
- Access Control: Restrict access to export files
- Secure Storage: Store backups in secure locations
- Retention Policy: Define backup retention periods
Performance
- Off-Peak Hours: Run large exports during low-traffic periods
- Resource Monitoring: Monitor database and system resources
- Incremental Backups: Use
--sincefor frequent small backups - Parallel Processing: Use multiple cores for large exports
Organization
Naming Convention: Use timestamps in filenames
backup_20240115_143000.json.gzDirectory Structure:
backups/ ├── daily/ ├── weekly/ ├── monthly/ └── schema/Documentation: Keep backup logs and metadata
Data export is a critical part of database management. Flash ORM's export system ensures your data is safely extracted, properly formatted, and ready for any use case - from simple backups to complex data migrations.