Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit e059e80

Browse files
author
Hans Kristian Flaatten
committed
feat(package): rewritten with ES6 syntax
BREAKING CHANGE: this breaks compability with older versions of Node.JS due to ES6 features. This version now requires Node.JS v4.0.0 or greater to work.
1 parent e625c0a commit e059e80

File tree

9 files changed

+591
-625
lines changed

9 files changed

+591
-625
lines changed

.eslintrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
"no-param-reassign": [2, { "props": false }]
18+
}
19+
}

.jshintrc

Lines changed: 0 additions & 63 deletions
This file was deleted.

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# MongoDB QueryString
1+
# MongoDB QueryString Parser
22

3-
[![Build status](https://img.shields.io/wercker/ci/566dca6762f42c407207777a.svg "Build status")](https://app.wercker.com/project/bykey/a31eed21b34f26d9b7af766b4614c260)
3+
[![Build status](https://app.wercker.com/status/b33b844c0ab10d56c318e116e587e2fa/s "wercker status")](https://app.wercker.com/project/bykey/b33b844c0ab10d56c318e116e587e2fa)
4+
[![Codacy grade](https://img.shields.io/codacy/grade/782d0dc482ea4033892f99f968090b35.svg "Codacy grade")](https://www.codacy.com/app/DNT/node-mongo-querystring)
5+
[![Codacy coverage](https://img.shields.io/codacy/coverage/782d0dc482ea4033892f99f968090b35.svg "Codacy coverage")](https://www.codacy.com/app/DNT/node-mongo-querystring)
46
[![NPM downloads](https://img.shields.io/npm/dm/mongo-querystring.svg "NPM downloads")](https://www.npmjs.com/package/mongo-querystring)
57
[![NPM version](https://img.shields.io/npm/v/mongo-querystring.svg "NPM version")](https://www.npmjs.com/package/mongo-querystring)
68
[![Node version](https://img.shields.io/node/v/mongo-querystring.svg "Node version")](https://www.npmjs.com/package/mongo-querystring)
7-
[![Dependency status](https://img.shields.io/david/turistforeningen/node-mongo-querystring.svg "Dependency status")](https://david-dm.org/turistforeningen/node-mongo-querystring)
9+
[![Dependency status](https://img.shields.io/david/Turistforeningen/node-mongo-querystring.svg "Dependency status")](https://david-dm.org/Turistforeningen/node-mongo-querystring)
810

911
Accept MongoDB query parameters through URI queries safe and easy. This is
1012
useful when building an API and accepting various user specificed queries.

examples/app.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
'use strict';
22

3-
var express = require('express');
4-
var app = module.exports = express();
3+
const express = require('express');
4+
const app = module.exports = express();
55

6-
var MongoQS = require('../index');
6+
const JSONStream = require('JSONStream');
7+
const MongoQS = require('../index');
8+
const mongo = require('./db');
79

810
// Create a new Mongo QueryString parser
9-
var qs = new MongoQS({
11+
const qs = new MongoQS({
1012
custom: {
1113
bbox: 'geojson',
12-
near: 'geojson'
13-
}
14+
near: 'geojson',
15+
},
1416
});
1517

16-
app.get('/api/places', function(req, res) {
18+
app.get('/api/places', (req, res) => {
1719
res.set('Content-Type', 'application/json');
1820

1921
// Parse the request query parameters
20-
var query = qs.parse(req.query);
21-
var collection = require('./db').db.collection('places');
22-
var cursor = collection.find(query).limit(3);
22+
const query = qs.parse(req.query);
23+
const collection = mongo.db.collection('places');
24+
const cursor = collection.find(query).limit(3);
2325

24-
cursor.stream().pipe(require('JSONStream').stringify()).pipe(res);
26+
cursor.stream().pipe(JSONStream.stringify()).pipe(res);
2527
});
2628

2729
if (!module.parent) {
28-
require('./db').once('ready', function() {
30+
mongo.once('ready', () => {
2931
app.listen(3000);
30-
console.log('Express started on port 3000');
32+
console.log('Express started on port 3000'); // eslint-disable-line no-console
3133
});
3234
}

examples/db.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
'use strict';
22

3-
var EventEmitter = require('events').EventEmitter;
4-
var MongoClient = require('mongodb').MongoClient;
5-
var inherits = require('util').inherits;
3+
const EventEmitter = require('events').EventEmitter;
4+
const MongoClient = require('mongodb').MongoClient;
5+
const inherits = require('util').inherits;
66

7-
var Mongo = function(uri) {
7+
function Mongo(uri) {
88
EventEmitter.call(this);
99

1010
this.db = null;
1111

12-
var $this = this;
13-
14-
new MongoClient.connect(uri, function(err, db) {
12+
MongoClient.connect(uri, (err, db) => {
1513
if (err) { throw err; }
1614

17-
$this.db = db;
18-
$this.emit('ready');
15+
this.db = db;
16+
this.emit('ready');
1917
});
2018

2119
return this;
22-
};
20+
}
2321

2422
inherits(Mongo, EventEmitter);
2523

examples/test.js

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
'use strict';
22

3-
var assert = require('assert');
4-
var request = require('supertest');
3+
const assert = require('assert');
4+
const request = require('supertest');
55

6-
var data = require('./data');
7-
var app = request(require('./app'));
8-
var db = require('./db');
6+
const data = require('./data');
7+
const app = request(require('./app'));
8+
const db = require('./db');
99

10-
describe('Example App', function() {
11-
before(function(done) {
10+
describe('Example App', () => {
11+
before(done => {
1212
if (db.db) { return done(); }
13-
db.once('ready', done);
13+
return db.once('ready', done);
1414
});
1515

16-
before(function(done) {
16+
before(done => {
1717
db.db.dropDatabase(done);
1818
});
1919

20-
before(function(done) {
21-
db.db.collection('places').createIndex({geojson: '2dsphere'}, done);
20+
before(done => {
21+
db.db.collection('places').createIndex({ geojson: '2dsphere' }, done);
2222
});
2323

24-
before(function(done) {
24+
before(done => {
2525
db.db.collection('places').insertMany(data, done);
2626
});
2727

28-
var url = '/api/places';
28+
const url = '/api/places';
2929

30-
it('returns all them places', function(done) {
30+
it('returns all them places', done => {
3131
app.get(url)
3232
.expect(200)
33-
.expect(function(res) {
33+
.expect(res => {
3434
assert.equal(res.body.length, 3);
3535
})
3636
.end(done);
3737
});
3838

39-
it('returns places matching name', function(done) {
40-
app.get(url + '?name=Vatnane')
39+
it('returns places matching name', done => {
40+
app.get(`${url}?name=Vatnane`)
4141
.expect(200)
42-
.expect(function(res) {
42+
.expect(res => {
4343
assert.equal(res.body.length, 1);
4444
assert.equal(res.body[0].name, 'Vatnane');
4545
})
4646
.end(done);
4747
});
4848

49-
it('returns places near point', function(done) {
50-
app.get(url + '?near=6.13037,61.00607')
49+
it('returns places near point', done => {
50+
app.get(`${url}?near=6.13037,61.00607`)
5151
.expect(200)
52-
.expect(function(res) {
52+
.expect(res => {
5353
assert.equal(res.body.length, 3);
5454
assert.equal(res.body[0].name, 'Solrenningen');
5555
assert.equal(res.body[1].name, 'Åsedalen');
@@ -58,49 +58,49 @@ describe('Example App', function() {
5858
.end(done);
5959
});
6060

61-
it('returns places near point with max distance', function(done) {
62-
app.get(url + '?near=6.13037,61.00607,7000')
61+
it('returns places near point with max distance', done => {
62+
app.get(`${url}?near=6.13037,61.00607,7000`)
6363
.expect(200)
64-
.expect(function(res) {
64+
.expect(res => {
6565
assert.equal(res.body.length, 2);
6666
assert.equal(res.body[0].name, 'Solrenningen');
6767
assert.equal(res.body[1].name, 'Åsedalen');
6868
})
6969
.end(done);
7070
});
7171

72-
it('returns places near point with max and min distance', function(done) {
73-
app.get(url + '?near=6.13037,61.00607,7000,1000')
72+
it('returns places near point with max and min distance', done => {
73+
app.get(`${url}?near=6.13037,61.00607,7000,1000`)
7474
.expect(200)
75-
.expect(function(res) {
75+
.expect(res => {
7676
assert.equal(res.body.length, 1);
7777
assert.equal(res.body[0].name, 'Åsedalen');
7878
})
7979
.end(done);
8080
});
8181

82-
it('returns places inside bbox', function(done) {
83-
var bbox = [
82+
it('returns places inside bbox', done => {
83+
const bbox = [
8484
'5.5419158935546875',
8585
'60.92859723298985',
8686
'6.0363006591796875',
87-
'61.018719220334525'
87+
'61.018719220334525',
8888
].join(',');
8989

90-
app.get(url + '?bbox=' + bbox)
90+
app.get(`${url}?bbox=${bbox}`)
9191
.expect(200)
92-
.expect(function(res) {
92+
.expect(res => {
9393
assert.equal(res.body.length, 2);
9494
assert.equal(res.body[0].name, 'Norddalshytten');
9595
assert.equal(res.body[1].name, 'Vardadalsbu');
9696
})
9797
.end(done);
9898
});
9999

100-
it('returns places with any of the following tags', function(done) {
101-
app.get(url + '?tags[]=Båt&tags[]=Stekeovn')
100+
it('returns places with any of the following tags', done => {
101+
app.get(`${url}?tags[]=Båt&tags[]=Stekeovn`)
102102
.expect(200)
103-
.expect(function(res) {
103+
.expect(res => {
104104
assert.equal(res.body.length, 3);
105105
assert.equal(res.body[0].name, 'Solrenningen');
106106
assert.equal(res.body[1].name, 'Åsedalen');
@@ -109,50 +109,50 @@ describe('Example App', function() {
109109
.end(done);
110110
});
111111

112-
it('returns places with visits less than 40', function(done) {
113-
app.get(url + '?visits=<40')
112+
it('returns places with visits less than 40', done => {
113+
app.get(`${url}?visits=<40`)
114114
.expect(200)
115-
.expect(function(res) {
115+
.expect(res => {
116116
assert.equal(res.body.length, 0);
117117
})
118118
.end(done);
119119
});
120120

121-
it('returns places with visits less than or equal to 40', function(done) {
122-
app.get(url + '?visits=<=40')
121+
it('returns places with visits less than or equal to 40', done => {
122+
app.get(`${url}?visits=<=40`)
123123
.expect(200)
124-
.expect(function(res) {
124+
.expect(res => {
125125
assert.equal(res.body.length, 1);
126126
assert.equal(res.body[0].name, 'Solrenningen');
127127
})
128128
.end(done);
129129
});
130130

131-
it('returns places with visits greater than 10,000', function(done) {
132-
app.get(url + '?visits=>10000')
131+
it('returns places with visits greater than 10,000', done => {
132+
app.get(`${url}?visits=>10000`)
133133
.expect(200)
134-
.expect(function(res) {
134+
.expect(res => {
135135
assert.equal(res.body.length, 1);
136136
assert.equal(res.body[0].name, 'Vardadalsbu');
137137
})
138138
.end(done);
139139
});
140140

141-
it('returns places with visits > or equal to 10,000', function(done) {
142-
app.get(url + '?visits=>=10000')
141+
it('returns places with visits > or equal to 10,000', done => {
142+
app.get(`${url}?visits=>=10000`)
143143
.expect(200)
144-
.expect(function(res) {
144+
.expect(res => {
145145
assert.equal(res.body.length, 2);
146146
assert.equal(res.body[0].name, 'Åsedalen');
147147
assert.equal(res.body[1].name, 'Vardadalsbu');
148148
})
149149
.end(done);
150150
});
151151

152-
it('returns places with visits > 40 and < 10,000', function(done) {
153-
app.get(url + '?visits=>40&visits=<10000')
152+
it('returns places with visits > 40 and < 10,000', done => {
153+
app.get(`${url}?visits=>40&visits=<10000`)
154154
.expect(200)
155-
.expect(function(res) {
155+
.expect(res => {
156156
assert.equal(res.body.length, 3);
157157
assert.equal(res.body[0].name, 'Norddalshytten');
158158
assert.equal(res.body[1].name, 'Vatnane');

0 commit comments

Comments
 (0)