Adjusting SQL system to handle relationship filtering.

This commit is contained in:
mike12345567 2022-01-13 17:40:11 +00:00
parent 41b9c94237
commit 479220652e
2 changed files with 15 additions and 8 deletions

View File

@ -72,16 +72,22 @@ class InternalBuilder {
// right now we only do filters on the specific table being queried // right now we only do filters on the specific table being queried
addFilters( addFilters(
tableName: string,
query: KnexQuery, query: KnexQuery,
filters: SearchFilters | undefined filters: SearchFilters | undefined,
opts: { relationship?: boolean; tableName?: string }
): KnexQuery { ): KnexQuery {
function iterate( function iterate(
structure: { [key: string]: any }, structure: { [key: string]: any },
fn: (key: string, value: any) => void fn: (key: string, value: any) => void
) { ) {
for (let [key, value] of Object.entries(structure)) { for (let [key, value] of Object.entries(structure)) {
fn(`${tableName}.${key}`, value) const isRelationshipField = key.includes(".")
if (!opts.relationship && !isRelationshipField) {
fn(`${opts.tableName}.${key}`, value)
}
if (opts.relationship && isRelationshipField) {
fn(key, value)
}
} }
} }
if (!filters) { if (!filters) {
@ -272,7 +278,7 @@ class InternalBuilder {
if (foundOffset) { if (foundOffset) {
query = query.offset(foundOffset) query = query.offset(foundOffset)
} }
query = this.addFilters(tableName, query, filters) query = this.addFilters(query, filters, { tableName })
// add sorting to pre-query // add sorting to pre-query
query = this.addSorting(query, json) query = this.addSorting(query, json)
// @ts-ignore // @ts-ignore
@ -285,20 +291,21 @@ class InternalBuilder {
preQuery = this.addSorting(preQuery, json) preQuery = this.addSorting(preQuery, json)
} }
// handle joins // handle joins
return this.addRelationships( query = this.addRelationships(
knex, knex,
preQuery, preQuery,
selectStatement, selectStatement,
tableName, tableName,
relationships relationships
) )
return this.addFilters(query, filters, { relationship: true })
} }
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: KnexQuery = knex(endpoint.entityId)
const parsedBody = parseBody(body) const parsedBody = parseBody(body)
query = this.addFilters(endpoint.entityId, query, filters) 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.update(parsedBody) return query.update(parsedBody)
@ -310,7 +317,7 @@ 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: KnexQuery = knex(endpoint.entityId)
query = this.addFilters(endpoint.entityId, query, filters) 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()

View File

@ -38,7 +38,7 @@ module S3Module {
signatureVersion: { signatureVersion: {
type: "string", type: "string",
required: false, required: false,
default: "v4" default: "v4",
}, },
}, },
query: { query: {