From 7eccbb851dac11b36e936ae52a67500f99cc7a52 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 1 Dec 2023 15:27:49 +0000 Subject: [PATCH] Fixing issues with other SQL functions than just reading. --- .../server/src/api/controllers/row/alias.ts | 2 +- packages/server/src/integrations/base/sql.ts | 46 +++++++++---------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/packages/server/src/api/controllers/row/alias.ts b/packages/server/src/api/controllers/row/alias.ts index 0c7a4bb8a0..fc00b505c4 100644 --- a/packages/server/src/api/controllers/row/alias.ts +++ b/packages/server/src/api/controllers/row/alias.ts @@ -88,7 +88,7 @@ export default class AliasTables { } const aliasedFilters: typeof filter = {} for (let key of Object.keys(filter)) { - aliasedFilters[aliasField(key)] = filter + aliasedFilters[aliasField(key)] = filter[key] } json.filters[filterKey as keyof SearchFilters] = aliasedFilters } diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index 57af95eabb..14bcb532cc 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -421,12 +421,24 @@ class InternalBuilder { return query.limit(BASE_LIMIT) } - create(knex: Knex, json: QueryJson, opts: QueryOptions): KnexQuery { - const { endpoint, body } = json - let query: KnexQuery = knex(endpoint.entityId) + knexWithAlias( + knex: Knex, + endpoint: { entityId: string; alias?: string; schema?: string } + ): { query: KnexQuery; name: string } { + const tableName = endpoint.entityId + const alias = endpoint.alias + const aliased = alias ? alias : tableName + const tableAliased = alias ? `${tableName} as ${alias}` : tableName + let query = knex(tableAliased) if (endpoint.schema) { query = query.withSchema(endpoint.schema) } + return { query, name: aliased } + } + + create(knex: Knex, json: QueryJson, opts: QueryOptions): KnexQuery { + const { endpoint, body } = json + let { query } = this.knexWithAlias(knex, endpoint) const parsedBody = parseBody(body) // make sure no null values in body for creation for (let [key, value] of Object.entries(parsedBody)) { @@ -445,10 +457,7 @@ class InternalBuilder { bulkCreate(knex: Knex, json: QueryJson): KnexQuery { const { endpoint, body } = json - let query: KnexQuery = knex(endpoint.entityId) - if (endpoint.schema) { - query = query.withSchema(endpoint.schema) - } + let { query } = this.knexWithAlias(knex, endpoint) if (!Array.isArray(body)) { return query } @@ -459,10 +468,6 @@ class InternalBuilder { read(knex: Knex, json: QueryJson, limit: number): KnexQuery { let { endpoint, resource, filters, paginate, relationships } = json - const tableName = endpoint.entityId - const alias = endpoint.alias - const aliased = alias ? alias : tableName - const tableAliased = alias ? `${tableName} as ${alias}` : tableName // select all if not specified if (!resource) { resource = { fields: [] } @@ -487,10 +492,9 @@ class InternalBuilder { foundLimit = paginate.limit } // start building the query - let query: KnexQuery = knex(tableAliased).limit(foundLimit) - if (endpoint.schema) { - query = query.withSchema(endpoint.schema) - } + + let { query, name: aliased } = this.knexWithAlias(knex, endpoint) + query = query.limit(foundLimit) if (foundOffset) { query = query.offset(foundOffset) } @@ -518,10 +522,7 @@ class InternalBuilder { update(knex: Knex, json: QueryJson, opts: QueryOptions): KnexQuery { const { endpoint, body, filters } = json - let query: KnexQuery = knex(endpoint.entityId) - if (endpoint.schema) { - query = query.withSchema(endpoint.schema) - } + let { query } = this.knexWithAlias(knex, endpoint) const parsedBody = parseBody(body) query = this.addFilters(query, filters, { tableName: endpoint.entityId }) // mysql can't use returning @@ -534,11 +535,8 @@ class InternalBuilder { delete(knex: Knex, json: QueryJson, opts: QueryOptions): KnexQuery { const { endpoint, filters } = json - let query: KnexQuery = knex(endpoint.entityId) - if (endpoint.schema) { - query = query.withSchema(endpoint.schema) - } - query = this.addFilters(query, filters, { tableName: endpoint.entityId }) + let { query, name: aliased } = this.knexWithAlias(knex, endpoint) + query = this.addFilters(query, filters, { tableName: aliased }) // mysql can't use returning if (opts.disableReturning) { return query.delete()