0.2.0 • Published 3 years ago
@reagent/migrator v0.2.0
Slonik Migrator
Run sequential migration files against a PostgreSQL database.
Installation
Install from NPM:
$ yarn add @reagent/migratorUsage
Migration files need to start with 3 digits and end with a .sql suffix:
001_create_users.sqlPlace your migrations in a directory and then build a runner that executes them:
//
// src/db/migrator.ts
//
import path from 'path';
import { createPool } from 'slonik';
import { Migrator } from '@reagent/migrator';
const { DATABASE_URL } = process.env;
(async () => {
const migrationsPath = path.resolve(__dirname, 'migrations');
const pool = await createPool(DATABASE_URL);
const migrator = new Migrator({ migrationsPath, pool });
await migrator.migrate();
})();You can run them with ts-node or tie them into your build process:
$ yarn ts-node src/db/migrator.tsThe migrator also supports logging with @reagent/logging
or a compatible logging interface:
import { createLogger } from '@reagent/logging';
(async () => {
// ...
const logger = createLogger({ stdout: true });
const migrator = new Migrator({ migrationsPath, pool, logger });
await migrator.migrate();
})();0.2.0
3 years ago