@@ -9,8 +9,11 @@ import { getDebugTableInfo } from '../replication/replication-utils.js';
99import { KEEPALIVE_STATEMENT , PUBLICATION_NAME } from '../replication/WalStream.js' ;
1010import * as types from '../types/types.js' ;
1111import { getApplicationName } from '../utils/application-name.js' ;
12+ import { PostgresTypeCache } from '../types/cache.js' ;
13+ import { CustomTypeRegistry , isKnownType } from '../types/registry.js' ;
1214
1315export class PostgresRouteAPIAdapter implements api . RouteAPI {
16+ private typeCache : PostgresTypeCache ;
1417 connectionTag : string ;
1518 // TODO this should probably be configurable one day
1619 publicationName = PUBLICATION_NAME ;
@@ -31,6 +34,7 @@ export class PostgresRouteAPIAdapter implements api.RouteAPI {
3134 connectionTag ?: string ,
3235 private config ?: types . ResolvedConnectionConfig
3336 ) {
37+ this . typeCache = new PostgresTypeCache ( config ?. typeRegistry ?? new CustomTypeRegistry ( ) , pool ) ;
3438 this . connectionTag = connectionTag ?? sync_rules . DEFAULT_TAG ;
3539 }
3640
@@ -297,6 +301,7 @@ LEFT JOIN (
297301 SELECT
298302 attrelid,
299303 attname,
304+ atttypid,
300305 format_type(atttypid, atttypmod) as data_type,
301306 (SELECT typname FROM pg_catalog.pg_type WHERE oid = atttypid) as pg_type,
302307 attnum,
@@ -311,6 +316,7 @@ LEFT JOIN (
311316)
312317GROUP BY schemaname, tablename, quoted_name`
313318 ) ;
319+ await this . typeCache . fetchTypesForSchema ( ) ;
314320 const rows = pgwire . pgwireRows ( results ) ;
315321
316322 let schemas : Record < string , service_types . DatabaseSchema > = { } ;
@@ -332,9 +338,11 @@ GROUP BY schemaname, tablename, quoted_name`
332338 if ( pg_type . startsWith ( '_' ) ) {
333339 pg_type = `${ pg_type . substring ( 1 ) } []` ;
334340 }
341+
342+ const knownType = this . typeCache . registry . lookupType ( Number ( column . atttypid ) ) ;
335343 table . columns . push ( {
336344 name : column . attname ,
337- sqlite_type : sync_rules . expressionTypeFromPostgresType ( pg_type ) . typeFlags ,
345+ sqlite_type : sync_rules . ExpressionType . fromTypeText ( knownType . sqliteType ( ) ) . typeFlags ,
338346 type : column . data_type ,
339347 internal_type : column . data_type ,
340348 pg_type : pg_type
0 commit comments