Skip to content

Commit 92ab986

Browse files
fix(issue-191): updated stage verification step post construction
If stage property under provider has varaible reference, then it was used literally same as expression in plugin construction. Thus, to use these properties correctly, need to move the stage verification logic post construction of the plugin.
1 parent 1cc7d1b commit 92ab986

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
@@ -105,13 +105,6 @@ class ServerlessDynamodbLocal {
105105
}
106106
};
107107

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-
115108
this.hooks = {
116109
"dynamodb:migrate:migrateHandler": this.migrateHandler.bind(this),
117110
"dynamodb:seed:seedHandler": this.seedHandler.bind(this),
@@ -135,6 +128,27 @@ class ServerlessDynamodbLocal {
135128
return host;
136129
}
137130

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+
138152
dynamodbOptions(options) {
139153
let dynamoOptions = {};
140154

@@ -164,25 +178,33 @@ class ServerlessDynamodbLocal {
164178
}
165179

166180
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+
}
170188
}
171189

172190
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);
175194

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+
}
186208
}
187209

188210
removeHandler() {
@@ -195,35 +217,41 @@ class ServerlessDynamodbLocal {
195217
}
196218

197219
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+
);
206229

207-
// otherwise endHandler will be mis-informed
208-
this.options = options;
230+
// otherwise endHandler will be mis-informed
231+
this.options = options;
209232

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

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);
217246
}
218-
return BbPromise.resolve()
219-
.then(() => options.migrate && this.migrateHandler())
220-
.then(() => options.seed && this.seedHandler());
221247
}
222248

223249
endHandler() {
224-
if (!this.options.noStart) {
250+
if (this.shouldExecute() && !this.options.noStart) {
225251
this.serverlessLog("DynamoDB - stopping local database");
226252
dynamodbLocal.stop(this.port);
253+
} else {
254+
this.serverlessLog("Skipping end: DynamoDB Local is not available for stage: " + this.stage);
227255
}
228256
}
229257

0 commit comments

Comments
 (0)