UNPKG

@directus/api

Version:

Directus is a real-time API and App dashboard for managing SQL database content

55 lines (53 loc) 2.06 kB
import { applyCaseWhen } from "./apply-case-when.js"; import { getColumn } from "./get-column.js"; import { joinFilterWithCases } from "../lib/apply-query/join-filter-with-cases.js"; import { parseFilterKey } from "../../../utils/parse-filter-key.js"; import { getHelpers } from "../../helpers/index.js"; import { getNodeAlias } from "./get-field-alias.js"; //#region src/database/run-ast/utils/get-column-pre-processor.ts function getColumnPreprocessor(knex, schema, table, cases, permissions, aliasMap, permissionsOnly) { const helpers = getHelpers(knex); return function(fieldNode, options) { const hasWhenCase = fieldNode.whenCase && fieldNode.whenCase.length > 0; const noAlias = options?.noAlias || hasWhenCase; const alias = getNodeAlias(fieldNode); const rawColumnAlias = noAlias ? false : alias; let field; if (fieldNode.type === "field" || fieldNode.type === "functionField") { const { fieldName } = parseFilterKey(fieldNode.name); field = schema.collections[table].fields[fieldName]; } else field = schema.collections[fieldNode.relation.collection].fields[fieldNode.relation.field]; let column; if (permissionsOnly) if (noAlias) column = knex.raw(1); else column = knex.raw("1 as ??", [alias]); else if (field?.type?.startsWith("geometry")) column = helpers.st.asText(table, field.field, rawColumnAlias); else if (fieldNode.type === "functionField") column = getColumn(knex, table, fieldNode.name, rawColumnAlias, schema, { query: { ...fieldNode.query, filter: joinFilterWithCases(fieldNode.query.filter, fieldNode.cases) }, permissions, cases: fieldNode.cases }); else column = getColumn(knex, table, fieldNode.name, rawColumnAlias, schema); if (hasWhenCase) { const columnCases = []; for (const index of fieldNode.whenCase) columnCases.push(cases[index]); column = applyCaseWhen({ column, columnCases, aliasMap, cases, table, alias, permissions }, { knex, schema }); } return column; }; } //#endregion export { getColumnPreprocessor };