From 37b01971f196981586ba88aa66c4d9213e7595ac Mon Sep 17 00:00:00 2001 From: Andrew Benz Date: Thu, 16 Apr 2020 16:07:45 -0500 Subject: [PATCH 1/2] Add ability to drop tables from a running DynamoDB instance --- .gitignore | 3 +++ README.md | 5 +++++ index.js | 34 ++++++++++++++++++++++++++++++++++ test/indexTest.js | 7 +++++++ 4 files changed, 49 insertions(+) diff --git a/.gitignore b/.gitignore index 42f0d66b..1a816345 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ /dynamodb/bin /node_modules + +# asdf config file +.tool-versions \ No newline at end of file diff --git a/README.md b/README.md index 839080b6..68e88a90 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,11 @@ If seed config is set to true, your configuration will be seeded automatically o ] ``` +## Dropping tables: sls dynamodb drop +When running a locally managed DynamoDB instance via sls dynamodb start, your tables go away when you stop the instance. If you use a long running server, like [Local Stack](https://github.com/localstack/localstack), you can use the drop command to drop all the tables that are defined in your `serverless.yml` file as described in the "migrations" section. + +Because this is intended to be run manually, there is no configuration for this command. + ## Using DynamoDB Local in your code You need to add the following parameters to the AWS NODE SDK dynamodb constructor diff --git a/index.js b/index.js index c0fb955c..d1cd50ab 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,10 @@ class ServerlessDynamodbLocal { lifecycleEvents: ["migrateHandler"], usage: "Creates local DynamoDB tables from the current Serverless configuration" }, + drop: { + lifecycleEvents: ["dropHandler"], + usage: "Drops local DynamoDB tables from the current Serverless configuration", + }, seed: { lifecycleEvents: ["seedHandler"], usage: "Seeds local DynamoDB tables with data", @@ -117,6 +121,7 @@ class ServerlessDynamodbLocal { this.hooks = { "dynamodb:migrate:migrateHandler": this.migrateHandler.bind(this), + "dynamodb:drop:dropHandler": this.dropHandler.bind(this), "dynamodb:seed:seedHandler": this.seedHandler.bind(this), "dynamodb:remove:removeHandler": this.removeHandler.bind(this), "dynamodb:install:installHandler": this.installHandler.bind(this), @@ -197,6 +202,16 @@ class ServerlessDynamodbLocal { } } + dropHandler() { + if (this.shouldExecute()) { + const dynamodb = this.dynamodbOptions(); + const tables = this.tables; + return BbPromise.each(tables, table => this.dropTable(dynamodb, table)); + } else { + this.serverlessLog("Skipping drop: DynamoDB Local is not available for stage: " + this.stage); + } + } + seedHandler() { if (this.shouldExecute()) { const options = this.options; @@ -372,5 +387,24 @@ class ServerlessDynamodbLocal { }); }); } + + dropTable(dynamodb, migration) { + return new BbPromise((resolve, reject) => { + dynamodb.raw.deleteTable({ TableName: migration.TableName }, err => { + if (err) { + if (err.name === "ResourceNotFoundException") { + this.serverlessLog(`DynamoDB - Warn - table ${migration.TableName} does not exist`); + resolve(); + } else { + this.serverlessLog("DynamoDB - Error - ", err); + reject(err); + } + } else { + this.serverlessLog("DynamoDB - dropped table " + migration.TableName); + resolve(migration); + } + }); + }); + } } module.exports = ServerlessDynamodbLocal; diff --git a/test/indexTest.js b/test/indexTest.js index 1ce35c57..5ace57de 100644 --- a/test/indexTest.js +++ b/test/indexTest.js @@ -102,6 +102,13 @@ describe ("createTable functon",function(){ }); }); +describe ("dropTable functon",function(){ + it ("Should check as a function",function(){ + const tbl = Plugin.prototype.dropTable; + assert.equal(typeof tbl, "function"); + }); +}); + describe ("Check the Seeder file",function(){ it("Table name shoud be a string",function(){ let tblName = seeder.writeSeeds.name; From 8fb89e12e508ae16dbbd4d899afa7d1629337bbc Mon Sep 17 00:00:00 2001 From: Andrew Benz Date: Thu, 16 Apr 2020 17:21:50 -0500 Subject: [PATCH 2/2] Fix lambda formatting --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d1cd50ab..98be220e 100644 --- a/index.js +++ b/index.js @@ -206,7 +206,7 @@ class ServerlessDynamodbLocal { if (this.shouldExecute()) { const dynamodb = this.dynamodbOptions(); const tables = this.tables; - return BbPromise.each(tables, table => this.dropTable(dynamodb, table)); + return BbPromise.each(tables, (table) => this.dropTable(dynamodb, table)); } else { this.serverlessLog("Skipping drop: DynamoDB Local is not available for stage: " + this.stage); } @@ -390,7 +390,7 @@ class ServerlessDynamodbLocal { dropTable(dynamodb, migration) { return new BbPromise((resolve, reject) => { - dynamodb.raw.deleteTable({ TableName: migration.TableName }, err => { + dynamodb.raw.deleteTable({ TableName: migration.TableName }, (err) => { if (err) { if (err.name === "ResourceNotFoundException") { this.serverlessLog(`DynamoDB - Warn - table ${migration.TableName} does not exist`);