Skip to content

Commit b22e410

Browse files
committed
Move cache up
1 parent db0151b commit b22e410

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

modules/module-postgres/src/module/PostgresModule.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ import { WalStreamReplicator } from '../replication/WalStreamReplicator.js';
1919
import * as types from '../types/types.js';
2020
import { PostgresConnectionConfig } from '../types/types.js';
2121
import { getApplicationName } from '../utils/application-name.js';
22+
import { CustomTypeRegistry } from '../types/registry.js';
2223

2324
export class PostgresModule extends replication.ReplicationModule<types.PostgresConnectionConfig> {
25+
private customTypes: CustomTypeRegistry = new CustomTypeRegistry();
26+
2427
constructor() {
2528
super({
2629
name: 'Postgres',
@@ -48,7 +51,7 @@ export class PostgresModule extends replication.ReplicationModule<types.Postgres
4851
protected createReplicator(context: system.ServiceContext): replication.AbstractReplicator {
4952
const normalisedConfig = this.resolveConfig(this.decodedConfig!);
5053
const syncRuleProvider = new ConfigurationFileSyncRulesProvider(context.configuration.sync_rules);
51-
const connectionFactory = new ConnectionManagerFactory(normalisedConfig);
54+
const connectionFactory = new ConnectionManagerFactory(normalisedConfig, this.customTypes);
5255

5356
return new WalStreamReplicator({
5457
id: this.getDefaultId(normalisedConfig.database),
@@ -75,7 +78,8 @@ export class PostgresModule extends replication.ReplicationModule<types.Postgres
7578
const connectionManager = new PgManager(normalisedConfig, {
7679
idleTimeout: 30_000,
7780
maxSize: 1,
78-
applicationName: getApplicationName()
81+
applicationName: getApplicationName(),
82+
registry: this.customTypes
7983
});
8084

8185
try {
@@ -106,7 +110,8 @@ export class PostgresModule extends replication.ReplicationModule<types.Postgres
106110
const connectionManager = new PgManager(normalizedConfig, {
107111
idleTimeout: 30_000,
108112
maxSize: 1,
109-
applicationName: getApplicationName()
113+
applicationName: getApplicationName(),
114+
registry: new CustomTypeRegistry()
110115
});
111116
const connection = await connectionManager.snapshotConnection();
112117
try {

modules/module-postgres/src/replication/ConnectionManagerFactory.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ import { PgManager } from './PgManager.js';
22
import { NormalizedPostgresConnectionConfig } from '../types/types.js';
33
import { PgPoolOptions } from '@powersync/service-jpgwire';
44
import { logger } from '@powersync/lib-services-framework';
5+
import { CustomTypeRegistry } from '../types/registry.js';
56

67
export class ConnectionManagerFactory {
78
private readonly connectionManagers: PgManager[];
89
public readonly dbConnectionConfig: NormalizedPostgresConnectionConfig;
910

10-
constructor(dbConnectionConfig: NormalizedPostgresConnectionConfig) {
11+
constructor(
12+
dbConnectionConfig: NormalizedPostgresConnectionConfig,
13+
private readonly registry: CustomTypeRegistry
14+
) {
1115
this.dbConnectionConfig = dbConnectionConfig;
1216
this.connectionManagers = [];
1317
}
1418

1519
create(poolOptions: PgPoolOptions) {
16-
const manager = new PgManager(this.dbConnectionConfig, poolOptions);
20+
const manager = new PgManager(this.dbConnectionConfig, { ...poolOptions, registry: this.registry });
1721
this.connectionManagers.push(manager);
1822
return manager;
1923
}

modules/module-postgres/src/replication/PgManager.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { NormalizedPostgresConnectionConfig } from '../types/types.js';
44
import { getApplicationName } from '../utils/application-name.js';
55
import { PostgresTypeCache } from '../types/cache.js';
66
import { getServerVersion } from '../utils/postgres_version.js';
7+
import { CustomTypeRegistry } from '../types/registry.js';
8+
9+
export interface PgManagerOptions extends pgwire.PgPoolOptions {
10+
registry: CustomTypeRegistry;
11+
}
712

813
/**
914
* Shorter timeout for snapshot connections than for replication connections.
@@ -22,11 +27,11 @@ export class PgManager {
2227

2328
constructor(
2429
public options: NormalizedPostgresConnectionConfig,
25-
public poolOptions: pgwire.PgPoolOptions
30+
public poolOptions: PgManagerOptions
2631
) {
2732
// The pool is lazy - no connections are opened until a query is performed.
2833
this.pool = pgwire.connectPgWirePool(this.options, poolOptions);
29-
this.types = new PostgresTypeCache(this.pool, () => this.getServerVersion());
34+
this.types = new PostgresTypeCache(poolOptions.registry, this.pool, () => this.getServerVersion());
3035
}
3136

3237
public get connectionTag() {

modules/module-postgres/src/types/cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import semver from 'semver';
77
* A cache of custom types for which information can be crawled from the source database.
88
*/
99
export class PostgresTypeCache {
10-
readonly registry: CustomTypeRegistry;
1110
private cachedVersion: semver.SemVer | null = null;
1211

1312
constructor(
13+
readonly registry: CustomTypeRegistry,
1414
private readonly pool: pgwire.PgClient,
1515
private readonly getVersion: () => Promise<semver.SemVer | null>
1616
) {

0 commit comments

Comments
 (0)