serverless-dynamodb-local
Advanced tools
@@ -0,0 +0,0 @@ # Contributor Covenant Code of Conduct |
@@ -0,0 +0,0 @@ ## How to contribute to serverless-dynamodb-local |
112
index.js
@@ -118,9 +118,2 @@ "use strict"; | ||
const stage = (this.options && this.options.stage) || (this.service.provider && this.service.provider.stage); | ||
if (this.config.stages && !this.config.stages.includes(stage)) { | ||
// don't do anything for this stage | ||
this.hooks = {}; | ||
return; | ||
} | ||
this.hooks = { | ||
@@ -149,2 +142,23 @@ "dynamodb:migrate:migrateHandler": this.migrateHandler.bind(this), | ||
/** | ||
* Get the stage | ||
* | ||
* @return {String} the current stage | ||
*/ | ||
get stage() { | ||
return (this.options && this.options.stage) || (this.service.provider && this.service.provider.stage); | ||
} | ||
/** | ||
* To check if the handler needs to be executed based on stage | ||
* | ||
* @return {Boolean} if the handler can run for the provided stage | ||
*/ | ||
shouldExecute() { | ||
if (this.config.stages && this.config.stages.includes(this.stage)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
dynamodbOptions(options) { | ||
@@ -179,21 +193,29 @@ let dynamoOptions = {}; | ||
migrateHandler() { | ||
const dynamodb = this.dynamodbOptions(); | ||
const tables = this.tables; | ||
return BbPromise.each(tables, (table) => this.createTable(dynamodb, table)); | ||
if (this.shouldExecute()) { | ||
const dynamodb = this.dynamodbOptions(); | ||
const tables = this.tables; | ||
return BbPromise.each(tables, (table) => this.createTable(dynamodb, table)); | ||
} else { | ||
this.serverlessLog("Skipping migration: DynamoDB Local is not available for stage: " + this.stage); | ||
} | ||
} | ||
seedHandler() { | ||
const options = this.options; | ||
const dynamodb = this.dynamodbOptions(options); | ||
if (this.shouldExecute()) { | ||
const options = this.options; | ||
const dynamodb = this.dynamodbOptions(options); | ||
return BbPromise.each(this.seedSources, (source) => { | ||
if (!source.table) { | ||
throw new Error("seeding source \"table\" property not defined"); | ||
} | ||
const seedPromise = seeder.locateSeeds(source.sources || []) | ||
.then((seeds) => seeder.writeSeeds(dynamodb.doc.batchWrite.bind(dynamodb.doc), source.table, seeds)); | ||
const rawSeedPromise = seeder.locateSeeds(source.rawsources || []) | ||
.then((seeds) => seeder.writeSeeds(dynamodb.raw.batchWriteItem.bind(dynamodb.raw), source.table, seeds)); | ||
return BbPromise.all([seedPromise, rawSeedPromise]); | ||
}); | ||
return BbPromise.each(this.seedSources, (source) => { | ||
if (!source.table) { | ||
throw new Error("seeding source \"table\" property not defined"); | ||
} | ||
const seedPromise = seeder.locateSeeds(source.sources || []) | ||
.then((seeds) => seeder.writeSeeds(dynamodb.doc.batchWrite.bind(dynamodb.doc), source.table, seeds)); | ||
const rawSeedPromise = seeder.locateSeeds(source.rawsources || []) | ||
.then((seeds) => seeder.writeSeeds(dynamodb.raw.batchWriteItem.bind(dynamodb.raw), source.table, seeds)); | ||
return BbPromise.all([seedPromise, rawSeedPromise]); | ||
}); | ||
} else { | ||
this.serverlessLog("Skipping seeding: DynamoDB Local is not available for stage: " + this.stage); | ||
} | ||
} | ||
@@ -211,31 +233,37 @@ | ||
startHandler() { | ||
const config = this.service.custom && this.service.custom.dynamodb || {}; | ||
const options = _.merge({ | ||
sharedDb: this.options.sharedDb || true, | ||
install_path: this.options.localPath | ||
}, | ||
config && config.start, | ||
this.options | ||
); | ||
if (this.shouldExecute()) { | ||
const config = this.service.custom && this.service.custom.dynamodb || {}; | ||
const options = _.merge({ | ||
sharedDb: this.options.sharedDb || true, | ||
install_path: this.options.localPath | ||
}, | ||
config && config.start, | ||
this.options | ||
); | ||
// otherwise endHandler will be mis-informed | ||
this.options = options; | ||
// otherwise endHandler will be mis-informed | ||
this.options = options; | ||
let dbPath = options.dbPath; | ||
if (dbPath) { | ||
options.dbPath = path.isAbsolute(dbPath) ? dbPath : path.join(this.serverless.config.servicePath, dbPath); | ||
} | ||
let dbPath = options.dbPath; | ||
if (dbPath) { | ||
options.dbPath = path.isAbsolute(dbPath) ? dbPath : path.join(this.serverless.config.servicePath, dbPath); | ||
} | ||
if (!options.noStart) { | ||
dynamodbLocal.start(options); | ||
if (!options.noStart) { | ||
dynamodbLocal.start(options); | ||
} | ||
return BbPromise.resolve() | ||
.then(() => options.migrate && this.migrateHandler()) | ||
.then(() => options.seed && this.seedHandler()); | ||
} else { | ||
this.serverlessLog("Skipping start: DynamoDB Local is not available for stage: " + this.stage); | ||
} | ||
return BbPromise.resolve() | ||
.then(() => options.migrate && this.migrateHandler()) | ||
.then(() => options.seed && this.seedHandler()); | ||
} | ||
endHandler() { | ||
if (!this.options.noStart) { | ||
if (this.shouldExecute() && !this.options.noStart) { | ||
this.serverlessLog("DynamoDB - stopping local database"); | ||
dynamodbLocal.stop(this.port); | ||
} else { | ||
this.serverlessLog("Skipping end: DynamoDB Local is not available for stage: " + this.stage); | ||
} | ||
@@ -242,0 +270,0 @@ } |
{ | ||
"name": "serverless-dynamodb-local", | ||
"version": "0.2.37", | ||
"version": "0.2.38", | ||
"engines": { | ||
@@ -5,0 +5,0 @@ "node": ">=4.0" |
@@ -123,19 +123,20 @@ serverless-dynamodb-local | ||
```yml | ||
dynamodb: | ||
start: | ||
seed: true | ||
custom: | ||
dynamodb: | ||
start: | ||
seed: true | ||
seed: | ||
domain: | ||
sources: | ||
- table: domain-widgets | ||
sources: [./domainWidgets.json] | ||
- table: domain-fidgets | ||
sources: [./domainFidgets.json] | ||
test: | ||
sources: | ||
- table: users | ||
rawsources: [./fake-test-users.json] | ||
- table: subscriptions | ||
sources: [./fake-test-subscriptions.json] | ||
seed: | ||
domain: | ||
sources: | ||
- table: domain-widgets | ||
sources: [./domainWidgets.json] | ||
- table: domain-fidgets | ||
sources: [./domainFidgets.json] | ||
test: | ||
sources: | ||
- table: users | ||
rawsources: [./fake-test-users.json] | ||
- table: subscriptions | ||
sources: [./fake-test-subscriptions.json] | ||
``` | ||
@@ -142,0 +143,0 @@ |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
726
3.86%223
0.45%3
-25%47468
-0.1%