Skip to content

Commit b5bac20

Browse files
committed
#163: Implement options in Memory driver
1 parent e4c28f5 commit b5bac20

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

drivers/db/Memory.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ export default class Memory extends Interface {
5959
*
6060
* @param {string} collection Name of the target collection
6161
* @param {object} conditions The search query
62+
* @param {object} options Options for the operation
6263
*/
63-
async read(collection, conditions) {
64+
async read(collection, conditions, options) {
6465
/* Fetch the collection, or provide an empty array if none exists */
6566
let records = new Utils().deepClone(this.memory[collection] || []);
6667

@@ -69,6 +70,37 @@ export default class Memory extends Interface {
6970
records = records.filter(record => this.isMatch(record, conditions));
7071
}
7172

73+
/* Limit and skip, if defined */
74+
if (options && 'limit' in options) {
75+
const skip = options.skip || 0;
76+
records = records.slice(skip, options.limit + skip);
77+
}
78+
79+
/* Sort, if defined */
80+
if (options && 'sort' in options) {
81+
records = records.sort((a, b) => {
82+
let i = 0;
83+
let result = 0;
84+
85+
/* Go through all sorted properties */
86+
while (result === 0 && i < options.sort.length) {
87+
const property = options.sort[i][0];
88+
89+
/* If property isn't available in record, forget about it */
90+
if (!(property in a) || !(property in b)) {
91+
continue;
92+
}
93+
94+
/* Sort by property, in the direction requested */
95+
result = (a[property] < b[property]) ? -1 : ((a[property] > b[property]) ? 1 : 0);
96+
result *= options.sort[i][1];
97+
i++;
98+
}
99+
100+
return result;
101+
});
102+
}
103+
72104
return records;
73105
}
74106

lib/Storage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ export default class Storage {
267267
const limit = (this.app.config.limit && this.app.config.limit > 0) ? this.app.config.limit : false;
268268

269269
/* Parse limit options */
270-
if ('limit' in request.query) {
270+
if ('limit' in request.query && Number(request.query.limit) !== 0) {
271271
options.limit = Number(request.query.limit) || null;
272272

273273
/* If max limit is set in config, ensure we don't go over it */

test/drivers/db/Memory.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,35 @@ test.serial('reads nothing in a non-existent collection', async t => {
193193
t.is(results.length, 0);
194194
});
195195

196+
test.serial('limits records according to limit option', async t => {
197+
const results = await t.context.memory.read('fifth', {}, { limit: 2 });
198+
199+
t.true(Array.isArray(results));
200+
t.is(results.length, 2);
201+
});
202+
203+
test.serial('skips records according to skip option', async t => {
204+
const results = await t.context.memory.read('fifth', {}, { limit: 2, skip: 2 });
205+
206+
t.true(Array.isArray(results));
207+
t.is(results.length, 2);
208+
t.is(results[0].name, 'North Yorkshire');
209+
});
210+
211+
test.serial('sorts records ascending according to sort option', async t => {
212+
const results = await t.context.memory.read('fifth', {}, { sort: [ [ 'name', 1 ] ] });
213+
214+
t.true(Array.isArray(results));
215+
t.is(results[0].name, 'Hamptons');
216+
});
217+
218+
test.serial('sorts records descending according to sort option', async t => {
219+
const results = await t.context.memory.read('fifth', {}, { sort: [ [ 'name', -1 ] ] });
220+
221+
t.true(Array.isArray(results));
222+
t.is(results[0].name, 'North Yorkshire');
223+
});
224+
196225
test.serial('modifies one record by ID', async t => {
197226
const results = await t.context.memory.modify('first', { _id: t.context.memory.memory.first[0]._id }, { new: 'hello' });
198227

0 commit comments

Comments
 (0)