Fixing issues with other SQL functions than just reading.

This commit is contained in:
mike12345567 2023-12-01 15:27:49 +00:00
parent 5c4dc0dc83
commit 7eccbb851d
2 changed files with 23 additions and 25 deletions

View File

@ -88,7 +88,7 @@ export default class AliasTables {
} }
const aliasedFilters: typeof filter = {} const aliasedFilters: typeof filter = {}
for (let key of Object.keys(filter)) { for (let key of Object.keys(filter)) {
aliasedFilters[aliasField(key)] = filter aliasedFilters[aliasField(key)] = filter[key]
} }
json.filters[filterKey as keyof SearchFilters] = aliasedFilters json.filters[filterKey as keyof SearchFilters] = aliasedFilters
} }

View File

@ -421,12 +421,24 @@ class InternalBuilder {
return query.limit(BASE_LIMIT) return query.limit(BASE_LIMIT)
} }
create(knex: Knex, json: QueryJson, opts: QueryOptions): KnexQuery { knexWithAlias(
const { endpoint, body } = json knex: Knex,
let query: KnexQuery = knex(endpoint.entityId) 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) { if (endpoint.schema) {
query = query.withSchema(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) const parsedBody = parseBody(body)
// make sure no null values in body for creation // make sure no null values in body for creation
for (let [key, value] of Object.entries(parsedBody)) { for (let [key, value] of Object.entries(parsedBody)) {
@ -445,10 +457,7 @@ class InternalBuilder {
bulkCreate(knex: Knex, json: QueryJson): KnexQuery { bulkCreate(knex: Knex, json: QueryJson): KnexQuery {
const { endpoint, body } = json const { endpoint, body } = json
let query: KnexQuery = knex(endpoint.entityId) let { query } = this.knexWithAlias(knex, endpoint)
if (endpoint.schema) {
query = query.withSchema(endpoint.schema)
}
if (!Array.isArray(body)) { if (!Array.isArray(body)) {
return query return query
} }
@ -459,10 +468,6 @@ class InternalBuilder {
read(knex: Knex, json: QueryJson, limit: number): KnexQuery { read(knex: Knex, json: QueryJson, limit: number): KnexQuery {
let { endpoint, resource, filters, paginate, relationships } = json 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 // select all if not specified
if (!resource) { if (!resource) {
resource = { fields: [] } resource = { fields: [] }
@ -487,10 +492,9 @@ class InternalBuilder {
foundLimit = paginate.limit foundLimit = paginate.limit
} }
// start building the query // start building the query
let query: KnexQuery = knex(tableAliased).limit(foundLimit)
if (endpoint.schema) { let { query, name: aliased } = this.knexWithAlias(knex, endpoint)
query = query.withSchema(endpoint.schema) query = query.limit(foundLimit)
}
if (foundOffset) { if (foundOffset) {
query = query.offset(foundOffset) query = query.offset(foundOffset)
} }
@ -518,10 +522,7 @@ class InternalBuilder {
update(knex: Knex, json: QueryJson, opts: QueryOptions): KnexQuery { update(knex: Knex, json: QueryJson, opts: QueryOptions): KnexQuery {
const { endpoint, body, filters } = json const { endpoint, body, filters } = json
let query: KnexQuery = knex(endpoint.entityId) let { query } = this.knexWithAlias(knex, endpoint)
if (endpoint.schema) {
query = query.withSchema(endpoint.schema)
}
const parsedBody = parseBody(body) const parsedBody = parseBody(body)
query = this.addFilters(query, filters, { tableName: endpoint.entityId }) query = this.addFilters(query, filters, { tableName: endpoint.entityId })
// mysql can't use returning // mysql can't use returning
@ -534,11 +535,8 @@ class InternalBuilder {
delete(knex: Knex, json: QueryJson, opts: QueryOptions): KnexQuery { delete(knex: Knex, json: QueryJson, opts: QueryOptions): KnexQuery {
const { endpoint, filters } = json const { endpoint, filters } = json
let query: KnexQuery = knex(endpoint.entityId) let { query, name: aliased } = this.knexWithAlias(knex, endpoint)
if (endpoint.schema) { query = this.addFilters(query, filters, { tableName: aliased })
query = query.withSchema(endpoint.schema)
}
query = this.addFilters(query, filters, { tableName: endpoint.entityId })
// mysql can't use returning // mysql can't use returning
if (opts.disableReturning) { if (opts.disableReturning) {
return query.delete() return query.delete()