-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
50 lines (44 loc) · 2.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Module dependencies
*/
var _ = require('@sailshq/lodash');
var runAlterStrategy = require('./private/run-alter-strategy');
var runDropStrategy = require('./private/run-drop-strategy');
var runSafeStrategy = require('./private/run-safe-strategy');
/**
* runAutoMigrations()
*
* Auto-migrate all models in this orm using the given strategy.
*
* @param {[type]} strategy [description]
* @param {[type]} orm [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
module.exports = function runAutoMigrations(strategy, orm, cb) {
// ╦ ╦╔═╗╦ ╦╔╦╗╔═╗╔╦╗╔═╗ ┌─┐┌┬┐┬─┐┌─┐┌┬┐┌─┐┌─┐┬ ┬
// ╚╗╔╝╠═╣║ ║ ║║╠═╣ ║ ║╣ └─┐ │ ├┬┘├─┤ │ ├┤ │ ┬└┬┘
// ╚╝ ╩ ╩╩═╝╩═╩╝╩ ╩ ╩ ╚═╝ └─┘ ┴ ┴└─┴ ┴ ┴ └─┘└─┘ ┴
if (!_.isString(strategy)) {
return cb(new Error('Strategy must be one of: `alter`, `drop`, or `safe`.'));
}
// ╦ ╦╔═╗╦ ╦╔╦╗╔═╗╔╦╗╔═╗ ┌─┐┬─┐┌┬┐
// ╚╗╔╝╠═╣║ ║ ║║╠═╣ ║ ║╣ │ │├┬┘│││
// ╚╝ ╩ ╩╩═╝╩═╩╝╩ ╩ ╩ ╚═╝ └─┘┴└─┴ ┴
if (!orm || !_.isObject(orm)) {
return cb(new Error('ORM must be an initialized Waterline ORM instance.'));
}
// Ensure a callback function exists
if (!cb || !_.isFunction(cb)) {
throw new Error('Missing callback argument.');
}
// ╦═╗╦ ╦╔╗╔ ┌┬┐┬┌─┐┬─┐┌─┐┌┬┐┬┌─┐┌┐┌ ┌─┐┌┬┐┬─┐┌─┐┌┬┐┌─┐┌─┐┬ ┬
// ╠╦╝║ ║║║║ │││││ ┬├┬┘├─┤ │ ││ ││││ └─┐ │ ├┬┘├─┤ │ ├┤ │ ┬└┬┘
// ╩╚═╚═╝╝╚╝ ┴ ┴┴└─┘┴└─┴ ┴ ┴ ┴└─┘┘└┘ └─┘ ┴ ┴└─┴ ┴ ┴ └─┘└─┘ ┴
switch(strategy){
case 'alter': runAlterStrategy(orm, cb); break;
case 'drop': runDropStrategy(orm, cb); break;
case 'safe': runSafeStrategy(orm, cb); break;
default: return cb(new Error('Strategy must be one of: `alter`, `drop`, or `safe`.'));
}
};