1+ /* eslint no-console: [0] */
12'use strict'
23
34const 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