Skip to main content

Overview

Migration providers supply migrations to the Migrator class. Kysely provides a built-in FileMigrationProvider for Node.js that reads migrations from a folder, but you can implement custom providers for other use cases.

MigrationProvider Interface

The base interface that all migration providers must implement.

getMigrations()

Returns all migrations, both old and new. The keys of the returned object are migration names and values are the migrations. The order of migrations is determined by the alphabetical order of the migration names. The items in the object don’t need to be sorted - Kysely sorts them automatically.

Example Implementation

FileMigrationProvider

Reads all migrations from a folder in Node.js. This provider automatically imports migration files with the following extensions:
  • .js
  • .ts (excluding .d.ts)
  • .mjs
  • .mts (excluding .d.mts)

Constructor

Configuration

FileMigrationProviderProps

fs
FileMigrationProviderFS
required
A file system implementation. In Node.js, use promises as fs from the node:fs module.The object must have a readdir(path: string): Promise<string[]> method.
path
FileMigrationProviderPath
required
A path utility implementation. In Node.js, use the path module.The object must have a join(...path: string[]): string method.
migrationFolder
string
required
The absolute or relative path to the folder containing migration files.

Example

Complete Example with Migrator

Migration File Format

Migration files should export a Migration object with up and optionally down methods:
Alternatively, you can use default exports:

How It Works

  1. File Discovery: The provider reads all files from the specified migrationFolder
  2. File Filtering: Only files with valid extensions (.js, .ts, .mjs, .mts) are processed
  3. Dynamic Import: Each file is dynamically imported using import()
  4. Migration Extraction: The provider handles both default exports and named exports
  5. Key Generation: Migration keys are derived from filenames (without extension)

Naming Conventions

Migration files should follow a naming convention that ensures proper ordering:
  • 001_initial_schema.ts
  • 002_add_users_table.ts
  • 003_add_posts_table.ts
  • 2024_01_15_create_comments.ts
The migration name (key) is the filename without the extension. Kysely sorts migrations alphabetically by these keys.

Helper Types

FileMigrationProviderFS

Interface for the file system dependency.
In Node.js, you can use:

FileMigrationProviderPath

Interface for the path utility dependency.
In Node.js, you can use:

Custom Migration Providers

You can create custom migration providers for different storage mechanisms (databases, remote APIs, etc.):

Best Practices

  1. Consistent Naming: Use a consistent naming scheme for migration files (e.g., timestamp prefix)
  2. One Change Per Migration: Keep migrations focused on a single logical change
  3. Provide Down Methods: Always implement down() methods when possible for rollback capability
  4. Test Migrations: Test both up() and down() methods before deploying
  5. Idempotent Operations: Make migrations safe to retry (use IF NOT EXISTS, etc.)
  6. Version Control: Always commit migration files to version control