Skip to content

Commit d10c33e

Browse files
committed
Cleanup
1 parent ed16df0 commit d10c33e

File tree

6 files changed

+17
-19
lines changed

6 files changed

+17
-19
lines changed

modules/module-postgres/src/api/PostgresRouteAPIAdapter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import { getDebugTableInfo } from '../replication/replication-utils.js';
99
import { KEEPALIVE_STATEMENT, PUBLICATION_NAME } from '../replication/WalStream.js';
1010
import * as types from '../types/types.js';
1111
import { getApplicationName } from '../utils/application-name.js';
12-
import { PostgresTypeCache } from '../types/cache.js';
13-
import { CustomTypeRegistry, isKnownType } from '../types/registry.js';
12+
import { CustomTypeRegistry } from '../types/registry.js';
13+
import { PostgresTypeResolver } from '../types/resolver.js';
1414

1515
export class PostgresRouteAPIAdapter implements api.RouteAPI {
16-
private typeCache: PostgresTypeCache;
16+
private typeCache: PostgresTypeResolver;
1717
connectionTag: string;
1818
// TODO this should probably be configurable one day
1919
publicationName = PUBLICATION_NAME;
@@ -34,7 +34,7 @@ export class PostgresRouteAPIAdapter implements api.RouteAPI {
3434
connectionTag?: string,
3535
private config?: types.ResolvedConnectionConfig
3636
) {
37-
this.typeCache = new PostgresTypeCache(config?.typeRegistry ?? new CustomTypeRegistry(), pool);
37+
this.typeCache = new PostgresTypeResolver(config?.typeRegistry ?? new CustomTypeRegistry(), pool);
3838
this.connectionTag = connectionTag ?? sync_rules.DEFAULT_TAG;
3939
}
4040

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as pgwire from '@powersync/service-jpgwire';
22
import semver from 'semver';
33
import { NormalizedPostgresConnectionConfig } from '../types/types.js';
44
import { getApplicationName } from '../utils/application-name.js';
5-
import { PostgresTypeCache } from '../types/cache.js';
5+
import { PostgresTypeResolver } from '../types/resolver.js';
66
import { getServerVersion } from '../utils/postgres_version.js';
77
import { CustomTypeRegistry } from '../types/registry.js';
88

@@ -21,7 +21,7 @@ export class PgManager {
2121
*/
2222
public readonly pool: pgwire.PgClient;
2323

24-
public readonly types: PostgresTypeCache;
24+
public readonly types: PostgresTypeResolver;
2525

2626
private connectionPromises: Promise<pgwire.PgConnection>[] = [];
2727

@@ -31,7 +31,7 @@ export class PgManager {
3131
) {
3232
// The pool is lazy - no connections are opened until a query is performed.
3333
this.pool = pgwire.connectPgWirePool(this.options, poolOptions);
34-
this.types = new PostgresTypeCache(poolOptions.registry, this.pool);
34+
this.types = new PostgresTypeResolver(poolOptions.registry, this.pool);
3535
}
3636

3737
public get connectionTag() {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,3 @@ export class CustomTypeRegistry {
276276
}
277277
}
278278
}
279-
280-
export function isKnownType(type: MaybeKnownType): type is KnownType {
281-
return type.type != 'unknown';
282-
}

modules/module-postgres/src/types/cache.ts renamed to modules/module-postgres/src/types/resolver.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import semver from 'semver';
55
import { getServerVersion } from '../utils/postgres_version.js';
66

77
/**
8-
* A cache of custom types for which information can be crawled from the source database.
8+
* Resolves descriptions used to decode values for custom postgres types.
9+
*
10+
* Custom types are resolved from the source database, which also involves crawling inner types (e.g. for composites).
911
*/
10-
export class PostgresTypeCache {
12+
export class PostgresTypeResolver {
1113
private cachedVersion: semver.SemVer | null = null;
1214

1315
constructor(
@@ -31,7 +33,7 @@ export class PostgresTypeCache {
3133
*/
3234
async supportsMultiRanges() {
3335
const version = await this.fetchVersion();
34-
return version.compare(PostgresTypeCache.minVersionForMultirange) >= 0;
36+
return version.compare(PostgresTypeResolver.minVersionForMultirange) >= 0;
3537
}
3638

3739
/**
@@ -138,7 +140,7 @@ WHERE t.oid = ANY($1)
138140
}
139141

140142
/**
141-
* Used for testing - fetches all custom types referenced by any column in the database.
143+
* Crawls all custom types referenced by table columns in the current database.
142144
*/
143145
public async fetchTypesForSchema() {
144146
const sql = `

modules/module-postgres/test/src/pg_test.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { describe, expect, test } from 'vitest';
1111
import { clearTestDb, connectPgPool, connectPgWire, TEST_URI } from './util.js';
1212
import { WalStream } from '@module/replication/WalStream.js';
13-
import { PostgresTypeCache } from '@module/types/cache.js';
13+
import { PostgresTypeResolver } from '@module/types/resolver.js';
1414
import { CustomTypeRegistry } from '@module/types/registry.js';
1515

1616
describe('pg data types', () => {
@@ -551,7 +551,7 @@ INSERT INTO test_data(id, time, timestamp, timestamptz) VALUES (1, '17:42:01.12'
551551
test('test replication - multiranges', async () => {
552552
const db = await connectPgPool();
553553

554-
if (!(await new PostgresTypeCache(new CustomTypeRegistry(), db).supportsMultiRanges())) {
554+
if (!(await new PostgresTypeResolver(new CustomTypeRegistry(), db).supportsMultiRanges())) {
555555
// This test requires Postgres 14 or later.
556556
return;
557557
}
@@ -620,7 +620,7 @@ INSERT INTO test_data(id, time, timestamp, timestamptz) VALUES (1, '17:42:01.12'
620620
* Return all the inserts from the first transaction in the replication stream.
621621
*/
622622
async function getReplicationTx(db: pgwire.PgClient, replicationStream: pgwire.ReplicationStream) {
623-
const typeCache = new PostgresTypeCache(new CustomTypeRegistry(), db);
623+
const typeCache = new PostgresTypeResolver(new CustomTypeRegistry(), db);
624624
await typeCache.fetchTypesForSchema();
625625

626626
let transformed: SqliteInputRow[] = [];

packages/jpgwire/src/pgwire_types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class PgType {
156156

157157
static _decodeArray(text: string, elemTypeOid: number): DatabaseInputValue[] {
158158
text = text.replace(/^\[.+=/, ''); // skip dimensions
159-
return new StructureParser(text).parseArray((raw) => (raw == null ? null : PgType.decode(raw, elemTypeOid)));
159+
return new StructureParser(text).parseArray((raw) => PgType.decode(raw, elemTypeOid));
160160
}
161161

162162
static _decodeBytea(text: string): Uint8Array {

0 commit comments

Comments
 (0)