1
0
Fork 0
mirror of synced 2024-09-12 23:43:09 +12:00

Optimise the reverse method in sqlAlias.ts.

This commit is contained in:
Sam Rose 2024-05-21 15:49:48 +01:00
parent 5247aa6900
commit 7bbb920aa7
No known key found for this signature in database

View file

@ -126,16 +126,25 @@ export default class AliasTables {
} }
reverse<T extends Row | Row[]>(rows: T): T { reverse<T extends Row | Row[]>(rows: T): T {
const mapping = new Map()
const process = (row: Row) => { const process = (row: Row) => {
const final: Row = {} const final: Row = {}
for (let [key, value] of Object.entries(row)) { for (const key of Object.keys(row)) {
if (!key.includes(".")) { let mappedKey = mapping.get(key)
final[key] = value if (!mappedKey) {
} else { const dotLocation = key.indexOf(".")
const [alias, column] = key.split(".") if (dotLocation === -1) {
const tableName = this.tableAliases[alias] || alias mappedKey = key
final[`${tableName}.${column}`] = value } else {
const alias = key.slice(0, dotLocation)
const column = key.slice(dotLocation + 1)
const tableName = this.tableAliases[alias] || alias
mappedKey = `${tableName}.${column}`
}
mapping.set(key, mappedKey)
} }
final[mappedKey] = row[key]
} }
return final return final
} }