Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add getAvailableValues for enum to Postgres parser; #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions src/dialects/postgres.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const pgArrayAggToArray = (agg: string) => agg.replace(/{/g, '').replace(/}/g, '

const getColumnType = (dbType: string): PropertyType => {
switch (dbType) {
case 'uuid': return 'uuid';
case 'USER-DEFINED':
return 'mixed';
case 'uuid':
return 'uuid';
case 'bigint':
case 'int8':
case 'bigserial':
Expand Down Expand Up @@ -58,17 +61,6 @@ const getColumnType = (dbType: string): PropertyType => {
}
};

const getColumnInfo = (column: Record<string, number | string>): ColumnInfo => ({
name: column.column_name as string,
isId: column.key_type === 'PRIMARY KEY',
position: column.ordinal_position as number,
defaultValue: column.column_default,
isNullable: column.is_nullable === 'YES',
isEditable: column.is_updatable === 'YES',
type: column.referenced_table ? 'reference' : getColumnType(column.data_type as string),
referencedTable: (column.referenced_table ?? null) as string | null,
});

export class PostgresParser extends BaseDatabaseParser {
public static dialects = ['postgresql' as const];

Expand Down Expand Up @@ -177,7 +169,7 @@ export class PostgresParser extends BaseDatabaseParser {

const relations = await relQuery;

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

return new Property(getColumnInfo(col));
});
return new Property(await this.getColumnInfo(col));
}));
}


async getColumnInfo(column: Record<string, number | string>): Promise<ColumnInfo> {
return {
name: column.column_name as string,
isId: column.key_type === 'PRIMARY KEY',
position: column.ordinal_position as number,
defaultValue: column.column_default,
isNullable: column.is_nullable === 'YES',
isEditable: column.is_updatable === 'YES',
type: column.referenced_table ? ('reference' as PropertyType) : getColumnType(column.data_type as string),
referencedTable: (column.referenced_table ?? null) as string | null,
availableValues: await this.getAvailableValues(column),
}
}

async getAvailableValues(column: Record<string, number | string>): Promise<string[] | null> {
if (column.data_type !== 'USER-DEFINED' || !column.udt_name) {
return null;
}
const query = this.knex
.from('pg_catalog.pg_enum as e')
.select('e.enumlabel')
.leftJoin('pg_catalog.pg_type as t', (c) => c.on('e.enumtypid', 't.oid'))
.where('t.typname', column.udt_name);
const labels = await query;
return labels.map((l) => l.enumlabel as string)
}
}