Skip to content

Commit 49e070c

Browse files
committed
Fix: Add getAvailableValues for enums to Postgres parser;
1 parent b82a013 commit 49e070c

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

src/dialects/postgres.parser.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const pgArrayAggToArray = (agg: string) => agg.replace(/{/g, '').replace(/}/g, '
99

1010
const getColumnType = (dbType: string): PropertyType => {
1111
switch (dbType) {
12-
case 'uuid': return 'uuid';
12+
case 'USER-DEFINED':
13+
return 'mixed';
14+
case 'uuid':
15+
return 'uuid';
1316
case 'bigint':
1417
case 'int8':
1518
case 'bigserial':
@@ -58,17 +61,6 @@ const getColumnType = (dbType: string): PropertyType => {
5861
}
5962
};
6063

61-
const getColumnInfo = (column: Record<string, number | string>): ColumnInfo => ({
62-
name: column.column_name as string,
63-
isId: column.key_type === 'PRIMARY KEY',
64-
position: column.ordinal_position as number,
65-
defaultValue: column.column_default,
66-
isNullable: column.is_nullable === 'YES',
67-
isEditable: column.is_updatable === 'YES',
68-
type: column.referenced_table ? 'reference' : getColumnType(column.data_type as string),
69-
referencedTable: (column.referenced_table ?? null) as string | null,
70-
});
71-
7264
export class PostgresParser extends BaseDatabaseParser {
7365
public static dialects = ['postgresql' as const];
7466

@@ -177,7 +169,7 @@ export class PostgresParser extends BaseDatabaseParser {
177169

178170
const relations = await relQuery;
179171

180-
return columns.map((col) => {
172+
return Promise.all(columns.map(async (col) => {
181173
const rel = relations.rows.find((r) => {
182174
const cols = pgArrayAggToArray(r.col);
183175
if (cols.length > 1) return null; // AdminJS doesn't support multiple foreign keys
@@ -189,7 +181,35 @@ export class PostgresParser extends BaseDatabaseParser {
189181
col.referenced_table = rel.referenced_table;
190182
}
191183

192-
return new Property(getColumnInfo(col));
193-
});
184+
return new Property(await this.getColumnInfo(col));
185+
}));
186+
}
187+
188+
189+
async getColumnInfo(column: Record<string, number | string>): Promise<ColumnInfo> {
190+
return {
191+
name: column.column_name as string,
192+
isId: column.key_type === 'PRIMARY KEY',
193+
position: column.ordinal_position as number,
194+
defaultValue: column.column_default,
195+
isNullable: column.is_nullable === 'YES',
196+
isEditable: column.is_updatable === 'YES',
197+
type: column.referenced_table ? ('reference' as PropertyType) : getColumnType(column.data_type as string),
198+
referencedTable: (column.referenced_table ?? null) as string | null,
199+
availableValues: await this.getAvailableValues(column),
200+
}
201+
}
202+
203+
async getAvailableValues(column: Record<string, number | string>): Promise<string[] | null> {
204+
if (column.data_type !== 'USER-DEFINED' || !column.udt_name) {
205+
return null;
206+
}
207+
const query = this.knex
208+
.from('pg_catalog.pg_enum as e')
209+
.select('e.enumlabel')
210+
.leftJoin('pg_catalog.pg_type as t', (c) => c.on('e.enumtypid', 't.oid'))
211+
.where('t.typname', column.udt_name);
212+
const labels = await query;
213+
return labels.map((l) => l.enumlabel as string)
194214
}
195215
}

0 commit comments

Comments
 (0)