import { QueryOptions, toExpression, Expression, combineExpressions, QueryField, Query, QuerySort, Filterable } from "@agrejus/db-framework-core"; import { EntityMap } from "../../types"; import { QueryOrdering } from "../types"; import { IDataAccessManager } from '../../data-access/types'; export abstract class QueryRoot { protected readonly manager: IDataAccessManager; protected filters: Filterable[] = []; protected mapValue: EntityMap> | null = null; protected takeValue: number | null = null; protected skipValue: number | null = null; protected sorting: { direction: QueryOrdering, selector: EntityMap }[] = []; protected minValue: boolean = false protected maxValue: boolean = false protected countValue: boolean = false protected sumValue: boolean = false; protected distinctValue: boolean = false; protected subscribeValue: boolean = false; private _compiledQuery: Query | null = null; constructor(queryable?: QueryRoot, manager?: IDataAccessManager) { if (manager != null) { this.manager = manager; } if (queryable != null) { this.subscribeValue = queryable.subscribeValue; this.manager = queryable.manager; this.manager = queryable.manager; this.filters = queryable.filters; this.takeValue = queryable.takeValue; this.skipValue = queryable.skipValue; this.mapValue = queryable.mapValue; this.sorting = queryable.sorting; this.minValue = queryable.minValue; this.maxValue = queryable.maxValue; this.countValue = queryable.countValue; this.sumValue = queryable.sumValue; this.distinctValue = queryable.distinctValue; } } protected getQueryOptions(): QueryOptions { const fields = this._getFields(this.mapValue); const sort = this._getSorting(this.sorting); return { count: this.countValue, distinct: this.distinctValue, max: this.maxValue, min: this.minValue, sort, skip: this.skipValue, sum: this.sumValue, take: this.takeValue, fields } } protected subscribeQuery(shape: (data: T[]) => U, done: (result: U, error?: any) => void) { if (this.subscribeValue === false) { return; } const query = this.getOrCompileQuery(); return this.manager.subscribe(query, shape, done); } private _getSorting(sorting: { direction: QueryOrdering, selector: EntityMap }[]) { const result: QuerySort[] = []; for (let i = 0; i < sorting.length; i++) { const sort = sorting[i]; const propertyName = this._getSortPropertyName(sort.selector); result.push({ direction: sort.direction, key: propertyName }) } return result; } private _getSortPropertyName(selector: EntityMap) { const stringified = selector.toString(); if (stringified.includes("=>") === false) { throw new Error("Only arrow functions allowed in .map()") } const [, body] = stringified.split("=>").map(w => w.trim()); return this._extractPropertyName(body); } private _getFields(map: EntityMap> | null): QueryField[] { if (map == null) { return []; } const stringified = map.toString(); if (stringified.includes("=>") === false) { throw new Error("Only arrow functions allowed in .map()") } const [, body] = stringified.split("=>").map(w => w.trim()); if (body.includes("{")) { const properties = body.replace(/{|}|\(|\)/g, "").split(",").map(w => w.trim()); const result: QueryField[] = []; for (let i = 0; i < properties.length; i++) { const property = properties[i]; const [destinationName, sourcePathAndName] = property.split(":").map(w => w.trim()); const sourceName = this._extractPropertyName(sourcePathAndName); result.push({ sourceName, destinationName }) } return result; } const field = this._extractPropertyName(body); return [{ destinationName: field, sourceName: field }]; } private _extractPropertyName(value: string) { const split = value.split("."); split.shift(); return split.join(".") } private _convertToExpression(filter: Filterable) { if (filter.params != null) { return toExpression(this.manager.schema, filter.filter, filter.params); } try { return toExpression(this.manager.schema, filter.filter, {}); } catch (e) { console.warn(`[WARNING] - Failed to parse selector to expression, falling back to memory filtering. Selector: ${filter.filter.toString()}, Params: ${JSON.stringify(filter.params ?? {})}`) return null; // fallback to memory filtering } } protected getExpression(): Expression | null { if (this.filters.length === 0) { return null; } const expressions: Expression[] = []; for (let i = 0; i < this.filters.length; i++) { const filter = this.filters[i]; const expression = this._convertToExpression(filter); if (expression == null) { return null; // fall back to memory filtering for everything } expressions.push(expression); } return combineExpressions(...expressions); } protected getOrCompileQuery() { if (this._compiledQuery != null) { return this._compiledQuery; } const expression = this.getExpression(); const options = this.getQueryOptions(); this._compiledQuery = { schema: this.manager.schema, options, filters: this.filters } if (expression != null) { this._compiledQuery.expression = expression; } return this._compiledQuery; } protected getData(done: (result: T[], error?: any) => void) { const query = this.getOrCompileQuery(); this.manager.fetch(query, done); } }