You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
910 B
JavaScript

4 years ago
"use strict";
const { validateArguments } = require("@validatem/core");
const node = require("../ast-node");
module.exports = function count(operations) {
4 years ago
const isCollection = require("../validators/operations/is-collection")(operations);
4 years ago
4 years ago
return function count(_collection) {
let [ collection ] = validateArguments(arguments, {
collection: [ isCollection ]
4 years ago
});
4 years ago
// TODO: Investigate whether this can be made more performant by counting a specific field rather than *. That would probably require stateful knowledge of the database schema, though.
let fieldReference = (collection != null)
? operations.remoteField({ collection: collection, field: "*" }) // FIXME: Make sure not to break this (internally) when making field name checks more strict
: operations.field("*");
4 years ago
return node({
type: "aggregrateFunction",
functionName: "count",
4 years ago
args: [ fieldReference ]
4 years ago
});
};
};