@@ -9,7 +9,10 @@ const pgArrayAggToArray = (agg: string) => agg.replace(/{/g, '').replace(/}/g, '
9
9
10
10
const getColumnType = ( dbType : string ) : PropertyType => {
11
11
switch ( dbType ) {
12
- case 'uuid' : return 'uuid' ;
12
+ case 'USER-DEFINED' :
13
+ return 'mixed' ;
14
+ case 'uuid' :
15
+ return 'uuid' ;
13
16
case 'bigint' :
14
17
case 'int8' :
15
18
case 'bigserial' :
@@ -58,17 +61,6 @@ const getColumnType = (dbType: string): PropertyType => {
58
61
}
59
62
} ;
60
63
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
-
72
64
export class PostgresParser extends BaseDatabaseParser {
73
65
public static dialects = [ 'postgresql' as const ] ;
74
66
@@ -177,7 +169,7 @@ export class PostgresParser extends BaseDatabaseParser {
177
169
178
170
const relations = await relQuery ;
179
171
180
- return columns . map ( ( col ) => {
172
+ return Promise . all ( columns . map ( async ( col ) => {
181
173
const rel = relations . rows . find ( ( r ) => {
182
174
const cols = pgArrayAggToArray ( r . col ) ;
183
175
if ( cols . length > 1 ) return null ; // AdminJS doesn't support multiple foreign keys
@@ -189,7 +181,35 @@ export class PostgresParser extends BaseDatabaseParser {
189
181
col . referenced_table = rel . referenced_table ;
190
182
}
191
183
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 )
194
214
}
195
215
}
0 commit comments