Giter Club home page Giter Club logo

serverless-typeorm-migrations's Introduction

Serverless

Serverless TypeORM Migrations

Database migrations for AWS Lambda and RDS using TypeORM Migrations.

About

This Serverless plugin can execute and rollback database migrations after deploys. See Usage

This plugin supports MySQL / MariaDB / Postgres / CockroachDB / SQLite / Microsoft SQL Server / sql.js

Inspired by serverless-pg-migrations. I use TypeORM so I wrote my own plugin

NOTES:

  • This plugin does not attempt to add handlers automatically (see Adding handlers)
  • This plugin does not create or drop databases
  • This plugin does not have a handler for checking database connection

Migrations

You need to specify your migration folder

For details on using migrations please see the TypeORM Migration docs.

Installation

$ yarn add serverless-typeorm-migrations

OR

$ npm install serverless-typeorm-migrations

Usage

Define a migration handler somewhere in your project. Example:

// /migrations.js

const { up, down } = require("serverless-typeorm-migrations/build/handlers");

module.exports.up = up;

module.exports.down = down;
// /migrations.ts

export { up, down } from 'serverless-typeorm-migrations/build/handlers';

Add the plugin and handlers to your serverless.yml:

provider:
  name: aws

plugins:
  - serverless-typeorm-migrations

functions:
  up:
    handler: migrations.up
    timeout: 30
    environment:
      SLS_TYPEORM_MIGRATIONS_ENGINE: "postgres"
      SLS_TYPEORM_MIGRATIONS_DATABASE_URL: "postgres://root:[email protected]:5432/database"
      SLS_TYPEORM_MIGRATION_FOLDER: "src/migration/**/*.js"
  down:
    handler: migrations.down
    timeout: 30
    environment:
      SLS_TYPEORM_MIGRATIONS_ENGINE: "postgres"
      SLS_TYPEORM_MIGRATIONS_DATABASE_URL: "postgres://root:[email protected]:5432/database"
      SLS_TYPEORM_MIGRATION_FOLDER: "src/migration/**/*.js"

Pass the function to the serverless deploy command to have it execute after the deploy is finished:

sls deploy --function up

You can also manually invoke the functions locally:

sls invoke local --function up

Or use the plugin directly without going through your function:

sls migrate up
sls migrate down

Configuration

The functions need to have the following environment variables :

  • SLS_TYPEORM_MIGRATIONS_DATABASE_URL set to a valid connection uri.
  • SLS_TYPEORM_MIGRATIONS_FOLDER pointing migrations folder
  • SLS_TYPEORM_MIGRATIONS_ENGINE defining database driver

NestJS example

If you are using NestJS with serverless framework you have to create a ormconfig.js file in your root folder within the following content to generate migration:

module.exports = {
  type: 'your_driver',
  host: process.env.DB_HOST,
  port: parseInt(process.env.DB_PORT),
  username: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  entities: ['your_entities_folder/**/*.ts'],
  migrations: ['your_migrations_folder/**/*.ts'],
  subscribers: ['your_subscribers_folder/**/*.ts'],
  cli: {
    entitiesDir: 'your_entities_folder',
    migrationsDir: 'your_migrations_folder',
    subscribersDir: 'your_subscribers_folder',
  },
};

Next you have to transpile .ts migration files to .js to make it work before deploying or invoking functions

Here is my package.json scripts as example

{
"migration:create": "typeorm migration:create -n",
"migration:generate": "ts-node node_modules/.bin/typeorm migration:generate -n",
"migration:up": "tsc src/migration/*.ts && serverless migrate up && rm -r src/migration/*.js",
"migration:down": "tsc src/migration/*.ts && serverless migrate down && rm -r src/migration/*.js"
}

And finally, configure the plugin with these environment variables

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.