Skip to content

Extract shared AbstractPostgresPersistenceAdapter for core (TODO A4) - #67

Merged
juhovuo merged 1 commit into
mainfrom
feature/core-postgres-base
Jul 5, 2026
Merged

juhovuo merged 1 commit into
mainfrom
feature/core-postgres-base

Conversation

@juhovuo

@juhovuo juhovuo commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

What

The third and largest adapter-base extraction (after digest #64 and journal #66) — the biggest single win, ~900 duplicated lines. Moves the ~40 pure-SQL core persistence methods into a shared AbstractPostgresPersistenceAdapter in @telia-oss/xjog-core-persistence, written once with $N positional SQL. core-pg/core-pglite keep only their driver layer.

Design — generic over ConnectionType

Core's methods take a connection?: ConnectionType (for transaction support), and withTransaction hands that connection to callers. To avoid changing that callback type, the base stays generic: AbstractPostgresPersistenceAdapter<ConnectionType> extends PersistenceAdapter<ConnectionType>, with a single abstract normalizer

protected abstract runQuery<T>(connection: ConnectionType | undefined, sql: string, params?: unknown[]): Promise<{ rows: T[]; rowCount: number }>;

Every SQL method keeps its existing (…, connection?) signature and calls this.runQuery(connection, '<$N sql>', [params]). Only the SQL is unified; each driver's connection type and withTransaction semantics are untouched.

Per driver

  • core-pg extends …<Pool | PoolClient>: runQuery(connection = this.pool, …) → rowCount ?? 0. Keeps connect, disconnect, withTransaction (BEGIN/COMMIT/ROLLBACK on a PoolClient), onDeathNote polling. pg-bind fully removed (dep + .d.ts).
  • core-pglite extends …<PGlite>: runQuery(connection = this.pool, …) → affectedRows ?? 0. Keeps connect/disconnect/withTransaction (native pool.transaction())/onDeathNote.
  • SELECT presence-checks branch on rows.length (never rowCount), since PGlite's affectedRows is undefined for SELECTs.
  • charts.state bytea: reads decode via the shared decodeBytea (util), writes use Buffer.from(JSON.stringify(state)) — both in the base.

Verification

The extracted base SQL is byte-identical to the SQL core-pglite already ran (pg's :name converted to the same $N), so the 22-test PGlitePersistenceAdapter suite passes unchanged and exercises the shared SQL that pg now runs too. Full-repo turbo run build test = 22/22 (core: 65 tests, same as baseline). lint clean. withTransaction callers confirmed to only pass the connection back into persistence methods. One pre-existing quirk deliberately preserved: countPausedCharts returns the raw driver value uncoerced. Changeset: minor for the three core packages.

Implements TODO item A4 — completes the pg/pglite dedup across all three families. Only A5 (real-Postgres smoke tests) remains in Track A.

Same pattern as the digest/journal extractions: the ~40 pure-SQL core
persistence methods (instances, charts, ongoing activities, deferred
events, external ids) are written once against $N positional
placeholders in an abstract base that stays generic over ConnectionType,
so core-pg and core-pglite keep their own connection type and
withTransaction signature unchanged. Each driver now only implements
runQuery plus its connection/transaction/death-note-polling layer.

State bytea decoding is unified via decodeBytea in the base instead of
being duplicated per driver. Removes pg-bind from core-pg now that all
queries use positional parameters.
@juhovuo
juhovuo merged commit f8a7acc into main Jul 5, 2026
1 check passed
@juhovuo
juhovuo deleted the feature/core-postgres-base branch July 5, 2026 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant