kunklejr/node-db-migrate — pull request #194 by KenPowers (2014-11-05 02:55:36+00:00)
Title: Add ability to migrate with promises.

These changes enable the use of promises in migrations. For example, the following:

```js
var async = require('async');

exports.up = function (db, callback) {
async.series([
db.createTable.bind(db, 'pets', {
id: { type: 'int', primaryKey: true },
name: 'string'
}),
db.createTable.bind(db, 'owners', {
id: { type: 'int', primaryKey: true },
name: 'string'
});
], callback);
};

exports.down = function (db, callback) {
async.series([
db.dropTable.bind(db, 'pets'),
db.dropTable.bind(db, 'owners')
], callback);
};
```

Can be turned into the following:

```js
exports.up = function (db, callback) {
return db.createTableAsync('pets', {
id: {type: 'int', primaryKey: true },
name: 'string'
}).then(function () {
return db.createTable('owners', {
id: { type: 'int', primaryKey: true },
name: 'string'
});
});
};

exports.down = function (db, callback) {
return db.dropTable('pets').then(function () {
return db.dropTable('owners');
});
};
```

The real benefit is how much control-flow stuff we gain from Bluebird without having to include any additional libraries (everything is built in to the promises): https://github.com/petkaantonov/bluebird/blob/master/API.md

Just for reference, this will improve even more with ES6:

```js
exports.down = function (db, callback) {
return db.dropTable('pets').then(() => db.dropTable('owners'));
};
```

Something similar is currently possible with LoDash:

```js
exports.down = function (db, callback) {
return db.dropTable('pets').then(_.bindKey(db, 'dropTable', 'owners'));
};
```

The Bluebird `Promise` constructor is exported on `index`:

```js
var Promise = require('db-migrate').Promise;
```

What still needs to be done:

* Documentation
* Tests
* Update migration template to get a reference to `Promise` (optional)
* Feedback