@@ -9,8 +9,11 @@ import { getDebugTableInfo } from '../replication/replication-utils.js';
9
9
import { KEEPALIVE_STATEMENT , PUBLICATION_NAME } from '../replication/WalStream.js' ;
10
10
import * as types from '../types/types.js' ;
11
11
import { getApplicationName } from '../utils/application-name.js' ;
12
+ import { PostgresTypeCache } from '../types/cache.js' ;
13
+ import { CustomTypeRegistry , isKnownType } from '../types/registry.js' ;
12
14
13
15
export class PostgresRouteAPIAdapter implements api . RouteAPI {
16
+ private typeCache : PostgresTypeCache ;
14
17
connectionTag : string ;
15
18
// TODO this should probably be configurable one day
16
19
publicationName = PUBLICATION_NAME ;
@@ -31,6 +34,7 @@ export class PostgresRouteAPIAdapter implements api.RouteAPI {
31
34
connectionTag ?: string ,
32
35
private config ?: types . ResolvedConnectionConfig
33
36
) {
37
+ this . typeCache = new PostgresTypeCache ( config ?. typeRegistry ?? new CustomTypeRegistry ( ) , pool ) ;
34
38
this . connectionTag = connectionTag ?? sync_rules . DEFAULT_TAG ;
35
39
}
36
40
@@ -297,6 +301,7 @@ LEFT JOIN (
297
301
SELECT
298
302
attrelid,
299
303
attname,
304
+ atttypid,
300
305
format_type(atttypid, atttypmod) as data_type,
301
306
(SELECT typname FROM pg_catalog.pg_type WHERE oid = atttypid) as pg_type,
302
307
attnum,
@@ -311,6 +316,7 @@ LEFT JOIN (
311
316
)
312
317
GROUP BY schemaname, tablename, quoted_name`
313
318
) ;
319
+ await this . typeCache . fetchTypesForSchema ( ) ;
314
320
const rows = pgwire . pgwireRows ( results ) ;
315
321
316
322
let schemas : Record < string , service_types . DatabaseSchema > = { } ;
@@ -332,9 +338,11 @@ GROUP BY schemaname, tablename, quoted_name`
332
338
if ( pg_type . startsWith ( '_' ) ) {
333
339
pg_type = `${ pg_type . substring ( 1 ) } []` ;
334
340
}
341
+
342
+ const knownType = this . typeCache . registry . lookupType ( Number ( column . atttypid ) ) ;
335
343
table . columns . push ( {
336
344
name : column . attname ,
337
- sqlite_type : sync_rules . expressionTypeFromPostgresType ( pg_type ) . typeFlags ,
345
+ sqlite_type : sync_rules . ExpressionType . fromTypeText ( knownType . sqliteType ( ) ) . typeFlags ,
338
346
type : column . data_type ,
339
347
internal_type : column . data_type ,
340
348
pg_type : pg_type
0 commit comments