JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1084
  • Score
    100M100P100Q107196F
  • License MIT

Table schema based database migration tool, built on top of the knex.js

Package Exports

  • knex-automigrate
  • knex-automigrate/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (knex-automigrate) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

knex-automigrate

NPM Version NPM Downloads Dependency Status

Table schema based database migration tool, built on top of the knex.js

  • Migration schema file name must be started with table_.
  • Currently supported dialects to index migration : mysql

Installation

$ npm install knex-automigrate -g

Usage

Usage: knex-automigrate [options] [command]


Commands:

  migrate:auto           Run all migration table schemas.

Options:

  -h, --help         output usage information
  -V, --version      output the version number
  --debug            Run with debugging.
  --knexfile [path]  Specify the knexfile path.
  --cwd [path]       Specify the working directory.
  --env [name]       environment, default: process.env.NODE_ENV || development

Before (traditional database migration with knex.js)

$ knex migrate:make create_users_table
// 201701010000_create_users_table.js
exports.up = function(knex, Promise) {
  return Promise.all([
    knex.schema.createTableIfNotExists('users', function(table) {
      table.increments('user_id').unsigned().comment('PK');
      table.string('email', 128).notNullable().comment('E-Mail');
      table.string('nickname', 128).notNullable().comment('Name');
    })
  ]);
});
$ knex migrate:latest
$ knex migrate:make alter_users_table
// 201701010000_alter_users_table.js
exports.up = function(knex, Promise) {
  return Promise.all([
    knex.schema.alterTable('users', function(table) {
      table.dropColumn('nickname');
      table.string('email', 64).notNullable().comment('E-Mail').alter();
      table.string('name', 64).notNullable().comment('Name');
    })
  ]);
});
$ knex migrate:latest

Migration files are,

App
 ├─ migrations
 │  ├─ 201701010000_create_users_table.js
 │  └─ 201701010000_alter_users_table.js
 └─ knexfile.js

After (database migration with knex-automigrate)

// table_users.js
exports.auto = function(migrator, knex) {
  return [
    migrator('users', function(table) {
      table.increments('user_id').unsigned().comment('PK');
      table.string('email', 128).notNullable().comment('E-Mail');
      table.string('nickname', 128).notNullable().comment('Name');
    });
  ];
});
$ knex-automigrate migrate:auto
// table_users.js
exports.auto = function(migrator, knex) {
  return [
    migrator('users', function(table) {
      table.increments('user_id').unsigned().comment('PK');
      table.string('email', 64).notNullable().comment('E-Mail');
      table.string('name', 64).notNullable().comment('Name');
    });
  ];
});

// view_users.js
exports.auto = function(migrator, knex) {
  return [
    migrator('user_information', (view) => {
      // If view.columns() is missing,
      // the columns will default to those defined in the 'select()' statement.
      view.as(knex('users').select('user_id', 'email', 'name'));
    }),
  ];
});
$ knex-automigrate migrate:auto

Migration files are,

App
 ├─ migrations
 │  ├─ table_users.js
 │  └─ view_users.js
 └─ knexfile.js

Dependencies

License

MIT License