Skip to content

Commit 5e53d76

Browse files
authored
Merge pull request #192 from RishikeshDarandale/topics/issue-191
fix(issue-191): updated stage verification step post construction
2 parents b4fff55 + 64d5465 commit 5e53d76

File tree

1 file changed

+70
-42
lines changed

1 file changed

+70
-42
lines changed

index.js

Lines changed: 70 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,6 @@ class ServerlessDynamodbLocal {
115115
}
116116
};
117117

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-
125118
this.hooks = {
126119
"dynamodb:migrate:migrateHandler": this.migrateHandler.bind(this),
127120
"dynamodb:seed:seedHandler": this.seedHandler.bind(this),
@@ -145,6 +138,27 @@ class ServerlessDynamodbLocal {
145138
return host;
146139
}
147140

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+
148162
dynamodbOptions(options) {
149163
let dynamoOptions = {};
150164

@@ -174,25 +188,33 @@ class ServerlessDynamodbLocal {
174188
}
175189

176190
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+
}
180198
}
181199

182200
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);
185204

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+
}
196218
}
197219

198220
removeHandler() {
@@ -205,35 +227,41 @@ class ServerlessDynamodbLocal {
205227
}
206228

207229
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+
);
216239

217-
// otherwise endHandler will be mis-informed
218-
this.options = options;
240+
// otherwise endHandler will be mis-informed
241+
this.options = options;
219242

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+
}
224247

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);
227256
}
228-
return BbPromise.resolve()
229-
.then(() => options.migrate && this.migrateHandler())
230-
.then(() => options.seed && this.seedHandler());
231257
}
232258

233259
endHandler() {
234-
if (!this.options.noStart) {
260+
if (this.shouldExecute() && !this.options.noStart) {
235261
this.serverlessLog("DynamoDB - stopping local database");
236262
dynamodbLocal.stop(this.port);
263+
} else {
264+
this.serverlessLog("Skipping end: DynamoDB Local is not available for stage: " + this.stage);
237265
}
238266
}
239267

0 commit comments

Comments
 (0)