UNPKG

respond-framework

Version:
30 lines (27 loc) 1.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _applySelector = require("../utils/applySelector.js"); var _joinMock = require("./join.mock.js"); const createAggregateStagesMock = async (specs = [], { db, collectionName, selector, sort }) => { const collection = db[collectionName]; const m1 = await collection.super.findAll(); // grab all models, as we have to manually join them in this file const m2 = await (0, _joinMock.joinInner)(db, m1, specs, selector); // allow specs with `inner` flag to filter docs in parent collection if no children in joined collection found const m3 = await (0, _joinMock.joinSum)(db, m2, specs); // produce sums as virtual fields on parent collection, optionally filtered by a date range const m4 = m3.filter((0, _applySelector.default)(selector)); // potentially apply selector to virtual sum fields if (sort.location) reverse(sort); return m4; }; var _default = exports.default = createAggregateStagesMock; const reverse = sort => { delete sort.location; const sortKey = Object.keys(sort)[0]; sort[sortKey] = -sort[sortKey]; // reverse to indicate geo sorting was triggered };