Skip to content

Commit c8f201c

Browse files
authored
Merge branch 'v1' into fix-dynamodb-ttl-configuration-from-cf
2 parents e2b9418 + a552a17 commit c8f201c

File tree

8 files changed

+406
-46
lines changed

8 files changed

+406
-46
lines changed

.travis.yml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,43 @@ node_js:
44

55
sudo: false
66

7+
before_install:
8+
- cd test
9+
- mkdir sample-template
10+
- cd sample-template
11+
- npm init --yes
12+
713
install:
14+
- npm install --save-dev serverless-offline
15+
- npm install -g [email protected]
16+
- npm install aws-sdk
17+
- npm install --save dynamodb-doc-client-wrapper
18+
- serverless create --template aws-nodejs
19+
- cd ..
20+
- cd ..
21+
- npm link
22+
- cd test/sample-template/node_modules
23+
- npm link serverless-dynamodb-local
24+
- cd ..
25+
- sed -i '18 a plugins:' serverless.yml
26+
- sed -i '19 a - serverless-offline' serverless.yml
27+
- sed -i '20 a - serverless-dynamodb-local' serverless.yml
28+
- cd ..
29+
- cd ..
30+
- npm install --save-dev mocha
31+
- npm install --save-dev should
32+
- npm install --save expect
33+
- npm install --save request
34+
- npm install --save chai
835
- travis_retry npm install
36+
- cd test/sample-template
937

38+
before_script:
39+
- sls dynamodb install
40+
- sls dynamodb start -p 3000 &
41+
- cd ..
42+
- cd ..
43+
- npm run test
44+
1045
script:
11-
- npm test
46+
- npm test

CODE_OF_CONDUCT.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [email protected]. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38+
39+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ custom:
6565
inMemory: true
6666
migrate: true
6767
seed: true
68+
# Uncomment only if you already have a DynamoDB running locally
69+
# noStart: true
6870
```
6971

7072
## Migrations: sls dynamodb migrate

index.js

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class ServerlessDynamodbLocal {
6666
}
6767
}
6868
},
69+
noStart: {
70+
shortcut: "n",
71+
default: false,
72+
usage: "Do not start DynamoDB local (in case it is already running)",
73+
},
6974
remove: {
7075
lifecycleEvents: ["removeHandler"],
7176
usage: "Removes local DynamoDB"
@@ -102,9 +107,15 @@ class ServerlessDynamodbLocal {
102107
return port;
103108
}
104109

110+
get host() {
111+
const config = this.config;
112+
const host = _.get(config, "start.host", "localhost");
113+
return host;
114+
}
115+
105116
dynamodbOptions() {
106117
const dynamoOptions = {
107-
endpoint: `http://localhost:${this.port}`,
118+
endpoint: `http://${this.host}:${this.port}`,
108119
region: "localhost",
109120
accessKeyId: "MOCK_ACCESS_KEY_ID",
110121
secretAccessKey: "MOCK_SECRET_ACCESS_KEY"
@@ -151,30 +162,60 @@ class ServerlessDynamodbLocal {
151162
config && config.start,
152163
this.options
153164
);
154-
155-
dynamodbLocal.start(options);
165+
if (!options.noStart) {
166+
dynamodbLocal.start(options);
167+
}
156168
return BbPromise.resolve()
157169
.then(() => options.migrate && this.migrateHandler())
158170
.then(() => options.seed && this.seedHandler());
159171
}
160172

161173
endHandler() {
162-
this.serverlessLog('DynamoDB - stopping local database');
163-
dynamodbLocal.stop(this.port);
174+
if (!this.options.noStart) {
175+
this.serverlessLog("DynamoDB - stopping local database");
176+
dynamodbLocal.stop(this.port);
177+
}
164178
}
165179

166-
/**
167-
* Gets the table definitions
168-
*/
169-
get tables() {
170-
const resources = this.service.resources.Resources;
180+
getDefaultStack() {
181+
return _.get(this.service, "resources");
182+
}
183+
184+
getAdditionalStacks() {
185+
return _.values(_.get(this.service, "custom.additionalStacks", {}));
186+
}
187+
188+
hasAdditionalStacksPlugin() {
189+
return _.get(this.service, "plugins", []).includes("serverless-plugin-additional-stacks");
190+
}
191+
192+
getTableDefinitionsFromStack(stack) {
193+
const resources = _.get(stack, "Resources", []);
171194
return Object.keys(resources).map((key) => {
172195
if (resources[key].Type === "AWS::DynamoDB::Table") {
173196
return resources[key].Properties;
174197
}
175198
}).filter((n) => n);
176199
}
177200

201+
/**
202+
* Gets the table definitions
203+
*/
204+
get tables() {
205+
let stacks = [];
206+
207+
const defaultStack = this.getDefaultStack();
208+
if (defaultStack) {
209+
stacks.push(defaultStack);
210+
}
211+
212+
if (this.hasAdditionalStacksPlugin()) {
213+
stacks = stacks.concat(this.getAdditionalStacks());
214+
}
215+
216+
return stacks.map((stack) => this.getTableDefinitionsFromStack(stack)).reduce((tables, tablesInStack) => tables.concat(tablesInStack), []);
217+
}
218+
178219
/**
179220
* Gets the seeding sources
180221
*/

package.json

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-dynamodb-local",
3-
"version": "0.2.22",
3+
"version": "0.2.24",
44
"engines": {
55
"node": ">=4.0"
66
},
@@ -12,29 +12,34 @@
1212
"url": "https://github.com/99xt/serverless-dynamodb-local"
1313
},
1414
"keywords": [
15-
"serverless framework plugin",
16-
"serverless applications",
17-
"serverless plugins",
18-
"api gateway",
19-
"lambda",
20-
"dynamodb",
21-
"dynamodb local",
22-
"aws",
23-
"aws lambda",
24-
"aws dynamodb",
25-
"amazon",
26-
"amazon web services",
27-
"serverless.com"
28-
],
15+
"serverless framework plugin",
16+
"serverless applications",
17+
"serverless plugins",
18+
"api gateway",
19+
"lambda",
20+
"dynamodb",
21+
"dynamodb local",
22+
"aws",
23+
"aws lambda",
24+
"aws dynamodb",
25+
"amazon",
26+
"amazon web services",
27+
"serverless.com"
28+
],
2929
"main": "index.js",
3030
"bin": {},
3131
"scripts": {
32-
"test": "echo \"Warning: no test specified\" && exit 0"
32+
"test": "mocha ./test"
3333
},
3434
"dependencies": {
3535
"aws-sdk": "^2.7.0",
3636
"bluebird": "^3.4.6",
3737
"dynamodb-localhost": "^0.0.5",
3838
"lodash": "^4.17.0"
39+
},
40+
"devDependencies": {
41+
"chai": "^4.1.1",
42+
"mocha": "^3.5.0",
43+
"should": "^11.2.1"
3944
}
4045
}

test/indexTest.js

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,68 @@
1-
var should = require("should");
2-
var request = require("request");
3-
var expect = require("chai").expect;
4-
var util = require("util");
5-
6-
var assert = require('chai').assert;
7-
var config = require('../index');
8-
9-
describe('Unit Test', function () {
10-
it('Check endpoint', function () {
11-
before(function () {
12-
config.listen(8989);
13-
});
14-
after(function () {
15-
console.log('after');
16-
});
1+
"use strict";
2+
//Define the modules required to mocha testing
3+
const assert = require("chai").assert;
4+
const http = require ("http");
5+
const expect = require("chai").expect;
6+
const should = require("should");
7+
const aws = require ("aws-sdk");
8+
const seeder = require("../src/seeder.js");
9+
const dataApp = require("../index.js");
10+
11+
describe("Port function",function(){
12+
it("Port should return number",function(){
13+
let myport = dataApp.prototype.port;
14+
assert(typeof myport, "number");
15+
});
16+
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);
1720
});
21+
});
22+
});
1823

19-
it('Check timeout test to 1000ms', () => {
20-
this.timeout(1000);
21-
assert.ok(true);
24+
describe("Check the dynamodb function",function(){
25+
it("Endpoint should listen to the port",function () {
26+
let server;
27+
before(function () {
28+
server = dynamodbOptions.listen(port);
29+
});
30+
after(function () {
31+
assert.ok;
32+
});
2233
});
34+
35+
it("Should be an object",function(){
36+
let dynamoOptions = dataApp.prototype.dynamodbOptions;
37+
let raw = new aws.DynamoDB(dynamoOptions);
38+
raw.should.be.type("object");
39+
});
40+
41+
it("Should be an object",function(){
42+
let dynamoOptions = dataApp.prototype.dynamodbOptions;
43+
let doc = new aws.DynamoDB(dynamoOptions);
44+
doc.should.be.type("object");
45+
});
2346
});
2447

48+
describe ("Start handler function",function(){
49+
it ("Should not be null",function(){
50+
let handler = dataApp.prototype.startHandler;
51+
assert(handler =! null);
52+
});
53+
});
54+
55+
56+
describe ("createTable functon",function(){
57+
it ("Should check as a function",function(){
58+
const tbl = dataApp.prototype.createTable;
59+
assert.equal(typeof tbl, "function");
60+
});
61+
});
62+
63+
describe ("Check the Seeder file",function(){
64+
it("Table name shoud be a string",function(){
65+
let tblName = seeder.writeSeeds.name;
66+
expect(tblName).to.be.a("string");
67+
});
68+
});

0 commit comments

Comments
 (0)