Refactor - simplify regex and use enum

This commit is contained in:
Mel O'Hagan 2022-06-23 09:41:43 +01:00
parent 9603a92c2c
commit ece1e7533d
1 changed files with 6 additions and 5 deletions

View File

@ -93,13 +93,14 @@ function generateSelectStatement(json: QueryJson, knex: Knex): any[] {
const { resource, meta } = json
const schema = meta?.table?.schema
return resource.fields.map(field => {
const shortFieldName = field.match(/(?<=\.).*$/g)?.[0]
if (shortFieldName && knex.client.config.client === "pg") {
const externalType = schema?.[shortFieldName].externalType
const fieldNames = field.split(/\./g)
const tableName = fieldNames[0]
const columnName = fieldNames[1]
if (columnName && knex.client.config.client === SqlClients.POSTGRES) {
const externalType = schema?.[columnName].externalType
if (externalType?.includes("money")) {
const fieldName = field.split(/\./g)
return knex.raw(
`"${fieldName?.[0]}"."${fieldName?.[1]}"::money::numeric as "${field}"`
`"${tableName}"."${columnName}"::money::numeric as "${field}"`
)
}
}