1
0
Fork 0
mirror of synced 2024-10-03 02:27:06 +13:00

Getting relationship aliasing working.

This commit is contained in:
mike12345567 2023-11-28 18:43:38 +00:00
parent 20dae6ed82
commit 65cddae9da
3 changed files with 46 additions and 11 deletions

View file

@ -58,6 +58,16 @@ export default class AliasTables {
}
}
aliasMap(tableNames: (string | undefined)[]) {
const map: Record<string, string> = {}
for (let tableName of tableNames) {
if (tableName) {
map[tableName] = this.getAlias(tableName)
}
}
return map
}
async queryWithAliasing(json: QueryJson) {
json = cloneDeep(json)
const aliasField = (field: string) => this.aliasField(field)
@ -86,7 +96,11 @@ export default class AliasTables {
if (json.relationships) {
json.relationships = json.relationships.map(relationship => ({
...relationship,
alias: this.getAlias(relationship.tableName),
aliases: this.aliasMap([
relationship.through,
relationship.tableName,
json.endpoint.entityId,
]),
}))
}
if (json.meta?.table) {

View file

@ -330,6 +330,17 @@ class InternalBuilder {
return query
}
tableNameWithSchema(
tableName: string,
opts?: { alias?: string; schema?: string }
) {
let withSchema = opts?.schema ? `${opts.schema}.${tableName}` : tableName
if (opts?.alias) {
withSchema += ` as ${opts.alias}`
}
return withSchema
}
addRelationships(
query: KnexQuery,
fromTable: string,
@ -339,9 +350,12 @@ class InternalBuilder {
if (!relationships) {
return query
}
const tableSets: Record<string, [any]> = {}
const tableSets: Record<string, [RelationshipsJson]> = {}
// add up all aliases
let aliases: Record<string, string> = {}
// aggregate into table sets (all the same to tables)
for (let relationship of relationships) {
aliases = { ...aliases, ...relationship.aliases }
const keyObj: { toTable: string; throughTable: string | undefined } = {
toTable: relationship.tableName,
throughTable: undefined,
@ -358,10 +372,17 @@ class InternalBuilder {
}
for (let [key, relationships] of Object.entries(tableSets)) {
const { toTable, throughTable } = JSON.parse(key)
const toTableWithSchema = schema ? `${schema}.${toTable}` : toTable
const throughTableWithSchema = schema
? `${schema}.${throughTable}`
: throughTable
const toAlias = aliases[toTable],
throughAlias = aliases[throughTable],
fromAlias = aliases[fromTable]
let toTableWithSchema = this.tableNameWithSchema(toTable, {
alias: toAlias,
schema,
})
let throughTableWithSchema = this.tableNameWithSchema(throughTable, {
alias: throughAlias,
schema,
})
if (!throughTable) {
// @ts-ignore
query = query.leftJoin(toTableWithSchema, function () {
@ -369,7 +390,7 @@ class InternalBuilder {
const from = relationship.from,
to = relationship.to
// @ts-ignore
this.orOn(`${fromTable}.${from}`, "=", `${toTable}.${to}`)
this.orOn(`${fromTable}.${from}`, "=", `${toAlias}.${to}`)
}
})
} else {
@ -381,9 +402,9 @@ class InternalBuilder {
const from = relationship.from
// @ts-ignore
this.orOn(
`${fromTable}.${fromPrimary}`,
`${fromAlias}.${fromPrimary}`,
"=",
`${throughTable}.${from}`
`${throughAlias}.${from}`
)
}
})
@ -392,7 +413,7 @@ class InternalBuilder {
const toPrimary = relationship.toPrimary
const to = relationship.to
// @ts-ignore
this.orOn(`${toTable}.${toPrimary}`, `${throughTable}.${to}`)
this.orOn(`${toAlias}.${toPrimary}`, `${throughAlias}.${to}`)
}
})
}

View file

@ -67,7 +67,7 @@ export interface RelationshipsJson {
fromPrimary?: string
toPrimary?: string
tableName: string
alias?: string
aliases?: Record<string, string>
column: string
}