UNPKG

@avonjs/avonjs

Version:

A fluent Node.js API generator.

58 lines (57 loc) 1.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Contracts_1 = require("../Contracts"); exports.default = (Parent) => { class Lookupable extends Parent { /** * The callback to be used for the field's default value. */ lookupCallback; /** * The key used to look up the resource. */ lookupKeyUsing; /** * Apply the filter to the given query. */ async applyLookup(request, queryBuilder, value) { if (typeof this.lookupCallback === 'function') { await this.lookupCallback?.apply(this, [request, queryBuilder, value]); } else { queryBuilder.where({ key: this.lookupKey(), value: value, operator: Contracts_1.Operator.eq, }); } } /** * Indicates the field is look up able. */ isLookupable() { return Boolean(this.lookupCallback); } /** * The callback used to determine if the field is lookupable. */ lookupable(callback = true) { this.lookupCallback = callback; return this; } /** * Get the look up key name. */ lookupKey() { return this.lookupKeyUsing ?? this.attribute; } /** * Set the look up key name. */ lookupUsing(lookupKey) { this.lookupKeyUsing = lookupKey; return this; } } return Lookupable; };