Giter VIP home page Giter VIP logo

loopback-db-migrate's Introduction

A library to add simple database migration support to loopback projects. Migrations that have been run will be stored in a table called 'Migrations'. The library will read the loopback datasources.json files based on the NODE_ENV environment variable just like loopback does. The usage is based on the node-db-migrate project.

NOTE: This does not currently work with the loopback in memory DB.

CLI Usage

``` loopback-db-migrate [up|down|create] [options]

Down migrations are run in reverse run order.

Options: --datasource specify database name (optional, default: db) --since specify date to run migrations from (options, default: run all migrations) --directory specify directory where migration scripts will live (options, default: '/server/migrations/'). A trailing slash is added at the end if not present


<h2>Using the CLI directly</h2>
Run all new migrations that have not previously been run, using datasources.json and database 'db':
```javascript
./node_modules/loopback-db-migrate/loopback-db-migrate.js up

Run all new migrations since 01012014 that have not previously been run, using datasources.json and datasources.qa.json and database 'my_db_name':

NODE_ENV=qa ./node_modules/loopback-db-migrate/loopback-db-migrate.js up --datasource my_db_name --since 01012014

Run all migrations living in the '/server/schema-migrations/' directory that have not previously been run, using datasource.json and database 'db'.

./node_modules/loopback-db-migrate/loopback-db-migrate.js up --directory /server/schema-migrations/

Using the CLI with npm by updating your package.json

```javascript "scripts": { "migrate-db-up": "loopback-db-migrate up --datasource some_db_name", "migrate-db-down": "loopback-db-migrate down --datasource some_db_name" }

npm run-script migrate-db-up npm run-script migrate-db-down

NODE_ENV=production npm run-script migrate-db-up NODE_ENV=production npm run-script migrate-db-down


<h2>Example migrations</h2>
```javascript
module.exports = {
    up: function(dataSource, next) {
        dataSource.models.Users.create({ ... }, next);
    },
    down: function(dataSource, next) {
        dataSource.models.Users.destroy({ ... }, next);
    }
};
/* executing raw sql */
module.exports = {
    up: function(dataSource, next) {
        dataSource.connector.query('CREATE TABLE `my_table` ...;', next);
    },
    down: function(dataSource, next) {
        dataSource.connector.query('DROP TABLE `my_table`;', next);
    }
};

loopback-db-migrate's People

Contributors

slively avatar alectejada avatar anotheredward avatar jbernalvallejo avatar elhoyos avatar slivelygh avatar

Watchers

 avatar

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.