Skip to content

DaySmart/serverless-sequelize-migrations

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serverless Sequelize Migrations

A plugin to manage sequelize migrations on serverless projects

Built with ❤︎ by Manoel Ferreira for Sol de Giz project.

Features:

  • Create migration file
  • List pending and executed migrations
  • Apply pending migrations
  • Revert applied migrations
  • Reset all applied migrations

Documentation

Installation

  1. Add Serverless Sequelize Migrations to your project:
npm install --save serverless-sequelize-migrations
  1. Inside your serverless.yml file add the following entry to the plugins section (if there is no plugin section in the file you'll need to add it):
plugins:
    - serverless-sequelize-migrations

You can check whether the plugin is ready to be used through the terminal. To do so, type the following command on the CLI:

serverless

the console should display SequelizeMigrations as one of the available plugins in your Serverless project.

Setting up Sequelize

For the plugin to work correctly, you must provide the secret ARN from AWS Secrets Manager and the database name:

custom:
  sequelize:
    <stage_name>:
      secretId: 'ARN of secret from AWS Secrets Manager'
      dbName: 'database name'

Replace the variables with the information of your own database. Note that you can add multiple stage names.

Obs: This plugin does not have support to create the database itself.

As per Sequelize docs, you'll have to manually install the driver for your database of choice:

# One of the following:
$ npm install --save pg pg-hstore # Postgres
$ npm install --save mysql2 # MySQL
$ npm install --save mariadb # MariaDB
$ npm install --save tedious # Microsoft SQL Server

Usage and command line options

To see the available commands of the plugin, run sls migrations on the terminal. The following should appear:

Plugin: SequelizeMigrations
migrations .................... Sequelize migrations management for Serverless
migrations create ............. Create a migration file
migrations up ................. Execute all pending migrations
migrations down ............... Rolls back one or more migrations
migrations reset .............. Rolls back all migrations
migrations list ............... Shows a list of migrations
    --path / -p ........................ Specify the migrations path (default is './migrations')
    --verbose / -v ..................... Shows sequelize logs

For any of these commands, you can specify two parameters:

  • --path to inform the path of migrations on your project.
  • --verbose to show sequelize execution logs during the operations.

In order to see the options of each command individually, you can run sls migrations <command> --help on the terminal.

The commands (those that have some option) and it's options are presented below:

  • migrations create
--name / -n (required) ... Specify the name of the migration to be created
  • migrations up
--rollback / -r .......... Rolls back applied migrations in case of error (default is false)
  • migrations down
--times / -t ............. Specify how many times to roll back (default is 1)
--name / -n .............. Specify the name of the migration to be rolled back (e.g. "--name create-users.js")
  • migrations list
--status / -s ............ Specify the status of migrations to be listed (--status pending [default] or --status executed)

Serverless Deployment

This plugin will run the migration up command before updating the AWS cloudformation stack if the stage being deployed to is configured

Custom migrations path

You can also define a migrations path variable on the custom section of your project service file.

custom:
  migrationsPath: './custom/migrations/path'

Important: if you inform the --path option through the CLI, this configuration will be ignored.

Credits and inspiration

This plugin was first based on Nevon's serverless-pg-migrations.

License

MIT

About

A Serverless plugin to manage sequelize migrations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%