prisma-criteria
Version:
Parses, validates, and creates a criteria object that can be passed to the Prisma "findMany" method to query a list of resources matching the given filters, pagination and order.
45 lines • 2.02 kB
JavaScript
import { FILTER_SEPARATOR, LIST_ELEMENTS_SEPARATOR, LIST_END, LIST_START, LOGIC_OPERATORS_WITH_SPACES_REGEX, RELATION_SCALAR_SEPARATOR } from './dsl.consts.js';
const coincidentFilterToFilterPrimitives = (filterCoincidence, index, filterIterators) => {
const { index: startOfFilterSentence, input } = filterCoincidence;
// Where the following sentence begins
const endOfFilterSentence = (filterIterators[index + 1]?.index) ??
(input.length);
const [logicOperator, field, operator, ...value] = input
.slice(startOfFilterSentence, endOfFilterSentence)
.trim()
.split(FILTER_SEPARATOR);
let scalarOperator = operator;
let relationalFilter;
if (operator?.includes(RELATION_SCALAR_SEPARATOR))
[] = operator.split(RELATION_SCALAR_SEPARATOR);
let singleValueOrListValue = value
.join(FILTER_SEPARATOR);
const isValueAList = singleValueOrListValue[0] === LIST_START &&
singleValueOrListValue[singleValueOrListValue.length - 1] === LIST_END;
if (isValueAList) {
singleValueOrListValue = singleValueOrListValue
.slice(LIST_START.length, -LIST_START.length)
.split(LIST_ELEMENTS_SEPARATOR);
}
return {
logicOperator,
field,
relationalFilter,
operator: scalarOperator,
value: singleValueOrListValue
};
};
export function filtersDSLParser(filtersSentence) {
if (filtersSentence === undefined)
return [];
// TODO --- The regex should match and group all the parts of the DSL
// to ensure the filter is valid. Then we should remove the checks for
// each part of the DSL since it matched therefore is a valid part.
const filterSentenceCoincidences = [
...filtersSentence.matchAll(LOGIC_OPERATORS_WITH_SPACES_REGEX)
];
const filterPrimitives = filterSentenceCoincidences
.map(coincidentFilterToFilterPrimitives);
return filterPrimitives;
}
//# sourceMappingURL=index.js.map