Skip to content

Commit 4fdffb3

Browse files
author
Hans Kristian Flaatten
committed
feat(api): mongoDB database wrapper
0 parents  commit 4fdffb3

File tree

8 files changed

+318
-0
lines changed

8 files changed

+318
-0
lines changed

.eslintrc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# vi:syntax=json
2+
{
3+
"extends": "airbnb-base",
4+
"env": {
5+
"mocha": true,
6+
"node": true
7+
},
8+
"parserOptions": {
9+
"ecmaVersion": 6,
10+
"sourceType": "script",
11+
"ecmaFeatures": {
12+
"modules": false
13+
}
14+
},
15+
"rules": {
16+
"strict": [2, "global"]
17+
}
18+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Den Norske Turistforening (DNT), Hans Kristian Flaatten
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# @turbasen/db-mongo
2+
3+
[![Build status](https://app.wercker.com/status/8af22dd80c3f309b1e116e251dd66cd0/s "wercker status")](https://app.wercker.com/project/bykey/8af22dd80c3f309b1e116e251dd66cd0)
4+
[![Codacy grade](https://img.shields.io/codacy/grade/eeb155a45852449da23b2e10b09c6ff9.svg "Codacy grade")](https://www.codacy.com/app/Turbasen/db-mongo)
5+
[![Codacy coverage](https://img.shields.io/codacy/coverage/eeb155a45852449da23b2e10b09c6ff9.svg "Codacy coverage")](https://www.codacy.com/app/Turbasen/db-mongo)
6+
[![NPM downloads](https://img.shields.io/npm/dm/@turbasen/db-mongo.svg "NPM downloads")](https://www.npmjs.com/package/@turbasen/db-mongo)
7+
[![NPM version](https://img.shields.io/npm/v/@turbasen/db-mongo.svg "NPM version")](https://www.npmjs.com/package/@turbasen/db-mongo)
8+
[![Node version](https://img.shields.io/node/v/@turbasen/db-mongo.svg "Node version")](https://www.npmjs.com/package/@turbasen/db-mongo)
9+
[![Dependency status](https://img.shields.io/david/Turbasen/db-mongo.svg "Dependency status")](https://david-dm.org/Turbasen/db-mongo)
10+
11+
Internal mongoDB wrapper for Nasjonal Turbase API.
12+
13+
## Getting started
14+
15+
Download [Docker for Mac or Windows](https://www.docker.com/products/docker).
16+
17+
Run in this directory:
18+
19+
```
20+
$ docker-compose up
21+
```
22+
23+
Docker is now watching for changes and will run the test suite automatically.
24+
25+
## Usage
26+
27+
Connects automatically using `MONGO_URI`, or `MONGO_PORT_27017_TCP_ADDR` +
28+
`MONGO_PORT_27017_TCP_PORT` environment variables, or default to `mongo` +
29+
`27017`.
30+
31+
```js
32+
const mongo = require('mongo');
33+
34+
mongo.on('ready', () => {
35+
console.log('mongoDB is ready!');
36+
});
37+
```
38+
39+
## [MIT lisenced](https://github.com/Turbasen/Auth/blob/master/LICENSE)

docker-compose.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '2'
2+
3+
services:
4+
mongo:
5+
image: mongo:3.0
6+
7+
node:
8+
image: node:argon
9+
working_dir: /usr/src/app
10+
volumes:
11+
- .:/usr/src/app
12+
links:
13+
- mongo
14+
depends_on:
15+
- mongo
16+
environment:
17+
- NODE_ENV=development
18+
- NPM_CONFIG_LOGLEVEL=silent
19+
- NPM_CONFIG_PROGRESS=false
20+
- NPM_CONFIG_SPIN=false
21+
command: npm run test:watch

index.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
const EventEmitter = require('events').EventEmitter;
4+
const MongoClient = require('mongodb').MongoClient;
5+
const inherits = require('util').inherits;
6+
7+
const MongoWrapper = function MongoWrapper(uri) {
8+
EventEmitter.call(this);
9+
10+
MongoClient.connect(uri, (err, database) => {
11+
if (err) { throw err; }
12+
13+
this.db = database;
14+
15+
this.api = {
16+
users: database.collection('api.users'),
17+
};
18+
19+
['turer', 'steder', 'områder', 'bilder', 'grupper'].forEach((type) => {
20+
this[type] = database.collection(type);
21+
});
22+
23+
this.emit('ready');
24+
});
25+
26+
return this;
27+
};
28+
29+
inherits(MongoWrapper, EventEmitter);
30+
31+
if (process.env.MONGO_URI) {
32+
module.exports = new MongoWrapper(process.env.MONGO_URI);
33+
} else {
34+
const addr = process.env.MONGO_PORT_27017_TCP_ADDR || 'mongo';
35+
const port = process.env.MONGO_PORT_27017_TCP_PORT || '27017';
36+
const db = 'test';
37+
38+
module.exports = new MongoWrapper(`mongodb://${addr}:${port}/${db}`);
39+
}
40+
41+
module.exports.MongoWrapper = MongoWrapper;

package.json

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "@turbasen/db-mongo",
3+
"version": null,
4+
"description": "Internal mongoDB wrapper for Nasjonal Turbase API",
5+
"main": "index.js",
6+
"files": [
7+
"index.js"
8+
],
9+
"scripts": {
10+
"codacy-coverage": "codacy-coverage",
11+
"cover": "istanbul cover --report lcovonly ./node_modules/.bin/_mocha -- -R spec test.js",
12+
"grunt:watch": "grunt watch",
13+
"lint": "eslint index.js test.js",
14+
"nsp": "nsp check",
15+
"semantic-release": "semantic-release",
16+
"test": "mocha -R tap -b --check-leaks test.js",
17+
"test:watch": "mocha -R progress -b --check-leaks -w test.js",
18+
"greenkeeper-postpublish": "greenkeeper-postpublish"
19+
},
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/Turbasen/db-mongo.git"
23+
},
24+
"keywords": [
25+
"nasjonal turbase",
26+
"turbasen",
27+
"db",
28+
"mongo",
29+
"mongodb"
30+
],
31+
"author": "Hans Kristian Flaatten <[email protected]>",
32+
"license": "MIT",
33+
"bugs": {
34+
"url": "https://github.com/Turbasen/db-mongo/issues"
35+
},
36+
"homepage": "https://github.com/Turbasen/db-mongo#readme",
37+
"dependencies": {
38+
"mongodb": "^2.1.18"
39+
},
40+
"devDependencies": {
41+
"codacy-coverage": "^1.1.3",
42+
"eslint": "^2.13.1",
43+
"eslint-config-airbnb-base": "^3.0.1",
44+
"eslint-plugin-import": "^1.10.0",
45+
"greenkeeper-postpublish": "^1.0.0",
46+
"istanbul": "^0.4.4",
47+
"mocha": "^2.5.3",
48+
"nsp": "^2.5.0",
49+
"semantic-release": "^4.3.5"
50+
},
51+
"engines": {
52+
"node": ">=4.0.0"
53+
}
54+
}

test.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
const mongo = require('.');
4+
const assert = require('assert');
5+
const mongodb = require('mongodb');
6+
7+
before(done => mongo.on('ready', done));
8+
9+
describe('mongo', () => {
10+
it('returns Mongo wrapper class', () => {
11+
assert(mongo instanceof mongo.MongoWrapper);
12+
assert(mongo.db instanceof mongodb.Db);
13+
14+
assert(mongo.api.users instanceof mongodb.Collection);
15+
16+
['turer', 'steder', 'områder', 'bilder', 'grupper'].forEach((type) => {
17+
assert(mongo[type] instanceof mongodb.Collection);
18+
});
19+
});
20+
21+
it('is connected to the database', (done) => {
22+
mongo.db.stats((err, stats) => {
23+
assert.ifError(err);
24+
assert.equal(stats.ok, 1);
25+
done();
26+
});
27+
});
28+
29+
it('connects via MONGO_URI environment variable', done => {
30+
process.env.MONGO_URI = 'mongodb://mongo:27017/test';
31+
32+
delete require.cache[require.resolve('.')];
33+
require('.').on('ready', function readyCb() { // eslint-disable-line global-require
34+
// assert(this instanceof mongo.MongoWrapper);
35+
assert(this.db instanceof mongodb.Db);
36+
done();
37+
});
38+
39+
delete process.env.MONGO_URI;
40+
});
41+
42+
it('connects via MONGO_PORT_27017_TCP_* environment variables', done => {
43+
process.env.MONGO_PORT_27017_TCP_ADDR = 'mongo';
44+
process.env.MONGO_PORT_27017_TCP_PORT = '27017';
45+
46+
delete require.cache[require.resolve('.')];
47+
require('.').on('ready', function readyCb() { // eslint-disable-line global-require
48+
assert(this.db instanceof mongodb.Db);
49+
done();
50+
});
51+
52+
delete process.env.MONGO_PORT_27017_TCP_ADDR;
53+
delete process.env.MONGO_PORT_27017_TCP_PORT;
54+
});
55+
});

wercker.yml

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
box: node:argon
2+
3+
build:
4+
services:
5+
- mongo:3.0
6+
- redis:3
7+
8+
steps:
9+
- script:
10+
name: wait for MongoDB to start
11+
code: |
12+
apt-get update --no-install-recommends -y
13+
apt-get install --no-install-recommends -y netcat
14+
while ! nc -q 1 $MONGO_PORT_27017_TCP_ADDR $MONGO_PORT_27017_TCP_PORT </dev/null; do sleep 3; done
15+
16+
- script:
17+
name: echo nodejs information
18+
code: |
19+
echo "node version $(node -v) running"
20+
echo "npm version $(npm -v) running"
21+
22+
- npm-install
23+
24+
- script:
25+
name: lint
26+
code: npm run lint
27+
28+
- npm-test
29+
30+
- script:
31+
name: test coverage
32+
code: |
33+
npm run cover
34+
cat ./coverage/lcov.info | npm run codacy-coverage
35+
36+
- script:
37+
name: node security project
38+
code: |
39+
npm run nsp
40+
41+
after-steps:
42+
- turistforeningen/slack-notifier:
43+
url: $SLACK_WEBHOOK_URL
44+
45+
npm:
46+
steps:
47+
# Rebuild node_modules to fix broken symlinks
48+
# https://github.com/wercker/docs/issues/310
49+
- script:
50+
name: npm rebuild
51+
code: npm rebuild
52+
53+
- script:
54+
name: semantic release pre
55+
code: npm run semantic-release -- pre
56+
57+
- turistforeningen/npm-publish
58+
59+
- script:
60+
name: semantic release post
61+
code: npm run semantic-release -- post
62+
63+
- script:
64+
name: greenkeeper postpublish
65+
code: npm run greenkeeper-postpublish
66+
67+
after-steps:
68+
- turistforeningen/slack-notifier:
69+
url: $SLACK_WEBHOOK_URL

0 commit comments

Comments
 (0)