@pgxsinkit/client 0.1.50 → 0.1.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/sync/tags.ts CHANGED
@@ -8,7 +8,7 @@ import { quoteIdentifier, quoteSqlLiteral } from "@pgxsinkit/contracts";
8
8
  import type { ApplyTarget } from "../local-tables";
9
9
  import { applyBulkDeletesToTable } from "./apply";
10
10
  import { drizzleOverPg } from "./drizzle-executor";
11
- import { getMetadataTables } from "./metadata-tables";
11
+ import { getMetadataTables, getSessionMetadataTables, pickMetadataTables } from "./metadata-tables";
12
12
 
13
13
  /**
14
14
  * Electric **tagged-subquery** reconciliation (ADR-0023). A shape whose `where` contains a subquery
@@ -74,6 +74,16 @@ export function shapeRowTagsTableName(metadataSchema: string): string {
74
74
  return `${quoteIdentifier(config.schema ?? "public")}.${quoteIdentifier(config.name)}`;
75
75
  }
76
76
 
77
+ /**
78
+ * The SESSION (`pg_temp`) tag store's qualified identifier (ADR-0042), derived from the session
79
+ * `shape_row_tags` pgTable config — the same relation name, in `pg_temp`. Used by the scope-blind
80
+ * {@link buildClearShapeTagsSql} so a desync/discard clears an ephemeral shape's tags wherever they live.
81
+ */
82
+ export function shapeRowTagsSessionTableName(): string {
83
+ const config = getTableConfig(getSessionMetadataTables().shapeRowTags);
84
+ return `${quoteIdentifier(config.schema ?? "public")}.${quoteIdentifier(config.name)}`;
85
+ }
86
+
77
87
  /** The `shape_table` key for the tag store: the synced table, schema-qualified (defaults `public`). */
78
88
  export function shapeTableId(schema: string | undefined, table: string): string {
79
89
  return `${schema ?? "public"}.${table}`;
@@ -87,11 +97,16 @@ export function shapeTableId(schema: string | undefined, table: string): string
87
97
  * guarded.
88
98
  */
89
99
  export function buildClearShapeTagsSql(shapeTable: string, metadataSchema: string = DEFAULT_METADATA_SCHEMA): string {
90
- const table = shapeRowTagsTableName(metadataSchema);
91
- return `DO $$ BEGIN
92
- IF to_regclass(${quoteSqlLiteral(table)}) IS NOT NULL THEN
100
+ // ADR-0042: SCOPE-BLIND — clear the shape's tags from BOTH the durable and the session (`pg_temp`) tag
101
+ // store, each guarded independently by `to_regclass` (a desync/discard may run on a persistent table whose
102
+ // session store never existed, or standalone before any engine boot). `to_regclass('pg_temp.shape_row_tags')`
103
+ // resolves the session table via the per-session `pg_temp` alias (probed on real PGlite).
104
+ const clearOne = (table: string) => ` IF to_regclass(${quoteSqlLiteral(table)}) IS NOT NULL THEN
93
105
  DELETE FROM ${table} WHERE shape_table = ${quoteSqlLiteral(shapeTable)};
94
- END IF;
106
+ END IF;`;
107
+ return `DO $$ BEGIN
108
+ ${clearOne(shapeRowTagsTableName(metadataSchema))}
109
+ ${clearOne(shapeRowTagsSessionTableName())}
95
110
  END $$;`;
96
111
  }
97
112
 
@@ -158,6 +173,8 @@ interface TagSyncOptions {
158
173
  shapeTable: string;
159
174
  messages: ChangeMessage<Row<unknown>>[];
160
175
  primaryKey: string[];
176
+ /** ADR-0042: route the tag DML to the SESSION (`pg_temp`) store for an ephemeral group. */
177
+ sessionScoped?: boolean;
161
178
  }
162
179
 
163
180
  /**
@@ -173,8 +190,9 @@ export async function applyShapeTagSync({
173
190
  shapeTable,
174
191
  messages,
175
192
  primaryKey,
193
+ sessionScoped = false,
176
194
  }: TagSyncOptions): Promise<void> {
177
- const { shapeRowTags } = getMetadataTables(metadataSchema);
195
+ const { shapeRowTags } = pickMetadataTables(metadataSchema, sessionScoped);
178
196
  const db = drizzleOverPg(pg);
179
197
 
180
198
  for (const message of messages) {
@@ -233,8 +251,9 @@ export async function addShapeRowTags({
233
251
  shapeTable,
234
252
  messages,
235
253
  primaryKey,
254
+ sessionScoped = false,
236
255
  }: TagSyncOptions): Promise<void> {
237
- const { shapeRowTags } = getMetadataTables(metadataSchema);
256
+ const { shapeRowTags } = pickMetadataTables(metadataSchema, sessionScoped);
238
257
  const db = drizzleOverPg(pg);
239
258
 
240
259
  for (const message of messages) {
@@ -259,6 +278,8 @@ interface MoveOutOptions {
259
278
  /** One entry per buffered `move-out` event, each its own pattern set. */
260
279
  patternSets: MovePattern[][];
261
280
  debug: boolean;
281
+ /** ADR-0042: route the tag reads/evictions to the SESSION (`pg_temp`) store for an ephemeral group. */
282
+ sessionScoped?: boolean;
262
283
  }
263
284
 
264
285
  /**
@@ -274,8 +295,9 @@ export async function applyShapeMoveOut({
274
295
  target,
275
296
  patternSets,
276
297
  debug,
298
+ sessionScoped = false,
277
299
  }: MoveOutOptions): Promise<number> {
278
- const { shapeRowTags } = getMetadataTables(metadataSchema);
300
+ const { shapeRowTags } = pickMetadataTables(metadataSchema, sessionScoped);
279
301
  const db = drizzleOverPg(pg);
280
302
  const evictedPkJsons = new Set<string>();
281
303
 
@@ -355,11 +377,14 @@ export async function clearShapeTags({
355
377
  pg,
356
378
  metadataSchema,
357
379
  shapeTable,
380
+ sessionScoped = false,
358
381
  }: {
359
382
  pg: PGliteInterface | Transaction;
360
383
  metadataSchema: string;
361
384
  shapeTable: string;
385
+ /** ADR-0042: clear the SESSION (`pg_temp`) tag store for an ephemeral group's must-refetch rebuild. */
386
+ sessionScoped?: boolean;
362
387
  }): Promise<void> {
363
- const { shapeRowTags } = getMetadataTables(metadataSchema);
388
+ const { shapeRowTags } = pickMetadataTables(metadataSchema, sessionScoped);
364
389
  await drizzleOverPg(pg).delete(shapeRowTags).where(eq(shapeRowTags.shape_table, shapeTable));
365
390
  }
package/src/sync/types.ts CHANGED
@@ -51,6 +51,16 @@ export interface SyncShapesToTablesOptions {
51
51
  /** The engine's per-table spec source (ADR-0029 D1): every shape's `tableKey` resolves against it. */
52
52
  registry: SyncTableRegistry;
53
53
  shapes: Record<string, ShapeToTableOptions>;
54
+ /**
55
+ * ADR-0042: this group's sync bookkeeping (subscription cursor + tagged-subquery reason sets) is
56
+ * SESSION-scoped — stored in the engine's `pg_temp` metadata relations that die with the engine, not the
57
+ * durable ones. Set for an `ephemeral`-retention group, whose whole cluster is already TEMP: the cursor
58
+ * and tags then die with the rows they index, so a returning engine re-streams the shape from scratch
59
+ * instead of resuming a stale durable cursor over a recreated-empty TEMP cluster. Default `false` — every
60
+ * persistent group's storage is byte-identical to before. One storage-scope bit; the engine never learns
61
+ * the retention model.
62
+ */
63
+ sessionScoped?: boolean | undefined;
54
64
  initialInsertMethod?: InitialInsertMethod | undefined;
55
65
  onInitialSync?: (() => void) | undefined;
56
66
  /**
@@ -113,6 +123,8 @@ export interface SyncShapeToTableOptions {
113
123
  registry: SyncTableRegistry;
114
124
  tableKey: string;
115
125
  shapeKey: string | null;
126
+ /** ADR-0042: SESSION-scope this shape's cursor + tags (ephemeral retention); see {@link SyncShapesToTablesOptions.sessionScoped}. */
127
+ sessionScoped?: boolean | undefined;
116
128
  initialInsertMethod?: InitialInsertMethod | undefined;
117
129
  onInitialSync?: (() => void) | undefined;
118
130
  onError?: ((error: FetchError | Error) => void) | undefined;