respond-framework
Version:
create as fast you think
65 lines (64 loc) • 1.75 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.createStagesCount = void 0;
var _toFromObjectIds = require("../helpers/toFromObjectIds.js");
var _join = require("./join.js");
const createAggregateStages = (specs = [], {
collection,
selector: $match,
project: $project,
sort: $sort,
limit: $limit,
skip: $skip
}) => {
$match = (0, _toFromObjectIds.toObjectIdsSelector)($match);
const stages = [];
if ($match) stages.push({
$match
});
specs.forEach(spec => {
const joinedCollection = collection.db[spec.from];
const arr = (0, _join.default)(spec, $match, joinedCollection);
if ($match) delete $match[spec.name]; // important: delete so only join collection is affected, as selector is applied within joinSum (if in fact passed from client)
stages.push(...arr);
});
if (!$sort.location?.lat || collection.config.useLocalDb) {
delete $sort.location;
stages.push({
$sort
});
} else {
stages.push({
$geoNear: {
near: {
type: 'Point',
coordinates: [$sort.location.lng, $sort.location.lat]
},
spherical: true,
distanceField: 'distance',
key: 'location'
}
});
}
stages.push({
$skip: $skip * $limit
});
stages.push({
$limit
});
if ($project) stages.push({
$project: (0, _toFromObjectIds.toProject)($project)
});
return stages;
};
var _default = exports.default = createAggregateStages;
const createStagesCount = stages => {
stages = stages.filter(s => !s.$sort && !s.$skip && !s.$limit && !s.$project && !s.$geoNear);
stages.push({
$count: 'count'
});
return stages;
};
exports.createStagesCount = createStagesCount;