DB Migrate TypeScript Plugin

2.0.0 · maintenance · verified Sun Apr 19

This package provides a plugin for `db-migrate` that enables the use of TypeScript for database migration files. It automatically transpiles `.ts` files found in the migration directory using your project's `tsconfig.json`. The current stable version is 2.0.0, released in December 2017. As a plugin, its release cadence is tied to the `db-migrate` ecosystem, but its own updates have been infrequent. Key differentiators include seamless integration with `db-migrate` for TypeScript-based migrations, leveraging existing `tsconfig.json` configurations, and simplifying database schema management for TypeScript projects without requiring manual transpilation steps. It requires `ts-node` as a peer dependency for runtime TypeScript compilation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure `tsconfig.json` for TypeScript migrations and provides an example of a `.ts` migration file. It also shows the npm commands to install the plugin and `ts-node`.

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "CommonJS",
    "esModuleInterop": true,
    "strict": true,
    "outDir": "./dist",
    "rootDir": "./migrations"
  },
  "include": ["./migrations/**/*.ts"]
}

// migrations/1678886400000_create_users_table.ts
import { Migration } from 'db-migrate';

export const up: Migration = async (db) => {
  await db.createTable('users', {
    id: { type: 'int', primaryKey: true, autoIncrement: true },
    name: { type: 'string', length: 50, notNull: true },
    email: { type: 'string', length: 100, notNull: true, unique: true },
    created_at: { type: 'timestamp', notNull: true, defaultValue: new Date() }
  });
};

export const down: Migration = async (db) => {
  await db.dropTable('users');
};

// To install: npm install db-migrate db-migrate-plugin-typescript ts-node @types/db-migrate --save-dev
// To run: db-migrate up

view raw JSON →