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