@@ -16,7 +16,7 @@ exports.register = function(plugin, options, next) {
1616 var dir = [ ] ;
1717 var logging = process . env . NODE_ENV === 'production' ? false : console . log ;
1818
19- Array . isArray ( options . models ) ? dir = options . models : dir . push ( options . models ) ;
19+ dir = Array . isArray ( options . models ) ? options . models : dir . push ( options . models ) ;
2020
2121 dir . forEach ( function ( folder , index , arr ) {
2222 arr [ index ] = path . join ( process . cwd ( ) , folder ) ;
@@ -30,47 +30,42 @@ exports.register = function(plugin, options, next) {
3030 logging : logging
3131 } ) ;
3232
33- sequelize . authenticate ( )
34- . then ( function ( ) {
35- var db = { } ;
36-
37- // recursively get all models in 'models' directory, and all nested sub-directories
38- var getModels = function ( folder ) {
39- var fileContents = fs . readdirSync ( folder )
40-
41- fileContents . forEach ( function ( fileName ) {
42- var stats = fs . lstatSync ( folder + '/' + fileName ) ;
43-
44- if ( stats . isDirectory ( ) ) {
45- getModels ( folder + '/' + fileName )
46- } else if ( fileName . indexOf ( '.' ) !== 0 ) {
47- var filePath = path . join ( folder , fileName ) ;
48- var model = sequelize . import ( filePath ) ;
49- db [ model . name ] = model ;
50- }
51- } ) ;
33+ var db = { } ;
34+
35+ // recursively get all models in 'models' directory, and all nested sub-directories
36+ var getModels = function ( folder ) {
37+ var fileContents = fs . readdirSync ( folder )
38+
39+ fileContents . forEach ( function ( fileName ) {
40+ var stats = fs . lstatSync ( folder + '/' + fileName ) ;
41+
42+ if ( stats . isDirectory ( ) ) {
43+ getModels ( folder + '/' + fileName )
44+ } else if ( fileName . indexOf ( '.' ) !== 0 ) {
45+ var filePath = path . join ( folder , fileName ) ;
46+ var model = sequelize . import ( filePath ) ;
47+ db [ model . name ] = model ;
5248 }
49+ } ) ;
50+ }
5351
54- dir . forEach ( function ( folder ) {
55- getModels ( folder ) ;
56- } ) ;
52+ dir . forEach ( function ( folder ) {
53+ getModels ( folder ) ;
54+ } ) ;
55+
56+ Object . keys ( db ) . forEach ( function ( modelName ) {
57+ if ( "associate" in db [ modelName ] ) {
58+ db [ modelName ] . associate ( db ) ;
59+ }
60+ } ) ;
5761
58- Object . keys ( db ) . forEach ( function ( modelName ) {
59- if ( "associate" in db [ modelName ] ) {
60- db [ modelName ] . associate ( db ) ;
61- }
62- } ) ;
62+ db . sequelize = sequelize ;
63+ db . Sequelize = Sequelize ;
6364
64- db . sequelize = sequelize ;
65- db . Sequelize = Sequelize ;
65+ plugin . expose ( 'db' , db ) ;
6666
67- plugin . expose ( 'db' , db ) ;
67+ return next ( ) ;
6868
69- return next ( ) ;
70- } )
71- . catch ( function ( err ) {
72- return next ( err ) ;
73- } ) ;
7469} ;
7570
7671exports . register . attributes = {
0 commit comments