Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/dynamodb/bin
/node_modules

# asdf config file
.tool-versions
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 34 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
7 changes: 7 additions & 0 deletions test/indexTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down