PR comments.
This commit is contained in:
parent
a9da4e11d3
commit
fac9f18bc2
|
@ -640,14 +640,26 @@ export class ExternalRequest<T extends Operation> {
|
||||||
) {
|
) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
const isMany = field.relationshipType === RelationshipType.MANY_TO_MANY
|
let tableId: string | undefined,
|
||||||
const tableId = isMany ? field.through! : field.tableId!
|
lookupField: string | undefined,
|
||||||
|
fieldName: string | undefined
|
||||||
|
if (isManyToMany(field)) {
|
||||||
|
tableId = field.through
|
||||||
|
lookupField = primaryKey
|
||||||
|
fieldName = field.throughTo || primaryKey
|
||||||
|
} else if (isManyToOne(field)) {
|
||||||
|
tableId = field.tableId
|
||||||
|
lookupField = field.foreignKey
|
||||||
|
fieldName = field.fieldName
|
||||||
|
}
|
||||||
|
if (!tableId || !lookupField || !fieldName) {
|
||||||
|
throw new Error(
|
||||||
|
"Unable to lookup relationships - undefined column properties."
|
||||||
|
)
|
||||||
|
}
|
||||||
const { tableName: relatedTableName } = breakExternalTableId(tableId)
|
const { tableName: relatedTableName } = breakExternalTableId(tableId)
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const linkPrimaryKey = this.tables[relatedTableName].primary[0]
|
const linkPrimaryKey = this.tables[relatedTableName].primary[0]
|
||||||
|
|
||||||
const lookupField = isMany ? primaryKey : field.foreignKey
|
|
||||||
const fieldName = isMany ? field.throughTo || primaryKey : field.fieldName
|
|
||||||
if (!lookupField || !row[lookupField]) {
|
if (!lookupField || !row[lookupField]) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -660,9 +672,12 @@ export class ExternalRequest<T extends Operation> {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
// this is the response from knex if no rows found
|
// this is the response from knex if no rows found
|
||||||
const rows: Row[] = response?.[0].read ? [] : (response as Row[])
|
const rows: Row[] =
|
||||||
const storeTo = isMany ? field.throughFrom || linkPrimaryKey : fieldName
|
!Array.isArray(response) || response?.[0].read ? [] : response
|
||||||
related[storeTo] = { rows, isMany, tableId }
|
const storeTo = isManyToMany(field)
|
||||||
|
? field.throughFrom || linkPrimaryKey
|
||||||
|
: fieldName
|
||||||
|
related[storeTo] = { rows, isMany: isManyToMany(field), tableId }
|
||||||
}
|
}
|
||||||
return related
|
return related
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,19 +16,17 @@ class CharSequence {
|
||||||
this.counters = [0]
|
this.counters = [0]
|
||||||
}
|
}
|
||||||
|
|
||||||
get character() {
|
getCharacter(): string {
|
||||||
return this.counters.map(i => CharSequence.alphabet[i]).join("")
|
const char = this.counters.map(i => CharSequence.alphabet[i]).join("")
|
||||||
}
|
|
||||||
|
|
||||||
next() {
|
|
||||||
for (let i = this.counters.length - 1; i >= 0; i--) {
|
for (let i = this.counters.length - 1; i >= 0; i--) {
|
||||||
if (this.counters[i] < CharSequence.alphabet.length - 1) {
|
if (this.counters[i] < CharSequence.alphabet.length - 1) {
|
||||||
this.counters[i]++
|
this.counters[i]++
|
||||||
return
|
return char
|
||||||
}
|
}
|
||||||
this.counters[i] = 0
|
this.counters[i] = 0
|
||||||
}
|
}
|
||||||
this.counters.unshift(0)
|
this.counters.unshift(0)
|
||||||
|
return char
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,8 +47,7 @@ export default class AliasTables {
|
||||||
if (this.aliases[tableName]) {
|
if (this.aliases[tableName]) {
|
||||||
return this.aliases[tableName]
|
return this.aliases[tableName]
|
||||||
}
|
}
|
||||||
const char = this.charSeq.character
|
const char = this.charSeq.getCharacter()
|
||||||
this.charSeq.next()
|
|
||||||
this.aliases[tableName] = char
|
this.aliases[tableName] = char
|
||||||
this.tableAliases[char] = tableName
|
this.tableAliases[char] = tableName
|
||||||
return char
|
return char
|
||||||
|
|
|
@ -164,14 +164,14 @@ describe("Captures of real examples", () => {
|
||||||
it("should handle over 'z' max character alias", () => {
|
it("should handle over 'z' max character alias", () => {
|
||||||
const tableNames = []
|
const tableNames = []
|
||||||
for (let i = 0; i < 100; i++) {
|
for (let i = 0; i < 100; i++) {
|
||||||
tableNames.push(generator.word())
|
tableNames.push(generator.guid())
|
||||||
}
|
}
|
||||||
const aliasing = new AliasTables(tableNames)
|
const aliasing = new AliasTables(tableNames)
|
||||||
let alias: string = ""
|
let alias: string = ""
|
||||||
for (let table of tableNames) {
|
for (let table of tableNames) {
|
||||||
alias = aliasing.getAlias(table)
|
alias = aliasing.getAlias(table)
|
||||||
}
|
}
|
||||||
expect(alias).toEqual("cu")
|
expect(alias).toEqual("cv")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue