UNPKG

@palmares/databases

Version:

Add support for working with databases with palmares framework

124 lines (122 loc) 5.08 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/engine/query/search.ts var search_exports = {}; __export(search_exports, { AdapterSearchQuery: () => AdapterSearchQuery, adapterSearchQuery: () => adapterSearchQuery }); module.exports = __toCommonJS(search_exports); function adapterSearchQuery(args) { let CustomAdapterQuerySearch = class CustomAdapterQuerySearch extends AdapterSearchQuery { static { __name(this, "CustomAdapterQuerySearch"); } parseSearchFieldValue = args.parseSearchFieldValue; }; return CustomAdapterQuerySearch; } __name(adapterSearchQuery, "adapterSearchQuery"); var AdapterSearchQuery = class { static { __name(this, "AdapterSearchQuery"); } /** * This will pretty much receive the search value and parse it and translate that into an object for each field. * The nicest thing is that we send you the `result`. This way you can organize the `search` object the way that * you want and makes sense for your engine. * * @example * ```ts * async parseSearchFieldValue<OperationType extends OperatorsOfQuery>( * operationType: OperationType, * value?: (OperationType extends 'or' | 'and' | 'in' | 'between' ? unknown[] : unknown) | undefined, * result?: any, * options?: { isNot?: boolean | undefined; ignoreCase?: boolean | undefined } | undefined * ): Promise<any> { * switch (operationType) { * case 'like': * if (options?.ignoreCase) result[Op.iLike] = value; * else if (options?.isNot && options.ignoreCase) result[Op.notILike] = value; * else if (options?.isNot) result[Op.notLike] = value; * else result[Op.like] = value; * return; * case 'is': * if (value === null && options?.isNot) result[Op.not] = value; * else if (value === null) result[Op.is] = value; * else if (options?.isNot) result[Op.ne] = value; * else result[Op.eq] = value; * return; * case 'in': * if (options?.isNot) result[Op.notIn] = value; * else result[Op.in] = value; * return; * case 'between': * if (options?.isNot) result[Op.notBetween] = value; * else result[Op.between] = value; * return; * case 'and': * result[Op.and] = value; * return; * case 'or': * result[Op.or] = value; * return; * case 'greaterThan': * result[Op.gt] = value; * return; * case 'greaterThanOrEqual': * result[Op.gte] = value; * return; * case 'lessThan': * result[Op.lt] = value; * return; * case 'lessThanOrEqual': * result[Op.lte] = value; * return; * default: * return; * } * } * ``` * * @param operationType - The operation type of the query, this can be `like`, `in`, `is`, `between` and so on. * @param key - The key of the query, this is the field name that we are querying. * @param modelInstance - The model instance that we are querying. * @param value - The value of the query, if the operation is `or`, `and`, `in` or `between` this will be an array of * values, otherwise this can be a string, a number and such. Be aware that this respects the `inputParser` of the * field if you have implemented it on {@link AdapterFieldParser}. This is nice because we can maintain the types of * the fields through the framework and you can parse the way that makes sense for your engine. * @param result - This is the result we will use on the query. It's an object that you can append the result of the * parsing. * @param options - This is an object with some options that you can use to parse the query, like if it's a negative * query, if it's case insensitive and so on. * * @returns - Values are changed in-place, so you should not return anything. */ // eslint-disable-next-line ts/require-await async parseSearchFieldValue(_operationType, _key, _modelInstance, _value, _result, _options) { return Array.isArray(_value) ? _value[0] : _value; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AdapterSearchQuery, adapterSearchQuery });