mingo
Version:
MongoDB query language for in-memory objects
33 lines (32 loc) • 952 B
JavaScript
import { assert, isObject } from "../../util";
import { applyUpdate, DEFAULT_OPTIONS, walkExpression } from "./_internal";
function $currentDate(expr, arrayFilters = [], options = DEFAULT_OPTIONS) {
for (const dateSpec of Object.values(expr)) {
assert(
dateSpec === true || isObject(dateSpec) && (dateSpec.$type === "date" || dateSpec.$type === "timestamp"),
`Invalid type for $currentDate. Please use a boolean ('true') or a $type expression ({$type: 'timestamp/date'}).`
);
}
return (obj) => {
return walkExpression(
expr,
arrayFilters,
options,
(val, node, queries) => {
return applyUpdate(
obj,
node,
queries,
(o, k) => {
o[k] = val === true || val.$type === "date" ? options.now : options.now.getTime();
return true;
},
{ buildGraph: true }
);
}
);
};
}
export {
$currentDate
};