Skip to content

Extract shared AbstractPostgresJournalPersistenceAdapter + decodeBytea (TODO A3) - #66

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

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

Conversation

@juhovuo

@juhovuo juhovuo commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

What

Second of the three adapter-base extractions (after digest A2 #64). Moves all pure-SQL journal methods into a shared AbstractPostgresJournalPersistenceAdapter in @telia-oss/xjog-journal-persistence, written once with $N positional SQL, and adds the shared decodeBytea helper to @telia-oss/xjog-util.

Details

  • Role-based query hooks (not a single shim) to preserve journal-pg's 4-connection topology: runWriteQuery (insertEntry, updateFullState), runUpdateQuery (emitJournalEntryNotification, deleteByChart), runReadQuery (readEntry, queryEntries, readFullState, queryFullStates, getCurrentTime). journal-pg maps these to its write/update/read Clients; journal-pglite maps all three to its single connection. The 4 new Client(...) connections, connect/disconnect, pg-listen subscription, and notification→re-query loop are unchanged.
  • Canonical SQL = pglite (the Fix correctness bugs and remove dead code across core and adapters #55-corrected versions): dynamic $N numbering via the shared createPositionalParameters, and parameterized JOIN (VALUES …) for the (machineId, chartId) array queries. This removes journal-pg's pg-bind bind(...) and its readConnection.escapeLiteral(...) string interpolation (SQL-injection-safer). pg-bind dropped from journal-pg's deps.
  • decodeBytea(value: Buffer | Uint8Array): string (TextDecoder) in util, used uniformly in the base row parsers. This is the exact drift class Fix correctness bugs and remove dead code across core and adapters #55 fixed — String(uint8array) yields "123,34,…" for PGlite's Uint8Array, while Buffer coerces fine; the helper handles both.

⚠️ Two intentional behavior changes for pg (please review)

Both fall out of the "canonical = pglite" directive and fix latent pg bugs:

  1. fullJournalStates reads now include "ownerId". pg's full-state SELECT list was missing ownerId (pg wrote it but never read it back, so FullStateEntry.ownerId was undefined for pg). The base uses pglite's field list, which includes it. pglite behavior unchanged.
  2. event parsing is now null-guarded (row.event ? … : null). pg previously parsed event unconditionally; the guard is required for decodeBytea and matches pglite. (For a null event, the old pg path already produced null via JSON.parse(String(null)), so this is equivalent in practice.)

Verification

build + test green across journal-persistence/pg/pglite/util; the pglite regression suite (repeated-record() append + full-state advance, and the chart-reference array-query) passes unchanged; full-repo turbo run build test = 22/22. lint clean. Changeset: minor for util + the three journal packages.

Implements TODO item A3. (A4 — the core family — is gated on this merging.)

Mirrors the digest A2 pattern: journal-pg and journal-pglite now share
one implementation of insertEntry, updateFullState, readEntry,
queryEntries, readFullState, queryFullStates, deleteByChart,
getCurrentTime, emitJournalEntryNotification and the row parsers,
written once against $N positional SQL in journal-persistence.

journal-pg keeps its 4-connection topology; the base routes to it via
three role-based hooks (runWriteQuery/runUpdateQuery/runReadQuery)
instead of a single query runner. The pg adapter's pg-bind bind(...)
calls and escapeLiteral string interpolation are replaced by the
parameterized VALUES-list approach already used in journal-pglite, and
pg-bind is dropped from journal-pg's dependencies.

Adds a shared decodeBytea helper to xjog-util so bytea columns are
decoded consistently across both drivers (pg's Buffer and PGlite's
Uint8Array), fixing the same String()-coercion drift previously fixed
for digests. Also fixes a latent bug where journal-pg's full-state SELECT
omitted the ownerId column.
@juhovuo
juhovuo merged commit 6fda287 into main Jul 5, 2026
1 check passed
@juhovuo
juhovuo deleted the feature/journal-postgres-base branch July 5, 2026 00:09
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