1
0
Fork 0
mirror of synced 2024-07-06 15:00:49 +12:00

PR comments.

This commit is contained in:
mike12345567 2024-02-27 13:40:31 +00:00
parent a9da4e11d3
commit fac9f18bc2
3 changed files with 30 additions and 18 deletions

View file

@ -640,14 +640,26 @@ export class ExternalRequest<T extends Operation> {
) {
continue
}
const isMany = field.relationshipType === RelationshipType.MANY_TO_MANY
const tableId = isMany ? field.through! : field.tableId!
let tableId: string | undefined,
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)
// @ts-ignore
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]) {
continue
}
@ -660,9 +672,12 @@ export class ExternalRequest<T extends Operation> {
},
})
// this is the response from knex if no rows found
const rows: Row[] = response?.[0].read ? [] : (response as Row[])
const storeTo = isMany ? field.throughFrom || linkPrimaryKey : fieldName
related[storeTo] = { rows, isMany, tableId }
const rows: Row[] =
!Array.isArray(response) || response?.[0].read ? [] : response
const storeTo = isManyToMany(field)
? field.throughFrom || linkPrimaryKey
: fieldName
related[storeTo] = { rows, isMany: isManyToMany(field), tableId }
}
return related
}

View file

@ -16,19 +16,17 @@ class CharSequence {
this.counters = [0]
}
get character() {
return this.counters.map(i => CharSequence.alphabet[i]).join("")
}
next() {
getCharacter(): string {
const char = this.counters.map(i => CharSequence.alphabet[i]).join("")
for (let i = this.counters.length - 1; i >= 0; i--) {
if (this.counters[i] < CharSequence.alphabet.length - 1) {
this.counters[i]++
return
return char
}
this.counters[i] = 0
}
this.counters.unshift(0)
return char
}
}
@ -49,8 +47,7 @@ export default class AliasTables {
if (this.aliases[tableName]) {
return this.aliases[tableName]
}
const char = this.charSeq.character
this.charSeq.next()
const char = this.charSeq.getCharacter()
this.aliases[tableName] = char
this.tableAliases[char] = tableName
return char

View file

@ -164,14 +164,14 @@ describe("Captures of real examples", () => {
it("should handle over 'z' max character alias", () => {
const tableNames = []
for (let i = 0; i < 100; i++) {
tableNames.push(generator.word())
tableNames.push(generator.guid())
}
const aliasing = new AliasTables(tableNames)
let alias: string = ""
for (let table of tableNames) {
alias = aliasing.getAlias(table)
}
expect(alias).toEqual("cu")
expect(alias).toEqual("cv")
})
})
})