UNPKG

@aciiverse/aciifx-cli

Version:

The powerful CLI for aciiFX backend in combination with fetcii

219 lines 8.52 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.filter = void 0; var filter; (function (filter_1) { let CompareOperator; (function (CompareOperator) { CompareOperator["Equal"] = "eq"; CompareOperator["NotEqual"] = "neq"; CompareOperator["GreaterThan"] = "gt"; CompareOperator["GreaterEqual"] = "ge"; CompareOperator["LessThan"] = "lt"; CompareOperator["LessEqual"] = "le"; CompareOperator["Inside"] = "in"; CompareOperator["NotInside"] = "ni"; })(CompareOperator = filter_1.CompareOperator || (filter_1.CompareOperator = {})); /** * @async * @method gets the query params * @param {Req} req the route request * @param {GetQueryParamsOptions} options for defining which params should readed * @returns {Promise<GetQueryParams>} all `query params` as an object * @author Flowtastisch * @memberof Aciiverse * @date 21.11.2024 */ function getQueryParams(req, options) { return __awaiter(this, void 0, void 0, function* () { const response = {}; if (options.all || options.filters) { // -> filters defined -> read filters const filters = req.query.$filters; if (filters && typeof filters === "string") { // -> filter string not undefined & valid try { const fObj = yield JSON.parse(filters); if (!fObj || Object.keys(fObj).length === 0) { // -> no filters response.filters = undefined; } else { // -> filters exists response.filters = fObj; } } catch (err) { // -> response stay undefined } } } if (options.all || options.orderBy) { // -> orderBy defined -> read orderBy const orderBy = req.query.$orderBy; if (orderBy && typeof orderBy === "string") { // -> orderBy string not undefined & valid try { const parsed = yield JSON.parse(orderBy); response.orderBy = parsed; } catch (err) { // -> response stay undefined } } } if (options.all || options.select) { // -> select defined -> read select const select = req.query.$select; if (select && typeof select === "string") { // -> select string not undefined & valid try { const parsed = yield JSON.parse(select); response.select = parsed; } catch (err) { // -> response stay undefined } } } if (options.all || options.topSkip) { // -> topSkip defined -> read topSkip const top = parseInt(req.query.$top), skip = parseInt(req.query.$skip); if (!isNaN(top) && typeof top === "number") { // -> top number not undefined & valid response.top = top; } if (!isNaN(skip) && typeof skip === "number") { // -> skip number defined & valid response.skip = skip; } else { // -> skip undefined -> skip initially to 0 response.skip = 0; } } return response; }); } filter_1.getQueryParams = getQueryParams; /** * @method gets only the first filter value, that exists for the searched property * @param {FiltersType} filters in which to search * @param {string} property search property * @author Flowtastisch * @memberof Aciiverse * @date 21.11.2024 */ function getFirstFilterValue(filters, property) { const content = getFilter(filters, property), innerFilters = content === null || content === void 0 ? void 0 : content.filters; if (!innerFilters || !innerFilters[0]) { // -> filter not defined return undefined; } return innerFilters[0].value; } filter_1.getFirstFilterValue = getFirstFilterValue; /** * @method gets a filter by it's property * @param {FiltersType} filters in which to search * @param {string} property * @returns {FilterContent} the raw filter content * @author Flowtastisch * @memberof Aciiverse * @date 21.11.2024 */ function getFilter(filters, property) { return filters[property]; } filter_1.getFilter = getFilter; /** * @method gets a filter by it's property * @param {FiltersType} filters in which to search * @param {string} property * @param {string} sqlProperty property as sql property * @returns {SQLFilterExport | undefined} the filter in sql syntax and it's values * @example sql: ` t.title = ? AND t.title != ? AND t.title LIKE %?% ` * @example values: `[ Acii, Lacii, Joyboy ]` * @author Flowtastisch * @memberof Aciiverse * @date 20.11.2024 */ function getFilterSQL(filters, property, sqlProperty) { const filter = filters[property], values = [], sql = []; if (!filter) { // -> filter not exists return undefined; } const and = getSQLAnd(filter.and), content = filter.filters; content.forEach((e) => { const operator = getSQLOperator(e.operator); if (e.operator === CompareOperator.Inside || e.operator === CompareOperator.NotInside) { // -> (NOT) LIKE value with `%` values.push(`%${e.value.trim()}%`); } else { // -> standard value string values.push(e.value); } sql.push(` ${sqlProperty} ${operator} ? `); }); const sqlStr = sql.join(and); return { sql: sqlStr, values: values }; } filter_1.getFilterSQL = getFilterSQL; /** * @method gets AND / OR * @param {boolean} and AND or OR * @returns {string} sql ready string * @author Flowtastisch * @memberof Aciiverse * @date 20.11.2024 */ function getSQLAnd(and) { if (and) { // -> AND return "AND"; } // -> OR return "OR"; } /** * @method gets the compare operator * @param {boolean} operator * @returns {string} sql ready string * @author Flowtastisch * @memberof Aciiverse * @date 20.11.2024 */ function getSQLOperator(operator) { switch (operator) { case CompareOperator.Equal: return "="; case CompareOperator.GreaterEqual: return ">="; case CompareOperator.GreaterThan: return ">"; case CompareOperator.Inside: return "LIKE"; case CompareOperator.LessEqual: return "<="; case CompareOperator.LessThan: return "<"; case CompareOperator.NotEqual: return "!="; case CompareOperator.NotInside: return "NOT LIKE"; } } })(filter || (exports.filter = filter = {})); //# sourceMappingURL=filter.js.map