@js-ak/db-manager
Version:
237 lines (236 loc) • 7.82 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processMappings = void 0;
/**
* A mapping of search operators to their corresponding processing functions.
*
* This `Map` associates an operator key (e.g., `$eq`, `$gt`) with a function that processes the search parameter,
* modifies the `queryArray`, and populates the `values` array with the appropriate values for the SQL query.
*
* @property $custom - Processes a custom operator, pushing the key and value to the `queryArray` and `values` respectively.
* @property $eq - Processes an equality check (`=`), handling both regular and `NULL` values.
* @property $gt - Processes a greater-than comparison (`>`).
* @property $gte - Processes a greater-than-or-equal comparison (`>=`).
* @property $in - Processes an inclusion check (`IN`), pushing the array of values.
* @property $@> - Processes a containment check (`@>`), typically for arrays, ranges, or JSON.
* @property $<@ - Processes an is-contained-by check (`<@`), typically for arrays or ranges.
* @property $&& - Processes an overlap check (`&&`), typically for arrays or ranges.
* @property $@ - Processes a distance operator (`@`), typically for geometric types.
* @property $~ - Processes a regular expression match (`~`).
* @property $? - Processes an existence check (`?`), typically for JSON keys.
* @property $json - Processes a JSON equality check.
* @property $jsonb - Processes a JSONB equality check.
* @property $like - Processes a pattern match (`LIKE`).
* @property $ilike - Processes a case-insensitive pattern match (`ILIKE`).
* @property $lt - Processes a less-than comparison (`<`).
* @property $lte - Processes a less-than-or-equal comparison (`<=`).
* @property $ne - Processes a not-equal comparison (`<>`), handling both regular and `NULL` values.
* @property $nin - Processes a not-inclusion check (`NOT IN`).
* @property $between - Processes a range check (`BETWEEN`).
* @property $nbetween - Processes a not-in-range check (`NOT BETWEEN`).
* @property $nlike - Processes a not-like pattern match (`NOT LIKE`).
* @property $nilike - Processes a not-ilike pattern match (`NOT ILIKE`).
*/
exports.processMappings = new Map([
[
"$custom",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "$custom", sign: v.$custom.sign });
values.push(v.$custom.value);
},
],
[
"$eq",
(key, value, queryArray, values) => {
const v = value;
if (v.$eq === null) {
queryArray.push({ key: `${key} IS NULL`, operator: "$withoutParameters" });
}
else {
queryArray.push({ key, operator: "=" });
values.push(v.$eq);
}
},
],
[
"$gt",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: ">" });
values.push(v.$gt);
},
],
[
"$gte",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: ">=" });
values.push(v.$gte);
},
],
[
"$in",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "$in" });
values.push(v.$in);
},
],
// @> is the "contains" operator.
// arrays: http://www.postgresql.org/docs/current/static/functions-array.html
// range types: http://www.postgresql.org/docs/current/static/functions-range.html
// geometric types: http://www.postgresql.org/docs/current/static/functions-geometry.html
// JSON(and JSONB): http://www.postgresql.org/docs/current/static/functions-json.html
[
"$@>",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "@>" });
values.push(v["$@>"]);
},
],
[
"$<@",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "<@" });
values.push(v["$<@"]);
},
],
[
"$&&",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "&&" });
values.push(v["$&&"]);
},
],
[
"$@",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "@" });
values.push(v["$@"]);
},
],
[
"$~",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "~" });
values.push(v["$~"]);
},
],
[
"$?",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "?" });
values.push(v["$?"]);
},
],
[
"$json",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "=" });
values.push(v.$json);
},
],
[
"$jsonb",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "=" });
values.push(v.$jsonb);
},
],
[
"$like",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "$like" });
values.push(v.$like);
},
],
[
"$ilike",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "$ilike" });
values.push(v.$ilike);
},
],
[
"$lt",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "<" });
values.push(v.$lt);
},
],
[
"$lte",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "<=" });
values.push(v.$lte);
},
],
[
"$ne",
(key, value, queryArray, values) => {
const v = value;
if (v.$ne === null) {
queryArray.push({ key: `${key} IS NOT NULL`, operator: "$withoutParameters" });
}
else {
queryArray.push({ key, operator: "<>" });
values.push(v.$ne);
}
},
],
[
"$nin",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "$nin" });
values.push(v.$nin);
},
],
[
"$between",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "$between" });
values.push(v.$between[0]);
values.push(v.$between[1]);
},
],
[
"$nbetween",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "$nbetween" });
values.push(v.$nbetween[0]);
values.push(v.$nbetween[1]);
},
],
[
"$nlike",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "$nlike" });
values.push(v.$nlike);
}
],
[
"$nilike",
(key, value, queryArray, values) => {
const v = value;
queryArray.push({ key, operator: "$nilike" });
values.push(v.$nilike);
}
],
]);