diff --git a/README.md b/README.md index bf6a576c..cc955278 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ All CLI options are optional: --migrate -m After starting DynamoDB local, create DynamoDB tables from the Serverless configuration. --seed -s After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload. --convertEmptyValues -e Set to true if you would like the document client to convert empty values (0-length strings, binary buffers, and sets) to be converted to NULL types when persisting to DynamoDB. +--pauseDbAfterSeeding -p After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates running process. When terminate signal received, it will stop the database on the running port. ``` All the above options can be added to serverless.yml to set default configuration: e.g. @@ -75,6 +76,7 @@ custom: migrate: true seed: true convertEmptyValues: true + pauseDbAfterSeeding: true # Uncomment only if you already have a DynamoDB running locally # noStart: true ``` diff --git a/index.js b/index.js index c0fb955c..74691ea6 100644 --- a/index.js +++ b/index.js @@ -88,7 +88,10 @@ class ServerlessDynamodbLocal { convertEmptyValues: { shortcut: "e", usage: "Set to true if you would like the document client to convert empty values (0-length strings, binary buffers, and sets) to be converted to NULL types when persisting to DynamoDB.", - } + }, + pauseDbAfterSeeding: { + usage: "After starting dynamodb local and all migrations and seeding is completed (if set to run), process will stay open until user terminates running process. When terminate signal received, it will stop the database on the running port." + } } }, noStart: { @@ -254,8 +257,21 @@ class ServerlessDynamodbLocal { } 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()) + .then(() => { + if (options.pauseDbAfterSeeding) { + this.serverlessLog("DynamoDB - Database is running. Waiting for user to stop..."); + return BbPromise.resolve() + .then(() => this.listenForTermination()) + .then(() => this.endHandler()); + } + }); } + endHandler() { if (this.shouldExecute() && !this.options.noStart) { this.serverlessLog("DynamoDB - stopping local database"); @@ -286,6 +302,24 @@ class ServerlessDynamodbLocal { }).filter((n) => n); } + listenForTermination() { + // SIGINT will be usually sent when user presses ctrl+c + const waitForSigInt = new Promise((resolve) => { + process.on("SIGINT", () => resolve("SIGINT")); + }); + + // SIGTERM is a default termination signal in many cases, + // for example when "killing" a subprocess spawned in node + // with child_process methods + const waitForSigTerm = new Promise((resolve) => { + process.on("SIGTERM", () => resolve("SIGTERM")); + }); + + return Promise.race([waitForSigInt, waitForSigTerm]).then((command) => { + this.serverlessLog(`Got ${command} signal. Will stop dynamodb local...`); + }); + } + /** * Gets the table definitions */