Skip to content

Implementation of the js-migrations RepoFacade using Knex

License

Notifications You must be signed in to change notification settings

js-migrations/knex

Repository files navigation

knex

Implementation of the js-migrations RepoFacade using Knex

Usage

  1. Install it with npm i @js-migrations/knex.
  2. Use the factory to create the repository facade.
  3. Use the facade with the @js-migrations/core service.

Use the factory

import knexMigrationsRepoFactory from '@js-migrations/knex/dist/factory';
import connectToDb from '@js-migrations/knex/dist/utils/connectToDb';

const migrationsRepoFacade = knexMigrationsRepoFactory({
  db: connectToDb({
    client: 'mysql',
    connection: {
      database: 'todoapp',
      host: '127.0.0.1',
      password: 'pword',
      user: 'todouser',
    },
  }),
  // Optional property.
  lockTableName: 'migrationsLock',
  // Optional property.
  migrations: [{
    down: async () => {},
    key: 'your_migration_name',
    up: async () => {},
  }],
  // Optional property.
  tableName: 'migrations',
});

Alternatives

There are a couple of notable alternatives to the JS-Migrations packages for Knex.

These packages are however focused on processing migration files rather than migration objects which makes type checking and composition more difficult. Type checking is more difficult because the files are dynamically imported at runtime, hence they cannot be analysed statically at compile time. Composition is more difficult because all of the migration files must be located in one directory, which may be challenging if you have more than one storage implementation requiring migrations (e.g. database storage and file storage).