Skip to content

Commit cd6e4a3

Browse files
authored
Merge branch 'v1' into v1
2 parents bec605f + 3aece40 commit cd6e4a3

File tree

7 files changed

+61
-29
lines changed

7 files changed

+61
-29
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ install:
3737

3838
before_script:
3939
- sls dynamodb install
40-
- sls dynamodb start -p 3000 &
40+
- sls offline start &
41+
- sleep 15
4142
- cd ..
4243
- cd ..
4344
- npm run test

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ serverless-dynamodb-local
33

44
[![Join the chat at https://gitter.im/99xt/serverless-dynamodb-local](https://badges.gitter.im/99xt/serverless-dynamodb-local.svg)](https://gitter.im/99xt/serverless-dynamodb-local?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
55
[![npm version](https://badge.fury.io/js/serverless-dynamodb-local.svg)](https://badge.fury.io/js/serverless-dynamodb-local)
6-
[![license](https://img.shields.io/npm/l/serverless-dynamodb-local.svg)](https://www.npmjs.com/package/serverless-dynamodb-local)
6+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
77

88
## This Plugin Requires
99
@@ -107,6 +107,8 @@ In `serverless.yml` seeding categories are defined under `dynamodb.seed`.
107107

108108
If `dynamodb.start.seed` is true, then seeding is performed after table migrations.
109109

110+
If you wish to use raw AWS AttributeValues to specify your seed data instead of Javascript types then simply change the variable of any such json files from `sources:` to `rawsources:`.
111+
110112
```yml
111113
dynamodb:
112114
start:
@@ -122,7 +124,7 @@ dynamodb:
122124
test:
123125
sources:
124126
- table: users
125-
sources: [./fake-test-users.json]
127+
rawsources: [./fake-test-users.json]
126128
- table: subscriptions
127129
sources: [./fake-test-subscriptions.json]
128130
```

index.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ class ServerlessDynamodbLocal {
2121
},
2222
seed: {
2323
lifecycleEvents: ["seedHandler"],
24-
usage: "Seeds local DynamoDB tables with data"
24+
usage: "Seeds local DynamoDB tables with data",
25+
options: {
26+
online: {
27+
shortcut: "o",
28+
usage: "Will connect to the tables online to do an online seed run"
29+
}
30+
}
2531
},
2632
start: {
2733
lifecycleEvents: ["startHandler"],
@@ -112,13 +118,25 @@ class ServerlessDynamodbLocal {
112118
return host;
113119
}
114120

115-
dynamodbOptions() {
116-
const dynamoOptions = {
117-
endpoint: `http://${this.host}:${this.port}`,
118-
region: "localhost",
119-
accessKeyId: "MOCK_ACCESS_KEY_ID",
120-
secretAccessKey: "MOCK_SECRET_ACCESS_KEY"
121-
};
121+
dynamodbOptions(options) {
122+
let dynamoOptions = {};
123+
124+
if(options && options.online){
125+
this.serverlessLog("Connecting to online tables...");
126+
if (!options.region) {
127+
throw new Error("please specify the region");
128+
}
129+
dynamoOptions = {
130+
region: options.region,
131+
};
132+
} else {
133+
dynamoOptions = {
134+
endpoint: `http://${this.host}:${this.port}`,
135+
region: "localhost",
136+
accessKeyId: "MOCK_ACCESS_KEY_ID",
137+
secretAccessKey: "MOCK_SECRET_ACCESS_KEY"
138+
};
139+
}
122140

123141
return {
124142
raw: new AWS.DynamoDB(dynamoOptions),
@@ -133,14 +151,18 @@ class ServerlessDynamodbLocal {
133151
}
134152

135153
seedHandler() {
136-
const documentClient = this.dynamodbOptions().doc;
137-
const seedSources = this.seedSources;
138-
return BbPromise.each(seedSources, (source) => {
154+
const options = this.options;
155+
const dynamodb = this.dynamodbOptions(options);
156+
157+
return BbPromise.each(this.seedSources, (source) => {
139158
if (!source.table) {
140159
throw new Error("seeding source \"table\" property not defined");
141160
}
142-
return seeder.locateSeeds(source.sources || [])
143-
.then((seeds) => seeder.writeSeeds(documentClient, source.table, seeds));
161+
const seedPromise = seeder.locateSeeds(source.sources || [])
162+
.then((seeds) => seeder.writeSeeds(dynamodb.doc.batchWrite.bind(dynamodb.doc), source.table, seeds));
163+
const rawSeedPromise = seeder.locateSeeds(source.rawsources || [])
164+
.then((seeds) => seeder.writeSeeds(dynamodb.raw.batchWriteItem.bind(dynamodb.raw), source.table, seeds));
165+
return BbPromise.all([seedPromise, rawSeedPromise]);
144166
});
145167
}
146168

@@ -246,6 +268,13 @@ class ServerlessDynamodbLocal {
246268
if (migration.TimeToLiveSpecification) {
247269
delete migration.TimeToLiveSpecification;
248270
}
271+
if (migration.SSESpecification) {
272+
migration.SSESpecification.Enabled = migration.SSESpecification.SSEEnabled;
273+
delete migration.SSESpecification.SSEEnabled;
274+
}
275+
if (migration.Tags) {
276+
delete migration.Tags;
277+
}
249278
dynamodb.raw.createTable(migration, (err) => {
250279
if (err) {
251280
if (err.name === 'ResourceInUseException') {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-dynamodb-local",
3-
"version": "0.2.25",
3+
"version": "0.2.30",
44
"engines": {
55
"node": ">=4.0"
66
},

src/seeder.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ const MIGRATION_SEED_CONCURRENCY = 5;
1515
/**
1616
* Writes a batch chunk of migration seeds to DynamoDB. DynamoDB has a limit on the number of
1717
* items that may be written in a batch operation.
18-
* @param {DynamoDocumentClient} dynamodb The DynamoDB Document client
18+
* @param {function} dynamodbWriteFunction The DynamoDB DocumentClient.batchWrite or DynamoDB.batchWriteItem function
1919
* @param {string} tableName The table name being written to
2020
* @param {any[]} seeds The migration seeds being written to the table
2121
*/
22-
function writeSeedBatch(dynamodb, tableName, seeds) {
22+
function writeSeedBatch(dynamodbWriteFunction, tableName, seeds) {
2323
const params = {
2424
RequestItems: {
2525
[tableName]: seeds.map((seed) => ({
@@ -34,7 +34,7 @@ function writeSeedBatch(dynamodb, tableName, seeds) {
3434
// again a few times in case the Database resources are in the middle of provisioning.
3535
let interval = 0;
3636
function execute(interval) {
37-
setTimeout(() => dynamodb.batchWrite(params, (err) => {
37+
setTimeout(() => dynamodbWriteFunction(params, (err) => {
3838
if (err) {
3939
if (err.code === "ResourceNotFoundException" && interval <= 5000) {
4040
execute(interval + 1000);
@@ -52,13 +52,13 @@ function writeSeedBatch(dynamodb, tableName, seeds) {
5252

5353
/**
5454
* Writes a seed corpus to the given database table
55-
* @param {DocumentClient} dynamodb The DynamoDB document instance
55+
* @param {function} dynamodbWriteFunction The DynamoDB DocumentClient.batchWrite or DynamoDB.batchWriteItem function
5656
* @param {string} tableName The table name
5757
* @param {any[]} seeds The seed values
5858
*/
59-
function writeSeeds(dynamodb, tableName, seeds) {
60-
if (!dynamodb) {
61-
throw new Error("dynamodb argument must be provided");
59+
function writeSeeds(dynamodbWriteFunction, tableName, seeds) {
60+
if (!dynamodbWriteFunction) {
61+
throw new Error("dynamodbWriteFunction argument must be provided");
6262
}
6363
if (!tableName) {
6464
throw new Error("table name argument must be provided");
@@ -71,7 +71,7 @@ function writeSeeds(dynamodb, tableName, seeds) {
7171
const seedChunks = _.chunk(seeds, MAX_MIGRATION_CHUNK);
7272
return BbPromise.map(
7373
seedChunks,
74-
(chunk) => writeSeedBatch(dynamodb, tableName, chunk),
74+
(chunk) => writeSeedBatch(dynamodbWriteFunction, tableName, chunk),
7575
{ concurrency: MIGRATION_SEED_CONCURRENCY }
7676
)
7777
.then(() => console.log("Seed running complete for table: " + tableName));

test/indexTest.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ describe("Port function",function(){
1616
assert(typeof service.port, "number");
1717
});
1818

19-
it("Port value should be >= 0 and < 65536",function () {
20-
let service = new Plugin(serverlessMock, {});
21-
http.get(`http://localhost:${service.port}`, function (response) {
19+
it("Port value should be >= 0 and < 65536",function(done){
20+
http.get(`http://localhost:${service.port}/shell/`, function (response) {
2221
assert.equal(response.statusCode, 200);
22+
done();
2323
});
2424
});
2525
});

test/testTable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe("Check Table operations", function() {
4646
if(err){
4747
should.exist(err);
4848
} else {
49-
should.not.exist(data);
49+
expect(data.TableNames).to.be.empty;
5050
}
5151
done();
5252
});

0 commit comments

Comments
 (0)