Skip to content

Commit 7cb25da

Browse files
authored
Merge pull request #39 from trailsjs/v3
migrate to v3
2 parents 7c7c9aa + c7d4e09 commit 7cb25da

20 files changed

+612
-452
lines changed

.travis.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
language: node_js
22
node_js:
3-
- 4
4-
- 5
5-
- 6
6-
- 7
3+
- 8
4+
- 9
75

86
notifications:
97
email: false
@@ -21,4 +19,4 @@ deploy:
2119
on:
2220
tags: true
2321
repo: trailsjs/trailpack-sequelize
24-
node: 7
22+
node: 8

api/services/FootprintService.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const _ = require('lodash')
4-
const Service = require('trails/service')
4+
const Service = require('trails/lib/Service')
55
const ModelError = require('../../lib').ModelError
66

77
const manageError = err => {
@@ -39,7 +39,7 @@ module.exports = class FootprintService extends Service {
3939
*/
4040
create(modelName, values, options) {
4141
const Model = this._getModel(modelName)
42-
const modelOptions = _.defaultsDeep({}, options, _.get(this.app.config, 'footprints.models.options'))
42+
const modelOptions = _.defaultsDeep({}, options, this.app.config.get('footprints.models.options'))
4343
if (!Model) {
4444
return Promise.reject(new ModelError('E_NOT_FOUND', `${modelName} can't be found`))
4545
}

api/services/SchemaMigrationService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const Service = require('trails/service')
3+
const Service = require('trails/lib/Service')
44

55
/**
66
* @module SchemaMigrationService

archetype/config/database.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

archetype/config/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.stores = require('./stores')
2+
exports.models = require('./models')

archetype/config/models.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Models Configuration
3+
*
4+
* @see {@link http://trailsjs.io/doc/trailpack/config
5+
*/
6+
module.exports = {
7+
/**
8+
* The default store if not defined by model
9+
*/
10+
defaultStore: 'sqlitedev',
11+
/**
12+
* The default migration strategy if not defined by the store or model
13+
* ENUM: none, safe, drop, alter,
14+
*/
15+
migrate: 'drop'
16+
17+
/**
18+
* You can also define model by model which store, migration, etc it will use.
19+
* A la carte model definitions
20+
* eg:
21+
*
22+
* Test: {
23+
* store: 'sqlitedev',
24+
* migrate: 'drop',
25+
* tableName: 'test',
26+
* options: {}
27+
* }
28+
*/
29+
}

archetype/config/stores.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Stores Configuration
3+
*
4+
* @see {@link http://trailsjs.io/doc/trailpack/config
5+
*/
6+
module.exports = {
7+
/**
8+
* Define the database stores. A store is typically a single database.
9+
*
10+
* Use the SQLite3 by default for development purposes.
11+
*
12+
* Set production connection info in config/env/production.js
13+
*/
14+
15+
16+
/**
17+
* Define a store called "sqlitedev" which uses SQLite3 to persist data.
18+
*/
19+
sqlitedev: {
20+
database: 'dev',
21+
storage: './.tmp/dev.sqlite',
22+
host: '127.0.0.1',
23+
dialect: 'sqlite'
24+
/**
25+
* The default migration strategy for this store.
26+
migrate: 'drop'
27+
*/
28+
}
29+
}

config/database.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

config/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
exports.trailpack = require('./trailpack')
2-
exports.database = require('./database')

index.js

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint no-console: [0] */
12
'use strict'
23

34
const Trailpack = require('trailpack/datastore')
@@ -9,21 +10,25 @@ module.exports = class SequelizeTrailpack extends Trailpack {
910
/**
1011
* Validate the database config, and api.model definitions
1112
*/
12-
validate() {
13-
const stores = _.get(this.app.config, 'database.stores')
13+
async validate() {
14+
const stores = this.app.config.get('stores')
15+
const models = this.app.config.get('models')
16+
1417
if (stores && Object.keys(stores).length === 0) {
15-
this.app.config.log.logger.warn('No store configured at config.database.stores, models will be ignored')
18+
this.app.config.log.logger.warn('No store configured at config.stores, models will be ignored')
1619
}
20+
1721
return Promise.all([
18-
lib.Validator.validateDatabaseConfig(this.app.config.database)
22+
lib.Validator.validateStoresConfig(stores),
23+
lib.Validator.validateModelsConfig(models)
1924
])
2025
}
2126

2227
/**
2328
* Merge configuration into models, load Sequelize collections.
2429
*/
2530
configure() {
26-
this.app.config.database.orm = 'sequelize'
31+
this.app.config.set('stores.orm', 'sequelize')
2732
_.merge(this.app.config, lib.FailsafeConfig)
2833
}
2934

@@ -36,40 +41,55 @@ module.exports = class SequelizeTrailpack extends Trailpack {
3641

3742
this.orm = this.orm || {}
3843
this.app.orm = {}
39-
this.connections = lib.Transformer.transformStores(this.app)
40-
this.models = lib.Transformer.transformModels(this.app)
41-
42-
_.each(this.models, (model, modelName) => {
43-
_.each(this.connections, (connection, name) => {
44-
if (model.connection == name) {
45-
const Model = connection.define(modelName, model.schema, model.config)
46-
47-
if (model.config) {
48-
if (model.config.classMethods) {
49-
for (const methodName in model.config.classMethods) {
50-
Model[methodName] = model.config.classMethods[methodName]
51-
}
52-
}
44+
this.connections = lib.Transformer.transformStoreConnections(this.app)
45+
this.app.models = lib.Transformer.transformModels(this.app)
46+
47+
this.stores = _.mapValues(this.app.config.get('stores'), (store, storeName) => {
48+
return {
49+
models: _.pickBy(this.app.models, { store: storeName }),
50+
connection: this.connections[storeName],
51+
migrate: store.migrate || this.app.config.get('models.migrate')
52+
}
53+
})
5354

54-
if (model.config.instanceMethods) {
55-
for (const methodName in model.config.instanceMethods) {
56-
Model.prototype[methodName] = model.config.instanceMethods[methodName]
57-
}
55+
// Loop through store models and define them in Sequelize
56+
_.each(this.stores, (store, storeName) => {
57+
_.each(store.models, (model, modelName) => {
58+
const Model = store.connection.define(modelName, model.schema, model.options)
59+
60+
if (model.options) {
61+
if (model.options.classMethods) {
62+
for (const methodName in model.options.classMethods) {
63+
Model[methodName] = model.options.classMethods[methodName]
5864
}
5965
}
6066

61-
this.app.orm[model.globalId] = Model
67+
if (model.options.instanceMethods) {
68+
for (const methodName in model.options.instanceMethods) {
69+
Model.prototype[methodName] = model.options.instanceMethods[methodName]
70+
}
71+
}
6272
}
73+
this.app.orm[model.globalId] = Model
6374
})
6475
})
6576

66-
_.each(this.models, (model, modelName) => {
67-
if (!this.app.orm[model.globalId]) return //ignore model if not configured
68-
69-
if (this.app.orm[model.globalId].associate)
70-
this.app.orm[model.globalId].associate(this.app.orm)
77+
// Loop through store models and associate
78+
_.each(this.stores, (store, storeName) => {
79+
// Run Associate on the Models
80+
_.each(store.models, (model, modelName) => {
81+
// ignore model if not configured
82+
if (!this.app.orm[model.globalId]) {
83+
return
84+
}
85+
// Associate models if method defined
86+
if (this.app.orm[model.globalId].associate) {
87+
this.app.orm[model.globalId].associate(this.app.orm)
88+
}
7189

72-
this.orm[model.globalId.toLowerCase()] = this.app.orm[model.globalId]
90+
// Reset the orm Model
91+
this.orm[model.globalId.toLowerCase()] = this.app.orm[model.globalId]
92+
})
7393
})
7494

7595
return this.migrate()
@@ -78,31 +98,33 @@ module.exports = class SequelizeTrailpack extends Trailpack {
7898
/**
7999
* Close all database connections
80100
*/
81-
unload() {
101+
async unload() {
82102
return Promise.all(
83-
_.map(this.connections, connection => {
103+
_.map(this.stores, store => {
84104
return new Promise((resolve, reject) => {
85-
connection.close()
105+
store.connection.close()
86106
resolve()
87107
})
88108
})
89109
)
90110
}
91111

92-
migrate() {
112+
async migrate() {
93113
const SchemaMigrationService = this.app.services.SchemaMigrationService
94-
const database = this.app.config.database
95-
96-
if (database.models.migrate == 'none') return
97114

98115
return Promise.all(
99-
_.map(this.connections, connection => {
100-
101-
if (database.models.migrate == 'drop') {
102-
return SchemaMigrationService.dropDB(connection)
116+
_.map(this.stores, store => {
117+
if (store.migrate === 'drop') {
118+
return SchemaMigrationService.dropDB(store.connection)
119+
}
120+
else if (store.migrate === 'alter') {
121+
return SchemaMigrationService.alterDB(store.connection)
122+
}
123+
else if (store.migrate === 'none') {
124+
return
103125
}
104-
else if (database.models.migrate == 'alter') {
105-
return SchemaMigrationService.alterDB(connection)
126+
else {
127+
return
106128
}
107129
})
108130
)

0 commit comments

Comments
 (0)