@builder6/query-mongodb 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +37 -0
- package/dist/index.js +272 -0
- package/dist/index.test.js +1941 -0
- package/dist/options.js +420 -0
- package/dist/options.test.js +960 -0
- package/dist/pipelines.js +751 -0
- package/dist/pipelines.test.js +782 -0
- package/dist/utils.js +45 -0
- package/dist/utils.test.js +36 -0
- package/index.js +1 -0
- package/options.js +1 -0
- package/package.json +60 -0
- package/src/index.js +497 -0
- package/src/index.test.js +2333 -0
- package/src/options.js +573 -0
- package/src/options.test.js +1112 -0
- package/src/pipelines.js +760 -0
- package/src/pipelines.test.js +1300 -0
- package/src/utils.js +33 -0
- package/src/utils.test.js +40 -0
package/src/utils.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Mongo doesn't seem to have the ability of simply returning its ids as strings
|
|
2
|
+
// to begin with. Bit of a pita, but hey...
|
|
3
|
+
// We'll replace ids with strings if required.
|
|
4
|
+
const replaceId = item =>
|
|
5
|
+
item._id ? { ...item, _id: item._id.toHexString() } : item;
|
|
6
|
+
|
|
7
|
+
// We can apply a limit for summaries calculated per group query. The realistic problem
|
|
8
|
+
// is that if a programmer makes the grid use server-side grouping as well as summaries,
|
|
9
|
+
// but *not* groupPaging, there may be enormous numbers of summary queries to run, and because
|
|
10
|
+
// this happens across levels, it can't easily be checked elsewhere and the server will just
|
|
11
|
+
// keep working on that query as long as it takes.
|
|
12
|
+
const createSummaryQueryExecutor = limit => {
|
|
13
|
+
let queriesExecuted = 0;
|
|
14
|
+
|
|
15
|
+
return fn =>
|
|
16
|
+
!limit || ++queriesExecuted <= limit ? fn() : Promise.resolve();
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const merge = os => Object.assign({}, ...os);
|
|
20
|
+
|
|
21
|
+
const debug = (id, f, options = {}) => {
|
|
22
|
+
const output = options.output || console.log;
|
|
23
|
+
const processResult = options.processResult || (result => result);
|
|
24
|
+
const processArgs = options.processArgs || (args => args);
|
|
25
|
+
return (...args) => {
|
|
26
|
+
output(`DEBUG(${id}): `, processArgs(args));
|
|
27
|
+
const result = f(...args);
|
|
28
|
+
output(`DEBUG(${id}/result): `, processResult(result));
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
module.exports = { replaceId, createSummaryQueryExecutor, merge, debug };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/* global suite, test */
|
|
2
|
+
|
|
3
|
+
const chai = require('chai');
|
|
4
|
+
const assert = chai.assert;
|
|
5
|
+
const sinon = require('sinon');
|
|
6
|
+
|
|
7
|
+
const { replaceId, createSummaryQueryExecutor } = require('./utils');
|
|
8
|
+
|
|
9
|
+
suite('utils', function() {
|
|
10
|
+
suite('replaceId', function() {
|
|
11
|
+
test('works', function() {
|
|
12
|
+
assert.deepEqual(replaceId({ _id: { toHexString: () => '42' } }), {
|
|
13
|
+
_id: '42'
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
suite('createSummaryQueryExecutor', function() {
|
|
19
|
+
test('works', function(done) {
|
|
20
|
+
const exec = createSummaryQueryExecutor(3);
|
|
21
|
+
const func = sinon.stub().resolves();
|
|
22
|
+
const promises = [
|
|
23
|
+
exec(func),
|
|
24
|
+
exec(func),
|
|
25
|
+
exec(func),
|
|
26
|
+
exec(func),
|
|
27
|
+
exec(func)
|
|
28
|
+
];
|
|
29
|
+
/* eslint-disable */
|
|
30
|
+
Promise.all(promises).then(rs => {
|
|
31
|
+
// five execs should render five results
|
|
32
|
+
assert.equal(rs.length, 5);
|
|
33
|
+
// but only three calls, because that's the limit
|
|
34
|
+
assert.equal(func.callCount, 3);
|
|
35
|
+
done();
|
|
36
|
+
});
|
|
37
|
+
/* eslint-enable */
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|