-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
77 lines (61 loc) · 2.1 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
const DatastoreTrailpack = require('trailpack/datastore');
const Wetland = require('wetland').Wetland;
const Homefront = require('homefront').Homefront;
module.exports = class WetlandTrailpack extends DatastoreTrailpack {
/**
* Validate configured stores
*/
validate() {
if (!this.app.config.database || !this.app.config.database.stores) {
return Promise.reject(new Error('No store configured.'));
}
if (!this.app.config.wetland && !this.app.config.database) {
return Promise.reject(new Error(`Wetland config not found.
Make sure you have a config file for wetland and try again.`));
}
}
/**
* Instantiate wetland with config
*/
configure() {
let config = this.app.config;
config.database.orm = 'wetland';
let databaseConfig = new Homefront(config.database);
if (config.wetland) {
databaseConfig.merge(config.wetland);
}
this.wetland = new Wetland(databaseConfig);
}
/**
* Run migrations
*/
initialize() {
super.initialize();
this.orm = this.wetland;
this.app.orm = this.wetland;
let config = this.wetland.getConfig();
let migration = config.fetch('migrate') || config.fetch('models.migrate');
if (!migration || migration === 'safe') {
return Promise.resolve();
}
if (this.app.config.env !== 'development') {
let message = `Refusing to run dev migrations because environment '${this.app.config.env}' isn't development.`;
return Promise.reject(new Error(message));
}
if (migration !== 'alter') {
return Promise.reject(new Error('Not running dev migrations. The only support method is "alter".'));
}
this.app.log.verbose('Starting dev migrations...');
return this.wetland.getMigrator().devMigrations()
.then(() => this.app.log.verbose('Dev migrations complete.'))
.catch(error => this.app.log.error(`Dev migrations failed: ${error}`));
}
constructor(app) {
super(app, {
config: require('./config'),
api : require('./api'),
pkg : require('./package')
});
}
};