-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
67 lines (58 loc) · 1.68 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
const DatastoreTrailpack = require('trailpack/datastore')
const Waterline = require('waterline')
const lib = require('./lib')
/**
* Waterline Trailpack
*
* Allow the trails application to interface with the Waterline ORM.
*/
module.exports = class WaterlineTrailpack extends DatastoreTrailpack {
/**
* Validate the database config, and api.model definitions
*/
validate () {
return Promise.all([
lib.Validator.validateDatabaseConfig(this.app.config.stores),
//lib.Validator.validateModels(this.app.api.models)
])
}
/**
* Merge configuration into models, load Waterline collections.
*/
configure () {
this.wl = new Waterline()
}
/**
* Initialize Waterline. This will compile the schema and connect to the
* database.
*/
initialize () {
this.models = lib.Transformer.transformModels(this.app)
this.adapters = lib.Transformer.transformAdapters(this.app)
this.connections = lib.Transformer.transformConnections(this.app)
Object.values(this.models).map(model => {
this.wl.loadCollection(Waterline.Collection.extend(model))
})
const wlConfig = { adapters: this.adapters, connections: this.connections }
return new Promise((resolve, reject) => {
this.wl.initialize(wlConfig, (err, orm) => {
if (err) return reject(err)
this.orm = orm
this.app.orm = lib.Transformer.transformWaterlineOrm(this.orm)
resolve()
})
})
}
unload () {
return new Promise((resolve, reject) => {
this.wl.teardown(resolve)
})
}
constructor (app) {
super(app, {
config: require('./config'),
pkg: require('./package'),
api: require('./api')
})
}
}