Skip to content

Commit 61c95f8

Browse files
authored
Merge branch 'v1' into aneil/allowCustomInstallPath
2 parents 0d42878 + 08d796f commit 61c95f8

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,23 @@ All CLI options are optional:
5353
--optimizeDbBeforeStartup -o Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.
5454
--migrate -m After starting DynamoDB local, create DynamoDB tables from the Serverless configuration.
5555
--seed -s After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload.
56+
--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.
5657
```
5758

5859
All the above options can be added to serverless.yml to set default configuration: e.g.
5960

6061
```yml
6162
custom:
6263
dynamodb:
64+
# If you only want to use DynamoDB Local in some stages, declare them here
65+
stages:
66+
- dev
6367
start:
6468
port: 8000
6569
inMemory: true
6670
migrate: true
6771
seed: true
72+
convertEmptyValues: true
6873
# Uncomment only if you already have a DynamoDB running locally
6974
# noStart: true
7075
```

index.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class ServerlessDynamodbLocal {
7474
seed: {
7575
shortcut: "s",
7676
usage: "After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload.",
77+
},
78+
convertEmptyValues: {
79+
shortcut: "e",
80+
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.",
7781
}
7882
}
7983
},
@@ -101,6 +105,13 @@ class ServerlessDynamodbLocal {
101105
}
102106
};
103107

108+
const stage = this.options.stage || 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+
104115
this.hooks = {
105116
"dynamodb:migrate:migrateHandler": this.migrateHandler.bind(this),
106117
"dynamodb:seed:seedHandler": this.seedHandler.bind(this),
@@ -113,13 +124,13 @@ class ServerlessDynamodbLocal {
113124
}
114125

115126
get port() {
116-
const config = this.config;
127+
const config = this.service.custom && this.service.custom.dynamodb || {};
117128
const port = _.get(config, "start.port", 8000);
118129
return port;
119130
}
120131

121132
get host() {
122-
const config = this.config;
133+
const config = this.service.custom && this.service.custom.dynamodb || {};
123134
const host = _.get(config, "start.host", "localhost");
124135
return host;
125136
}
@@ -134,13 +145,15 @@ class ServerlessDynamodbLocal {
134145
}
135146
dynamoOptions = {
136147
region: options.region,
148+
convertEmptyValues: options && options.convertEmptyValues ? options.convertEmptyValues : false,
137149
};
138150
} else {
139151
dynamoOptions = {
140152
endpoint: `http://${this.host}:${this.port}`,
141153
region: "localhost",
142154
accessKeyId: "MOCK_ACCESS_KEY_ID",
143-
secretAccessKey: "MOCK_SECRET_ACCESS_KEY"
155+
secretAccessKey: "MOCK_SECRET_ACCESS_KEY",
156+
convertEmptyValues: options && options.convertEmptyValues ? options.convertEmptyValues : false,
144157
};
145158
}
146159

@@ -182,7 +195,7 @@ class ServerlessDynamodbLocal {
182195
}
183196

184197
startHandler() {
185-
const config = this.config;
198+
const config = this.service.custom && this.service.custom.dynamodb || {};
186199
const options = _.merge({
187200
sharedDb: this.options.sharedDb || true,
188201
install_path: this.options.localPath
@@ -279,6 +292,9 @@ class ServerlessDynamodbLocal {
279292
migration.SSESpecification.Enabled = migration.SSESpecification.SSEEnabled;
280293
delete migration.SSESpecification.SSEEnabled;
281294
}
295+
if (migration.PointInTimeRecoverySpecification) {
296+
delete migration.PointInTimeRecoverySpecification;
297+
}
282298
if (migration.Tags) {
283299
delete migration.Tags;
284300
}

src/seeder.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ function fileExists(fileName) {
9696
*/
9797
function unmarshalBuffer(json) {
9898
_.forEach(json, function(value, key) {
99-
if (value.type==="Buffer") {
99+
// Null check to prevent creation of Buffer when value is null
100+
if (value !== null && value.type==="Buffer") {
100101
json[key]= new Buffer(value.data);
101102
}
102103
});

test/indexTest.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ const expect = require("chai").expect;
66
const should = require("should");
77
const aws = require ("aws-sdk");
88
const seeder = require("../src/seeder.js");
9-
const dataApp = require("../index.js");
9+
const Plugin = require("../index.js");
10+
11+
const serverlessMock = require("./serverlessMock");
1012

1113
describe("Port function",function(){
1214
it("Port should return number",function(){
13-
let myport = dataApp.prototype.port;
14-
assert(typeof myport, "number");
15+
let service = new Plugin(serverlessMock, {});
16+
assert(typeof service.port, "number");
1517
});
1618

1719
it("Port value should be >= 0 and < 65536",function(done){
18-
http.get(`http://localhost:${dataApp.prototype.port}/shell/`, function (response) {
20+
let service = new Plugin(serverlessMock, {});
21+
http.get(`http://localhost:${service.port}/shell/`, function (response) {
1922
assert.equal(response.statusCode, 200);
2023
done();
2124
});
@@ -34,29 +37,29 @@ describe("Check the dynamodb function",function(){
3437
});
3538

3639
it("Should be an object",function(){
37-
let dynamoOptions = dataApp.prototype.dynamodbOptions;
40+
let dynamoOptions = Plugin.prototype.dynamodbOptions;
3841
let raw = new aws.DynamoDB(dynamoOptions);
3942
raw.should.be.type("object");
4043
});
4144

4245
it("Should be an object",function(){
43-
let dynamoOptions = dataApp.prototype.dynamodbOptions;
46+
let dynamoOptions = Plugin.prototype.dynamodbOptions;
4447
let doc = new aws.DynamoDB(dynamoOptions);
4548
doc.should.be.type("object");
4649
});
4750
});
4851

4952
describe ("Start handler function",function(){
5053
it ("Should not be null",function(){
51-
let handler = dataApp.prototype.startHandler;
54+
let handler = Plugin.prototype.startHandler;
5255
assert(handler =! null);
5356
});
5457
});
5558

5659

5760
describe ("createTable functon",function(){
5861
it ("Should check as a function",function(){
59-
const tbl = dataApp.prototype.createTable;
62+
const tbl = Plugin.prototype.createTable;
6063
assert.equal(typeof tbl, "function");
6164
});
6265
});

test/serverlessMock.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
service: {},
3+
cli: {
4+
log: () => {}
5+
},
6+
custom: {}
7+
};

0 commit comments

Comments
 (0)