1+ /* eslint no-console: [0] */
12'use strict'
23
34const Trailpack = require ( 'trailpack/datastore' )
@@ -15,7 +16,7 @@ module.exports = class SequelizeTrailpack extends Trailpack {
1516 this . app . config . log . logger . warn ( 'No store configured at config.database.stores, models will be ignored' )
1617 }
1718 return Promise . all ( [
18- lib . Validator . validateDatabaseConfig ( this . app . config . database )
19+ lib . Validator . validateStoresConfig ( stores )
1920 ] )
2021 }
2122
@@ -36,40 +37,55 @@ module.exports = class SequelizeTrailpack extends Trailpack {
3637
3738 this . orm = this . orm || { }
3839 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- }
40+ this . connections = lib . Transformer . transformStoreConnections ( this . app )
41+ this . app . models = lib . Transformer . transformModels ( this . app )
42+
43+ this . stores = _ . mapValues ( this . app . config . get ( 'stores' ) , ( store , storeName ) => {
44+ return {
45+ models : _ . pickBy ( this . app . models , { store : storeName } ) ,
46+ connection : this . connections [ storeName ] ,
47+ migrate : store . migrate || this . app . config . get ( 'models.migrate' )
48+ }
49+ } )
5350
54- if ( model . config . instanceMethods ) {
55- for ( const methodName in model . config . instanceMethods ) {
56- Model . prototype [ methodName ] = model . config . instanceMethods [ methodName ]
57- }
51+ // Loop through store models and define them in Sequelize
52+ _ . each ( this . stores , ( store , storeName ) => {
53+ _ . each ( store . models , ( model , modelName ) => {
54+ const Model = store . connection . define ( modelName , model . schema , model . options )
55+
56+ if ( model . options ) {
57+ if ( model . options . classMethods ) {
58+ for ( const methodName in model . options . classMethods ) {
59+ Model [ methodName ] = model . options . classMethods [ methodName ]
5860 }
5961 }
6062
61- this . app . orm [ model . globalId ] = Model
63+ if ( model . options . instanceMethods ) {
64+ for ( const methodName in model . options . instanceMethods ) {
65+ Model . prototype [ methodName ] = model . options . instanceMethods [ methodName ]
66+ }
67+ }
6268 }
69+ this . app . orm [ model . globalId ] = Model
6370 } )
6471 } )
6572
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 )
73+ // Loop through store models and associate
74+ _ . each ( this . stores , ( store , storeName ) => {
75+ // Run Associate on the Models
76+ _ . each ( store . models , ( model , modelName ) => {
77+ // ignore model if not configured
78+ if ( ! this . app . orm [ model . globalId ] ) {
79+ return
80+ }
81+ // Associate models if method defined
82+ if ( this . app . orm [ model . globalId ] . associate ) {
83+ this . app . orm [ model . globalId ] . associate ( this . app . orm )
84+ }
7185
72- this . orm [ model . globalId . toLowerCase ( ) ] = this . app . orm [ model . globalId ]
86+ // Reset the orm Model
87+ this . orm [ model . globalId . toLowerCase ( ) ] = this . app . orm [ model . globalId ]
88+ } )
7389 } )
7490
7591 return this . migrate ( )
@@ -80,9 +96,9 @@ module.exports = class SequelizeTrailpack extends Trailpack {
8096 */
8197 async unload ( ) {
8298 return Promise . all (
83- _ . map ( this . connections , connection => {
99+ _ . map ( this . stores , store => {
84100 return new Promise ( ( resolve , reject ) => {
85- connection . close ( )
101+ store . connection . close ( )
86102 resolve ( )
87103 } )
88104 } )
@@ -91,18 +107,20 @@ module.exports = class SequelizeTrailpack extends Trailpack {
91107
92108 async migrate ( ) {
93109 const SchemaMigrationService = this . app . services . SchemaMigrationService
94- const migrate = this . app . config . get ( 'database.models.migrate' )
95-
96- if ( migrate === 'none' ) return
97110
98111 return Promise . all (
99- _ . map ( this . connections , connection => {
100-
101- if ( migrate === 'drop' ) {
102- return SchemaMigrationService . dropDB ( connection )
112+ _ . map ( this . stores , store => {
113+ if ( store . migrate === 'drop' ) {
114+ return SchemaMigrationService . dropDB ( store . connection )
115+ }
116+ else if ( store . migrate === 'alter' ) {
117+ return SchemaMigrationService . alterDB ( store . connection )
118+ }
119+ else if ( store . migrate === 'none' ) {
120+ return
103121 }
104- else if ( migrate === 'alter' ) {
105- return SchemaMigrationService . alterDB ( connection )
122+ else {
123+ return
106124 }
107125 } )
108126 )
0 commit comments