@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/dist/index.js CHANGED
@@ -1244,6 +1244,7 @@ async function applyBulkUpdatesToTable({ pg, target, messages, debug }) {
1244
1244
 
1245
1245
  // packages/client/src/sync/metadata-tables.ts
1246
1246
  import { index, jsonb, pgSchema, primaryKey, text } from "drizzle-orm/pg-core";
1247
+ var SESSION_METADATA_SCHEMA = "pg_temp";
1247
1248
  function buildSubscriptionsMetadata(schema) {
1248
1249
  return schema.table("subscriptions_metadata", {
1249
1250
  key: text("key").primaryKey(),
@@ -1274,6 +1275,20 @@ function getMetadataTables(metadataSchema) {
1274
1275
  }
1275
1276
  return tables;
1276
1277
  }
1278
+ var sessionCache;
1279
+ function getSessionMetadataTables() {
1280
+ if (!sessionCache) {
1281
+ const schema = pgSchema(SESSION_METADATA_SCHEMA);
1282
+ sessionCache = {
1283
+ subscriptionsMetadata: buildSubscriptionsMetadata(schema),
1284
+ shapeRowTags: buildShapeRowTags(schema)
1285
+ };
1286
+ }
1287
+ return sessionCache;
1288
+ }
1289
+ function pickMetadataTables(metadataSchema, sessionScoped) {
1290
+ return sessionScoped ? getSessionMetadataTables() : getMetadataTables(metadataSchema);
1291
+ }
1277
1292
 
1278
1293
  // packages/client/src/sync/tags.ts
1279
1294
  var DEFAULT_METADATA_SCHEMA = "pgxsinkit";
@@ -1287,15 +1302,20 @@ function shapeRowTagsTableName(metadataSchema) {
1287
1302
  const config = getTableConfig3(getMetadataTables(metadataSchema).shapeRowTags);
1288
1303
  return `${quoteIdentifier3(config.schema ?? "public")}.${quoteIdentifier3(config.name)}`;
1289
1304
  }
1305
+ function shapeRowTagsSessionTableName() {
1306
+ const config = getTableConfig3(getSessionMetadataTables().shapeRowTags);
1307
+ return `${quoteIdentifier3(config.schema ?? "public")}.${quoteIdentifier3(config.name)}`;
1308
+ }
1290
1309
  function shapeTableId(schema, table) {
1291
1310
  return `${schema ?? "public"}.${table}`;
1292
1311
  }
1293
1312
  function buildClearShapeTagsSql(shapeTable, metadataSchema = DEFAULT_METADATA_SCHEMA) {
1294
- const table = shapeRowTagsTableName(metadataSchema);
1295
- return `DO $$ BEGIN
1296
- IF to_regclass(${quoteSqlLiteral(table)}) IS NOT NULL THEN
1313
+ const clearOne = (table) => ` IF to_regclass(${quoteSqlLiteral(table)}) IS NOT NULL THEN
1297
1314
  DELETE FROM ${table} WHERE shape_table = ${quoteSqlLiteral(shapeTable)};
1298
- END IF;
1315
+ END IF;`;
1316
+ return `DO $$ BEGIN
1317
+ ${clearOne(shapeRowTagsTableName(metadataSchema))}
1318
+ ${clearOne(shapeRowTagsSessionTableName())}
1299
1319
  END $$;`;
1300
1320
  }
1301
1321
  function serializeRowPk(value, primaryKey2) {
@@ -1336,9 +1356,10 @@ async function applyShapeTagSync({
1336
1356
  metadataSchema,
1337
1357
  shapeTable,
1338
1358
  messages,
1339
- primaryKey: primaryKey2
1359
+ primaryKey: primaryKey2,
1360
+ sessionScoped = false
1340
1361
  }) {
1341
- const { shapeRowTags } = getMetadataTables(metadataSchema);
1362
+ const { shapeRowTags } = pickMetadataTables(metadataSchema, sessionScoped);
1342
1363
  const db = drizzleOverPg(pg);
1343
1364
  for (const message of messages) {
1344
1365
  const data = message.value;
@@ -1366,9 +1387,10 @@ async function addShapeRowTags({
1366
1387
  metadataSchema,
1367
1388
  shapeTable,
1368
1389
  messages,
1369
- primaryKey: primaryKey2
1390
+ primaryKey: primaryKey2,
1391
+ sessionScoped = false
1370
1392
  }) {
1371
- const { shapeRowTags } = getMetadataTables(metadataSchema);
1393
+ const { shapeRowTags } = pickMetadataTables(metadataSchema, sessionScoped);
1372
1394
  const db = drizzleOverPg(pg);
1373
1395
  for (const message of messages) {
1374
1396
  const tags = message.headers.tags;
@@ -1385,9 +1407,10 @@ async function applyShapeMoveOut({
1385
1407
  shapeTable,
1386
1408
  target,
1387
1409
  patternSets,
1388
- debug
1410
+ debug,
1411
+ sessionScoped = false
1389
1412
  }) {
1390
- const { shapeRowTags } = getMetadataTables(metadataSchema);
1413
+ const { shapeRowTags } = pickMetadataTables(metadataSchema, sessionScoped);
1391
1414
  const db = drizzleOverPg(pg);
1392
1415
  const evictedPkJsons = new Set;
1393
1416
  for (const patterns of patternSets) {
@@ -1426,9 +1449,10 @@ async function applyShapeMoveOut({
1426
1449
  async function clearShapeTags({
1427
1450
  pg,
1428
1451
  metadataSchema,
1429
- shapeTable
1452
+ shapeTable,
1453
+ sessionScoped = false
1430
1454
  }) {
1431
- const { shapeRowTags } = getMetadataTables(metadataSchema);
1455
+ const { shapeRowTags } = pickMetadataTables(metadataSchema, sessionScoped);
1432
1456
  await drizzleOverPg(pg).delete(shapeRowTags).where(eq2(shapeRowTags.shape_table, shapeTable));
1433
1457
  }
1434
1458
 
@@ -1719,17 +1743,19 @@ function buildTableColumnSql(columns, primaryKeyColumns, localSchema) {
1719
1743
  ].join(`,
1720
1744
  `);
1721
1745
  }
1722
- function renderCreateTableSql(table) {
1746
+ function renderCreateTableSql(table, options = {}) {
1723
1747
  const config = getTableConfig4(table);
1748
+ const temp = options.temp === true;
1724
1749
  const schemaName = config.schema ?? "public";
1725
- const qualifiedTableName = qualifyIdentifier(schemaName, config.name);
1750
+ const tableRef = temp ? maybeQuoteIdentifier(config.name) : qualifyIdentifier(schemaName, config.name);
1726
1751
  const primaryKeyColumns = collectPgTablePrimaryKeyColumns(config);
1727
1752
  const columnSql = buildTableColumnSql(config.columns, primaryKeyColumns, schemaName);
1728
- const statements = [`CREATE TABLE IF NOT EXISTS ${qualifiedTableName} (
1753
+ const createKeyword = temp ? "CREATE TEMP TABLE" : "CREATE TABLE";
1754
+ const statements = [`${createKeyword} IF NOT EXISTS ${tableRef} (
1729
1755
  ${columnSql}
1730
1756
  );`];
1731
1757
  for (const index2 of config.indexes) {
1732
- statements.push(renderCreateIndexSql(qualifiedTableName, index2));
1758
+ statements.push(renderCreateIndexSql(tableRef, index2));
1733
1759
  }
1734
1760
  return statements;
1735
1761
  }
@@ -4410,6 +4436,7 @@ async function startConfiguredSync(pg, input) {
4410
4436
  groupKey: group.groupKey,
4411
4437
  specs: group.specs,
4412
4438
  registry: input.registry,
4439
+ ...group.retention === "ephemeral" ? { sessionScoped: true } : {},
4413
4440
  ...bootStamp ? { bootStamp } : {},
4414
4441
  ...countsTowardBoot && input.dbReady ? { dbReady: input.dbReady } : {},
4415
4442
  ...input.shapeHeaders ? { headers: input.shapeHeaders } : {},
@@ -4507,6 +4534,7 @@ async function startGroupSync(pg, input) {
4507
4534
  key: input.groupKey,
4508
4535
  registry: input.registry,
4509
4536
  shapes,
4537
+ ...input.sessionScoped ? { sessionScoped: input.sessionScoped } : {},
4510
4538
  ...input.dbReady ? { dbReady: input.dbReady } : {},
4511
4539
  ...input.bootStamp ? { bootStamp: input.bootStamp } : {},
4512
4540
  ...input.onGroupInitialSync ? { onInitialSync: input.onGroupInitialSync } : {},
@@ -4857,9 +4885,10 @@ import { quoteIdentifier as quoteIdentifier6 } from "@pgxsinkit/contracts";
4857
4885
  async function getSubscriptionState({
4858
4886
  pg,
4859
4887
  metadataSchema,
4860
- subscriptionKey
4888
+ subscriptionKey,
4889
+ sessionScoped = false
4861
4890
  }) {
4862
- const { subscriptionsMetadata } = getMetadataTables(metadataSchema);
4891
+ const { subscriptionsMetadata } = pickMetadataTables(metadataSchema, sessionScoped);
4863
4892
  const rows = await drizzleOverPg(pg).select().from(subscriptionsMetadata).where(eq6(subscriptionsMetadata.key, subscriptionKey));
4864
4893
  if (rows.length === 0) {
4865
4894
  return null;
@@ -4892,12 +4921,13 @@ async function updateSubscriptionState({
4892
4921
  subscriptionKey,
4893
4922
  shapeMetadata,
4894
4923
  lastLsn,
4895
- debug
4924
+ debug,
4925
+ sessionScoped = false
4896
4926
  }) {
4897
4927
  if (debug) {
4898
4928
  console.log("updating subscription state", subscriptionKey, shapeMetadata, lastLsn);
4899
4929
  }
4900
- const { subscriptionsMetadata } = getMetadataTables(metadataSchema);
4930
+ const { subscriptionsMetadata } = pickMetadataTables(metadataSchema, sessionScoped);
4901
4931
  const lastLsnText = lastLsn.toString();
4902
4932
  await drizzleOverPg(pg).insert(subscriptionsMetadata).values({ key: subscriptionKey, shape_metadata: shapeMetadata, last_lsn: lastLsnText }).onConflictDoUpdate({
4903
4933
  target: subscriptionsMetadata.key,
@@ -4905,19 +4935,25 @@ async function updateSubscriptionState({
4905
4935
  });
4906
4936
  }
4907
4937
  async function deleteSubscriptionState({ pg, metadataSchema, subscriptionKey }) {
4938
+ const db = drizzleOverPg(pg);
4908
4939
  const { subscriptionsMetadata } = getMetadataTables(metadataSchema);
4909
- await drizzleOverPg(pg).delete(subscriptionsMetadata).where(eq6(subscriptionsMetadata.key, subscriptionKey));
4940
+ const session = getSessionMetadataTables();
4941
+ await db.delete(subscriptionsMetadata).where(eq6(subscriptionsMetadata.key, subscriptionKey));
4942
+ await db.delete(session.subscriptionsMetadata).where(eq6(session.subscriptionsMetadata.key, subscriptionKey));
4910
4943
  }
4911
4944
  async function migrateSubscriptionMetadataTables({
4912
4945
  pg,
4913
4946
  metadataSchema
4914
4947
  }) {
4915
4948
  const { subscriptionsMetadata, shapeRowTags } = getMetadataTables(metadataSchema);
4949
+ const session = getSessionMetadataTables();
4916
4950
  const statements = [
4917
4951
  `SET ${metadataSchema}.syncing = false;`,
4918
4952
  `CREATE SCHEMA IF NOT EXISTS ${quoteIdentifier6(metadataSchema)};`,
4919
4953
  ...renderCreateTableSql(subscriptionsMetadata),
4920
- ...renderCreateTableSql(shapeRowTags)
4954
+ ...renderCreateTableSql(shapeRowTags),
4955
+ ...renderCreateTableSql(session.subscriptionsMetadata, { temp: true }),
4956
+ ...renderCreateTableSql(session.shapeRowTags, { temp: true })
4921
4957
  ];
4922
4958
  await pg.exec(statements.join(`
4923
4959
  `));
@@ -4977,6 +5013,7 @@ async function createSyncEngine(pg, options) {
4977
5013
  key,
4978
5014
  registry,
4979
5015
  shapes,
5016
+ sessionScoped = false,
4980
5017
  initialInsertMethod,
4981
5018
  onInitialSync,
4982
5019
  onError,
@@ -5018,7 +5055,8 @@ async function createSyncEngine(pg, options) {
5018
5055
  subState = await getSubscriptionState({
5019
5056
  pg,
5020
5057
  metadataSchema,
5021
- subscriptionKey: key
5058
+ subscriptionKey: key,
5059
+ sessionScoped
5022
5060
  });
5023
5061
  if (debug && subState) {
5024
5062
  console.log("resuming from subscription state", subState);
@@ -5124,7 +5162,7 @@ async function createSyncEngine(pg, options) {
5124
5162
  } else {
5125
5163
  await drizzleOverPg(tx).execute(sql6`TRUNCATE ${target.table}`);
5126
5164
  }
5127
- await clearShapeTags({ pg: tx, metadataSchema, shapeTable });
5165
+ await clearShapeTags({ pg: tx, metadataSchema, shapeTable, sessionScoped });
5128
5166
  }
5129
5167
  const moveInMessages = moveInsToCommit.get(shapeName) ?? [];
5130
5168
  if (moveInMessages.length > 0) {
@@ -5133,7 +5171,8 @@ async function createSyncEngine(pg, options) {
5133
5171
  metadataSchema,
5134
5172
  shapeTable,
5135
5173
  messages: moveInMessages,
5136
- primaryKey: target.primaryKey
5174
+ primaryKey: target.primaryKey,
5175
+ sessionScoped
5137
5176
  });
5138
5177
  }
5139
5178
  await applyShapeTagSync({
@@ -5141,7 +5180,8 @@ async function createSyncEngine(pg, options) {
5141
5180
  metadataSchema,
5142
5181
  shapeTable,
5143
5182
  messages: initialMessages,
5144
- primaryKey: target.primaryKey
5183
+ primaryKey: target.primaryKey,
5184
+ sessionScoped
5145
5185
  });
5146
5186
  if (moveInMessages.length > 0) {
5147
5187
  await applyUpsertsToTable({
@@ -5221,7 +5261,8 @@ async function createSyncEngine(pg, options) {
5221
5261
  shapeTable,
5222
5262
  target,
5223
5263
  patternSets,
5224
- debug
5264
+ debug,
5265
+ sessionScoped
5225
5266
  });
5226
5267
  }
5227
5268
  }
@@ -5230,6 +5271,7 @@ async function createSyncEngine(pg, options) {
5230
5271
  pg: tx,
5231
5272
  metadataSchema,
5232
5273
  subscriptionKey: key,
5274
+ sessionScoped,
5233
5275
  shapeMetadata: Object.fromEntries(Object.keys(shapes).map((shapeName) => {
5234
5276
  const stream = getShapeStream(shapeName);
5235
5277
  return [
@@ -5514,6 +5556,7 @@ async function createSyncEngine(pg, options) {
5514
5556
  }
5515
5557
  },
5516
5558
  key: options2.shapeKey,
5559
+ ...options2.sessionScoped ? { sessionScoped: options2.sessionScoped } : {},
5517
5560
  onInitialSync: options2.onInitialSync,
5518
5561
  onError: options2.onError,
5519
5562
  onSyncError: options2.onSyncError,
@@ -5535,6 +5578,7 @@ async function createSyncEngine(pg, options) {
5535
5578
  };
5536
5579
  };
5537
5580
  const deleteSubscription = async (key) => {
5581
+ await initMetadataTables();
5538
5582
  await deleteSubscriptionState({
5539
5583
  pg,
5540
5584
  metadataSchema,
@@ -8166,4 +8210,4 @@ export {
8166
8210
  BRIDGE_CHANNEL
8167
8211
  };
8168
8212
 
8169
- //# debugId=804640A8064D585A64756E2164756E21
8213
+ //# debugId=A2F9A60B406BFCC764756E2164756E21