1.0.0 • Published 7 years ago

db-migrate-boilerplate v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

db-migrate-boilerplate

Reduce the amount of boilerplate in your project needed to support raw SQL migrations using db-migrate.

Usage

Install via npm:

$ npm install db-migrate-boilerplate

Replace the default migration handler generated by db-migrate with this:

'use strict'

const path = require('path')
const boilerplate = require('db-migrate-boilerplate')

module.exports = boilerplate({
  // TODO replace with your correct paths
  upPath: path.join(__dirname, 'sqls', '20161226111110-test-up.sql'),
  downPath: path.join(__dirname, 'sqls', '20161226111110-test-down.sql')
})

Make sure to replace the paths with your expected values.

Why?

db-migrate generates a lot of boilerplate code when creating a raw SQL migration. Here is an example:

'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
  * We receive the dbmigrate dependency from dbmigrate initially.
  * This enables us to not have to rely on NODE_PATH.
  */
exports.setup = function(options, seedLink) {
  dbm = options.dbmigrate;
  type = dbm.dataType;
  seed = seedLink;
  Promise = options.Promise;
};

exports.up = function(db) {
  var filePath = path.join(__dirname, 'sqls', '20161226111110-test-up.sql');
  return new Promise( function( resolve, reject ) {
    fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
      if (err) return reject(err);
      console.log('received data: ' + data);

      resolve(data);
    });
  })
  .then(function(data) {
    return db.runSql(data);
  });
};

exports.down = function(db) {
  var filePath = path.join(__dirname, 'sqls', '20161226111110-test-down.sql');
  return new Promise( function( resolve, reject ) {
    fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
      if (err) return reject(err);
      console.log('received data: ' + data);

      resolve(data);
    });
  })
  .then(function(data) {
    return db.runSql(data);
  });
};

exports._meta = {
  "version": 1
};

License

MIT