Skip to content

Commit 36c6cea

Browse files
refactor(postgres): refactor: optimize table metadata parsing in PostgresIntrospector
Co-authored-by: Rúben Ferreira <43851289+rubenferreira97@users.noreply.github.com>
1 parent 74e6cde commit 36c6cea

1 file changed

Lines changed: 25 additions & 22 deletions

File tree

src/dialect/postgres/postgres-introspector.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -97,29 +97,32 @@ export class PostgresIntrospector implements DatabaseIntrospector {
9797
}
9898

9999
#parseTableMetadata(columns: RawColumnMetadata[]): TableMetadata[] {
100-
const tableDictionary: Map<string, TableMetadata> = new Map()
101-
102-
for (let i = 0, len = columns.length; i < len; i++) {
103-
const column = columns[i]
104-
105-
const { schema, table } = column
106-
107-
const tableKey = `schema:${schema};table:${table}`
108-
109-
if (!tableDictionary.has(tableKey)) {
110-
tableDictionary.set(
111-
tableKey,
112-
freeze({
113-
columns: [],
114-
isForeign: column.table_type === 'f',
115-
isView: column.table_type === 'v',
116-
name: table,
117-
schema,
118-
}),
119-
)
100+
const tables: TableMetadata[] = []
101+
const schemas = new Map<string, Map<string, TableMetadata>>()
102+
103+
for (const column of columns) {
104+
let schema = schemas.get(column.schema)
105+
106+
if (!schema) {
107+
schema = new Map<string, TableMetadata>()
108+
schemas.set(column.schema, schema)
109+
}
110+
111+
let table = schema.get(column.table)
112+
113+
if (!table) {
114+
table = freeze({
115+
columns: [],
116+
isForeign: column.table_type === 'f',
117+
isView: column.table_type === 'v',
118+
name: column.table,
119+
schema: column.schema,
120+
})
121+
schema.set(column.table, table)
122+
tables.push(table)
120123
}
121124

122-
tableDictionary.get(tableKey)!.columns.push(
125+
table.columns.push(
123126
freeze({
124127
comment: column.column_description ?? undefined,
125128
dataType: column.type,
@@ -132,7 +135,7 @@ export class PostgresIntrospector implements DatabaseIntrospector {
132135
)
133136
}
134137

135-
return Array.from(tableDictionary.values())
138+
return tables
136139
}
137140
}
138141

0 commit comments

Comments
 (0)