forked from trailsjs/trailpack-mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (99 loc) · 3.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
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
'use strict'
const _ = require('lodash')
const mongoose = require('mongoose')
const DatastoreTrailpack = require('trailpack-datastore')
const lib = require('./lib')
/**
* Mongoose integration for Trails. Allows mongoose to read its configration from the
* trails datastore config, and auto-migrate on startup.
*/
module.exports = class MongooseTrailpack extends DatastoreTrailpack {
/**
* Ensure that this trailpack supports the configured migration
*/
validate () {
if (!_.includes([ 'none', 'drop', 'create' ], this.app.config.database.models.migrate)) {
throw new Error('Migrate must be configured to either "create" or "drop"')
}
}
/**
* Create default configuration
*/
configure () {
this.app.config.database.orm = 'mongoose'
this.mongoose = mongoose
}
/**
* Initialize mongoose connections, and perform migrations.
*/
initialize () {
super.initialize()
// Binding promise library
// We will use default one
mongoose.Promise = global.Promise
this.models = lib.Transformer.transformModels(this.app)
this.orm = this.orm || {}
// Need to pick only mongoose stores
const stores = lib.Transformer.pickStores(this.app.config.database.stores)
// iterating only through mongo stores
this.connections = _.mapValues(stores, (_store, storeName) => {
const store = _.merge({ }, _store)
if (!_.isString(store.uri))
throw new Error('Store have to contain "uri" option')
if (!_.isObject(store.options))
store.options = {}
const connection = mongoose.createConnection(store.uri, store.options)
const models = _.pickBy(this.models, { connection: storeName })
_.map(models, model => {
const schema = new mongoose.Schema(model.schema, model.schemaOptions)
// Bind statics & methods
schema.statics = model.statics
schema.methods = model.methods
model.onSchema(this.app, schema)
//create model
this.orm[model.globalId] = connection.model(model.globalId, schema, model.tableName)
this.packs.mongoose.orm[model.identity] = this.orm[model.globalId]
})
return connection
})
this.app.orm = this.orm
return this.migrate()
}
/**
* Close all database connections
*/
unload () {
return Promise.all(
_.map(this.connections, connection => {
return new Promise((resolve, reject) => {
connection.close((err) => {
if (err)
return reject(err)
resolve()
})
})
})
)
}
constructor (app) {
super(app, {
config: require('./config'),
api: require('./api'),
pkg: require('./package')
})
}
/**
* Run migrations
*/
migrate () {
const SchemaMigrationService = this.app.services.SchemaMigrationService
const database = this.app.config.database
if (database.models.migrate == 'none') return
return Promise.all(
_.map(this.connections, connection => {
if (database.models.migrate == 'drop') {
return SchemaMigrationService.drop(connection)
}
}))
}
}