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

Commit 78ab44d

Browse files
committed
feat(custom-query): add support for before and between queries
1 parent 0a28bd0 commit 78ab44d

File tree

3 files changed

+154
-1
lines changed

3 files changed

+154
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,17 @@ useful when building an API and accepting various user specificed queries.
5959

6060
* Custom query functions
6161
* `after` (date)
62+
* `before` (date)
63+
* `between` (date|date)
6264

6365
| operation | query string | query object |
6466
|-----------|---------------|--------------|
6567
| after | `?after=2014-01-01` | `{ endret: { $gte: "2014-01-01T00:00:00.000Z" } }` |
6668
| after | `?after=1388534400` | `{ endret: { $gte: "2014-01-01T00:00:00.000Z" } }` |
69+
| before | `?before=2014-01-01` | `{ endret: { $lt: "2014-01-01T00:00:00.000Z" } }` |
70+
| before | `?before=1388534400` | `{ endret: { $lt: "2014-01-01T00:00:00.000Z" } }` |
71+
| between | `?between=2014-01-01|2015-01-01` | `{ endret: { $gte: "2014-01-01T00:00:00.000Z", $lt: "2015-01-01T00:00:00.000Z" } }` |
72+
| between | `?between=1388534400|1420088400` | `{ endret: { $gte: "2014-01-01T00:00:00.000Z", $lt: "2015-01-01T00:00:00.000Z" } }` |
6773

6874
## Install
6975

index.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ module.exports = function MongoQS(options) {
3131
this.custom.after = this.customAfter(this.custom.after);
3232
}
3333

34+
if (this.custom.before) {
35+
this.custom.before = this.customBefore(this.custom.before);
36+
}
37+
38+
if (this.custom.between) {
39+
this.custom.between = this.customBetween(this.custom.between);
40+
}
41+
42+
3443
return this;
3544
};
3645

@@ -91,7 +100,7 @@ module.exports.prototype.customNear = field => (query, point) => {
91100
}
92101
};
93102

94-
module.exports.prototype.customAfter = field => (query, value) => {
103+
function parseDate(value) {
95104
let date = value;
96105

97106
if (!isNaN(date)) {
@@ -103,13 +112,45 @@ module.exports.prototype.customAfter = field => (query, value) => {
103112

104113
date = new Date(date);
105114

115+
return date;
116+
}
117+
118+
module.exports.prototype.customAfter = field => (query, value) => {
119+
const date = parseDate(value);
120+
106121
if (date.toString() !== 'Invalid Date') {
107122
query[field] = {
108123
$gte: date.toISOString(),
109124
};
110125
}
111126
};
112127

128+
module.exports.prototype.customBefore = field => (query, value) => {
129+
const date = parseDate(value);
130+
131+
if (date.toString() !== 'Invalid Date') {
132+
query[field] = {
133+
$lt: date.toISOString(),
134+
};
135+
}
136+
};
137+
138+
module.exports.prototype.customBetween = field => (query, value) => {
139+
const dates = value.split('|');
140+
const afterValue = dates[0];
141+
const beforeValue = dates[1];
142+
143+
const after = parseDate(afterValue);
144+
const before = parseDate(beforeValue);
145+
146+
if (after.toString() !== 'Invalid Date' && before.toString() !== 'Invalid Date') {
147+
query[field] = {
148+
$gte: after.toISOString(),
149+
$lt: before.toISOString(),
150+
};
151+
}
152+
};
153+
113154
module.exports.prototype.parseString = function parseString(string, array) {
114155
let op = string[0] || '';
115156
const eq = string[1] === '=';

test.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,81 @@ describe('customAfter()', () => {
143143
});
144144
});
145145

146+
describe('customBefore()', () => {
147+
it('does not return before query for invalid date', () => {
148+
['foo', '2015-13-40'].forEach((date) => {
149+
mqs.customBefore('endret')(query, date);
150+
assert.deepEqual(query, {});
151+
});
152+
});
153+
154+
it('returns before query for valid ISO date', () => {
155+
mqs.customBefore('endret')(query, '2014-09-22T11:50:37.843Z');
156+
assert.deepEqual(query, {
157+
endret: {
158+
$lt: '2014-09-22T11:50:37.843Z',
159+
},
160+
});
161+
});
162+
163+
it('returns before query for milliseconds timestamp', () => {
164+
mqs.customBefore('endret')(query, '1411386637843');
165+
assert.deepEqual(query, {
166+
endret: {
167+
$lt: '2014-09-22T11:50:37.843Z',
168+
},
169+
});
170+
});
171+
172+
it('returns before query for unix timestamp', () => {
173+
mqs.customBefore('endret')(query, '1411386637');
174+
assert.deepEqual(query, {
175+
endret: {
176+
$lt: '2014-09-22T11:50:37.000Z',
177+
},
178+
});
179+
});
180+
});
181+
182+
describe('customBetween()', () => {
183+
it('does not return between query for invalid date', () => {
184+
['foo|bar', '2015-13-40|2020-42-69'].forEach((date) => {
185+
mqs.customBetween('endret')(query, date);
186+
assert.deepEqual(query, {});
187+
});
188+
});
189+
190+
it('returns between query for valid ISO date', () => {
191+
mqs.customBetween('endret')(query, '2014-09-22T11:50:37.843Z|2015-09-22T11:50:37.843Z');
192+
assert.deepEqual(query, {
193+
endret: {
194+
$gte: '2014-09-22T11:50:37.843Z',
195+
$lt: '2015-09-22T11:50:37.843Z',
196+
},
197+
});
198+
});
199+
200+
it('returns between query for milliseconds timestamp', () => {
201+
mqs.customBetween('endret')(query, '1411386637843|1442922637843');
202+
assert.deepEqual(query, {
203+
endret: {
204+
$gte: '2014-09-22T11:50:37.843Z',
205+
$lt: '2015-09-22T11:50:37.843Z',
206+
},
207+
});
208+
});
209+
210+
it('returns before query for unix timestamp', () => {
211+
mqs.customBetween('endret')(query, '1411386637|1442922637');
212+
assert.deepEqual(query, {
213+
endret: {
214+
$gte: '2014-09-22T11:50:37.000Z',
215+
$lt: '2015-09-22T11:50:37.000Z',
216+
},
217+
});
218+
});
219+
});
220+
146221
describe('parseStringVal()', () => {
147222
describe('true', () => {
148223
[
@@ -926,6 +1001,37 @@ describe('parse()', () => {
9261001
});
9271002
});
9281003

1004+
it('returns custom before query', () => {
1005+
mqs = new MongoQS({
1006+
custom: {
1007+
before: 'endret',
1008+
},
1009+
});
1010+
assert.deepEqual(mqs.parse({
1011+
before: '2014-01-01',
1012+
}), {
1013+
endret: {
1014+
$lt: '2014-01-01T00:00:00.000Z',
1015+
},
1016+
});
1017+
});
1018+
1019+
it('returns custom between query', () => {
1020+
mqs = new MongoQS({
1021+
custom: {
1022+
between: 'endret',
1023+
},
1024+
});
1025+
assert.deepEqual(mqs.parse({
1026+
between: '2014-01-01|2015-01-01',
1027+
}), {
1028+
endret: {
1029+
$gte: '2014-01-01T00:00:00.000Z',
1030+
$lt: '2015-01-01T00:00:00.000Z',
1031+
},
1032+
});
1033+
});
1034+
9291035
it('returns custom function query', () => {
9301036
mqs = new MongoQS({
9311037
custom: {

0 commit comments

Comments
 (0)