@literallyjoel/migrations
v1.1.4
Published
Universal SQL migration utility for Bun & Node
Downloads
18
Readme
@literallyjoel/migrations
Simple, universal SQL migration management for Bun and Node.js
Features
- ✅ Works in Bun and Node.js
- 📝 Plain SQL migrations — no ORM dependency
- 🔧 Customizable migrations & rollback directories
- ⚙️ Easy configuration via
migrations.config.ts - 📊 Tracks applied migrations in your database
- 🔄 Supports Bun's native
sqlor Node'spgdriver - 🎯 TypeScript & ESM native
🚀 Installation
Using npm
npm install -D @literallyjoel/migrationsUsing pnpm
pnpm add -D @literallyjoel/migrationsUsing Bun
bun add -d @literallyjoel/migrations🔧 Setup
Create a migrations.config.ts (or .js) file in your project root:
Example (Bun with native SQL)
import { sql } from "bun";
import path from "path";
export default {
migrationsDir: path.resolve("./src/db/migrations"),
rollbackDir: path.resolve("./src/db/rollbacks"),
sql, // Bun's built-in SQL client
};Example (Node.js + pg)
import { Pool } from "pg";
import path from "path";
import "dotenv/config";
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
export default {
sql: pool, // Any client with .query()
migrationsDir: path.resolve("./migrations"),
rollbackDir: path.resolve("./rollbacks"),
};Example (ESM .js config)
import { Pool } from "pg";
import path from "path";
import dotenv from "dotenv";
dotenv.config();
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
export default {
sql: pool,
migrationsDir: path.resolve("./migrations"),
rollbackDir: path.resolve("./rollbacks"),
};Tip: Add a
.envfile with yourDATABASE_URLfor easy connection management.
📖 CLI Usage
Run commands using npx (Node) or bunx (Bun):
Create a new migration
npx migrate create --table=users
# or
bunx migrate create --table=usersThis creates a timestamped SQL file like 1234567890_users.sql in your migrations directory.
Apply all pending migrations
npx migrate applyApplies all migrations that haven't been run yet.
Apply a specific migration
npx migrate apply --file=1234567890_users.sqlRollback migrations
npx migrate rollbackRuns all rollback scripts in your rollback directory (in reverse order).
Custom directory
npx migrate create --table=posts --dir=./database/migrations💻 Programmatic Usage
You can also use the library programmatically in your code:
import {
loadConfig,
createMigration,
applyMigrations,
rollbackMigrations,
} from "@literallyjoel/migrations";
const config = await loadConfig();
// Create a migration manually
await createMigration({ table: "users" }, config);
// Apply migrations
await applyMigrations({}, config);
// Apply a specific migration
await applyMigrations({ file: "1234567890_users.sql" }, config);
// Rollback
await rollbackMigrations(config);🔍 How It Works
- Migration Storage: Migrations are stored as
.sqlfiles with timestamps - Tracking: Applied migrations are tracked in an
applied_migrationstable - Transactions: Each migration runs in a transaction for safety
- Failure Handling: If a migration fails, it's rolled back and the process stops
🌍 Environment Variables
You can configure the tool using environment variables:
MIGRATIONS_DIR=./db/migrations
ROLLBACK_DIR=./db/rollbacks
DATABASE_URL=postgresql://user:pass@localhost:5432/mydbThese will be used as defaults if no config file is found.
📝 Example Migration
migrations/1234567890_users.sql:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
name VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE INDEX idx_users_email ON users(email);rollbacks/1234567890_users.sql:
DROP TABLE IF EXISTS users;🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
MIT © Joel
🐛 Troubleshooting
"No SQL client found"
Make sure you've created a migrations.config.ts file and configured the sql property with your database client.
"Migrations directory does not exist"
The migrations directory is created automatically when you run create, but make sure the path in your config is correct.
Module not found errors
Make sure you're using Node.js 18+ and that your project is configured for ESM (has "type": "module" in package.json).
