import { Knex } from 'knex' ; import { getHelpers } from '../database/helpers' ; import { REGEX_BETWEEN_PARENS } from '@directus/shared/constants' ; import { applyFunctionToColumnName } from './apply-function-to-column-name' ; param knex param collection param field param alias returns export function getColumn ( knex , table , column , alias = applyFunctionToColumnName ( column ) ) { const { date : fn } = getHelpers ( knex ) ; if ( column . includes ( '(' ) && column . includes ( ')' ) ) { const functionName = column . split ( '(' ) [ 0 ] ; const columnName = column . match ( REGEX_BETWEEN_PARENS ) ! [ 1 ] ; if ( functionName in fn ) { const result = fn [ functionName as keyof typeof fn ] ( table , columnName ) as Knex . Raw ; if ( alias ) { return knex . raw ( result + ' AS ??' , [ alias ] ) ; } return result ; } else { throw new Error ( `Invalid function specified "${ functionName }"` ) ; } } if ( alias && column !== alias ) { return knex . ref ( `${ table }.${ column }` ) . as ( alias ) ; } return knex . ref ( `${ table }.${ column }` ) ; }