From ece1e7533d71106d27fc2d476a7ba001b291940d Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Thu, 23 Jun 2022 09:41:43 +0100 Subject: [PATCH] Refactor - simplify regex and use enum --- packages/server/src/integrations/base/sql.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index 87243c073e..c9841347a6 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -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}"` ) } }