Latter now supports multiple database engines with automatic adapter detection and consistent APIs.
- Connection String:
sqlite:./path/to/database.db - Features:
- Built-in support via Bun's native SQLite
- File-based database
- No external dependencies
- Perfect for development and small applications
- Connection String:
postgresql://username:password@host:port/database - Features:
- Connection pooling for better performance
- Automatic transaction management
- PostgreSQL-specific data types support
- Production-ready with proper error handling
- Connection String:
mysql://username:password@host:port/database - Features:
- Connection pooling for better performance
- Session configuration for compatibility
- UTF8MB4 charset support
- Optimized for MySQL best practices
Latter automatically detects the appropriate database adapter based on the connection string:
import { Latter } from 'latter';
// SQLite - automatically uses SQLiteAdapter
const sqliteLatter = new Latter({
database: 'sqlite:./app.db',
migrationsDir: './migrations'
});
// PostgreSQL - automatically uses PostgresAdapter
const postgresLatter = new Latter({
database: 'postgresql://user:pass@localhost:5432/mydb',
migrationsDir: './migrations'
});
// MySQL - automatically uses MySQLAdapter
const mysqlLatter = new Latter({
database: 'mysql://user:pass@localhost:3306/mydb',
migrationsDir: './migrations'
});You can also provide custom database adapters:
import { Latter } from 'latter';
import { CustomAdapter } from './custom-adapter';
const latter = new Latter({
database: 'custom://connection-string',
migrationsDir: './migrations',
adapter: new CustomAdapter('custom://connection-string')
});Each database adapter creates a migrations table with the appropriate schema:
CREATE TABLE latter_migrations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
version TEXT NOT NULL,
timestamp INTEGER NOT NULL,
applied_at DATETIME DEFAULT CURRENT_TIMESTAMP,
checksum TEXT NOT NULL
);CREATE TABLE latter_migrations (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
version VARCHAR(255) NOT NULL,
timestamp BIGINT NOT NULL,
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
checksum VARCHAR(255) NOT NULL
);CREATE TABLE latter_migrations (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
version VARCHAR(255) NOT NULL,
timestamp BIGINT NOT NULL,
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
checksum VARCHAR(255) NOT NULL,
INDEX idx_name (name),
INDEX idx_timestamp (timestamp)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;The following packages are required for the new database adapters:
{
"dependencies": {
"pg": "^8.11.3", // PostgreSQL
"mysql2": "^3.6.5" // MySQL
}
}import { Latter } from 'latter';
const latter = new Latter({
database: 'postgresql://postgres:password@localhost:5432/myapp',
migrationsDir: './migrations',
verbose: true
});
try {
await latter.migrate();
const status = await latter.status();
console.log('Migration status:', status);
} finally {
await latter.close();
}import { Latter } from 'latter';
const latter = new Latter({
database: 'mysql://root:password@localhost:3306/myapp',
migrationsDir: './migrations',
verbose: true
});
try {
await latter.migrate();
const status = await latter.status();
console.log('Migration status:', status);
} finally {
await latter.close();
}All database adapters work seamlessly with the CLI:
# PostgreSQL
latter migrate --database postgresql://user:pass@localhost:5432/db --migrations-dir ./migrations
# MySQL
latter migrate --database mysql://user:pass@localhost:3306/db --migrations-dir ./migrations
# SQLite
latter migrate --database sqlite:./app.db --migrations-dir ./migrationsThe library includes comprehensive tests for all database adapters:
# Run all tests
bun test
# Run specific adapter tests
bun test test/adapters.test.ts- Connection Management: Always call
latter.close()to properly close database connections - Error Handling: Wrap migration operations in try-catch blocks
- Environment Variables: Use environment variables for database credentials in production
- Connection Pooling: PostgreSQL and MySQL adapters automatically handle connection pooling
- Transactions: All migrations run within transactions for data consistency
- Support for additional database engines (MongoDB, Redis, etc.)
- Migration dependency resolution
- Parallel migration execution
- Migration validation and linting