JSPM

  • Created
  • Published
  • Downloads 60
  • Score
    100M100P100Q56859F
  • License MIT

Perform migrations from code in typescript

Package Exports

  • sequelize-do-migrations

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 (sequelize-do-migrations) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

sequelize-do-migrations

sequelize-do-migrations requires sequelize.

To install the library use the following command.

With yarn:

yarn add sequelize-do-migrations

Or npm:

npm install --save sequelize-do-migrations

How to use

To use the library you must import runMigrations:

import { runMigrations } from 'sequelize-do-migrations';

Call the function passing it the connection of sequelize, passing it the connection of sequelize, optionally you can pass another parameter with options:

runMigrations(sequelize, options);

Return: An array of strings with the name of the files executed and saved in the database.

The options:

{
  path?: string;
  showLogs?: boolean;
}

By default, showLogs is false, if true, prints logs by console.log.

The path to the migrations folder, by default, is in the same folder from where the function is called, it looks for a folder called migrations or migration unless the path parameter is modified.

An example of migration is to create a file called 01_XXXXX.ts (It can be any name as long as it is not repeated) inside the migrations folder with the following code:

import { DataTypes, Sequelize } from 'sequelize';

export const up = async (sequelize: Sequelize) => {
  const user = await sequelize.models.User.describe();
  if (!user.location) {
    await sequelize.getQueryInterface().addColumn('Users', 'location', {
      type: DataTypes.GEOGRAPHY,
    });
  }
};

export const down = async (sequelize: Sequelize) => {
  const user = await sequelize.models.User.describe();
  if (user.location) {
    await sequelize.getQueryInterface().removeColumn('Users', 'location');
  }
};