-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
141 lines (121 loc) · 3.81 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* eslint no-console: [0] */
'use strict'
const Trailpack = require('trailpack/datastore')
const lib = require('./lib')
const _ = require('lodash')
module.exports = class SequelizeTrailpack extends Trailpack {
/**
* Validate the database config, and api.model definitions
*/
async validate() {
const stores = this.app.config.get('stores')
const models = this.app.config.get('models')
if (stores && Object.keys(stores).length === 0) {
this.app.config.log.logger.warn('No store configured at config.stores, models will be ignored')
}
return Promise.all([
lib.Validator.validateStoresConfig(stores),
lib.Validator.validateModelsConfig(models)
])
}
/**
* Merge configuration into models, load Sequelize collections.
*/
configure() {
this.app.config.set('stores.orm', 'sequelize')
_.merge(this.app.config, lib.FailsafeConfig)
}
/**
* Initialize Sequelize. This will compile the schema and connect to the
* database.
*/
initialize() {
super.initialize()
this.orm = this.orm || {}
this.app.orm = {}
this.connections = lib.Transformer.transformStoreConnections(this.app)
this.app.models = lib.Transformer.transformModels(this.app)
this.stores = _.mapValues(this.app.config.get('stores'), (store, storeName) => {
return {
models: _.pickBy(this.app.models, { store: storeName }),
connection: this.connections[storeName],
migrate: store.migrate || this.app.config.get('models.migrate')
}
})
// Loop through store models and define them in Sequelize
_.each(this.stores, (store, storeName) => {
_.each(store.models, (model, modelName) => {
const Model = store.connection.define(modelName, model.schema, model.options)
if (model.options) {
if (model.options.classMethods) {
for (const methodName in model.options.classMethods) {
Model[methodName] = model.options.classMethods[methodName]
}
}
if (model.options.instanceMethods) {
for (const methodName in model.options.instanceMethods) {
Model.prototype[methodName] = model.options.instanceMethods[methodName]
}
}
}
this.app.orm[model.globalId] = Model
})
})
// Loop through store models and associate
_.each(this.stores, (store, storeName) => {
// Run Associate on the Models
_.each(store.models, (model, modelName) => {
// ignore model if not configured
if (!this.app.orm[model.globalId]) {
return
}
// Associate models if method defined
if (this.app.orm[model.globalId].associate) {
this.app.orm[model.globalId].associate(this.app.orm)
}
// Reset the orm Model
this.orm[model.globalId.toLowerCase()] = this.app.orm[model.globalId]
})
})
return this.migrate()
}
/**
* Close all database connections
*/
async unload() {
return Promise.all(
_.map(this.stores, store => {
return new Promise((resolve, reject) => {
store.connection.close()
resolve()
})
})
)
}
async migrate() {
const SchemaMigrationService = this.app.services.SchemaMigrationService
return Promise.all(
_.map(this.stores, store => {
if (store.migrate === 'drop') {
return SchemaMigrationService.dropDB(store.connection)
}
else if (store.migrate === 'alter') {
return SchemaMigrationService.alterDB(store.connection)
}
else if (store.migrate === 'none') {
return
}
else {
return
}
})
)
}
constructor(app) {
super(app, {
config: require('./config'),
api: require('./api'),
pkg: require('./package')
})
}
}