UNPKG

@avonjs/avonjs

Version:

A fluent Node.js API generator.

63 lines (62 loc) 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Contracts_1 = require("../Contracts"); exports.default = (Parent) => { class FilterableFields extends Parent { /** * Apply the filter into the given repository. */ async apply(request, queryBuilder, value) { if (typeof this.field.filterableCallback === 'function') { this.isValidNullValue(value) ? this.applyNullFilter(request, queryBuilder) : this.field.applyFilter(request, queryBuilder, this.parseValue(value)); } else if (this.field.filterableCallback) { //@ts-ignore super.apply(request, queryBuilder, value); } } /** * Get the attribute that the date filter should perform on it. */ filterableAttribute(request) { return this.field.filterableAttribute(request); } /** * Determine if the given value is considered a valid null value if the field supports them. */ isValidNullValue(value) { return this.field.isValidNullValue(value); } /** * Determine if the field supports null values. */ isNullable() { return this.field.isNullable(); } /** * Apply filter for valid null values. */ applyNullFilter(request, queryBuilder) { return queryBuilder.where({ key: this.field.filterableAttribute(request), operator: Contracts_1.Operator.eq, value: this.field.nullValue(), }); } /** * Parse the value given from request. */ parseValue(value) { return value; } /** * Get the query parameter key for filter. */ key() { return `${this.field.constructor.name}:${this.field.attribute}`; } } return FilterableFields; };