@@ -105,13 +105,6 @@ class ServerlessDynamodbLocal {
105
105
}
106
106
} ;
107
107
108
- const stage = ( this . options && this . options . stage ) || ( this . service . provider && this . service . provider . stage ) ;
109
- if ( this . config . stages && ! this . config . stages . includes ( stage ) ) {
110
- // don't do anything for this stage
111
- this . hooks = { } ;
112
- return ;
113
- }
114
-
115
108
this . hooks = {
116
109
"dynamodb:migrate:migrateHandler" : this . migrateHandler . bind ( this ) ,
117
110
"dynamodb:seed:seedHandler" : this . seedHandler . bind ( this ) ,
@@ -135,6 +128,27 @@ class ServerlessDynamodbLocal {
135
128
return host ;
136
129
}
137
130
131
+ /**
132
+ * Get the stage
133
+ *
134
+ * @return {String } the current stage
135
+ */
136
+ get stage ( ) {
137
+ return ( this . options && this . options . stage ) || ( this . service . provider && this . service . provider . stage ) ;
138
+ }
139
+
140
+ /**
141
+ * To check if the handler needs to be executed based on stage
142
+ *
143
+ * @return {Boolean } if the handler can run for the provided stage
144
+ */
145
+ shouldExecute ( ) {
146
+ if ( this . config . stages && this . config . stages . includes ( this . stage ) ) {
147
+ return true ;
148
+ }
149
+ return false ;
150
+ }
151
+
138
152
dynamodbOptions ( options ) {
139
153
let dynamoOptions = { } ;
140
154
@@ -164,25 +178,33 @@ class ServerlessDynamodbLocal {
164
178
}
165
179
166
180
migrateHandler ( ) {
167
- const dynamodb = this . dynamodbOptions ( ) ;
168
- const tables = this . tables ;
169
- return BbPromise . each ( tables , ( table ) => this . createTable ( dynamodb , table ) ) ;
181
+ if ( this . shouldExecute ( ) ) {
182
+ const dynamodb = this . dynamodbOptions ( ) ;
183
+ const tables = this . tables ;
184
+ return BbPromise . each ( tables , ( table ) => this . createTable ( dynamodb , table ) ) ;
185
+ } else {
186
+ this . serverlessLog ( "Skipping migration: DynamoDB Local is not available for stage: " + this . stage ) ;
187
+ }
170
188
}
171
189
172
190
seedHandler ( ) {
173
- const options = this . options ;
174
- const dynamodb = this . dynamodbOptions ( options ) ;
191
+ if ( this . shouldExecute ( ) ) {
192
+ const options = this . options ;
193
+ const dynamodb = this . dynamodbOptions ( options ) ;
175
194
176
- return BbPromise . each ( this . seedSources , ( source ) => {
177
- if ( ! source . table ) {
178
- throw new Error ( "seeding source \"table\" property not defined" ) ;
179
- }
180
- const seedPromise = seeder . locateSeeds ( source . sources || [ ] )
181
- . then ( ( seeds ) => seeder . writeSeeds ( dynamodb . doc . batchWrite . bind ( dynamodb . doc ) , source . table , seeds ) ) ;
182
- const rawSeedPromise = seeder . locateSeeds ( source . rawsources || [ ] )
183
- . then ( ( seeds ) => seeder . writeSeeds ( dynamodb . raw . batchWriteItem . bind ( dynamodb . raw ) , source . table , seeds ) ) ;
184
- return BbPromise . all ( [ seedPromise , rawSeedPromise ] ) ;
185
- } ) ;
195
+ return BbPromise . each ( this . seedSources , ( source ) => {
196
+ if ( ! source . table ) {
197
+ throw new Error ( "seeding source \"table\" property not defined" ) ;
198
+ }
199
+ const seedPromise = seeder . locateSeeds ( source . sources || [ ] )
200
+ . then ( ( seeds ) => seeder . writeSeeds ( dynamodb . doc . batchWrite . bind ( dynamodb . doc ) , source . table , seeds ) ) ;
201
+ const rawSeedPromise = seeder . locateSeeds ( source . rawsources || [ ] )
202
+ . then ( ( seeds ) => seeder . writeSeeds ( dynamodb . raw . batchWriteItem . bind ( dynamodb . raw ) , source . table , seeds ) ) ;
203
+ return BbPromise . all ( [ seedPromise , rawSeedPromise ] ) ;
204
+ } ) ;
205
+ } else {
206
+ this . serverlessLog ( "Skipping seeding: DynamoDB Local is not available for stage: " + this . stage ) ;
207
+ }
186
208
}
187
209
188
210
removeHandler ( ) {
@@ -195,35 +217,41 @@ class ServerlessDynamodbLocal {
195
217
}
196
218
197
219
startHandler ( ) {
198
- const config = this . service . custom && this . service . custom . dynamodb || { } ;
199
- const options = _ . merge ( {
200
- sharedDb : this . options . sharedDb || true ,
201
- install_path : this . options . localPath
202
- } ,
203
- config && config . start ,
204
- this . options
205
- ) ;
220
+ if ( this . shouldExecute ( ) ) {
221
+ const config = this . service . custom && this . service . custom . dynamodb || { } ;
222
+ const options = _ . merge ( {
223
+ sharedDb : this . options . sharedDb || true ,
224
+ install_path : this . options . localPath
225
+ } ,
226
+ config && config . start ,
227
+ this . options
228
+ ) ;
206
229
207
- // otherwise endHandler will be mis-informed
208
- this . options = options ;
230
+ // otherwise endHandler will be mis-informed
231
+ this . options = options ;
209
232
210
- let dbPath = options . dbPath ;
211
- if ( dbPath ) {
212
- options . dbPath = path . isAbsolute ( dbPath ) ? dbPath : path . join ( this . serverless . config . servicePath , dbPath ) ;
213
- }
233
+ let dbPath = options . dbPath ;
234
+ if ( dbPath ) {
235
+ options . dbPath = path . isAbsolute ( dbPath ) ? dbPath : path . join ( this . serverless . config . servicePath , dbPath ) ;
236
+ }
214
237
215
- if ( ! options . noStart ) {
216
- dynamodbLocal . start ( options ) ;
238
+ if ( ! options . noStart ) {
239
+ dynamodbLocal . start ( options ) ;
240
+ }
241
+ return BbPromise . resolve ( )
242
+ . then ( ( ) => options . migrate && this . migrateHandler ( ) )
243
+ . then ( ( ) => options . seed && this . seedHandler ( ) ) ;
244
+ } else {
245
+ this . serverlessLog ( "Skipping start: DynamoDB Local is not available for stage: " + this . stage ) ;
217
246
}
218
- return BbPromise . resolve ( )
219
- . then ( ( ) => options . migrate && this . migrateHandler ( ) )
220
- . then ( ( ) => options . seed && this . seedHandler ( ) ) ;
221
247
}
222
248
223
249
endHandler ( ) {
224
- if ( ! this . options . noStart ) {
250
+ if ( this . shouldExecute ( ) && ! this . options . noStart ) {
225
251
this . serverlessLog ( "DynamoDB - stopping local database" ) ;
226
252
dynamodbLocal . stop ( this . port ) ;
253
+ } else {
254
+ this . serverlessLog ( "Skipping end: DynamoDB Local is not available for stage: " + this . stage ) ;
227
255
}
228
256
}
229
257
0 commit comments