-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (49 loc) · 1.49 KB
/
index.js
File metadata and controls
58 lines (49 loc) · 1.49 KB
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
51
52
53
54
55
56
57
58
var debug = require('debug')('loopback-component-migrate');
var loopback = require('loopback');
var migrationDef = require('./models/migration.json');
// Remove proerties that will confuse LB
function getSettings(def) {
var settings = {};
for (var s in def) {
if (s === 'name' || s === 'properties') {
continue;
} else {
settings[s] = def[s];
}
}
return settings;
}
/**
* @param {Object} app The app instance
* @param {Object} options The options object
*/
module.exports = function(app, options) {
var loopback = app.loopback;
options = options || {};
var dataSource = options.dataSource || 'db';
if (typeof dataSource === 'string') {
dataSource = app.dataSources[dataSource];
}
var migrationModelSettings = getSettings(migrationDef);
if (typeof options.acls !== 'object') {
migrationModelSettings.acls = [];
} else {
migrationModelSettings.acls = options.acls;
}
// Support for loopback 2.x.
if (app.loopback.version.startsWith(2)) {
Object.keys(migrationModelSettings.methods).forEach(key => {
migrationModelSettings.methods[key].isStatic = true;
});
}
debug('Creating Migration model using settings: %o', migrationModelSettings);
var MigrationModel = dataSource.createModel(
migrationDef.name,
migrationDef.properties,
migrationModelSettings);
var Migration = require('./models/migration')(MigrationModel, options);
app.model(Migration, {
public: !!options.enableRest,
dataSource: dataSource
});
};