Ley is a lightweight, driver-agnostic database migration tool for Node.js, currently at version 0.8.1. It provides both a command-line interface (CLI) and a programmatic API for managing database schema changes. Ley's core differentiators include its agnosticism towards specific database drivers (supporting `pg`, `postgres`, `mysql`, `mysql2`, `better-sqlite3`, and custom drivers without bundling them), its lightweight nature, and its transactional approach to migrations, ensuring atomicity for each change. It emphasizes working directly with your chosen driver's API, avoiding new abstractions, and enforces an append-only, immutable task chain for migrations to maintain database integrity across environments. Releases are consistent, with recent updates focusing on ESM support and improved TypeScript integration.
npm install leyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up Ley with a PostgreSQL driver (`pg`) and TypeScript. It includes `package.json` scripts, a `ley.config.ts` file for driver configuration, and an example migration file (`0000_initial.ts`) with `up` and `down` functions using ESM syntax.
Update any CLI usage or programmatic configurations from `--client` to `--driver`, and `opts.client` to `opts.driver`.
Always create new migrations for changes. Do not reorder, rename, or modify previously applied migration files. Use tools like `ley new` to ensure correct sequential or timestamped naming.
Install the appropriate database driver for your project (e.g., `npm install pg`). Then, configure `ley.config.js` (or `ts`) to provide an instance of this driver or a custom driver object.
Install `tsm` (`npm install --save-dev tsm`). Update your `package.json` scripts to run `ley` commands using `node --loader tsm ley <command>`, for example: `"migrate:up": "node --loader tsm ley up"`.
Generate new migration files with `ley new --esm` to get ESM syntax. If you convert an existing project to ESM, update `ley.config.js` and migration files to use `export default` and `export function` syntax instead of `module.exports` and `exports.up`.
Ensure the `migrations` directory exists in your project root or at the path specified in `ley.config.js` or via CLI options. Use `npx ley new <name>` to create your first migration file.
Install `tsm` (`npm install --save-dev tsm`) and update your `package.json` scripts to invoke `ley` using the `tsm` loader, e.g., `"migrate:up": "node --loader tsm ley up"`.
Install the required database driver (e.g., `npm install pg`). If using a custom driver, ensure it exports an object with a `connect` method that returns an object conforming to Ley's `LeyClient` interface (e.g., `query`, `release`, `start`, `commit`, `rollback`).
Generate new migration files using `npx ley new --esm` and update existing ones to use ESM `export async function up(...) {}` syntax. Alternatively, if your project should be CJS, remove `"type": "module"` from `package.json`.