Skip to content

Commit b95ec9e

Browse files
authored
Merge branch 'v1' into v1
2 parents f8c6373 + 2321871 commit b95ec9e

File tree

8 files changed

+48
-32
lines changed

8 files changed

+48
-32
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
@@ -110,6 +110,8 @@ In `serverless.yml` seeding categories are defined under `dynamodb.seed`.
110110

111111
If `dynamodb.start.seed` is true, then seeding is performed after table migrations.
112112

113+
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:`.
114+
113115
```yml
114116
dynamodb:
115117
start:
@@ -125,7 +127,7 @@ dynamodb:
125127
test:
126128
sources:
127129
- table: users
128-
sources: [./fake-test-users.json]
130+
rawsources: [./fake-test-users.json]
129131
- table: subscriptions
130132
sources: [./fake-test-subscriptions.json]
131133
```

index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class ServerlessDynamodbLocal {
1010
this.serverless = serverless;
1111
this.service = serverless.service;
1212
this.serverlessLog = serverless.cli.log.bind(serverless.cli);
13-
this.config = this.service.custom && this.service.custom.dynamodb || {};
1413
this.options = options;
1514
this.provider = "aws";
1615
this.commands = {
@@ -115,13 +114,13 @@ class ServerlessDynamodbLocal {
115114
}
116115

117116
get port() {
118-
const config = this.config;
117+
const config = this.service.custom && this.service.custom.dynamodb || {};
119118
const port = _.get(config, "start.port", 8000);
120119
return port;
121120
}
122121

123122
get host() {
124-
const config = this.config;
123+
const config = this.service.custom && this.service.custom.dynamodb || {};
125124
const host = _.get(config, "start.host", "localhost");
126125
return host;
127126
}
@@ -160,14 +159,17 @@ class ServerlessDynamodbLocal {
160159

161160
seedHandler() {
162161
const options = this.options;
163-
const documentClient = this.dynamodbOptions(options).doc;
164-
const seedSources = this.seedSources;
165-
return BbPromise.each(seedSources, (source) => {
162+
const dynamodb = this.dynamodbOptions(options);
163+
164+
return BbPromise.each(this.seedSources, (source) => {
166165
if (!source.table) {
167166
throw new Error("seeding source \"table\" property not defined");
168167
}
169-
return seeder.locateSeeds(source.sources || [])
170-
.then((seeds) => seeder.writeSeeds(documentClient, source.table, seeds));
168+
const seedPromise = seeder.locateSeeds(source.sources || [])
169+
.then((seeds) => seeder.writeSeeds(dynamodb.doc.batchWrite.bind(dynamodb.doc), source.table, seeds));
170+
const rawSeedPromise = seeder.locateSeeds(source.rawsources || [])
171+
.then((seeds) => seeder.writeSeeds(dynamodb.raw.batchWriteItem.bind(dynamodb.raw), source.table, seeds));
172+
return BbPromise.all([seedPromise, rawSeedPromise]);
171173
});
172174
}
173175

@@ -181,7 +183,7 @@ class ServerlessDynamodbLocal {
181183
}
182184

183185
startHandler() {
184-
const config = this.config;
186+
const config = this.service.custom && this.service.custom.dynamodb || {};
185187
const options = _.merge({
186188
sharedDb: this.options.sharedDb || true
187189
},

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.28",
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: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ 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

17-
it("Port value should be >= 0 and < 65536",function () {
18-
http.get(`http://localhost:${dataApp.prototype.port}`, function (response) {
19-
assert.equal(response.statusCode, 200);
19+
it("Port value should be >= 0 and < 65536",function(done){
20+
let service = new Plugin(serverlessMock, {});
21+
http.get(`http://localhost:${service.port}/shell/`, function (response) {
22+
assert.equal(response.statusCode, 200);
23+
done();
2024
});
2125
});
2226
});
@@ -33,29 +37,29 @@ describe("Check the dynamodb function",function(){
3337
});
3438

3539
it("Should be an object",function(){
36-
let dynamoOptions = dataApp.prototype.dynamodbOptions;
40+
let dynamoOptions = Plugin.prototype.dynamodbOptions;
3741
let raw = new aws.DynamoDB(dynamoOptions);
3842
raw.should.be.type("object");
3943
});
4044

4145
it("Should be an object",function(){
42-
let dynamoOptions = dataApp.prototype.dynamodbOptions;
46+
let dynamoOptions = Plugin.prototype.dynamodbOptions;
4347
let doc = new aws.DynamoDB(dynamoOptions);
4448
doc.should.be.type("object");
4549
});
4650
});
4751

4852
describe ("Start handler function",function(){
4953
it ("Should not be null",function(){
50-
let handler = dataApp.prototype.startHandler;
54+
let handler = Plugin.prototype.startHandler;
5155
assert(handler =! null);
5256
});
5357
});
5458

5559

5660
describe ("createTable functon",function(){
5761
it ("Should check as a function",function(){
58-
const tbl = dataApp.prototype.createTable;
62+
const tbl = Plugin.prototype.createTable;
5963
assert.equal(typeof tbl, "function");
6064
});
6165
});

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+
};

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)