@rebasepro/server-postgres 0.27.0 → 0.28.0

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.
Files changed (100) hide show
  1. package/dist/{BranchService-B9Lu8e-P.js → BranchService-BPssiD8_.js} +37 -11
  2. package/dist/BranchService-BPssiD8_.js.map +1 -0
  3. package/dist/auth/services.d.ts +19 -7
  4. package/dist/backup/pg-tools.d.ts +14 -0
  5. package/dist/backup/retention.d.ts +14 -1
  6. package/dist/{backup-cli-BZM2Fj1I.js → backup-cli-C1wcj-rc.js} +59 -11
  7. package/dist/backup-cli-C1wcj-rc.js.map +1 -0
  8. package/dist/{boot-ddl-yukSebLA.js → boot-ddl-CIGoIGcC.js} +2 -2
  9. package/dist/{boot-ddl-yukSebLA.js.map → boot-ddl-CIGoIGcC.js.map} +1 -1
  10. package/dist/branch-argv.d.ts +20 -0
  11. package/dist/{cli-console-BtzxzzvU.js → cli-console-C0FYkICX.js} +5 -5
  12. package/dist/{cli-console-BtzxzzvU.js.map → cli-console-C0FYkICX.js.map} +1 -1
  13. package/dist/{cli-errors-BBSJqlYX.js → cli-errors-DYets7cp.js} +5 -5
  14. package/dist/{cli-errors-BBSJqlYX.js.map → cli-errors-DYets7cp.js.map} +1 -1
  15. package/dist/cli.js +43 -41
  16. package/dist/cli.js.map +1 -1
  17. package/dist/data-transformer.d.ts +11 -1
  18. package/dist/{databasePoolManager-3stMNFL-.js → databasePoolManager-BjoVvuAq.js} +2 -2
  19. package/dist/databasePoolManager-BjoVvuAq.js.map +1 -0
  20. package/dist/{deferred-index-build-DRnoMq-O.js → deferred-index-build-BcJ_oulu.js} +5 -5
  21. package/dist/{deferred-index-build-DRnoMq-O.js.map → deferred-index-build-BcJ_oulu.js.map} +1 -1
  22. package/dist/{doctor-Cs6AtizY.js → doctor-TJApVglQ.js} +6 -6
  23. package/dist/{doctor-Cs6AtizY.js.map → doctor-TJApVglQ.js.map} +1 -1
  24. package/dist/{ensure-collection-policies-H8HN1Gbd.js → ensure-collection-policies-BNwSQfVx.js} +63 -25
  25. package/dist/ensure-collection-policies-BNwSQfVx.js.map +1 -0
  26. package/dist/{ensure-collection-tables-BeF1lArf.js → ensure-collection-tables-CSbgkTBx.js} +8 -9
  27. package/dist/{ensure-collection-tables-BeF1lArf.js.map → ensure-collection-tables-CSbgkTBx.js.map} +1 -1
  28. package/dist/{ensure-tables-DDkNaPN2.js → ensure-tables-CF-8zgYA.js} +3 -3
  29. package/dist/{ensure-tables-DDkNaPN2.js.map → ensure-tables-CF-8zgYA.js.map} +1 -1
  30. package/dist/{enum-labels-kept-B6loB2U_.js → enum-labels-kept-BCgFGAbJ.js} +3 -3
  31. package/dist/{enum-labels-kept-B6loB2U_.js.map → enum-labels-kept-BCgFGAbJ.js.map} +1 -1
  32. package/dist/{generate-drizzle-schema-D_O8_iyT.js → generate-drizzle-schema-BAIp9nKT.js} +2 -2
  33. package/dist/{generate-drizzle-schema-D_O8_iyT.js.map → generate-drizzle-schema-BAIp9nKT.js.map} +1 -1
  34. package/dist/{generate-drizzle-schema-logic-D0E64iwx.js → generate-drizzle-schema-logic-CVRmZjP3.js} +2 -2
  35. package/dist/{generate-drizzle-schema-logic-D0E64iwx.js.map → generate-drizzle-schema-logic-CVRmZjP3.js.map} +1 -1
  36. package/dist/{generate-files-BpgyRwhU.js → generate-files-DutUrsSS.js} +3 -3
  37. package/dist/{generate-files-BpgyRwhU.js.map → generate-files-DutUrsSS.js.map} +1 -1
  38. package/dist/{generate-postgres-ddl-logic-CogQBrzs.js → generate-postgres-ddl-logic-0rbAqwYT.js} +22 -10
  39. package/dist/generate-postgres-ddl-logic-0rbAqwYT.js.map +1 -0
  40. package/dist/{generated-schema-diff-X9NA2Vig.js → generated-schema-diff-ChFg6UxI.js} +13 -4
  41. package/dist/{generated-schema-diff-X9NA2Vig.js.map → generated-schema-diff-ChFg6UxI.js.map} +1 -1
  42. package/dist/history/HistoryService.d.ts +5 -0
  43. package/dist/index.es.js +14 -13
  44. package/dist/index.es.js.map +1 -1
  45. package/dist/{migrate-baseline-Bfk1LGC6.js → migrate-baseline-DPawvckr.js} +54 -11
  46. package/dist/{migrate-baseline-Bfk1LGC6.js.map → migrate-baseline-DPawvckr.js.map} +1 -1
  47. package/dist/{migrate-policy-stamps-DHxwcawC.js → migrate-policy-stamps-B-8vTSIm.js} +6 -6
  48. package/dist/{migrate-policy-stamps-DHxwcawC.js.map → migrate-policy-stamps-B-8vTSIm.js.map} +1 -1
  49. package/dist/{open-rebase-DZ5JbZpg.js → open-rebase-D9n8idex.js} +1436 -200
  50. package/dist/open-rebase-D9n8idex.js.map +1 -0
  51. package/dist/{pg-error-utils-BueZEJ0S.js → pg-error-utils-BIHfsGZL.js} +2 -2
  52. package/dist/{pg-error-utils-BueZEJ0S.js.map → pg-error-utils-BIHfsGZL.js.map} +1 -1
  53. package/dist/{plan-schema-YagMlmNE.js → plan-schema-DMVh77yb.js} +28 -1
  54. package/dist/plan-schema-DMVh77yb.js.map +1 -0
  55. package/dist/{policy-drift-Dqp_yFeE.js → policy-drift-0Cp4Lc0x.js} +13 -4
  56. package/dist/policy-drift-0Cp4Lc0x.js.map +1 -0
  57. package/dist/{push-index-prebuild-hVRrfTi-.js → push-index-prebuild-DR3P4yOw.js} +4 -4
  58. package/dist/{push-index-prebuild-hVRrfTi-.js.map → push-index-prebuild-DR3P4yOw.js.map} +1 -1
  59. package/dist/{push-kept-defaults-M_ZCcXuy.js → push-kept-defaults-BSqPzp0T.js} +2 -2
  60. package/dist/{push-kept-defaults-M_ZCcXuy.js.map → push-kept-defaults-BSqPzp0T.js.map} +1 -1
  61. package/dist/{push-legacy-renames-DcoAWmGt.js → push-legacy-renames-BIxBmVLl.js} +4 -4
  62. package/dist/{push-legacy-renames-DcoAWmGt.js.map → push-legacy-renames-BIxBmVLl.js.map} +1 -1
  63. package/dist/{push-policy-plan-Bq1bcb3Y.js → push-policy-plan-BHsasXa7.js} +4 -4
  64. package/dist/{push-policy-plan-Bq1bcb3Y.js.map → push-policy-plan-BHsasXa7.js.map} +1 -1
  65. package/dist/{render-ddl-D5jeXKR-.js → render-ddl-NpPPDACz.js} +20 -3
  66. package/dist/render-ddl-NpPPDACz.js.map +1 -0
  67. package/dist/schema/doctor-cli.js +3 -3
  68. package/dist/schema/ensure-collection-policies.d.ts +6 -4
  69. package/dist/schema/generate-drizzle-schema.js +1 -1
  70. package/dist/schema/generate-postgres-ddl-logic.d.ts +16 -0
  71. package/dist/schema/generate-postgres-ddl.js +1 -1
  72. package/dist/schema/generated-schema-diff.d.ts +6 -0
  73. package/dist/schema/introspect-db.js +1 -1
  74. package/dist/schema/plan/render-ddl.d.ts +11 -0
  75. package/dist/schema/reconcile-schema.d.ts +21 -0
  76. package/dist/security/policy-drift.d.ts +9 -1
  77. package/dist/services/BranchService.d.ts +6 -0
  78. package/dist/services/RelationService.d.ts +6 -6
  79. package/dist/services/cdc/CdcListener.d.ts +12 -0
  80. package/dist/services/cdc/WalCdcListener.d.ts +92 -0
  81. package/dist/services/cdc/pgoutput.d.ts +121 -0
  82. package/dist/services/cdc/wal-cdc.d.ts +29 -0
  83. package/dist/services/collection-helpers.d.ts +14 -0
  84. package/dist/services/junction-writes.d.ts +9 -1
  85. package/dist/services/nested-path.d.ts +2 -1
  86. package/dist/services/realtimeService.d.ts +47 -2
  87. package/dist/utils/drizzle-conditions.d.ts +11 -0
  88. package/dist/{websocket-C9Fx7iSx.js → websocket-D4wrzF3b.js} +6 -5
  89. package/dist/websocket-D4wrzF3b.js.map +1 -0
  90. package/package.json +7 -7
  91. package/dist/BranchService-B9Lu8e-P.js.map +0 -1
  92. package/dist/backup-cli-BZM2Fj1I.js.map +0 -1
  93. package/dist/databasePoolManager-3stMNFL-.js.map +0 -1
  94. package/dist/ensure-collection-policies-H8HN1Gbd.js.map +0 -1
  95. package/dist/generate-postgres-ddl-logic-CogQBrzs.js.map +0 -1
  96. package/dist/open-rebase-DZ5JbZpg.js.map +0 -1
  97. package/dist/plan-schema-YagMlmNE.js.map +0 -1
  98. package/dist/policy-drift-Dqp_yFeE.js.map +0 -1
  99. package/dist/render-ddl-D5jeXKR-.js.map +0 -1
  100. package/dist/websocket-C9Fx7iSx.js.map +0 -1
@@ -2,34 +2,35 @@ import { createRequire as __createRequire } from "module";
2
2
  __createRequire(import.meta.url);
3
3
  import { i as createPostgresDatabaseConnection } from "./connection-czvI7TZL.js";
4
4
  import { _ as visibleColumnProjection, a as buildSearchColumnSpec, r as SEARCH_UNACCENT_FN, s as hiddenColumnsOption } from "./search-column-BM-GV6vH.js";
5
- import { a as diagnoseDbError, c as isEconnrefused, n as closedDatabaseName, r as deepestErrorMessage, rt as extractCauseMessage, u as parseHostInfo } from "./cli-errors-BBSJqlYX.js";
6
- import { a as pgErrorToFriendlyMessage, i as isRowLevelSecurityDenial, n as extractPgError, o as reachedDatabase, r as isRoleSwitchingPermissionError, s as sanitizeErrorForClient, t as classifyConnectFailure } from "./pg-error-utils-BueZEJ0S.js";
7
- import { t as BranchService } from "./BranchService-B9Lu8e-P.js";
5
+ import { a as diagnoseDbError, c as isEconnrefused, n as closedDatabaseName, r as deepestErrorMessage, rt as extractCauseMessage, u as parseHostInfo } from "./cli-errors-DYets7cp.js";
6
+ import { a as pgErrorToFriendlyMessage, i as isRowLevelSecurityDenial, n as extractPgError, o as reachedDatabase, r as isRoleSwitchingPermissionError, s as sanitizeErrorForClient, t as classifyConnectFailure } from "./pg-error-utils-BIHfsGZL.js";
7
+ import { t as BranchService } from "./BranchService-BPssiD8_.js";
8
8
  import { a as ensureAppRoleGrants, c as revokeUserRoleSql, d as warnOnAnonymousGrants, f as warnOnLegacyRlsFunctions, i as ensureAppRole, n as applyAuthContext, o as findExposedInternalTables, p as warnOnRoleSchemaCollision, r as detectConnectionPosture, t as REBASE_USER_ROLE$1, u as validatePolicyPgRoles } from "./rls-enforcement-BOHuHVlc.js";
9
9
  import { u as resolveColumnName } from "./column-plan-helpers-BeSztMor.js";
10
10
  import { t as AUTH_USERS_COLUMNS } from "./auth-users-columns-C72EMoDJ.js";
11
- import { n as planSchema } from "./plan-schema-YagMlmNE.js";
12
- import { t as generateSchema } from "./generate-drizzle-schema-logic-D0E64iwx.js";
13
- import { r as renderPgType } from "./render-ddl-D5jeXKR-.js";
14
- import { u as typesAgree } from "./deferred-index-build-DRnoMq-O.js";
15
- import { a as isLockTimeout, c as resolveBootLockOptions, l as sleep, n as BOOT_LOCK_TIMEOUT_MS } from "./boot-ddl-yukSebLA.js";
16
- import { a as readSchemaFactsFor, r as planCollectionSchemaEnsure } from "./ensure-collection-tables-BeF1lArf.js";
11
+ import { n as planSchema } from "./plan-schema-DMVh77yb.js";
12
+ import { t as generateSchema } from "./generate-drizzle-schema-logic-CVRmZjP3.js";
13
+ import { i as renderPgType } from "./render-ddl-NpPPDACz.js";
14
+ import { s as diffPlanAgainstCatalogue, u as typesAgree } from "./deferred-index-build-BcJ_oulu.js";
15
+ import { a as isLockTimeout, c as resolveBootLockOptions, l as sleep, n as BOOT_LOCK_TIMEOUT_MS } from "./boot-ddl-CIGoIGcC.js";
16
+ import { a as readSchemaFactsFor, r as planCollectionSchemaEnsure } from "./ensure-collection-tables-CSbgkTBx.js";
17
17
  import { a as mergeDatabaseBranch, i as diffDatabaseBranch } from "./branch-diff-CxE4VlxT.js";
18
- import { t as DatabasePoolManager } from "./databasePoolManager-3stMNFL-.js";
19
- import { a as resolveAuthSchema, i as probeAuthSchema, r as assertAuthSchemaCompatible, t as ensureAuthTablesExist } from "./ensure-tables-DDkNaPN2.js";
20
- import { a as buildBaasSchema, c as introspectSchema, i as warnOnGeneratedSchemaDrift, l as readRlsStatus, n as diffGeneratedSchemaAgainstCatalogue, o as readCatalogueSchema, s as buildCollectionsFromSchema } from "./generated-schema-diff-X9NA2Vig.js";
18
+ import { t as DatabasePoolManager } from "./databasePoolManager-BjoVvuAq.js";
19
+ import { a as resolveAuthSchema, i as probeAuthSchema, r as assertAuthSchemaCompatible, t as ensureAuthTablesExist } from "./ensure-tables-CF-8zgYA.js";
20
+ import { a as buildBaasSchema, c as introspectSchema, i as warnOnGeneratedSchemaDrift, l as readRlsStatus, n as diffGeneratedSchemaAgainstCatalogue, o as readCatalogueSchema, s as buildCollectionsFromSchema } from "./generated-schema-diff-ChFg6UxI.js";
21
21
  import { Client, DatabaseError, Pool } from "pg";
22
22
  import { drizzle } from "drizzle-orm/node-postgres";
23
23
  import { ApiError, assertQueryFieldsReadable, createDdlBootstrapper, createEmailService, logger, rawQueryLoggingEnabled, resolveBatchRefs, rowETag, splitFieldOps } from "@rebasepro/server";
24
24
  import { and, asc, count, desc, eq, getTableColumns, getTableName, gt, ilike, inArray, is, isNotNull, isNull, isTable, lt, lte, not, notInArray, or, relations, sql } from "drizzle-orm";
25
25
  import { PgArray, PgNumeric, PgTable, PgTimestamp, PgTimestampString, PgTransaction, bigint, boolean, getTableConfig, index, integer, jsonb, pgSchema, pgTable, text, timestamp, unique, uuid } from "drizzle-orm/pg-core";
26
- import { ALL_WHERE_FILTER_OPS, ANONYMOUS_USER_ID, DEFAULT_COMMIT_PATHS, DEFAULT_DATA_SOURCE_KEY, INCLUDE_COUNT_KEY, JUNCTION_PIVOT_KEY, ListLimitError, MAX_INCLUDE_DEPTH, Vector, encodeRelationAggregateSort, hasForeignKeyOnTarget, isChannelBusInstance, isChannelBusInstance as isChannelBusInstance$1, isManyToMany, isRelationalCollectionConfig, isSQLAdmin, parseEnvBoolean, parseRelationAggregateSort, resolveClientListLimit, resourceEnvSuffix, resourceKeyOf, roleNameProblem } from "@rebasepro/types";
27
- import { COMPOSITE_ID_SEPARATOR, COMPOSITE_ID_SEPARATOR as COMPOSITE_ID_SEPARATOR$1, CollectionRegistry, DEFAULT_ONE_OF_TYPE, DEFAULT_ONE_OF_VALUE, OrderBySpecError, REBASE_USER_ROLE, UnknownFilterOperatorError, apiHiddenFieldNames, applyDefaultValuesOnCreate, buildCompositeId, buildCompositeId as buildCompositeId$1, buildPropertyCallbacks, buildSdkData, callbackRefusal, canReadField, classifyTable, createAccessorSlugResolver, createRelationRef, createRelationRefWithData, deserializeFilter, detectJunctionTables, encodeCursor, fieldKeyForColumn, findRelation, firstSqlRow, getColumnName, getDeclaredPrimaryKeys, getJunctionConfigForRelation, getTableName as getTableName$1, getTenantConfig, hasFieldAccessRules, includableRelationNames, isAddressableId, normalizeDriverOrderBy, normalizeEmail, normalizeInclude, normalizeToEntityRelation, parseIdValues, parseIdValues as parseIdValues$1, parseOrderBySpecStrict, queryableFieldNames, requireCallbackClient, requireCallbackCollection, resolveCollectionRelations, resolveTenantWrite, restoresSoftDeletedRow, revokeInternalTableSql, softDeleteFieldOf, sqlRows, tenantBypassRoles, toCallbackError, toFilterTuples, unknownAggregateField, unknownFilterField, unknownIncludeRelation, unknownOrderByField, unknownProjectionField, unknownRelationTargetField, unknownVectorProperty, updateDateAutoValues, updateUserAutoValues, withRelationReferencesOnly } from "@rebasepro/common";
26
+ import { ALL_WHERE_FILTER_OPS, ANONYMOUS_USER_ID, DEFAULT_COMMIT_PATHS, DEFAULT_DATA_SOURCE_KEY, INCLUDE_COUNT_KEY, JUNCTION_PIVOT_KEY, ListLimitError, MAX_INCLUDE_DEPTH, Vector, encodeRelationAggregateSort, hasForeignKeyOnTarget, isChannelBusInstance, isChannelBusInstance as isChannelBusInstance$1, isManyToMany, isRelationalCollectionConfig, isSQLAdmin, joinConditions, parseEnvBoolean, parseRelationAggregateSort, resolveClientListLimit, resourceEnvSuffix, resourceKeyOf, roleNameProblem, soleJoinCondition } from "@rebasepro/types";
27
+ import { COMPOSITE_ID_SEPARATOR, COMPOSITE_ID_SEPARATOR as COMPOSITE_ID_SEPARATOR$1, CollectionRegistry, DEFAULT_ONE_OF_TYPE, DEFAULT_ONE_OF_VALUE, OrderBySpecError, REBASE_USER_ROLE, UnknownFilterOperatorError, apiHiddenFieldNames, applyDefaultValuesOnCreate, buildCompositeId, buildCompositeId as buildCompositeId$1, buildPropertyCallbacks, buildSdkData, callbackRefusal, canReadField, classifyTable, createAccessorSlugResolver, createRelationRef, createRelationRefWithData, deserializeFilter, detectJunctionTables, encodeCursor, fieldKeyForColumn, findRelation, firstSqlRow, getColumnName, getDeclaredPrimaryKeys, getJunctionConfigForRelation, getTableName as getTableName$1, getTenantConfig, hasFieldAccessRules, includableRelationNames, isAddressableId, normalizeDriverOrderBy, normalizeEmail, normalizeInclude, normalizeToEntityRelation, parseIdValues, parseIdValues as parseIdValues$1, parseOrderBySpecStrict, queryableFieldNames, requireCallbackClient, requireCallbackCollection, resolveCollectionRelations, resolveTenantWrite, restoresSoftDeletedRow, revokeInternalTableSql, softDeleteFieldOf, sqlRows, tenantBypassRoles, toCallbackError, toFilterTuples, unknownAggregateField, unknownFilterField, unknownIncludeRelation, unknownOrderByField, unknownProjectionField, unknownRelationTargetField, unknownVectorProperty, updateDateAutoValues, updateUserAutoValues, warnOnTableDdl, withRelationReferencesOnly } from "@rebasepro/common";
28
28
  import { camelCase, generateForeignKeyName, isPrototypePollutingKey, legacyForeignKeyName, splitSearchTerms, toSnakeCase, toWireKey, unref } from "@rebasepro/utils";
29
29
  import { AsyncLocalStorage } from "node:async_hooks";
30
+ import { createHash } from "node:crypto";
30
31
  import { WebSocket } from "ws";
31
32
  import { EventEmitter } from "events";
32
- import { createHash, randomUUID } from "crypto";
33
+ import { createHash as createHash$1, randomBytes, randomUUID } from "crypto";
33
34
  //#region src/services/field-viewer.ts
34
35
  /**
35
36
  * Who the current read is *for*, available to the row walk without threading it
@@ -363,6 +364,24 @@ function primaryKeyColumns(table, primaryKeys, collectionPath) {
363
364
  });
364
365
  }
365
366
  /**
367
+ * The column a table holds under the property key `key`, or `undefined`.
368
+ *
369
+ * A Drizzle table is an object whose own properties are its columns — and
370
+ * also everything any object answers to. `key in table` is true of
371
+ * `constructor`, `toString` and `hasOwnProperty`, inherited from the
372
+ * prototype, and of `enableRLS`, a method Drizzle sets on the table itself;
373
+ * reading one of those as a column put a function into the statement as a
374
+ * bound parameter. A filter on `constructor` then compiled to a comparison
375
+ * between two parameters — "a filter that names nothing" without the 400 —
376
+ * and `__proto__` crashed the builder into a 500. Only an own, object-valued
377
+ * property is a column.
378
+ */
379
+ function columnAtKey(table, key) {
380
+ if (!Object.prototype.hasOwnProperty.call(table, key)) return void 0;
381
+ const value = table[key];
382
+ return typeof value === "object" && value !== null ? value : void 0;
383
+ }
384
+ /**
366
385
  * The column a `joinPath` step names, on a table that may key it either way.
367
386
  *
368
387
  * A `JoinStep` is written in database terms — `on: { from: "author_id" }` —
@@ -617,8 +636,7 @@ function junctionTableMissing(junctionName) {
617
636
  return /* @__PURE__ */ new Error(`The relation declares a junction table "${junctionName}", and no such table is registered. Either the generated Drizzle schema predates it — run \`rebase schema generate\` — or the table does not exist yet: \`rebase db push\` creates the junctions a relation declares. If neither helps, check the \`through.table\` name on the relation.`);
618
637
  }
619
638
  var relationColumn = (table, collection, column) => {
620
- const key = fieldKeyForColumn(collection, column);
621
- return (key in table ? table[key] : void 0) || void 0;
639
+ return columnAtKey(table, fieldKeyForColumn(collection, column));
622
640
  };
623
641
  /**
624
642
  * A table's column by the name a `joinPath` step gives it — the SQL column — or
@@ -802,6 +820,23 @@ function toMembershipList(value) {
802
820
  return Array.isArray(value) ? value : [value];
803
821
  }
804
822
  /**
823
+ * "The jsonb array holds at least one of `values`", as one containment test per
824
+ * value: `(col @> '[3]' OR col @> '[7]')`.
825
+ *
826
+ * Containment compares elements by JSON type and value, which is the question
827
+ * `array-contains` asks of a single value — so the two operators agree, and a
828
+ * number array is searchable by numbers. `?|` is not that question: it asks
829
+ * whether any of a list of *strings* is a top-level key or string element, so
830
+ * `[1, 2, 3] ?| array['3']` is false and a filter for "any of [3]" on a number
831
+ * array matched nothing.
832
+ *
833
+ * An OR of `@>` rather than `@> ANY(...)`: a GIN index serves each `@>` arm and
834
+ * combines them, and does not serve an array comparison at all.
835
+ */
836
+ function jsonArrayContainsAny(operand, values) {
837
+ return sql`(${sql.join(values.map((v) => sql`${operand} @> ${JSON.stringify([v])}`), sql` OR `)})`;
838
+ }
839
+ /**
805
840
  * Unified condition builder for Drizzle/PostgreSQL queries.
806
841
  *
807
842
  * This class uses static methods and satisfies the ConditionBuilderStatic<SQL> type.
@@ -877,29 +912,33 @@ var DrizzleConditionBuilder = class DrizzleConditionBuilder {
877
912
  const sourceAlias = "__rel_src";
878
913
  const aliasFor = (index) => `__rel_j${index}`;
879
914
  const fromRef = (stepIndex, column) => sql`${sql.identifier(stepIndex === 0 ? sourceAlias : aliasFor(stepIndex - 1))}.${sql.identifier(getColumnName(column))}`;
880
- const pairs = (step) => {
881
- const from = Array.isArray(step.on.from) ? step.on.from : [step.on.from];
882
- const to = Array.isArray(step.on.to) ? step.on.to : [step.on.to];
915
+ const alternatives = (step) => joinConditions(step).map((on) => {
916
+ const from = Array.isArray(on.from) ? on.from : [on.from];
917
+ const to = Array.isArray(on.to) ? on.to : [on.to];
883
918
  if (from.length !== to.length) throw new Error(`Join step on '${step.table}' has ${from.length} \`from\` columns and ${to.length} \`to\` columns`);
884
919
  return from.map((f, i) => ({
885
920
  from: f,
886
921
  to: to[i]
887
922
  }));
923
+ });
924
+ const anyOf = (step, pair) => {
925
+ const each = alternatives(step).map((pairs) => sql.join(pairs.map(pair), sql` AND `));
926
+ return each.length === 1 ? each[0] : sql`(${sql.join(each.map((c) => sql`(${c})`), sql` OR `)})`;
888
927
  };
889
928
  const inner = joinPath.slice(0, -1);
890
929
  const last = joinPath[joinPath.length - 1];
891
930
  const joins = inner.map((step, index) => {
892
931
  const table = registry.getTable(step.table);
893
932
  if (!table) throw new Error(`Join table not found: ${step.table}`);
894
- const on = pairs(step).map(({ from, to }) => sql`${fromRef(index, from)} = ${sql.identifier(aliasFor(index))}.${sql.identifier(getColumnName(to))}`);
895
- return sql`JOIN ${table} AS ${sql.identifier(aliasFor(index))} ON ${sql.join(on, sql` AND `)}`;
933
+ const on = anyOf(step, ({ from, to }) => sql`${fromRef(index, from)} = ${sql.identifier(aliasFor(index))}.${sql.identifier(getColumnName(to))}`);
934
+ return sql`JOIN ${table} AS ${sql.identifier(aliasFor(index))} ON ${on}`;
896
935
  });
897
936
  const targetColumn = (name) => {
898
937
  const column = joinColumn(targetTable, name);
899
938
  if (!column) throw new Error(`Join step column '${name}' not found in the target table of this joinPath`);
900
939
  return column;
901
940
  };
902
- const correlation = sql.join(pairs(last).map(({ from, to }) => sql`${fromRef(inner.length, from)} = ${targetColumn(to)}`), sql` AND `);
941
+ const correlation = anyOf(last, ({ from, to }) => sql`${fromRef(inner.length, from)} = ${targetColumn(to)}`);
903
942
  const joinsSql = joins.length > 0 ? sql` ${sql.join(joins, sql` `)}` : sql``;
904
943
  const parentRow = sql.join(parentKey.map(({ column, value }) => sql`${sql.identifier(sourceAlias)}.${sql.identifier(column.name)} = ${value}`), sql` AND `);
905
944
  return sql`EXISTS (SELECT 1 FROM ${parentTable} AS ${sql.identifier(sourceAlias)}${joinsSql} WHERE ${parentRow} AND ${correlation})`;
@@ -931,7 +970,7 @@ var DrizzleConditionBuilder = class DrizzleConditionBuilder {
931
970
  */
932
971
  static resolveFilterTarget(table, field, collectionPath, mode, options) {
933
972
  const { collection, registry, sourceIdColumn } = options;
934
- const columnAt = (key) => (key in table ? table[key] : void 0) || void 0;
973
+ const columnAt = (key) => columnAtKey(table, key);
935
974
  const direct = columnAt(field);
936
975
  if (direct) return {
937
976
  kind: "column",
@@ -975,7 +1014,7 @@ var DrizzleConditionBuilder = class DrizzleConditionBuilder {
975
1014
  if (target) return target;
976
1015
  }
977
1016
  }
978
- for (const guess of [
1017
+ if (!collection) for (const guess of [
979
1018
  `${field}Id`,
980
1019
  toWireKey(generateForeignKeyName(field)),
981
1020
  `${field}_id`,
@@ -1030,7 +1069,7 @@ var DrizzleConditionBuilder = class DrizzleConditionBuilder {
1030
1069
  const targetCollection = targetOf(relation);
1031
1070
  const targetTable = targetCollection && registry.getTable(getTableName$1(targetCollection));
1032
1071
  if (!targetCollection || !targetTable) throw new Error(`Table not found for the target of relation '${relation.relationName}' on '${collectionPath}', so '${field}' has nothing to filter against.`);
1033
- const targetColumn = relationColumn(targetTable, targetCollection, path.fieldKey) ?? (path.fieldKey in targetTable ? targetTable[path.fieldKey] : void 0);
1072
+ const targetColumn = relationColumn(targetTable, targetCollection, path.fieldKey) ?? columnAtKey(targetTable, path.fieldKey);
1034
1073
  if (!targetColumn) {
1035
1074
  const answer = unknownRelationTargetField({
1036
1075
  field,
@@ -1422,7 +1461,7 @@ var DrizzleConditionBuilder = class DrizzleConditionBuilder {
1422
1461
  const isNativeArray = meta.dataType === "array" || meta.columnType === "PgArray";
1423
1462
  if (op === "array-contains-any") {
1424
1463
  if (Array.isArray(value) && value.length === 0) return sql`FALSE`;
1425
- if (Array.isArray(value) && value.length > 0) return isNativeArray ? sql`${ref} && ARRAY[${sql.join(value.map((v) => sql`${v}`), sql`, `)}]` : sql`${ref} ?| array[${sql.join(value.map((v) => sql`${String(v)}`), sql`, `)}]`;
1464
+ if (Array.isArray(value) && value.length > 0) return isNativeArray ? sql`${ref} && ARRAY[${sql.join(value.map((v) => sql`${v}`), sql`, `)}]` : jsonArrayContainsAny(ref, value);
1426
1465
  }
1427
1466
  return isNativeArray ? sql`${ref} @> ARRAY[${value}]` : sql`${ref} @> ${JSON.stringify([value])}`;
1428
1467
  };
@@ -1541,7 +1580,7 @@ var DrizzleConditionBuilder = class DrizzleConditionBuilder {
1541
1580
  const alias = "__rel_agg";
1542
1581
  let aggregand = sql`*`;
1543
1582
  if (spec.field) {
1544
- const column = namesHiddenFromCaller(targetCollection).has(spec.field) ? void 0 : relationColumn(targetTable, targetCollection, spec.field) ?? (spec.field in targetTable ? targetTable[spec.field] : void 0);
1583
+ const column = namesHiddenFromCaller(targetCollection).has(spec.field) ? void 0 : relationColumn(targetTable, targetCollection, spec.field) ?? columnAtKey(targetTable, spec.field);
1545
1584
  if (!column) {
1546
1585
  const validFields = listedColumnKeys(targetTable, targetCollection);
1547
1586
  throw ApiError.badRequest(`Unknown field '${spec.field}' on '${targetCollection.slug}', the target of relation '${spec.relation}' on collection '${collectionPath}'` + (validFields.length > 0 ? `. Valid fields: ${validFields.join(", ")}` : ""), "UNKNOWN_ORDER_BY_FIELD", {
@@ -1625,10 +1664,7 @@ var DrizzleConditionBuilder = class DrizzleConditionBuilder {
1625
1664
  const isNativeArray = meta.dataType === "array" || meta.columnType === "PgArray";
1626
1665
  if (Array.isArray(value) && value.length === 0) return sql`FALSE`;
1627
1666
  if (Array.isArray(value) && value.length > 0) if (isNativeArray) return sql`${column} && ARRAY[${sql.join(value.map((v) => sql`${v}`), sql`, `)}]`;
1628
- else {
1629
- const textValues = value.map((v) => String(v));
1630
- return sql`${column} ?| array[${sql.join(textValues.map((v) => sql`${v}`), sql`, `)}]`;
1631
- }
1667
+ else return jsonArrayContainsAny(column, value);
1632
1668
  if (isNativeArray) return sql`${column} @> ARRAY[${value}]`;
1633
1669
  return sql`${column} @> ${JSON.stringify([value])}`;
1634
1670
  }
@@ -1711,14 +1747,7 @@ var DrizzleConditionBuilder = class DrizzleConditionBuilder {
1711
1747
  let currentTable = targetTable;
1712
1748
  const sourceTableName = getTableName(parentTable);
1713
1749
  for (let index = joinPath.length - 1; index >= 0; index--) {
1714
- const joinStep = joinPath[index];
1715
- const { fromTableName, toTableName } = this.joinStepTableNames(joinPath, index, sourceTableName);
1716
- const fromColName = this.getColumnNamesFromColumns(joinStep.on.from)[0];
1717
- const toColName = this.getColumnNamesFromColumns(joinStep.on.to)[0];
1718
- const fromTable = registry.getTable(fromTableName);
1719
- const toTable = registry.getTable(toTableName);
1720
- if (!fromTable || !toTable) throw new Error(`Join tables not found for step: from ${fromTableName} to ${toTableName}`);
1721
- const { joinTable, condition, additionalJoins } = this.buildSingleJoinCondition(currentTable, fromTable, toTable, fromColName, toColName, fromTableName, toTableName, registry);
1750
+ const { joinTable, condition, additionalJoins } = this.buildReverseJoinStep(joinPath, index, sourceTableName, currentTable, registry, true);
1722
1751
  joins.push({
1723
1752
  table: joinTable,
1724
1753
  condition
@@ -1737,6 +1766,30 @@ var DrizzleConditionBuilder = class DrizzleConditionBuilder {
1737
1766
  };
1738
1767
  }
1739
1768
  /**
1769
+ * One step of a join path walked target → parent: the table it brings in
1770
+ * and the condition it joins on.
1771
+ *
1772
+ * A step listing alternatives joins on any of them, OR-ed. Each alternative
1773
+ * is built the way a plain step is, and they must all bring in the same
1774
+ * table — they are several ways to match one pair of tables, not several
1775
+ * paths. The junction fallback is only tried for a plain step: guessing a
1776
+ * junction for one alternative of several is guessing twice.
1777
+ */
1778
+ static buildReverseJoinStep(joinPath, index, sourceTableName, currentTable, registry, allowJunctionFallback) {
1779
+ const { fromTableName, toTableName } = this.joinStepTableNames(joinPath, index, sourceTableName);
1780
+ const fromTable = registry.getTable(fromTableName);
1781
+ const toTable = registry.getTable(toTableName);
1782
+ if (!fromTable || !toTable) throw new Error(`Join tables not found for step: from ${fromTableName} to ${toTableName}`);
1783
+ const alternatives = joinConditions(joinPath[index]);
1784
+ const built = alternatives.map((on) => this.buildSingleJoinCondition(currentTable, fromTable, toTable, this.getColumnNamesFromColumns(on.from)[0], this.getColumnNamesFromColumns(on.to)[0], fromTableName, toTableName, allowJunctionFallback && alternatives.length === 1 ? registry : void 0));
1785
+ if (built.length === 1) return built[0];
1786
+ if (built.some((step) => step.joinTable !== built[0].joinTable)) throw new Error(`The alternatives of the join into '${joinPath[index].table}' do not join the same tables`);
1787
+ return {
1788
+ joinTable: built[0].joinTable,
1789
+ condition: or(...built.map((step) => step.condition))
1790
+ };
1791
+ }
1792
+ /**
1740
1793
  * Build a single join condition between tables
1741
1794
  */
1742
1795
  static buildSingleJoinCondition(currentTable, fromTable, toTable, fromColName, toColName, fromTableName, toTableName, registry) {
@@ -2147,14 +2200,7 @@ var DrizzleConditionBuilder = class DrizzleConditionBuilder {
2147
2200
  let currentTable = targetTable;
2148
2201
  const sourceTableName = getTableName(parentTable);
2149
2202
  for (let index = joinPath.length - 1; index >= 0; index--) {
2150
- const joinStep = joinPath[index];
2151
- const { fromTableName, toTableName } = this.joinStepTableNames(joinPath, index, sourceTableName);
2152
- const fromColName = this.getColumnNamesFromColumns(joinStep.on.from)[0];
2153
- const toColName = this.getColumnNamesFromColumns(joinStep.on.to)[0];
2154
- const fromTable = registry.getTable(fromTableName);
2155
- const toTable = registry.getTable(toTableName);
2156
- if (!fromTable || !toTable) throw new Error(`Join tables not found for step: from ${fromTableName} to ${toTableName}`);
2157
- const { joinTable, condition } = this.buildSingleJoinCondition(currentTable, fromTable, toTable, fromColName, toColName, fromTableName, toTableName);
2203
+ const { joinTable, condition } = this.buildReverseJoinStep(joinPath, index, sourceTableName, currentTable, registry, false);
2158
2204
  query = query.innerJoin(joinTable, condition);
2159
2205
  currentTable = joinTable;
2160
2206
  }
@@ -2196,9 +2242,10 @@ var DrizzleConditionBuilder = class DrizzleConditionBuilder {
2196
2242
  */
2197
2243
  static joinStepTableNames(joinPath, index, sourceTableName) {
2198
2244
  const step = joinPath[index];
2245
+ const [on] = joinConditions(step);
2199
2246
  return {
2200
- fromTableName: this.getTableNamesFromColumns(step.on.from)[0] || (index === 0 ? sourceTableName : joinPath[index - 1].table),
2201
- toTableName: this.getTableNamesFromColumns(step.on.to)[0] || step.table
2247
+ fromTableName: this.getTableNamesFromColumns(on.from)[0] || (index === 0 ? sourceTableName : joinPath[index - 1].table),
2248
+ toTableName: this.getTableNamesFromColumns(on.to)[0] || step.table
2202
2249
  };
2203
2250
  }
2204
2251
  static getTableNamesFromColumns(columns) {
@@ -2673,6 +2720,8 @@ function bindThroughJunction(registry, through, label) {
2673
2720
  table,
2674
2721
  parentColumn: column(table, through.sourceColumn, label, "source"),
2675
2722
  targetColumn: column(table, through.targetColumn, label, "target"),
2723
+ parentKey: through.sourceColumn,
2724
+ targetKey: through.targetColumn,
2676
2725
  label,
2677
2726
  payload: through.properties ?? {}
2678
2727
  };
@@ -2683,7 +2732,7 @@ var columnPart = (spec) => {
2683
2732
  return one.includes(".") ? one.split(".")[1] : one;
2684
2733
  };
2685
2734
  /** The table a column spec names, or undefined when it is unqualified. */
2686
- var tablePart = (spec) => {
2735
+ var tablePart$1 = (spec) => {
2687
2736
  const one = Array.isArray(spec) ? spec[0] : spec;
2688
2737
  return one.includes(".") ? one.split(".")[0] : void 0;
2689
2738
  };
@@ -2704,12 +2753,13 @@ function bindJoinPathJunction(registry, joinPath, parentTableName, targetTableNa
2704
2753
  let targetColumnName;
2705
2754
  let previousTable = parentTableName;
2706
2755
  for (const step of joinPath) {
2707
- const fromTable = tablePart(step.on.from) ?? previousTable;
2708
- const toTable = tablePart(step.on.to) ?? step.table;
2709
- if (toTable === junctionName && fromTable === parentTableName) parentColumnName = columnPart(step.on.to);
2710
- else if (fromTable === junctionName && toTable === parentTableName) parentColumnName = columnPart(step.on.from);
2711
- else if (toTable === junctionName && fromTable === targetTableName) targetColumnName = columnPart(step.on.to);
2712
- else if (fromTable === junctionName && toTable === targetTableName) targetColumnName = columnPart(step.on.from);
2756
+ const on = soleJoinCondition(step);
2757
+ const fromTable = tablePart$1(on.from) ?? previousTable;
2758
+ const toTable = tablePart$1(on.to) ?? step.table;
2759
+ if (toTable === junctionName && fromTable === parentTableName) parentColumnName = columnPart(on.to);
2760
+ else if (fromTable === junctionName && toTable === parentTableName) parentColumnName = columnPart(on.from);
2761
+ else if (toTable === junctionName && fromTable === targetTableName) targetColumnName = columnPart(on.to);
2762
+ else if (fromTable === junctionName && toTable === targetTableName) targetColumnName = columnPart(on.from);
2713
2763
  previousTable = step.table;
2714
2764
  }
2715
2765
  if (!parentColumnName || !targetColumnName) throw misconfigured(label, `its joinPath does not connect '${parentTableName}' and '${targetTableName}' through '${junctionName}'`);
@@ -2717,6 +2767,8 @@ function bindJoinPathJunction(registry, joinPath, parentTableName, targetTableNa
2717
2767
  table,
2718
2768
  parentColumn: column(table, parentColumnName, label, "source"),
2719
2769
  targetColumn: column(table, targetColumnName, label, "target"),
2770
+ parentKey: parentColumnName,
2771
+ targetKey: targetColumnName,
2720
2772
  label,
2721
2773
  payload: {}
2722
2774
  };
@@ -2822,8 +2874,8 @@ async function applyJunctionMembership(tx, binding, parentId, links, visibleTarg
2822
2874
  const repayloaded = [...wantedById.entries()].filter(([key, link]) => existingById.has(key) && link.pivot && Object.keys(link.pivot).length > 0 && !holdsPivot(storedPivotById.get(key), link.pivot)).map(([, link]) => link);
2823
2875
  if (removed.length > 0) await removeLinks(tx, binding, parentId, removed);
2824
2876
  if (added.length > 0) await tx.insert(binding.table).values(added.map((link) => ({
2825
- [binding.parentColumn.name]: parentId,
2826
- [binding.targetColumn.name]: link.id,
2877
+ [binding.parentKey]: parentId,
2878
+ [binding.targetKey]: link.id,
2827
2879
  ...link.pivot ?? {}
2828
2880
  }))).onConflictDoNothing();
2829
2881
  for (const link of repayloaded) await tx.update(binding.table).set(link.pivot).where(and(eq(binding.parentColumn, parentId), eq(binding.targetColumn, link.id)));
@@ -3090,20 +3142,28 @@ function applyDynamicJoin(query, joinTable, condition) {
3090
3142
  * `on.from`/`on.to` are a column or a tuple, and a tuple is how a step joins on
3091
3143
  * a composite key. Comparing the first pair alone joined each row to every row
3092
3144
  * sharing that column — every locale of a translation, not the one addressed.
3145
+ *
3146
+ * A step listing several conditions joins on any one of them, OR-ed: a row
3147
+ * that meets two of them is still one row of an inner join, so a link held in
3148
+ * either of two columns is read once either way.
3093
3149
  */
3094
3150
  function joinStepCondition(currentTable, joinTable, step) {
3095
- const from = Array.isArray(step.on.from) ? step.on.from : [step.on.from];
3096
- const to = Array.isArray(step.on.to) ? step.on.to : [step.on.to];
3097
- const pairs = [];
3098
- for (let i = 0; i < Math.max(from.length, to.length); i++) {
3099
- const fromColumn = from[i] ?? "(none)";
3100
- const toColumn = to[i] ?? "(none)";
3101
- const fromCol = joinColumn(currentTable, fromColumn);
3102
- const toCol = joinColumn(joinTable, toColumn);
3103
- if (!fromCol || !toCol) return { missing: `${fromColumn} -> ${toColumn}` };
3104
- pairs.push(eq(fromCol, toCol));
3151
+ const alternatives = [];
3152
+ for (const on of joinConditions(step)) {
3153
+ const from = Array.isArray(on.from) ? on.from : [on.from];
3154
+ const to = Array.isArray(on.to) ? on.to : [on.to];
3155
+ const pairs = [];
3156
+ for (let i = 0; i < Math.max(from.length, to.length); i++) {
3157
+ const fromColumn = from[i] ?? "(none)";
3158
+ const toColumn = to[i] ?? "(none)";
3159
+ const fromCol = joinColumn(currentTable, fromColumn);
3160
+ const toCol = joinColumn(joinTable, toColumn);
3161
+ if (!fromCol || !toCol) return { missing: `${fromColumn} -> ${toColumn}` };
3162
+ pairs.push(eq(fromCol, toCol));
3163
+ }
3164
+ alternatives.push(pairs.length === 1 ? pairs[0] : and(...pairs));
3105
3165
  }
3106
- return { condition: pairs.length === 1 ? pairs[0] : and(...pairs) };
3166
+ return { condition: alternatives.length === 1 ? alternatives[0] : or(...alternatives) };
3107
3167
  }
3108
3168
  /**
3109
3169
  * Typed wrapper for DrizzleConditionBuilder.buildRelationQuery on dynamic queries.
@@ -3843,14 +3903,25 @@ var RelationService = class {
3843
3903
  //#endregion
3844
3904
  //#region src/data-transformer.ts
3845
3905
  /**
3846
- * Helper function to sanitize and convert dates to ISO strings
3906
+ * The last pass over a row before it is written: `undefined` and `NaN` become
3907
+ * `null`, a `Date` becomes its ISO string, recursively through arrays and
3908
+ * objects.
3909
+ *
3910
+ * Strings are left as they came. This pass knows no property types, and a
3911
+ * string is text unless its property says otherwise: reading `"Nan"` as a
3912
+ * failed number nulled a person's name, and re-printing anything shaped like
3913
+ * an ISO timestamp cut the microseconds out of text that happened to hold one.
3914
+ * The coercions a string *does* need are made by its property, in
3915
+ * {@link serializePropertyToServer}: `"NaN"` on a `number`, a date's text on a
3916
+ * `date`.
3847
3917
  */
3848
3918
  function sanitizeAndConvertDates(obj) {
3849
3919
  if (obj === null || obj === void 0) return null;
3850
3920
  if (typeof obj === "number" && isNaN(obj)) return null;
3851
- if (typeof obj === "string" && obj.toLowerCase() === "nan") return null;
3852
3921
  if (Array.isArray(obj)) return obj.map((v) => sanitizeAndConvertDates(v));
3853
3922
  if (obj instanceof Date) return obj.toISOString();
3923
+ if (ArrayBuffer.isView(obj)) return obj;
3924
+ if (obj instanceof ArrayBuffer) return Buffer.from(obj);
3854
3925
  if (typeof obj === "object") {
3855
3926
  const newObj = {};
3856
3927
  for (const key in obj) {
@@ -3860,14 +3931,18 @@ function sanitizeAndConvertDates(obj) {
3860
3931
  }
3861
3932
  return newObj;
3862
3933
  }
3863
- if (typeof obj === "string") {
3864
- if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/.test(obj) || /^\w{3} \w{3} \d{2} \d{4} \d{2}:\d{2}:\d{2} GMT[+-]\d{4} \(.+\)$/.test(obj)) {
3865
- const date = new Date(obj);
3866
- if (!isNaN(date.getTime())) return date.toISOString();
3867
- }
3868
- }
3869
3934
  return obj;
3870
3935
  }
3936
+ /** A UTC ISO timestamp, as `Date.prototype.toISOString` and most clients write one. */
3937
+ var ISO_DATE_TEXT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/;
3938
+ /** `Date.prototype.toString()`'s form, which Postgres cannot parse. */
3939
+ var JS_DATE_TEXT = /^\w{3} \w{3} \d{2} \d{4} \d{2}:\d{2}:\d{2} GMT[+-]\d{4} \(.+\)$/;
3940
+ /** A date property's text in the ISO form Postgres reads; anything else as it came. */
3941
+ function normalizeDateText(value) {
3942
+ if (typeof value !== "string" || !(ISO_DATE_TEXT.test(value) || JS_DATE_TEXT.test(value))) return value;
3943
+ const date = new Date(value);
3944
+ return isNaN(date.getTime()) ? value : date.toISOString();
3945
+ }
3871
3946
  /**
3872
3947
  * Transform relations for database storage (relation objects to IDs)
3873
3948
  */
@@ -3983,7 +4058,8 @@ function serializePropertyToServer(value, property, propertyKey) {
3983
4058
  if (value === null || value === void 0) return value;
3984
4059
  const fieldLabel = propertyKey ? `'${propertyKey}'` : `a '${property.type}' field`;
3985
4060
  switch (property.type) {
3986
- case "date": return property.columnType === "time" ? dateToTimeOfDay(value) : value;
4061
+ case "date": return property.columnType === "time" ? dateToTimeOfDay(value) : normalizeDateText(value);
4062
+ case "number": return typeof value === "string" && value.trim().toLowerCase() === "nan" ? null : value;
3987
4063
  case "relation":
3988
4064
  if (Array.isArray(value)) return value.map((v) => serializePropertyToServer(v, property, propertyKey));
3989
4065
  else if (typeof value === "object" && value !== null && "id" in value) return value.id;
@@ -4406,7 +4482,8 @@ function isJunctionBackedRelation(relation) {
4406
4482
  }
4407
4483
  /**
4408
4484
  * Reject a create or update whose body names a relation Rebase does not write
4409
- * — a multi-hop `via` (`writable: false`) — before anything is read or written.
4485
+ * — a multi-hop `via`, or one whose step joins on either of several columns
4486
+ * (`writable: false`) — before anything is read or written.
4410
4487
  *
4411
4488
  * A single-hop `via` is written through this door as it shipped (to-one: the
4412
4489
  * target's key; to-many: one junction's rows). A longer chain used to get as
@@ -4431,7 +4508,7 @@ function assertNoReadOnlyRelationValues(values, collection) {
4431
4508
  }
4432
4509
  if (named.length === 0) return;
4433
4510
  const list = named.map((key) => `'${key}'`).join(", ");
4434
- throw ApiError.badRequest(`${list} on '${collection.slug}' ${named.length > 1 ? "are multi-hop `via` relations" : "is a multi-hop `via` relation"}, which is read-only: only single-hop via relations are writable — a to-one chain of one step, or a to-many chain through one junction table. Write the rows the chain joins directly.`, "RELATION_NOT_WRITABLE", {
4511
+ throw ApiError.badRequest(`${list} on '${collection.slug}' ${named.length > 1 ? "are multi-hop or either-column `via` relations" : "is a multi-hop or either-column `via` relation"}, which is read-only: only single-hop via relations are writable — a to-one chain of one step, or a to-many chain through one junction table, each step joining on one condition. Write the rows the chain joins directly.`, "RELATION_NOT_WRITABLE", {
4435
4512
  collection: collection.slug,
4436
4513
  fields: named
4437
4514
  });
@@ -4772,7 +4849,7 @@ var FetchService = class FetchService {
4772
4849
  return expressions;
4773
4850
  }
4774
4851
  resolveOrderByField(table, orderBy, collection) {
4775
- const columnAt = (key) => (key in table ? table[key] : void 0) || void 0;
4852
+ const columnAt = (key) => columnAtKey(table, key);
4776
4853
  const direct = columnAt(orderBy);
4777
4854
  if (direct) return direct;
4778
4855
  const declaredRelation = collection ? resolveCollectionRelations(collection)[orderBy] : void 0;
@@ -4780,7 +4857,7 @@ var FetchService = class FetchService {
4780
4857
  const foreignKey = columnAt(fieldKeyForColumn(collection, declaredRelation.localKey));
4781
4858
  if (foreignKey) return foreignKey;
4782
4859
  }
4783
- for (const guess of [
4860
+ if (!collection) for (const guess of [
4784
4861
  `${orderBy}Id`,
4785
4862
  toWireKey(generateForeignKeyName(orderBy)),
4786
4863
  `${orderBy}_id`,
@@ -6052,7 +6129,7 @@ var RelationWriteService = class {
6052
6129
  const parsedNewTargetId = parseIdValues$1(newValue, targetPks)[targetIdInfo.fieldName];
6053
6130
  const targetIdField = targetTable[targetIdInfo.fieldName];
6054
6131
  await tx.update(targetTable).set({ [fkField]: null }).where(and(eq(foreignKeyColumn, sourceKeyValue), visible));
6055
- await tx.update(targetTable).set({ [fkField]: sourceKeyValue }).where(eq(targetIdField, parsedNewTargetId));
6132
+ await tx.update(targetTable).set({ [fkField]: sourceKeyValue }).where(and(eq(targetIdField, parsedNewTargetId), visible));
6056
6133
  }
6057
6134
  } catch (e) {
6058
6135
  if (e instanceof ApiError || e?.name === "ApiError" || reachedDatabase(e)) throw e;
@@ -6116,14 +6193,15 @@ var RelationWriteService = class {
6116
6193
  const parentRows = await tx.select({ val: parentSourceCol }).from(parentTable).where(eq(parentIdCol, parsedParentId)).limit(1);
6117
6194
  if (parentRows.length === 0) continue;
6118
6195
  const parentFKValue = parentRows[0].val;
6196
+ const visible = await this.reads.visibleTargetCondition(parentCollection, relation, parentId);
6119
6197
  if (newTargetId === null || newTargetId === void 0) {
6120
- if (parentFKValue !== null && parentFKValue !== void 0) await tx.update(targetTable).set({ [targetFKColName]: null }).where(eq(targetFKCol, String(parentFKValue)));
6198
+ if (parentFKValue !== null && parentFKValue !== void 0) await tx.update(targetTable).set({ [targetFKColName]: null }).where(and(eq(targetFKCol, String(parentFKValue)), visible));
6121
6199
  continue;
6122
6200
  }
6123
6201
  const parsedTargetId = parseIdValues$1(newTargetId, targetPks)[targetIdInfo.fieldName];
6124
- if (parentFKValue !== null && parentFKValue !== void 0) await tx.update(targetTable).set({ [targetFKColName]: null }).where(eq(targetFKCol, String(parentFKValue)));
6202
+ if (parentFKValue !== null && parentFKValue !== void 0) await tx.update(targetTable).set({ [targetFKColName]: null }).where(and(eq(targetFKCol, String(parentFKValue)), visible));
6125
6203
  else throw ApiError.badRequest(`Cannot write relation '${label}': row '${parentId}' has no value in '${parentSourceColName}', which is the column its joinPath joins on, so there is nothing for the related row to point at.`, "RELATION_SOURCE_KEY_EMPTY");
6126
- await tx.update(targetTable).set({ [targetFKColName]: parentFKValue }).where(eq(targetIdCol, parsedTargetId));
6204
+ await tx.update(targetTable).set({ [targetFKColName]: parentFKValue }).where(and(eq(targetIdCol, parsedTargetId), visible));
6127
6205
  }
6128
6206
  }
6129
6207
  /**
@@ -6134,18 +6212,19 @@ var RelationWriteService = class {
6134
6212
  const parentTableName = getTableName$1(parentCollection);
6135
6213
  const joinPath = relation.joinPath;
6136
6214
  let index = joinPath.length - 1;
6137
- const targetFKColName = DrizzleConditionBuilder.getColumnNamesFromColumns(joinPath[index].on.to)[0];
6138
- let currentFrom = joinPath[index].on.from;
6215
+ const targetFKColName = DrizzleConditionBuilder.getColumnNamesFromColumns(soleJoinCondition(joinPath[index]).to)[0];
6216
+ let currentFrom = soleJoinCondition(joinPath[index]).from;
6139
6217
  let safety = 0;
6140
6218
  while (safety++ < 10) {
6141
6219
  const qualifiedTable = DrizzleConditionBuilder.getTableNamesFromColumns(currentFrom)[0];
6142
6220
  if ((qualifiedTable || (index === 0 ? parentTableName : joinPath[index - 1].table)) === parentTableName) break;
6143
6221
  const prevIndex = qualifiedTable ? joinPath.findIndex((s) => {
6144
- return (Array.isArray(s.on.to) ? s.on.to[0] : s.on.to) === currentFrom;
6222
+ const on = soleJoinCondition(s);
6223
+ return (Array.isArray(on.to) ? on.to[0] : on.to) === currentFrom;
6145
6224
  }) : index - 1;
6146
6225
  if (prevIndex < 0) throw new Error(`Could not resolve parent source column for joinPath relation '${relation.relationName}'`);
6147
6226
  index = prevIndex;
6148
- currentFrom = joinPath[index].on.from;
6227
+ currentFrom = soleJoinCondition(joinPath[index]).from;
6149
6228
  }
6150
6229
  return {
6151
6230
  targetFKColName,
@@ -6162,8 +6241,8 @@ var RelationWriteService = class {
6162
6241
  const binding = bindThroughJunction(this.registry, relation.through, `${parentCollection.slug}.${relationKey}`);
6163
6242
  const parsedNewEntityId = this.parsedId(targetCollection, newEntityId);
6164
6243
  const junctionData = {
6165
- [binding.parentColumn.name]: parentId,
6166
- [binding.targetColumn.name]: parsedNewEntityId
6244
+ [binding.parentKey]: parentId,
6245
+ [binding.targetKey]: parsedNewEntityId
6167
6246
  };
6168
6247
  await tx.insert(binding.table).values(junctionData).onConflictDoNothing();
6169
6248
  logger.info(`Linked '${relationKey}' ${parsedNewEntityId} to ${parentId}`);
@@ -6813,7 +6892,7 @@ var PersistService = class {
6813
6892
  switch (relation.kind) {
6814
6893
  case "hasOne":
6815
6894
  case "hasMany": return fieldKeyForColumn(hop.targetCollection, relation.foreignKeyOnTarget);
6816
- case "via": return relation.joinPath.length === 1 ? fieldKeyForColumn(hop.targetCollection, DrizzleConditionBuilder.getColumnNamesFromColumns(relation.joinPath[0].on.to)[0]) : void 0;
6895
+ case "via": return relation.joinPath.length === 1 && joinConditions(relation.joinPath[0]).length === 1 ? fieldKeyForColumn(hop.targetCollection, DrizzleConditionBuilder.getColumnNamesFromColumns(joinConditions(relation.joinPath[0])[0].to)[0]) : void 0;
6817
6896
  default: return;
6818
6897
  }
6819
6898
  }
@@ -8198,6 +8277,135 @@ async function generateSchemaCommit(input) {
8198
8277
  };
8199
8278
  }
8200
8279
  //#endregion
8280
+ //#region src/schema/reconcile-schema.ts
8281
+ /**
8282
+ * The reviewed half of schema-as-code: what boot will not do.
8283
+ *
8284
+ * Boot is additive (`ConstraintPolicy` in `plan/diff-plan.ts`). It creates
8285
+ * tables, columns, enum values and indexes, and it never relaxes a constraint
8286
+ * or drops a column, because an unattended boot runs against customer data
8287
+ * with nobody reading a diff. That leaves two states a collection change can
8288
+ * reach and nothing in the platform can leave:
8289
+ *
8290
+ * - **A property made optional.** The column stays NOT NULL. Boot reports the
8291
+ * drift on every start and applies nothing.
8292
+ * - **A property removed.** The column stays, with its data. If it was NOT NULL
8293
+ * with no default, every insert through the API now fails on a field nobody
8294
+ * can send (`orphanedRequiredColumns`).
8295
+ *
8296
+ * This plans both, for a person to read. It relaxes every column the
8297
+ * collections declare optional and the database still requires — the
8298
+ * `drop-not-null` actions the live editor's `converge` policy already plans —
8299
+ * and it lists every column no collection declares. It drops only the columns
8300
+ * the caller names, one by one: removing a property is not consent to destroy
8301
+ * the column's data, so the drop is a separate, explicit act.
8302
+ *
8303
+ * Pure, given the database's catalogue. `readReconcileFacts` reads it and
8304
+ * counts the rows each drop destroys, which is the number someone confirming
8305
+ * a drop needs to see.
8306
+ */
8307
+ /** The table, quoted for SQL. Names come from the catalogue and the plan, both already validated. */
8308
+ var relation = (table) => `"${table.schema}"."${table.table}"`;
8309
+ /** Every column a collection's table carries that no property declares. */
8310
+ function undeclaredColumns(tables, existing) {
8311
+ const found = [];
8312
+ for (const table of tables) {
8313
+ const live = existing.tables.get(table.qualified);
8314
+ if (!live) continue;
8315
+ const declared = /* @__PURE__ */ new Set();
8316
+ for (const column of table.columns) {
8317
+ declared.add(column.column);
8318
+ if (column.legacyColumn) declared.add(column.legacyColumn);
8319
+ }
8320
+ for (const column of [...live].sort()) {
8321
+ if (declared.has(column)) continue;
8322
+ const key = `${table.qualified}.${column}`;
8323
+ found.push({
8324
+ collection: table.slug,
8325
+ table: table.qualified,
8326
+ column,
8327
+ blocksInserts: existing.notNullColumns?.has(key) === true && existing.columnDefaults?.has(key) !== true,
8328
+ tablePlan: table
8329
+ });
8330
+ }
8331
+ }
8332
+ return found;
8333
+ }
8334
+ function planSchemaReconcile(collections, existing, options = {}) {
8335
+ const plan = planSchema(collections);
8336
+ const tables = plan.tables.filter((table) => table.kind === "collection" && table.slug);
8337
+ const converge = diffPlanAgainstCatalogue(plan, existing, { constraints: "converge" });
8338
+ const relaxed = [];
8339
+ const relaxStatements = [];
8340
+ for (const action of converge.actions) {
8341
+ if (action.kind !== "drop-not-null") continue;
8342
+ const table = tables.find((t) => action.target.startsWith(`${t.qualified}.`));
8343
+ if (!table) continue;
8344
+ relaxed.push({
8345
+ collection: table.slug,
8346
+ table: table.qualified,
8347
+ column: action.target.slice(table.qualified.length + 1)
8348
+ });
8349
+ relaxStatements.push(action.sql);
8350
+ }
8351
+ const undeclared = undeclaredColumns(tables, existing);
8352
+ const problems = [];
8353
+ const dropped = [];
8354
+ for (const request of new Set(options.drop ?? [])) {
8355
+ const dot = request.indexOf(".");
8356
+ const slug = dot > 0 ? request.slice(0, dot) : "";
8357
+ const column = dot > 0 ? request.slice(dot + 1) : "";
8358
+ if (!slug || !column) {
8359
+ problems.push(`"${request}" is not <collection>.<column>.`);
8360
+ continue;
8361
+ }
8362
+ const table = tables.find((t) => t.slug === slug);
8363
+ if (!table) {
8364
+ problems.push(`"${request}": no collection "${slug}".`);
8365
+ continue;
8366
+ }
8367
+ const match = undeclared.find((u) => u.collection === slug && u.column === column);
8368
+ if (match) {
8369
+ dropped.push(match);
8370
+ continue;
8371
+ }
8372
+ problems.push(table.columns.some((c) => c.column === column) ? `"${request}": the "${slug}" collection still declares "${column}". Remove the property, deploy, then drop it.` : `"${request}": "${table.qualified}" has no column "${column}".`);
8373
+ }
8374
+ const statements = [...relaxStatements, ...dropped.map((d) => `ALTER TABLE ${relation(d.tablePlan)} DROP COLUMN "${d.column}";`)];
8375
+ const strip = ({ tablePlan: _table, ...column }) => column;
8376
+ return {
8377
+ relaxed,
8378
+ undeclared: undeclared.map(strip),
8379
+ dropped: dropped.map(strip),
8380
+ statements,
8381
+ problems,
8382
+ digest: reconcileDigest(statements)
8383
+ };
8384
+ }
8385
+ /**
8386
+ * The identity of a plan: its statements, in order. A caller applies only on
8387
+ * the digest it was shown, so the statements that run are the ones read.
8388
+ */
8389
+ function reconcileDigest(statements) {
8390
+ return createHash("sha256").update(statements.join("\n")).digest("hex").slice(0, 16);
8391
+ }
8392
+ /**
8393
+ * Plan against a live database, with the row counts a drop destroys.
8394
+ *
8395
+ * The count is the column's non-null values. It is what turns "drop
8396
+ * songbuzz_voice_cache.wav" into "drop 12,408 recordings", which is the
8397
+ * sentence a person should be agreeing to.
8398
+ */
8399
+ async function readReconcileFacts(client, collections, options = {}) {
8400
+ const plan = planSchemaReconcile(collections, await readSchemaFactsFor(client, collections), options);
8401
+ for (const column of plan.dropped) {
8402
+ const [schema, table] = column.table.split(".");
8403
+ const { rows } = await client.query(`SELECT count(*) AS n FROM "${schema}"."${table}" WHERE "${column.column}" IS NOT NULL`);
8404
+ column.rowsWithValue = Number(rows[0]?.n ?? 0);
8405
+ }
8406
+ return plan;
8407
+ }
8408
+ //#endregion
8201
8409
  //#region src/services/sql-script.ts
8202
8410
  /**
8203
8411
  * What the Studio console keeps of a result: its first 10,000 rows, or its
@@ -8962,7 +9170,7 @@ async function openSqlCursorOnPool(pool, sql, options = {}) {
8962
9170
  * `LISTEN` is session state, so this connection must not go through a
8963
9171
  * transaction-mode pooler (PgBouncer): give it the direct database URL.
8964
9172
  */
8965
- var DEFAULT_RECONNECT_DELAY_MS = 3e3;
9173
+ var DEFAULT_RECONNECT_DELAY_MS$1 = 3e3;
8966
9174
  /** Guards the identifier interpolated into `LISTEN`. */
8967
9175
  var SAFE_CHANNEL = /^[A-Za-z_][A-Za-z0-9_]*$/;
8968
9176
  /**
@@ -9100,7 +9308,7 @@ var PgNotifyListener = class {
9100
9308
  await pending.end();
9101
9309
  } catch {}
9102
9310
  if (first && !this.options.retryInitialConnect) throw err;
9103
- const retry = `${logLabel} LISTEN connection could not be opened (${connectFailureReason(err)}); retrying every ${Math.round((this.options.reconnectDelayMs ?? DEFAULT_RECONNECT_DELAY_MS) / 1e3)} s.`;
9311
+ const retry = `${logLabel} LISTEN connection could not be opened (${connectFailureReason(err)}); retrying every ${Math.round((this.options.reconnectDelayMs ?? DEFAULT_RECONNECT_DELAY_MS$1) / 1e3)} s.`;
9104
9312
  if (this.outageWarned) logger.debug(retry);
9105
9313
  else {
9106
9314
  this.outageWarned = true;
@@ -9175,7 +9383,7 @@ var PgNotifyListener = class {
9175
9383
  this.reconnectTimer = void 0;
9176
9384
  if (!this.running) return;
9177
9385
  await this.connect();
9178
- }, this.options.reconnectDelayMs ?? DEFAULT_RECONNECT_DELAY_MS);
9386
+ }, this.options.reconnectDelayMs ?? DEFAULT_RECONNECT_DELAY_MS$1);
9179
9387
  }
9180
9388
  };
9181
9389
  //#endregion
@@ -9539,6 +9747,7 @@ var PostgresBackendDriver = class PostgresBackendDriver {
9539
9747
  sourceOnly: options?.sourceOnly,
9540
9748
  existing: await readSchemaFactsFor(this.schemaFactsQueryable(), after)
9541
9749
  }),
9750
+ planSchemaReconcile: async (collections, options) => readReconcileFacts(this.schemaFactsQueryable(), collections, options),
9542
9751
  listen: (...args) => this.listen(...args),
9543
9752
  ...this.branchService ? {
9544
9753
  createBranch: this.branchService.createBranch.bind(this.branchService),
@@ -10547,6 +10756,7 @@ var PostgresBackendDriver = class PostgresBackendDriver {
10547
10756
  const values = operation.values ? resolveBatchRefs(operation.values, named) : void 0;
10548
10757
  const rawId = operation.id !== void 0 ? resolveBatchRefs(operation.id, named) : void 0;
10549
10758
  const id = rawId === void 0 || rawId === null ? void 0 : String(rawId);
10759
+ if ((operation.op === "update" || operation.op === "delete") && (id === void 0 || id === "")) throw ApiError.badRequest(`This ${operation.op} has no id: its \`id\` resolved to ${rawId === "" ? "an empty string" : String(rawId)}.`, "INVALID_BATCH_REF");
10550
10760
  let row = null;
10551
10761
  switch (operation.op) {
10552
10762
  case "create":
@@ -10616,6 +10826,7 @@ var PostgresBackendDriver = class PostgresBackendDriver {
10616
10826
  if (precondition) await precondition.check(await this.asRestServes(stored, targetPath));
10617
10827
  if (!stored) throw ApiError.notFound(`No row "${row.id}" in "${targetPath}" to delete.`);
10618
10828
  const targetRow = { ...stored };
10829
+ const address = (resolvedCollection ? deriveRowAddress(stored, resolvedCollection, this.registry) : "") || row.id.toString();
10619
10830
  const contextForCallback = this.buildCallContext();
10620
10831
  try {
10621
10832
  frame.stage = "beforeDelete";
@@ -10688,18 +10899,18 @@ var PostgresBackendDriver = class PostgresBackendDriver {
10688
10899
  }
10689
10900
  if (this.historyService && resolvedCollection?.history && !unlinking) await this.historyService.recordHistory({
10690
10901
  tableName: resolvedCollection.slug,
10691
- id: row.id.toString(),
10902
+ id: address,
10692
10903
  action: "delete",
10693
10904
  values: stored,
10694
10905
  updatedBy: this.user?.uid
10695
10906
  });
10696
10907
  if (this._deferNotifications) this._pendingNotifications.push({
10697
10908
  path: targetPath,
10698
- id: row.id.toString(),
10909
+ id: address,
10699
10910
  row: null,
10700
10911
  databaseId: resolvedCollection?.databaseId
10701
10912
  });
10702
- else await this.realtimeService.notifyUpdate(targetPath, row.id.toString(), null, resolvedCollection?.databaseId);
10913
+ else await this.realtimeService.notifyUpdate(targetPath, address, null, resolvedCollection?.databaseId);
10703
10914
  }
10704
10915
  /**
10705
10916
  * The child lists a save rewrote through the parent, as notifications —
@@ -11838,8 +12049,8 @@ var MAX_NOTIFY_BYTES = 7900;
11838
12049
  * notification.
11839
12050
  */
11840
12051
  var CHUNK_SLACK_BYTES = 64;
11841
- var quoteIdent = (name) => `"${name.replace(/"/g, "\"\"")}"`;
11842
- var quoteLiteral = (value) => `'${value.replace(/'/g, "''")}'`;
12052
+ var quoteIdent$1 = (name) => `"${name.replace(/"/g, "\"\"")}"`;
12053
+ var quoteLiteral$1 = (value) => `'${value.replace(/'/g, "''")}'`;
11843
12054
  /**
11844
12055
  * The row-level function: `{ schema, table, op, row }`, one per changed row.
11845
12056
  *
@@ -11922,7 +12133,7 @@ BEGIN
11922
12133
  )::text;
11923
12134
  END IF;
11924
12135
 
11925
- PERFORM pg_notify(${quoteLiteral(CDC_CHANNEL)}, payload);
12136
+ PERFORM pg_notify(${quoteLiteral$1(CDC_CHANNEL)}, payload);
11926
12137
  RETURN NULL;
11927
12138
  END;
11928
12139
  $rebase_cdc$;
@@ -11957,7 +12168,7 @@ var COMPAT_PATTERN = `${COMPAT_DECLARATION}'([^']*)'`;
11957
12168
  * document is fetched to name its row.
11958
12169
  */
11959
12170
  function buildCdcStatementFunctionSql(compatUntil) {
11960
- const channel = quoteLiteral(CDC_CHANNEL);
12171
+ const channel = quoteLiteral$1(CDC_CHANNEL);
11961
12172
  const idEnvelope = `,"cols":["id"],"keys":[]}`;
11962
12173
  const chunked = (source, keyExpr) => `SELECT string_agg(k, ',') AS keys
11963
12174
  FROM (SELECT k, (sum(octet_length(k) + 1) OVER (ROWS UNBOUNDED PRECEDING) - 1) / budget AS c
@@ -11973,7 +12184,7 @@ CREATE OR REPLACE FUNCTION ${CDC_STATEMENT_FUNCTION}() RETURNS trigger
11973
12184
  LANGUAGE plpgsql AS $rebase_cdc$
11974
12185
  DECLARE
11975
12186
  -- One row per notification until then: see CDC_ROW_COMPAT_MS in trigger-cdc.ts.
11976
- ${COMPAT_DECLARATION}${quoteLiteral(compatUntil)};
12187
+ ${COMPAT_DECLARATION}${quoteLiteral$1(compatUntil)};
11977
12188
  head text;
11978
12189
  cols text[];
11979
12190
  collist text;
@@ -12108,8 +12319,8 @@ $rebase_cdc$;
12108
12319
  * function serves every table. Empty means "the primary key".
12109
12320
  */
12110
12321
  function buildCdcTriggerSql(schema, table, identityColumns = [], level = "statement") {
12111
- const qualified = `${quoteIdent(schema)}.${quoteIdent(table)}`;
12112
- return ALL_TRIGGER_NAMES.map((name) => `DROP TRIGGER IF EXISTS ${quoteIdent(name)} ON ${qualified};\n`).join("") + buildCdcTriggerCreateSql(schema, table, identityColumns, level);
12322
+ const qualified = `${quoteIdent$1(schema)}.${quoteIdent$1(table)}`;
12323
+ return ALL_TRIGGER_NAMES.map((name) => `DROP TRIGGER IF EXISTS ${quoteIdent$1(name)} ON ${qualified};\n`).join("") + buildCdcTriggerCreateSql(schema, table, identityColumns, level);
12113
12324
  }
12114
12325
  /**
12115
12326
  * `CREATE TRIGGER` alone, for a table that has no capture trigger yet.
@@ -12119,15 +12330,15 @@ function buildCdcTriggerSql(schema, table, identityColumns = [], level = "statem
12119
12330
  * `CREATE TRIGGER` takes SHARE ROW EXCLUSIVE, which readers never wait for.
12120
12331
  */
12121
12332
  var buildCdcTriggerCreateSql = (schema, table, identityColumns, level) => {
12122
- const qualified = `${quoteIdent(schema)}.${quoteIdent(table)}`;
12123
- const args = [...new Set(identityColumns)].map(quoteLiteral).join(", ");
12124
- return triggersFor(level).map((spec) => `CREATE TRIGGER ${quoteIdent(spec.name)} ${spec.timing} ON ${qualified} ${spec.forEach} EXECUTE FUNCTION ${spec.fn === "statement" ? CDC_STATEMENT_FUNCTION : CDC_TRIGGER_FUNCTION}(${args});`).join("\n");
12333
+ const qualified = `${quoteIdent$1(schema)}.${quoteIdent$1(table)}`;
12334
+ const args = [...new Set(identityColumns)].map(quoteLiteral$1).join(", ");
12335
+ return triggersFor(level).map((spec) => `CREATE TRIGGER ${quoteIdent$1(spec.name)} ${spec.timing} ON ${qualified} ${spec.forEach} EXECUTE FUNCTION ${spec.fn === "statement" ? CDC_STATEMENT_FUNCTION : CDC_TRIGGER_FUNCTION}(${args});`).join("\n");
12125
12336
  };
12126
12337
  /** `to_regprocedure` of each function, as a SQL expression. */
12127
- var ROW_FUNCTION_OID = `to_regprocedure(${quoteLiteral(`${CDC_TRIGGER_FUNCTION}()`)})`;
12128
- var STATEMENT_FUNCTION_OID = `to_regprocedure(${quoteLiteral(`${CDC_STATEMENT_FUNCTION}()`)})`;
12338
+ var ROW_FUNCTION_OID = `to_regprocedure(${quoteLiteral$1(`${CDC_TRIGGER_FUNCTION}()`)})`;
12339
+ var STATEMENT_FUNCTION_OID = `to_regprocedure(${quoteLiteral$1(`${CDC_STATEMENT_FUNCTION}()`)})`;
12129
12340
  /** The installed statement function's `compat_until` literal, or NULL when it is not installed. */
12130
- var INSTALLED_COMPAT_SQL = `(SELECT substring(p.prosrc FROM ${quoteLiteral(COMPAT_PATTERN)}) FROM pg_proc p WHERE p.oid = ${STATEMENT_FUNCTION_OID})`;
12341
+ var INSTALLED_COMPAT_SQL = `(SELECT substring(p.prosrc FROM ${quoteLiteral$1(COMPAT_PATTERN)}) FROM pg_proc p WHERE p.oid = ${STATEMENT_FUNCTION_OID})`;
12131
12342
  /** The window end a `compat_until` literal names, in epoch ms; `-Infinity` for none or unreadable. */
12132
12343
  var compatMs = (literal) => {
12133
12344
  if (typeof literal !== "string" || literal === "" || literal === "-infinity") return -Infinity;
@@ -12151,7 +12362,7 @@ var functionBody = (sql) => sql.slice(sql.indexOf(DOLLAR_QUOTE) + 12, sql.lastIn
12151
12362
  var COMPAT_LINE_SQL_PATTERN = `${COMPAT_DECLARATION}'[^']*'`;
12152
12363
  var withoutCompat = (body) => body.replace(new RegExp(COMPAT_LINE_SQL_PATTERN), "");
12153
12364
  /** Whether each installed function is the current one, as SQL columns of one row. */
12154
- var functionsCurrentSql = () => `(SELECT p.prosrc = ${quoteLiteral(functionBody(buildCdcRowFunctionSql()))} FROM pg_proc p WHERE p.oid = ${ROW_FUNCTION_OID}) AS row_function_current, (SELECT regexp_replace(p.prosrc, ${quoteLiteral(COMPAT_LINE_SQL_PATTERN)}, '') = ${quoteLiteral(withoutCompat(functionBody(buildCdcStatementFunctionSql("-infinity"))))} FROM pg_proc p WHERE p.oid = ${STATEMENT_FUNCTION_OID}) AS statement_code_current, ${INSTALLED_COMPAT_SQL} AS compat_until`;
12365
+ var functionsCurrentSql = () => `(SELECT p.prosrc = ${quoteLiteral$1(functionBody(buildCdcRowFunctionSql()))} FROM pg_proc p WHERE p.oid = ${ROW_FUNCTION_OID}) AS row_function_current, (SELECT regexp_replace(p.prosrc, ${quoteLiteral$1(COMPAT_LINE_SQL_PATTERN)}, '') = ${quoteLiteral$1(withoutCompat(functionBody(buildCdcStatementFunctionSql("-infinity"))))} FROM pg_proc p WHERE p.oid = ${STATEMENT_FUNCTION_OID}) AS statement_code_current, ${INSTALLED_COMPAT_SQL} AS compat_until`;
12155
12366
  var readInstalledFunctions = (row) => ({
12156
12367
  rowCurrent: row?.row_function_current === true,
12157
12368
  statementCodeCurrent: row?.statement_code_current === true,
@@ -12243,11 +12454,11 @@ async function settleInstalledCapture(run, options) {
12243
12454
  */
12244
12455
  async function resolveIdentityColumns(run, ref) {
12245
12456
  const requested = ref.identityColumns ?? [];
12246
- const qualified = `${quoteIdent(ref.schema)}.${quoteIdent(ref.table)}`;
12457
+ const qualified = `${quoteIdent$1(ref.schema)}.${quoteIdent$1(ref.table)}`;
12247
12458
  let columns;
12248
12459
  let relkind = null;
12249
12460
  try {
12250
- const rows = await run(`SELECT a.attname AS name, coalesce(a.attnum = ANY (i.indkey), false) AS is_primary, c.relkind::text AS relkind FROM pg_attribute a JOIN pg_class c ON c.oid = a.attrelid LEFT JOIN pg_index i ON i.indrelid = a.attrelid AND i.indisprimary WHERE a.attrelid = to_regclass(${quoteLiteral(qualified)}) AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum`);
12461
+ const rows = await run(`SELECT a.attname AS name, coalesce(a.attnum = ANY (i.indkey), false) AS is_primary, c.relkind::text AS relkind FROM pg_attribute a JOIN pg_class c ON c.oid = a.attrelid LEFT JOIN pg_index i ON i.indrelid = a.attrelid AND i.indisprimary WHERE a.attrelid = to_regclass(${quoteLiteral$1(qualified)}) AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum`);
12251
12462
  columns = rows.map((row) => ({
12252
12463
  name: String(row.name),
12253
12464
  primary: row.is_primary === true
@@ -12287,7 +12498,7 @@ var decodeTriggerArgs = (hex) => {
12287
12498
  * 4.4 s while a report read the table.
12288
12499
  */
12289
12500
  async function readCaptureState(run, refs) {
12290
- const rows = await run(`WITH target(key, rel) AS (VALUES ${refs.map((ref) => `(${quoteLiteral(`${ref.schema}.${ref.table}`)}, to_regclass(${quoteLiteral(`${quoteIdent(ref.schema)}.${quoteIdent(ref.table)}`)}))`).join(", ")})
12501
+ const rows = await run(`WITH target(key, rel) AS (VALUES ${refs.map((ref) => `(${quoteLiteral$1(`${ref.schema}.${ref.table}`)}, to_regclass(${quoteLiteral$1(`${quoteIdent$1(ref.schema)}.${quoteIdent$1(ref.table)}`)}))`).join(", ")})
12291
12502
  SELECT t.key,
12292
12503
  (SELECT c.relkind::text FROM pg_class c WHERE c.oid = t.rel) AS relkind,
12293
12504
  (SELECT coalesce(json_agg(json_build_object('name', a.attname,
@@ -12309,7 +12520,7 @@ async function readCaptureState(run, refs) {
12309
12520
  'newTable', tr.tgnewtable,
12310
12521
  'oldTable', tr.tgoldtable)), '[]'::json)
12311
12522
  FROM pg_trigger tr
12312
- WHERE tr.tgrelid = t.rel AND tr.tgname IN (${ALL_TRIGGER_NAMES.map(quoteLiteral).join(", ")})) AS triggers,
12523
+ WHERE tr.tgrelid = t.rel AND tr.tgname IN (${ALL_TRIGGER_NAMES.map(quoteLiteral$1).join(", ")})) AS triggers,
12313
12524
  f.*
12314
12525
  FROM target t, (SELECT ${functionsCurrentSql()}) f`);
12315
12526
  const parseJson = (value) => typeof value === "string" ? JSON.parse(value) : value;
@@ -12599,6 +12810,853 @@ var CdcListener = class {
12599
12810
  }
12600
12811
  };
12601
12812
  //#endregion
12813
+ //#region src/services/cdc/pgoutput.ts
12814
+ /** Microseconds between the Unix epoch and Postgres's, 2000-01-01. */
12815
+ var PG_EPOCH_OFFSET_US = 946684800000000n;
12816
+ /** A cursor over one message. Reading past the end throws, which the caller treats as a malformed frame. */
12817
+ var Reader = class {
12818
+ buf;
12819
+ offset = 0;
12820
+ constructor(buf) {
12821
+ this.buf = buf;
12822
+ }
12823
+ byte() {
12824
+ this.need(1);
12825
+ return String.fromCharCode(this.buf[this.offset++]);
12826
+ }
12827
+ int8() {
12828
+ this.need(1);
12829
+ return this.buf.readInt8(this.offset++);
12830
+ }
12831
+ int16() {
12832
+ this.need(2);
12833
+ const value = this.buf.readInt16BE(this.offset);
12834
+ this.offset += 2;
12835
+ return value;
12836
+ }
12837
+ int32() {
12838
+ this.need(4);
12839
+ const value = this.buf.readInt32BE(this.offset);
12840
+ this.offset += 4;
12841
+ return value;
12842
+ }
12843
+ uint32() {
12844
+ this.need(4);
12845
+ const value = this.buf.readUInt32BE(this.offset);
12846
+ this.offset += 4;
12847
+ return value;
12848
+ }
12849
+ uint64() {
12850
+ this.need(8);
12851
+ const value = this.buf.readBigUInt64BE(this.offset);
12852
+ this.offset += 8;
12853
+ return value;
12854
+ }
12855
+ /** A NUL-terminated string. */
12856
+ cstring() {
12857
+ const end = this.buf.indexOf(0, this.offset);
12858
+ if (end < 0) throw new Error("unterminated string");
12859
+ const value = this.buf.toString("utf8", this.offset, end);
12860
+ this.offset = end + 1;
12861
+ return value;
12862
+ }
12863
+ bytes(length) {
12864
+ this.need(length);
12865
+ const value = this.buf.subarray(this.offset, this.offset + length);
12866
+ this.offset += length;
12867
+ return value;
12868
+ }
12869
+ rest() {
12870
+ return this.buf.subarray(this.offset);
12871
+ }
12872
+ need(length) {
12873
+ if (this.offset + length > this.buf.length) throw new Error("message ends early");
12874
+ }
12875
+ };
12876
+ /** The payload of one CopyData frame from a replication stream. */
12877
+ function parseReplicationFrame(payload) {
12878
+ const reader = new Reader(payload);
12879
+ const kind = reader.byte();
12880
+ if (kind === "w") {
12881
+ const walStart = reader.uint64();
12882
+ const walEnd = reader.uint64();
12883
+ reader.uint64();
12884
+ return {
12885
+ kind: "xlog",
12886
+ walStart,
12887
+ walEnd,
12888
+ data: reader.rest()
12889
+ };
12890
+ }
12891
+ if (kind === "k") {
12892
+ const walEnd = reader.uint64();
12893
+ reader.uint64();
12894
+ return {
12895
+ kind: "keepalive",
12896
+ walEnd,
12897
+ replyRequested: reader.byte() === ""
12898
+ };
12899
+ }
12900
+ throw new Error(`unknown replication frame '${kind}'`);
12901
+ }
12902
+ function readTuple(reader) {
12903
+ const count = reader.int16();
12904
+ const values = [];
12905
+ for (let i = 0; i < count; i++) {
12906
+ const kind = reader.byte();
12907
+ if (kind === "n") values.push(null);
12908
+ else if (kind === "u") values.push(void 0);
12909
+ else if (kind === "t") values.push(reader.bytes(reader.int32()).toString("utf8"));
12910
+ else if (kind === "b") {
12911
+ reader.bytes(reader.int32());
12912
+ values.push(void 0);
12913
+ } else throw new Error(`unknown tuple column kind '${kind}'`);
12914
+ }
12915
+ return values;
12916
+ }
12917
+ /** One `pgoutput` message: the data of an `xlog` frame. */
12918
+ function parsePgoutputMessage(data) {
12919
+ const reader = new Reader(data);
12920
+ const type = reader.byte();
12921
+ switch (type) {
12922
+ case "B": {
12923
+ const finalLsn = reader.uint64();
12924
+ reader.uint64();
12925
+ return {
12926
+ tag: "begin",
12927
+ finalLsn,
12928
+ xid: reader.uint32()
12929
+ };
12930
+ }
12931
+ case "C":
12932
+ reader.int8();
12933
+ return {
12934
+ tag: "commit",
12935
+ commitLsn: reader.uint64(),
12936
+ endLsn: reader.uint64()
12937
+ };
12938
+ case "R": {
12939
+ const id = reader.uint32();
12940
+ const schema = reader.cstring();
12941
+ const table = reader.cstring();
12942
+ const replicaIdentity = String.fromCharCode(reader.int8());
12943
+ const count = reader.int16();
12944
+ const columns = [];
12945
+ for (let i = 0; i < count; i++) {
12946
+ const flags = reader.int8();
12947
+ const name = reader.cstring();
12948
+ const typeOid = reader.uint32();
12949
+ reader.int32();
12950
+ columns.push({
12951
+ name,
12952
+ isKey: (flags & 1) === 1,
12953
+ typeOid
12954
+ });
12955
+ }
12956
+ return {
12957
+ tag: "relation",
12958
+ relation: {
12959
+ id,
12960
+ schema: schema || "pg_catalog",
12961
+ table,
12962
+ replicaIdentity,
12963
+ columns
12964
+ }
12965
+ };
12966
+ }
12967
+ case "I": {
12968
+ const relationId = reader.uint32();
12969
+ if (reader.byte() !== "N") throw new Error("insert without a new tuple");
12970
+ return {
12971
+ tag: "insert",
12972
+ relationId,
12973
+ newTuple: readTuple(reader)
12974
+ };
12975
+ }
12976
+ case "U": {
12977
+ const relationId = reader.uint32();
12978
+ let marker = reader.byte();
12979
+ let oldTuple;
12980
+ let oldIsKeyOnly;
12981
+ if (marker === "K" || marker === "O") {
12982
+ oldIsKeyOnly = marker === "K";
12983
+ oldTuple = readTuple(reader);
12984
+ marker = reader.byte();
12985
+ }
12986
+ if (marker !== "N") throw new Error("update without a new tuple");
12987
+ return {
12988
+ tag: "update",
12989
+ relationId,
12990
+ oldTuple,
12991
+ oldIsKeyOnly,
12992
+ newTuple: readTuple(reader)
12993
+ };
12994
+ }
12995
+ case "D": {
12996
+ const relationId = reader.uint32();
12997
+ const marker = reader.byte();
12998
+ if (marker !== "K" && marker !== "O") throw new Error("delete without an old tuple");
12999
+ return {
13000
+ tag: "delete",
13001
+ relationId,
13002
+ oldIsKeyOnly: marker === "K",
13003
+ oldTuple: readTuple(reader)
13004
+ };
13005
+ }
13006
+ case "T": {
13007
+ const count = reader.int32();
13008
+ reader.int8();
13009
+ const relationIds = [];
13010
+ for (let i = 0; i < count; i++) relationIds.push(reader.uint32());
13011
+ return {
13012
+ tag: "truncate",
13013
+ relationIds
13014
+ };
13015
+ }
13016
+ default: return {
13017
+ tag: "other",
13018
+ type
13019
+ };
13020
+ }
13021
+ }
13022
+ /**
13023
+ * A standby status update: everything up to `lsn` is written, flushed and
13024
+ * applied. Capture keeps nothing, so the three positions are the same — the
13025
+ * end of the last transaction handed on.
13026
+ *
13027
+ * @param replyRequested Ask the server to answer at once with a keepalive,
13028
+ * which is how the connection proves it is still alive.
13029
+ */
13030
+ function encodeStandbyStatus(lsn, replyRequested, nowMs = Date.now()) {
13031
+ const buf = Buffer.alloc(34);
13032
+ buf.write("r", 0, "latin1");
13033
+ buf.writeBigUInt64BE(lsn, 1);
13034
+ buf.writeBigUInt64BE(lsn, 9);
13035
+ buf.writeBigUInt64BE(lsn, 17);
13036
+ buf.writeBigUInt64BE(BigInt(nowMs) * 1000n - PG_EPOCH_OFFSET_US, 25);
13037
+ buf.writeUInt8(replyRequested ? 1 : 0, 33);
13038
+ return buf;
13039
+ }
13040
+ /** A frontend CopyData message around `payload`: `d`, a length that counts itself, the bytes. */
13041
+ function copyDataFrame(payload) {
13042
+ const header = Buffer.alloc(5);
13043
+ header.write("d", 0, "latin1");
13044
+ header.writeUInt32BE(payload.length + 4, 1);
13045
+ return Buffer.concat([header, payload]);
13046
+ }
13047
+ /** `16/B374D848`, the way Postgres writes an LSN. */
13048
+ function formatLsn(lsn) {
13049
+ return `${(lsn >> 32n).toString(16).toUpperCase()}/${(lsn & 4294967295n).toString(16).toUpperCase()}`;
13050
+ }
13051
+ var BOOL_OID = 16;
13052
+ /**
13053
+ * A key value as the trigger would have sent it, for the cases where text and
13054
+ * JSON differ. Addresses are built with `String()`, so an integer or a uuid
13055
+ * reads the same either way; a boolean is `t` as text and `true` as JSON.
13056
+ */
13057
+ function keyValue(column, value) {
13058
+ if (value !== null && column.typeOid === BOOL_OID) return value === "t" ? "true" : "false";
13059
+ return value;
13060
+ }
13061
+ /**
13062
+ * The columns that name a row, read from one tuple: the replica identity (the
13063
+ * primary key, by default) and each requested column the table has — the
13064
+ * trigger's rule, so both name a row with the same columns (see
13065
+ * `resolveIdentityColumns` in trigger-cdc.ts).
13066
+ *
13067
+ * `wanted` is what provisioning asked for: the collection's key, or a
13068
+ * junction's two ids. A column the tuple cannot supply is left out — an old
13069
+ * key-only tuple carries the replica identity and sends every other column as
13070
+ * a null it never had. The key goes whole or not at all, because half a
13071
+ * composite key addresses the wrong row. With nothing left the row is
13072
+ * unaddressable, and the change is announced as one to the whole table, as the
13073
+ * trigger does with a key too large to send.
13074
+ */
13075
+ function identityOf(relation, tuple, wanted, keyOnly) {
13076
+ const read = (name) => {
13077
+ const index = relation.columns.findIndex((column) => column.name === name);
13078
+ if (index < 0) return void 0;
13079
+ const column = relation.columns[index];
13080
+ if (keyOnly && !column.isKey) return void 0;
13081
+ const value = tuple[index];
13082
+ return value === void 0 ? void 0 : { value: keyValue(column, value) };
13083
+ };
13084
+ const row = {};
13085
+ const key = relation.columns.filter((column) => column.isKey).map((column) => column.name);
13086
+ const keyValues = key.map(read);
13087
+ if (keyValues.every((cell) => cell !== void 0)) key.forEach((name, i) => {
13088
+ row[name] = keyValues[i]?.value;
13089
+ });
13090
+ for (const name of wanted ?? []) {
13091
+ if (name in row) continue;
13092
+ const cell = read(name);
13093
+ if (cell) row[name] = cell.value;
13094
+ }
13095
+ return Object.keys(row).length > 0 ? row : void 0;
13096
+ }
13097
+ /**
13098
+ * Turns a `pgoutput` stream into the change events the trigger listener emits,
13099
+ * so `RealtimeService` cannot tell which one it is reading.
13100
+ *
13101
+ * Holds the relation descriptions the stream sends ahead of a table's first
13102
+ * row (and again after its schema changes). The stream only ever carries
13103
+ * committed transactions, so a change can be handed on as soon as it arrives.
13104
+ */
13105
+ var PgoutputChanges = class {
13106
+ identityColumns;
13107
+ relations = /* @__PURE__ */ new Map();
13108
+ /**
13109
+ * @param identityColumns The columns a table's rows are addressed by, as
13110
+ * provisioning attaches them; `undefined` for the replica identity.
13111
+ */
13112
+ constructor(identityColumns) {
13113
+ this.identityColumns = identityColumns;
13114
+ }
13115
+ /** The changes one message describes — none for anything but a row change. */
13116
+ apply(message) {
13117
+ switch (message.tag) {
13118
+ case "relation":
13119
+ this.relations.set(message.relation.id, message.relation);
13120
+ return [];
13121
+ case "insert": return this.change("INSERT", message.relationId, message.newTuple, false);
13122
+ case "update": {
13123
+ const events = this.change("UPDATE", message.relationId, message.newTuple, false);
13124
+ if (message.oldTuple) {
13125
+ const before = this.change("UPDATE", message.relationId, message.oldTuple, message.oldIsKeyOnly === true);
13126
+ if (before.length && JSON.stringify(before[0].row) !== JSON.stringify(events[0]?.row)) events.push(...before);
13127
+ }
13128
+ return events;
13129
+ }
13130
+ case "delete": return this.change("DELETE", message.relationId, message.oldTuple, message.oldIsKeyOnly);
13131
+ case "truncate": return message.relationIds.flatMap((id) => {
13132
+ const relation = this.relations.get(id);
13133
+ return relation ? [{
13134
+ schema: relation.schema,
13135
+ table: relation.table,
13136
+ op: "TRUNCATE",
13137
+ row: {},
13138
+ truncated: false
13139
+ }] : [];
13140
+ });
13141
+ default: return [];
13142
+ }
13143
+ }
13144
+ change(op, relationId, tuple, keyOnly) {
13145
+ const relation = this.relations.get(relationId);
13146
+ if (!relation) throw new Error(`row for relation ${relationId}, which the stream never described`);
13147
+ const row = identityOf(relation, tuple, this.identityColumns(relation.schema, relation.table), keyOnly);
13148
+ return [{
13149
+ schema: relation.schema,
13150
+ table: relation.table,
13151
+ op,
13152
+ row: row ?? {},
13153
+ truncated: row === void 0
13154
+ }];
13155
+ }
13156
+ };
13157
+ //#endregion
13158
+ //#region src/services/cdc/WalCdcListener.ts
13159
+ /**
13160
+ * Change capture read from the write-ahead log.
13161
+ *
13162
+ * The trigger listener's counterpart (see {@link CdcListener}): it hands
13163
+ * `RealtimeService` the same {@link CdcChangeEvent}s, but takes them from a
13164
+ * logical replication stream instead of `pg_notify`. Writers pay for neither a
13165
+ * trigger nor NOTIFY, whose commit-time lock serializes every notifying
13166
+ * transaction on the server, disk flush included (see
13167
+ * `tooling/scripts/bench-cdc-notify.mts`).
13168
+ *
13169
+ * Each instance opens its own replication connection and creates a
13170
+ * **temporary** slot on it. A temporary slot lives exactly as long as the
13171
+ * connection: a crashed or partitioned instance cannot leave a slot behind
13172
+ * holding WAL until the disk fills, which is the failure every permanent slot
13173
+ * invites. The price is that a reconnect starts a new slot from the present,
13174
+ * so whatever committed in the gap is not replayed — `onReconnect` tells
13175
+ * subscribers to look again, as the LISTEN connection's does.
13176
+ *
13177
+ * Requires `wal_level=logical`, a role with the REPLICATION attribute (or the
13178
+ * host's equivalent, e.g. `rds_replication`), a free replication slot and WAL
13179
+ * sender, and the publication provisioning creates ({@link CDC_PUBLICATION}).
13180
+ * The connection must reach Postgres directly: a pooler does not carry the
13181
+ * replication protocol.
13182
+ */
13183
+ var DEFAULT_RECONNECT_DELAY_MS = 3e3;
13184
+ var DEFAULT_HEARTBEAT_INTERVAL_MS = 3e4;
13185
+ var DEFAULT_HEARTBEAT_TIMEOUT_MS = 1e4;
13186
+ var DEFAULT_STATUS_INTERVAL_MS = 1e4;
13187
+ var DEFAULT_SLOT_TIMEOUT_MS = 3e4;
13188
+ /** Changes handed on at once from inside one large transaction, rather than all of it at its commit. */
13189
+ var EVENTS_PER_BATCH = 1e3;
13190
+ var SAFE_IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;
13191
+ function failureReason(err) {
13192
+ const message = err instanceof Error ? err.message : String(err);
13193
+ const code = err instanceof Error && "code" in err && typeof err.code === "string" ? err.code : void 0;
13194
+ return code && !message.includes(code) ? `${message} [${code}]` : message;
13195
+ }
13196
+ /**
13197
+ * The `START_REPLICATION` command, driven through `pg`'s query queue.
13198
+ *
13199
+ * `pg` hands every CopyData frame of the active query to its
13200
+ * `handleCopyData`, and the rest of these handlers are the ones it calls on
13201
+ * any query. Frames back to the server — standby status updates — are written
13202
+ * to the connection's socket as whole CopyData messages, between the frames
13203
+ * `pg` itself writes.
13204
+ */
13205
+ var ReplicationStream = class {
13206
+ command;
13207
+ handlers;
13208
+ started;
13209
+ resolveStarted;
13210
+ rejectStarted;
13211
+ connection;
13212
+ isStarted = false;
13213
+ constructor(command, handlers) {
13214
+ this.command = command;
13215
+ this.handlers = handlers;
13216
+ this.started = new Promise((resolve, reject) => {
13217
+ this.resolveStarted = resolve;
13218
+ this.rejectStarted = reject;
13219
+ });
13220
+ this.started.catch(() => {});
13221
+ }
13222
+ submit(connection) {
13223
+ this.connection = connection;
13224
+ connection.once("replicationStart", () => {
13225
+ this.isStarted = true;
13226
+ this.resolveStarted();
13227
+ });
13228
+ connection.query(this.command);
13229
+ }
13230
+ send(payload) {
13231
+ if (this.isStarted) this.connection?.stream.write(copyDataFrame(payload));
13232
+ }
13233
+ handleCopyData(message) {
13234
+ this.handlers.onFrame(message.chunk);
13235
+ }
13236
+ handleError(err) {
13237
+ if (this.isStarted) {
13238
+ this.isStarted = false;
13239
+ this.handlers.onClosed(err);
13240
+ } else this.rejectStarted(err);
13241
+ }
13242
+ handleReadyForQuery() {
13243
+ if (this.isStarted) {
13244
+ this.isStarted = false;
13245
+ this.handlers.onClosed();
13246
+ } else this.rejectStarted(/* @__PURE__ */ new Error("replication ended before it started"));
13247
+ }
13248
+ handleCopyInResponse() {}
13249
+ handleRowDescription() {}
13250
+ handleDataRow() {}
13251
+ handlePortalSuspended() {}
13252
+ handleEmptyQuery() {}
13253
+ handleCommandComplete() {}
13254
+ };
13255
+ /**
13256
+ * A logical replication consumer that repairs itself, shaped like
13257
+ * {@link PgNotifyListener}: the first connect is validated and rethrown, so a
13258
+ * caller can fall back to the trigger listener; every later drop is repaired in
13259
+ * the background.
13260
+ */
13261
+ var WalCdcListener = class {
13262
+ options;
13263
+ client;
13264
+ /** The connection being opened: its frames count before it is adopted, an abandoned one's never do. */
13265
+ connecting;
13266
+ stream;
13267
+ running = false;
13268
+ reconnectTimer;
13269
+ heartbeatTimer;
13270
+ heartbeatDeadline;
13271
+ statusTimer;
13272
+ downSince;
13273
+ lastHeartbeatAt;
13274
+ lastFrameAt = 0;
13275
+ outageWarned = false;
13276
+ /** The end of the last transaction handed on: what is reported as read. */
13277
+ confirmedLsn = 0n;
13278
+ inTransaction = false;
13279
+ pending = [];
13280
+ slotName;
13281
+ constructor(options) {
13282
+ this.options = options;
13283
+ if (!SAFE_IDENTIFIER.test(options.publication)) throw new Error(`Unsafe publication name "${options.publication}" — expected a plain SQL identifier.`);
13284
+ }
13285
+ get active() {
13286
+ return this.running;
13287
+ }
13288
+ /** Streaming on a connection that answered its last heartbeat. */
13289
+ get connected() {
13290
+ return this.running && this.stream !== void 0;
13291
+ }
13292
+ status() {
13293
+ return {
13294
+ channel: `wal:${this.options.publication}`,
13295
+ connected: this.connected,
13296
+ ...this.downSince !== void 0 && !this.connected ? { downSince: this.downSince } : {},
13297
+ ...this.lastHeartbeatAt !== void 0 ? { lastHeartbeatAt: this.lastHeartbeatAt } : {}
13298
+ };
13299
+ }
13300
+ /**
13301
+ * Open the stream. Rejects if the first connection, the slot or
13302
+ * `START_REPLICATION` fails — `wal_level` is not `logical`, the role may
13303
+ * not replicate, the publication is missing, no slot is free — leaving
13304
+ * the listener stopped so the caller can use triggers instead.
13305
+ */
13306
+ async start() {
13307
+ if (this.running) return;
13308
+ this.running = true;
13309
+ try {
13310
+ await this.connect({ first: true });
13311
+ } catch (err) {
13312
+ this.running = false;
13313
+ throw err;
13314
+ }
13315
+ }
13316
+ /** Close the stream; the server drops the temporary slot with it. Idempotent. */
13317
+ async stop() {
13318
+ this.running = false;
13319
+ this.stopTimers();
13320
+ if (this.reconnectTimer) {
13321
+ clearTimeout(this.reconnectTimer);
13322
+ this.reconnectTimer = void 0;
13323
+ }
13324
+ const client = this.client;
13325
+ this.client = void 0;
13326
+ this.stream = void 0;
13327
+ if (client) try {
13328
+ await client.end();
13329
+ } catch {}
13330
+ }
13331
+ async connect({ first = false } = {}) {
13332
+ const { connectionString, applicationName, publication } = this.options;
13333
+ let pending;
13334
+ try {
13335
+ const client = new Client({
13336
+ connectionString,
13337
+ replication: "database",
13338
+ keepAlive: true,
13339
+ keepAliveInitialDelayMillis: 3e4,
13340
+ ...applicationName ? { application_name: applicationName } : {}
13341
+ });
13342
+ pending = client;
13343
+ this.connecting = client;
13344
+ client.on("error", (err) => {
13345
+ if (client !== this.client) return;
13346
+ logger.error("❌ [CDC] WAL stream connection error", { detail: err.message });
13347
+ this.abandon(client);
13348
+ });
13349
+ client.on("end", () => {
13350
+ if (client !== this.client || !this.running) return;
13351
+ logger.warn("⚠️ [CDC] WAL stream disconnected unexpectedly.");
13352
+ this.abandon(client);
13353
+ });
13354
+ await client.connect();
13355
+ const slot = `rebase_cdc_${randomBytes(8).toString("hex")}`;
13356
+ await this.withTimeout(client.query(`CREATE_REPLICATION_SLOT ${slot} TEMPORARY LOGICAL pgoutput NOEXPORT_SNAPSHOT`), this.options.slotTimeoutMs ?? DEFAULT_SLOT_TIMEOUT_MS, "creating the replication slot (it waits for transactions already running to finish)");
13357
+ const changes = new PgoutputChanges(this.options.identityColumns);
13358
+ this.confirmedLsn = 0n;
13359
+ this.inTransaction = false;
13360
+ this.pending = [];
13361
+ const stream = new ReplicationStream(`START_REPLICATION SLOT ${slot} LOGICAL 0/0 (proto_version '1', publication_names '${publication}')`, {
13362
+ onFrame: (payload) => this.onFrame(client, stream, changes, payload),
13363
+ onClosed: (err) => {
13364
+ if (client !== this.client) return;
13365
+ logger.warn(`⚠️ [CDC] WAL stream ended${err ? ` (${err.message})` : ""}; reopening it.`);
13366
+ this.abandon(client);
13367
+ }
13368
+ });
13369
+ client.query(stream);
13370
+ await stream.started;
13371
+ this.client = client;
13372
+ this.connecting = void 0;
13373
+ this.stream = stream;
13374
+ this.slotName = slot;
13375
+ pending = void 0;
13376
+ this.downSince = void 0;
13377
+ this.outageWarned = false;
13378
+ this.lastFrameAt = Date.now();
13379
+ this.lastHeartbeatAt = Date.now();
13380
+ this.startTimers(client);
13381
+ logger.debug(`📡 [CDC] Streaming publication "${publication}" through temporary slot ${slot}.`);
13382
+ } catch (err) {
13383
+ this.connecting = void 0;
13384
+ if (pending) try {
13385
+ await pending.end();
13386
+ } catch {}
13387
+ if (first) throw err;
13388
+ const retry = `[CDC] WAL stream could not be opened (${failureReason(err)}); retrying every ${Math.round((this.options.reconnectDelayMs ?? DEFAULT_RECONNECT_DELAY_MS) / 1e3)} s.`;
13389
+ if (this.outageWarned) logger.debug(retry);
13390
+ else {
13391
+ this.outageWarned = true;
13392
+ logger.warn(`⚠️ ${retry}`);
13393
+ }
13394
+ this.scheduleReconnect();
13395
+ return;
13396
+ }
13397
+ if (!first && this.options.onReconnect) {
13398
+ logger.warn("⚠️ [CDC] WAL stream reopened on a new slot; changes committed while it was down were missed.");
13399
+ try {
13400
+ this.options.onReconnect();
13401
+ } catch (err) {
13402
+ logger.error("❌ [CDC] Error resyncing after a reconnect", { error: err });
13403
+ }
13404
+ }
13405
+ }
13406
+ onFrame(client, stream, changes, payload) {
13407
+ if (client !== this.client && client !== this.connecting) return;
13408
+ this.lastFrameAt = Date.now();
13409
+ this.clearHeartbeatDeadline();
13410
+ try {
13411
+ const frame = parseReplicationFrame(payload);
13412
+ if (frame.kind === "keepalive") {
13413
+ this.lastHeartbeatAt = Date.now();
13414
+ if (!this.inTransaction && frame.walEnd > this.confirmedLsn) this.confirmedLsn = frame.walEnd;
13415
+ if (frame.replyRequested) stream.send(encodeStandbyStatus(this.confirmedLsn, false));
13416
+ return;
13417
+ }
13418
+ const message = parsePgoutputMessage(frame.data);
13419
+ if (message.tag === "begin") {
13420
+ this.inTransaction = true;
13421
+ return;
13422
+ }
13423
+ if (message.tag === "commit") {
13424
+ this.inTransaction = false;
13425
+ this.flush();
13426
+ if (message.endLsn > this.confirmedLsn) this.confirmedLsn = message.endLsn;
13427
+ return;
13428
+ }
13429
+ const events = changes.apply(message);
13430
+ if (events.length > 0) {
13431
+ this.pending.push(...events);
13432
+ if (this.pending.length >= EVENTS_PER_BATCH) this.flush();
13433
+ }
13434
+ } catch (err) {
13435
+ logger.error(`❌ [CDC] Unreadable WAL stream frame — reopening the stream (${failureReason(err)}).`);
13436
+ if (client === this.client) this.abandon(client);
13437
+ else client.end().catch(() => {});
13438
+ }
13439
+ }
13440
+ /** Hand on what has been read. Every change in the stream is committed, so this needs no commit to wait for. */
13441
+ flush() {
13442
+ if (this.pending.length === 0) return;
13443
+ const events = this.pending;
13444
+ this.pending = [];
13445
+ Promise.resolve(this.options.onEvents(events)).catch((err) => logger.error("❌ [CDC] Error handling WAL changes", { error: err }));
13446
+ }
13447
+ abandon(client) {
13448
+ if (client !== this.client) return;
13449
+ this.flush();
13450
+ this.client = void 0;
13451
+ this.stream = void 0;
13452
+ this.downSince ??= Date.now();
13453
+ this.stopTimers();
13454
+ client.end().catch(() => {});
13455
+ this.scheduleReconnect();
13456
+ }
13457
+ startTimers(client) {
13458
+ this.stopTimers();
13459
+ const statusInterval = this.options.statusIntervalMs ?? DEFAULT_STATUS_INTERVAL_MS;
13460
+ this.statusTimer = setInterval(() => {
13461
+ if (client === this.client) this.stream?.send(encodeStandbyStatus(this.confirmedLsn, false));
13462
+ }, statusInterval);
13463
+ unref(this.statusTimer);
13464
+ const interval = this.options.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS;
13465
+ if (interval <= 0) return;
13466
+ this.heartbeatTimer = setInterval(() => this.heartbeat(client), interval);
13467
+ unref(this.heartbeatTimer);
13468
+ }
13469
+ /**
13470
+ * Ask the server to answer at once. A half-open connection — packets
13471
+ * stopped, no FIN, no RST — fires no error, so a stream that went quiet
13472
+ * this way would look idle forever while every change went unseen.
13473
+ */
13474
+ heartbeat(client) {
13475
+ if (client !== this.client || this.heartbeatDeadline) return;
13476
+ const sentAt = Date.now();
13477
+ this.stream?.send(encodeStandbyStatus(this.confirmedLsn, true));
13478
+ const timeoutMs = this.options.heartbeatTimeoutMs ?? DEFAULT_HEARTBEAT_TIMEOUT_MS;
13479
+ this.heartbeatDeadline = setTimeout(() => {
13480
+ this.heartbeatDeadline = void 0;
13481
+ if (client !== this.client || this.lastFrameAt >= sentAt) return;
13482
+ logger.warn(`⚠️ [CDC] WAL stream did not answer a heartbeat in ${timeoutMs}ms — treating it as dead and reopening it.`);
13483
+ this.abandon(client);
13484
+ }, timeoutMs);
13485
+ unref(this.heartbeatDeadline);
13486
+ }
13487
+ clearHeartbeatDeadline() {
13488
+ if (this.heartbeatDeadline) clearTimeout(this.heartbeatDeadline);
13489
+ this.heartbeatDeadline = void 0;
13490
+ }
13491
+ stopTimers() {
13492
+ if (this.statusTimer) clearInterval(this.statusTimer);
13493
+ if (this.heartbeatTimer) clearInterval(this.heartbeatTimer);
13494
+ this.statusTimer = void 0;
13495
+ this.heartbeatTimer = void 0;
13496
+ this.clearHeartbeatDeadline();
13497
+ }
13498
+ scheduleReconnect() {
13499
+ if (!this.running || this.reconnectTimer) return;
13500
+ this.downSince ??= Date.now();
13501
+ this.reconnectTimer = setTimeout(async () => {
13502
+ this.reconnectTimer = void 0;
13503
+ if (!this.running) return;
13504
+ await this.connect();
13505
+ }, this.options.reconnectDelayMs ?? DEFAULT_RECONNECT_DELAY_MS);
13506
+ }
13507
+ async withTimeout(promise, ms, what) {
13508
+ let timer;
13509
+ try {
13510
+ return await Promise.race([promise, new Promise((_resolve, reject) => {
13511
+ timer = setTimeout(() => reject(/* @__PURE__ */ new Error(`timed out after ${ms}ms ${what}`)), ms);
13512
+ })]);
13513
+ } finally {
13514
+ if (timer) clearTimeout(timer);
13515
+ }
13516
+ }
13517
+ /** For logs and tests: the slot of the current stream, and how far it has read. */
13518
+ position() {
13519
+ return {
13520
+ slot: this.slotName,
13521
+ confirmedLsn: formatLsn(this.confirmedLsn)
13522
+ };
13523
+ }
13524
+ };
13525
+ //#endregion
13526
+ //#region src/services/cdc/wal-cdc.ts
13527
+ /**
13528
+ * The provisioning half of WAL change capture: the publication a
13529
+ * {@link WalCdcListener} streams.
13530
+ *
13531
+ * `REALTIME_CDC=wal` swaps the capture triggers for logical decoding. What a
13532
+ * trigger did per table — say which tables are captured — a publication does
13533
+ * once: `pgoutput` streams the inserts, updates, deletes and truncates of the
13534
+ * tables in it, and nothing else.
13535
+ *
13536
+ * Tables are only ever added. During a rolling deploy two versions provision
13537
+ * at once, and an older one that does not know a newer collection must not
13538
+ * take its table back out from under the instances streaming it. A table that
13539
+ * stops being a collection stays published; its changes reach no subscriber
13540
+ * and are dropped as unmapped, as an unmapped trigger's were.
13541
+ *
13542
+ * A table without a replica identity — no primary key, or `REPLICA IDENTITY
13543
+ * NOTHING` — is never added: Postgres refuses every UPDATE and DELETE on a
13544
+ * table that publishes them and cannot say which row changed, so publishing it
13545
+ * would break writes to it. It is reported and left uncaptured.
13546
+ */
13547
+ /** The publication WAL capture streams. A plain identifier: it is interpolated into replication commands. */
13548
+ var CDC_PUBLICATION = "rebase_cdc";
13549
+ var quoteIdent = (name) => `"${name.replace(/"/g, "\"\"")}"`;
13550
+ var quoteLiteral = (value) => `'${value.replace(/'/g, "''")}'`;
13551
+ /** SQLSTATE 42710: the publication, or the table's membership of it, already exists — another instance got there first. */
13552
+ function isDuplicateObject(err) {
13553
+ const code = err instanceof Error && "code" in err ? err.code : void 0;
13554
+ const causeCode = err instanceof Error && err.cause instanceof Error && "code" in err.cause ? err.cause.code : void 0;
13555
+ return code === "42710" || causeCode === "42710";
13556
+ }
13557
+ /**
13558
+ * Why this database cannot do WAL capture, or `undefined` when it can as far
13559
+ * as the server's settings go. Whether the streaming role may replicate is
13560
+ * only known when it tries — a host can grant it through role membership
13561
+ * (`rds_replication`, `cloudsqlsuperuser`) that no attribute shows.
13562
+ */
13563
+ async function walCaptureBlocker(run) {
13564
+ const rows = await run("SELECT current_setting('wal_level') AS wal_level, current_setting('server_version_num')::int AS version");
13565
+ const walLevel = String(rows[0]?.wal_level ?? "");
13566
+ const version = Number(rows[0]?.version ?? 0);
13567
+ if (version > 0 && version < 1e5) return `Postgres ${Math.floor(version / 1e4)} has no pgoutput (10 or later is needed)`;
13568
+ if (walLevel !== "logical") return `wal_level is "${walLevel || "unknown"}", and logical decoding needs wal_level=logical (a server restart)`;
13569
+ }
13570
+ async function readTableStates(run, tables) {
13571
+ const states = /* @__PURE__ */ new Map();
13572
+ if (tables.length === 0) return states;
13573
+ const names = tables.map((t) => quoteLiteral(`${quoteIdent(t.schema)}.${quoteIdent(t.table)}`)).join(", ");
13574
+ const rows = await run(`SELECT n.name, c.oid IS NOT NULL AS exists, c.relkind::text AS relkind, c.relreplident::text AS replica_identity, EXISTS (SELECT 1 FROM pg_index i WHERE i.indrelid = c.oid AND i.indisprimary) AS has_pk, EXISTS (SELECT 1 FROM pg_index i WHERE i.indrelid = c.oid AND i.indisreplident) AS has_identity_index, EXISTS (SELECT 1 FROM pg_publication_rel r JOIN pg_publication p ON p.oid = r.prpubid WHERE r.prrelid = c.oid AND p.pubname = ${quoteLiteral(CDC_PUBLICATION)}) AS published FROM unnest(ARRAY[${names}]::text[]) AS n(name) LEFT JOIN pg_class c ON c.oid = to_regclass(n.name)`);
13575
+ for (const row of rows) states.set(String(row.name), {
13576
+ exists: row.exists === true,
13577
+ relkind: String(row.relkind ?? ""),
13578
+ replicaIdentity: String(row.replica_identity ?? ""),
13579
+ hasPrimaryKey: row.has_pk === true,
13580
+ hasIdentityIndex: row.has_identity_index === true,
13581
+ published: row.published === true
13582
+ });
13583
+ return states;
13584
+ }
13585
+ /** Why a table cannot join the publication, or `undefined` when it can. */
13586
+ function unpublishable(state, version) {
13587
+ if (!state?.exists) return "the table does not exist — is it migrated?";
13588
+ if (state.relkind === "p" && version < 13e4) return "a partitioned table is published through its root from Postgres 13";
13589
+ if (state.relkind !== "r" && state.relkind !== "p") return "only tables can be published, and this is a view or a foreign table";
13590
+ if (state.replicaIdentity === "n" || state.replicaIdentity === "d" && !state.hasPrimaryKey || state.replicaIdentity === "i" && !state.hasIdentityIndex) return "it has no primary key or replica identity, and Postgres refuses every UPDATE and DELETE on a published table that cannot say which row changed — add a primary key, or set REPLICA IDENTITY FULL";
13591
+ }
13592
+ async function withLockBudget(run, statement) {
13593
+ for (let attempt = 1;; attempt++) try {
13594
+ await run(`SET LOCAL lock_timeout = '${BOOT_LOCK_TIMEOUT_MS}ms';\n${statement}`);
13595
+ return;
13596
+ } catch (err) {
13597
+ if (!isLockTimeout(err) || attempt >= 3) throw err;
13598
+ await sleep(500 * attempt * (1 + Math.random()));
13599
+ }
13600
+ }
13601
+ /**
13602
+ * Create the publication if it is missing and add every table it lacks.
13603
+ *
13604
+ * Throws when the publication cannot be created or read at all (no
13605
+ * privilege), which the caller treats as "WAL capture is unavailable". A
13606
+ * single table that cannot be added is reported in `skipped` instead, so one
13607
+ * of them cannot turn capture off for the rest.
13608
+ */
13609
+ async function provisionWalPublication(run, tables) {
13610
+ const versionRows = await run("SELECT current_setting('server_version_num')::int AS version");
13611
+ const version = Number(versionRows[0]?.version ?? 0);
13612
+ if ((await run(`SELECT 1 FROM pg_publication WHERE pubname = ${quoteLiteral("rebase_cdc")}`)).length === 0) {
13613
+ const options = "publish = 'insert, update, delete, truncate'" + (version >= 13e4 ? ", publish_via_partition_root = true" : "");
13614
+ try {
13615
+ await run(`CREATE PUBLICATION ${quoteIdent(CDC_PUBLICATION)} WITH (${options})`);
13616
+ } catch (err) {
13617
+ if (!isDuplicateObject(err)) throw err;
13618
+ }
13619
+ }
13620
+ const byTable = /* @__PURE__ */ new Map();
13621
+ for (const ref of tables) byTable.set(`${quoteIdent(ref.schema)}.${quoteIdent(ref.table)}`, ref);
13622
+ const states = await readTableStates(run, [...byTable.values()]);
13623
+ const published = [];
13624
+ const skipped = [];
13625
+ for (const [qualified, ref] of byTable) {
13626
+ const state = states.get(qualified);
13627
+ if (state?.published) {
13628
+ published.push(ref);
13629
+ continue;
13630
+ }
13631
+ const reason = unpublishable(state, version);
13632
+ if (reason) {
13633
+ skipped.push({
13634
+ ...ref,
13635
+ reason
13636
+ });
13637
+ continue;
13638
+ }
13639
+ try {
13640
+ await withLockBudget(run, `ALTER PUBLICATION ${quoteIdent(CDC_PUBLICATION)} ADD TABLE ${qualified}`);
13641
+ published.push(ref);
13642
+ } catch (err) {
13643
+ if (isDuplicateObject(err)) published.push(ref);
13644
+ else skipped.push({
13645
+ ...ref,
13646
+ reason: err instanceof Error ? err.message : String(err)
13647
+ });
13648
+ }
13649
+ }
13650
+ return {
13651
+ published,
13652
+ skipped
13653
+ };
13654
+ }
13655
+ /** One line per table the publication could not take: its writes reach realtime only through this server. */
13656
+ function reportUnpublishedTables(result) {
13657
+ for (const table of result.skipped) logger.warn(`⚠️ [CDC] "${table.schema}.${table.table}" is not captured from the WAL — ${table.reason}.`);
13658
+ }
13659
+ //#endregion
12602
13660
  //#region src/services/socket-liveness.ts
12603
13661
  /**
12604
13662
  * Letting go of client sockets the server can no longer reach.
@@ -13835,6 +14893,8 @@ var RealtimeService = class RealtimeService extends EventEmitter {
13835
14893
  * never track presence, and each of their closes was a `DELETE` too.
13836
14894
  */
13837
14895
  presenceTrackers = /* @__PURE__ */ new Set();
14896
+ /** Each client's roster writes still running, chained in order — see {@link presenceStoreOp}. */
14897
+ presenceStoreQueues = /* @__PURE__ */ new Map();
13838
14898
  /**
13839
14899
  * Channels whose oversized ephemeral broadcasts have already been reported,
13840
14900
  * so a hot channel logs the problem once rather than once per message.
@@ -13984,7 +15044,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
13984
15044
  * refetches once per this interval, which is the whole cost.
13985
15045
  */
13986
15046
  static REFETCH_MAX_WAIT_MS = DEFAULT_REFETCH_MAX_WAIT_MS;
13987
- /** Dedicated LISTEN client for DB-level change events (undefined unless CDC is enabled). */
15047
+ /** Where DB-level change events come from — capture triggers or the WAL (undefined unless CDC is enabled). */
13988
15048
  cdcListener;
13989
15049
  /** Whether database-level CDC is the active cross-instance change source. */
13990
15050
  cdcActive = false;
@@ -14497,7 +15557,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
14497
15557
  this.clients.delete(clientId);
14498
15558
  this.clientOptIns.delete(clientId);
14499
15559
  for (const key of [...this.subscriptionsByClient.get(clientId) ?? []]) this.dropSubscription(key);
14500
- if (this.presenceTrackers.delete(clientId)) await this.presenceStoreOp(() => this.presenceStore.removeClient(clientId), "client removal");
15560
+ if (this.presenceTrackers.delete(clientId)) await this.presenceStoreOp(clientId, (store) => store.removeClient(clientId), "client removal");
14501
15561
  for (const channel of [...this.channelsByClient.get(clientId) ?? []]) {
14502
15562
  const members = this.channels.get(channel);
14503
15563
  if (members?.has(clientId)) {
@@ -14547,6 +15607,21 @@ var RealtimeService = class RealtimeService extends EventEmitter {
14547
15607
  }
14548
15608
  }
14549
15609
  /**
15610
+ * Whether `clientId` is a socket this service still holds.
15611
+ *
15612
+ * A frame reaches this service after the socket door has awaited — the
15613
+ * identity recheck, the data rate limiter — and a channel frame may await
15614
+ * again for its authorizer. A client that closed inside one of those awaits
15615
+ * was already removed by `removeClient`, which runs on `close`, so whatever
15616
+ * its frame registers now is never cleaned up: a subscription that keeps
15617
+ * its group and its refetches for the life of the process, or a membership
15618
+ * that keeps a channel alive with nobody in it. Checked synchronously
15619
+ * before the registration, so nothing can close the socket in between.
15620
+ */
15621
+ socketIsOpen(clientId) {
15622
+ return this.clients.has(clientId);
15623
+ }
15624
+ /**
14550
15625
  * Refuse a new subscription past the socket's ceiling, and say so.
14551
15626
  *
14552
15627
  * Every collection subscription is a refetch on every write to its
@@ -14568,6 +15643,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
14568
15643
  const subscriptionId = request.subscriptionId;
14569
15644
  const key = this.subscriptionKey(clientId, subscriptionId);
14570
15645
  let canDeliver;
15646
+ if (!this.socketIsOpen(clientId)) return;
14571
15647
  try {
14572
15648
  const collection = this.registry.getCollectionByPath(request.path);
14573
15649
  if (!collection) {
@@ -14665,6 +15741,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
14665
15741
  const subscriptionId = request.subscriptionId;
14666
15742
  const key = this.subscriptionKey(clientId, subscriptionId);
14667
15743
  let canDeliver;
15744
+ if (!this.socketIsOpen(clientId)) return;
14668
15745
  try {
14669
15746
  const collection = this.registry.getCollectionByPath(request.path);
14670
15747
  if (!collection) {
@@ -14750,16 +15827,18 @@ var RealtimeService = class RealtimeService extends EventEmitter {
14750
15827
  const subscription = this._subscriptions.get(key);
14751
15828
  if (!subscription) continue;
14752
15829
  const subscriptionId = subscription.subscriptionId;
14753
- if (subscription.type === "collection" && subscription.collectionRequest) {
15830
+ const request = subscription.type === "collection" ? subscription.collectionRequest : void 0;
15831
+ const judged = request ? {
15832
+ filter: request.filter,
15833
+ logical: request.logical,
15834
+ orderBy: request.orderBy,
15835
+ fields: request.fields,
15836
+ include: request.include
15837
+ } : subscription.type === "single" ? { include: subscription.include } : void 0;
15838
+ if (judged) {
14754
15839
  const collection = this.registry.getCollectionByPath(subscription.path);
14755
- const request = subscription.collectionRequest;
14756
15840
  try {
14757
- if (collection) assertReadRequestReadable({
14758
- filter: request.filter,
14759
- logical: request.logical,
14760
- orderBy: request.orderBy,
14761
- fields: request.fields
14762
- }, collection, { roles: authContext.roles ?? ["anon"] });
15841
+ if (collection) assertReadRequestReadable(judged, collection, { roles: authContext.roles ?? ["anon"] });
14763
15842
  } catch (e) {
14764
15843
  if (!(e instanceof ApiError)) throw e;
14765
15844
  await this.handleUnsubscribe(clientId, subscriptionId);
@@ -15220,7 +16299,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15220
16299
  * themselves would keep a page of rows per subscriber in memory.
15221
16300
  */
15222
16301
  frameDigest(body) {
15223
- return createHash("sha256").update(body).digest("base64");
16302
+ return createHash$1("sha256").update(body).digest("base64");
15224
16303
  }
15225
16304
  /**
15226
16305
  * Send a data frame to a socket subscriber — unless it is exactly the one
@@ -15384,6 +16463,10 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15384
16463
  */
15385
16464
  handleChannelMessage(clientId, type, payload, authContext) {
15386
16465
  const channel = payload?.channel;
16466
+ if (typeof channel !== "string" || channel === "") {
16467
+ this.sendError(clientId, "A channel frame needs `channel`, a non-empty string.", void 0, "INVALID_CHANNEL");
16468
+ return;
16469
+ }
15387
16470
  if (type === "leave_channel") {
15388
16471
  this.leaveChannel(clientId, channel);
15389
16472
  return;
@@ -15402,6 +16485,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15402
16485
  }
15403
16486
  /** Perform an already-authorized channel frame. */
15404
16487
  dispatchChannelMessage(clientId, type, channel, payload) {
16488
+ if (!this.socketIsOpen(clientId)) return;
15405
16489
  switch (type) {
15406
16490
  case "join_channel":
15407
16491
  this.joinChannel(clientId, channel);
@@ -15853,7 +16937,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15853
16937
  lastSeen: Date.now()
15854
16938
  });
15855
16939
  this.indexClientChannel(this.presenceByClient, clientId, channel);
15856
- this.presenceStoreOp(() => this.presenceStore.track(channel, clientId, state), "track");
16940
+ this.presenceStoreOp(clientId, (store) => store.track(channel, clientId, state), "track");
15857
16941
  if (changed) {
15858
16942
  this.deliverPresenceDiff(channel, { [clientId]: state }, {});
15859
16943
  this.publishPresenceDiff(channel, { [clientId]: state }, {});
@@ -15876,7 +16960,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15876
16960
  this.unindexClientChannel(this.presenceByClient, clientId, channel);
15877
16961
  this.deliverPresenceDiff(channel, {}, { [clientId]: entry.state });
15878
16962
  this.publishPresenceDiff(channel, {}, { [clientId]: entry.state });
15879
- if (!options?.skipStore) this.presenceStoreOp(() => this.presenceStore.remove(channel, clientId), "remove");
16963
+ if (!options?.skipStore) this.presenceStoreOp(clientId, (store) => store.remove(channel, clientId), "remove");
15880
16964
  }
15881
16965
  if (channelPresence.size === 0) this.presence.delete(channel);
15882
16966
  }
@@ -16046,14 +17130,33 @@ var RealtimeService = class RealtimeService extends EventEmitter {
16046
17130
  }, RealtimeService.BUS_GOSSIP_INTERVAL_MS);
16047
17131
  unref(this.busGossipInterval);
16048
17132
  }
16049
- /** Run a roster write when there is a roster, and never let it throw. */
16050
- async presenceStoreOp(op, label) {
16051
- if (!this.presenceStore) return;
16052
- try {
16053
- await op();
16054
- } catch (error) {
16055
- logger.warn(`⚠️ [Presence] Shared roster ${label} failed`, { error });
16056
- }
17133
+ /**
17134
+ * Run one of `clientId`'s roster writes when there is a roster, after the
17135
+ * ones it made before, and never let it throw.
17136
+ *
17137
+ * In order, because each is its own statement on the pool: a removal sent
17138
+ * while the client's upsert was still on its way could run first, and the
17139
+ * upsert then put the row back. That row is this instance's, so the
17140
+ * stale-roster sweep leaves it alone, and the in-process sweep no longer
17141
+ * knows the client — the departed client stayed in every roster for as
17142
+ * long as the instance ran.
17143
+ */
17144
+ presenceStoreOp(clientId, op, label) {
17145
+ if (!this.presenceStore) return Promise.resolve();
17146
+ const next = (this.presenceStoreQueues.get(clientId) ?? Promise.resolve()).then(async () => {
17147
+ const store = this.presenceStore;
17148
+ if (!store) return;
17149
+ try {
17150
+ await op(store);
17151
+ } catch (error) {
17152
+ logger.warn(`⚠️ [Presence] Shared roster ${label} failed`, { error });
17153
+ }
17154
+ });
17155
+ this.presenceStoreQueues.set(clientId, next);
17156
+ next.finally(() => {
17157
+ if (this.presenceStoreQueues.get(clientId) === next) this.presenceStoreQueues.delete(clientId);
17158
+ });
17159
+ return next;
16057
17160
  }
16058
17161
  /** Periodic cleanup for stale presences */
16059
17162
  ensurePresenceCleanup() {
@@ -16149,6 +17252,8 @@ var RealtimeService = class RealtimeService extends EventEmitter {
16149
17252
  }
16150
17253
  this.oversizedBroadcastWarned.clear();
16151
17254
  if (this.presenceStore) {
17255
+ await Promise.allSettled([...this.presenceStoreQueues.values()]);
17256
+ this.presenceStoreQueues.clear();
16152
17257
  try {
16153
17258
  await this.presenceStore.removeInstance();
16154
17259
  } catch (error) {
@@ -16234,13 +17339,38 @@ var RealtimeService = class RealtimeService extends EventEmitter {
16234
17339
  * (bypass PgBouncer — LISTEN needs a session connection).
16235
17340
  */
16236
17341
  async enableCdc(connectionString) {
17342
+ await this.activateCdc(() => new CdcListener(connectionString, (events) => this.handleCdcEvents(events), () => this.resyncSubscriptions(), this.listenerApplicationName));
17343
+ }
17344
+ /**
17345
+ * Enable change capture read from the write-ahead log instead of NOTIFY —
17346
+ * `REALTIME_CDC=wal`. Same events, same pipeline as {@link enableCdc};
17347
+ * writers pay for no trigger and no NOTIFY. Rejects when the stream cannot
17348
+ * be opened (see {@link WalCdcListener.start}), leaving CDC inactive so the
17349
+ * caller can enable the trigger listener instead.
17350
+ *
17351
+ * @param tables What provisioning captures, with the columns each table's
17352
+ * rows are addressed by. A table it does not name is addressed by
17353
+ * its replica identity.
17354
+ */
17355
+ async enableWalCdc(connectionString, tables) {
17356
+ const identity = new Map(tables.map((t) => [`${t.schema}.${t.table}`, t.identityColumns ?? []]));
17357
+ await this.activateCdc(() => new WalCdcListener({
17358
+ connectionString,
17359
+ publication: CDC_PUBLICATION,
17360
+ onEvents: (events) => this.handleCdcEvents(events),
17361
+ onReconnect: () => this.resyncSubscriptions(),
17362
+ identityColumns: (schema, table) => identity.get(`${schema}.${table}`),
17363
+ applicationName: this.listenerApplicationName
17364
+ }));
17365
+ }
17366
+ async activateCdc(create) {
16237
17367
  if (this.cdcActive) {
16238
17368
  logger.warn("⚠️ [CDC] enableCdc called but CDC is already active. Ignoring.");
16239
17369
  return;
16240
17370
  }
16241
17371
  this.cdcTableMap = this.buildCdcTableMap();
16242
17372
  this.junctionLinkMap = buildJunctionLinkMap(this.registry);
16243
- this.cdcListener = new CdcListener(connectionString, (events) => this.handleCdcEvents(events), () => this.resyncSubscriptions(), this.listenerApplicationName);
17373
+ this.cdcListener = create();
16244
17374
  try {
16245
17375
  await this.cdcListener.start();
16246
17376
  } catch (err) {
@@ -16376,19 +17506,15 @@ var RealtimeService = class RealtimeService extends EventEmitter {
16376
17506
  const links = this.junctionLinkMap?.get(`${event.schema}.${event.table}`) ?? this.junctionLinkMap?.get(event.table);
16377
17507
  if (!links?.length) return false;
16378
17508
  if (event.op === "TRUNCATE") {
16379
- for (const link of links) {
16380
- const prefix = `${link.parentCollection.slug}/`;
16381
- const suffix = `/${link.relationKey}`;
16382
- const lists = [...this.subscribedPaths.keys()].filter((path) => path.startsWith(prefix) && path.endsWith(suffix) && path.split("/").length === 3);
16383
- for (const path of lists) await this.notifyUpdate(path, ANY_ROW, null, link.parentCollection.databaseId, false, "cdc");
16384
- }
17509
+ for (const link of links) await this.notifyEveryListOf(link, null);
16385
17510
  return true;
16386
17511
  }
16387
17512
  for (const link of links) {
16388
17513
  const sourceId = event.row?.[link.sourceColumn];
16389
17514
  const targetId = event.row?.[link.targetColumn];
16390
17515
  if (sourceId === void 0 || sourceId === null || targetId === void 0 || targetId === null) {
16391
- this.debugLog(`📡 [CDC] Junction row on ${event.table} is missing '${link.sourceColumn}'/'${link.targetColumn}' — skipping.`);
17516
+ this.debugLog(`📡 [CDC] Junction row on ${event.table} does not name '${link.sourceColumn}'/'${link.targetColumn}' — refreshing every list of ${link.parentCollection.slug}.${link.relationKey}.`);
17517
+ await this.notifyEveryListOf(link, { _rebase_invalidated: true });
16392
17518
  continue;
16393
17519
  }
16394
17520
  const path = `${link.parentCollection.slug}/${String(sourceId)}/${link.relationKey}`;
@@ -16398,6 +17524,17 @@ var RealtimeService = class RealtimeService extends EventEmitter {
16398
17524
  return true;
16399
17525
  }
16400
17526
  /**
17527
+ * Notify every subscribed child list of one many-to-many relation,
17528
+ * whichever parent it belongs to — for a junction change that names no
17529
+ * single list.
17530
+ */
17531
+ async notifyEveryListOf(link, row) {
17532
+ const prefix = `${link.parentCollection.slug}/`;
17533
+ const suffix = `/${link.relationKey}`;
17534
+ const lists = [...this.subscribedPaths.keys()].filter((path) => path.startsWith(prefix) && path.endsWith(suffix) && path.split("/").length === 3);
17535
+ for (const path of lists) await this.notifyUpdate(path, ANY_ROW, row, link.parentCollection.databaseId, false, "cdc");
17536
+ }
17537
+ /**
16401
17538
  * Compute the canonical (possibly composite) id string from a captured row,
16402
17539
  * or {@link ANY_ROW} when it names none — a key too large to notify, a
16403
17540
  * table with no key.
@@ -16726,6 +17863,8 @@ function columnNames(table) {
16726
17863
  var quote$1 = (xs) => Array.from(xs).map((s) => `\`${s}\``).join(", ");
16727
17864
  /** `on.from` / `on.to` accept a single column or a composite tuple. */
16728
17865
  var asColumns = (value) => Array.isArray(value) ? value : [value];
17866
+ /** The table a `table.column` spelling names, or `undefined` for a bare column. */
17867
+ var tablePart = (column) => column?.includes(".") ? column.split(".")[0] : void 0;
16729
17868
  /**
16730
17869
  * A one-column link into a key of several columns, or `undefined`.
16731
17870
  *
@@ -16950,21 +18089,42 @@ function findRelationDefects(collections, registry, given) {
16950
18089
  break;
16951
18090
  }
16952
18091
  const stepColumns = columnNames(stepTable);
16953
- for (const column of asColumns(step.on.from)) if (!prevColumns.has(column)) defects.push({
16954
- ...at,
16955
- problem: `step ${i + 1} joins \`${prevName}.${column}\` → \`${step.table}\`, but \`${column}\` is not a column on \`${prevName}\``,
16956
- fix: `\`joinPath[${i}].on.from\` names columns on ${i === 0 ? "this collection's table" : `the previous step's table (\`${prevName}\`)`}: ${quote$1(prevColumns)}`
16957
- });
16958
- for (const column of asColumns(step.on.to)) if (!stepColumns.has(column)) defects.push({
18092
+ const alternatives = Array.isArray(step.on) ? step.on : [step.on];
18093
+ if (alternatives.length === 0) defects.push({
16959
18094
  ...at,
16960
- problem: `step ${i + 1} joins into \`${step.table}.${column}\`, but \`${column}\` is not a column on \`${step.table}\``,
16961
- fix: `\`joinPath[${i}].on.to\` names columns on \`${step.table}\`: ${quote$1(stepColumns)}`
16962
- });
16963
- if (asColumns(step.on.from).length !== asColumns(step.on.to).length) defects.push({
16964
- ...at,
16965
- problem: `step ${i + 1} compares ${asColumns(step.on.from).length} column(s) against ${asColumns(step.on.to).length}`,
16966
- fix: `\`from\` and \`to\` must name the same number of columns in \`joinPath[${i}]\``
18095
+ problem: `\`joinPath[${i}].on\` lists no conditions, so step ${i + 1} joins \`${prevName}\` → \`${step.table}\` on nothing`,
18096
+ fix: `give \`joinPath[${i}].on\` at least one \`{ from, to }\`, or write the one condition as an object`
16967
18097
  });
18098
+ if (alternatives.length > 1) {
18099
+ const pairs = alternatives.map((on) => ({
18100
+ from: tablePart(asColumns(on.from)[0]) ?? prevName,
18101
+ to: tablePart(asColumns(on.to)[0]) ?? step.table
18102
+ }));
18103
+ if (pairs.some((p) => p.from !== pairs[0].from || p.to !== pairs[0].to)) defects.push({
18104
+ ...at,
18105
+ problem: `the alternatives of step ${i + 1} join different tables: ` + pairs.map((p, j) => `alternative ${j + 1} joins \`${p.from}\` → \`${p.to}\``).join(", "),
18106
+ fix: `every entry of \`joinPath[${i}].on\` joins \`${prevName}\` to \`${step.table}\`; a path through other tables is a step, or a relation, of its own`
18107
+ });
18108
+ }
18109
+ for (const [j, on] of alternatives.entries()) {
18110
+ const where = Array.isArray(step.on) ? `joinPath[${i}].on[${j}]` : `joinPath[${i}].on`;
18111
+ const alt = Array.isArray(step.on) ? ` (alternative ${j + 1})` : "";
18112
+ for (const column of asColumns(on.from)) if (!prevColumns.has(column)) defects.push({
18113
+ ...at,
18114
+ problem: `step ${i + 1}${alt} joins \`${prevName}.${column}\` → \`${step.table}\`, but \`${column}\` is not a column on \`${prevName}\``,
18115
+ fix: `\`${where}.from\` names columns on ${i === 0 ? "this collection's table" : `the previous step's table (\`${prevName}\`)`}: ${quote$1(prevColumns)}`
18116
+ });
18117
+ for (const column of asColumns(on.to)) if (!stepColumns.has(column)) defects.push({
18118
+ ...at,
18119
+ problem: `step ${i + 1}${alt} joins into \`${step.table}.${column}\`, but \`${column}\` is not a column on \`${step.table}\``,
18120
+ fix: `\`${where}.to\` names columns on \`${step.table}\`: ${quote$1(stepColumns)}`
18121
+ });
18122
+ if (asColumns(on.from).length !== asColumns(on.to).length) defects.push({
18123
+ ...at,
18124
+ problem: `step ${i + 1}${alt} compares ${asColumns(on.from).length} column(s) against ${asColumns(on.to).length}`,
18125
+ fix: `\`from\` and \`to\` must name the same number of columns in \`${where}\``
18126
+ });
18127
+ }
16968
18128
  prevName = step.table;
16969
18129
  prevColumns = stepColumns;
16970
18130
  }
@@ -17369,7 +18529,8 @@ var UserService = class {
17369
18529
  OR CAST(${table}.${sql.raw(idColumn)} AS text) ILIKE ${pattern})`);
17370
18530
  }
17371
18531
  const whereClause = conditions.length > 0 ? sql`WHERE ${sql.join(conditions, sql` AND `)}` : sql``;
17372
- const orderByClause = roleId ? sql`ORDER BY ${sql.raw(usersTableName)}.${sql.raw(orderColumn)} ${direction}` : sql`ORDER BY array_length(${sql.raw(usersTableName)}.roles, 1) DESC NULLS LAST, ${sql.raw(usersTableName)}.${sql.raw(orderColumn)} ${direction}`;
18532
+ const tieBreak = sql`${sql.raw(usersTableName)}.${sql.raw(idColumn)} ASC`;
18533
+ const orderByClause = roleId ? sql`ORDER BY ${sql.raw(usersTableName)}.${sql.raw(orderColumn)} ${direction}, ${tieBreak}` : sql`ORDER BY array_length(${sql.raw(usersTableName)}.roles, 1) DESC NULLS LAST, ${sql.raw(usersTableName)}.${sql.raw(orderColumn)} ${direction}, ${tieBreak}`;
17373
18534
  const total = (await this.db.execute(sql`
17374
18535
  SELECT count(*)::int as total FROM ${sql.raw(usersTableName)}
17375
18536
  ${whereClause}
@@ -17964,10 +19125,21 @@ var PasswordResetTokenService = class {
17964
19125
  };
17965
19126
  }
17966
19127
  /**
17967
- * Mark token as used
19128
+ * Mark the token used if it is still valid, and say whether this call did.
19129
+ *
19130
+ * The check and the mark are one statement, so of two requests presenting
19131
+ * the same token only one gets `true`.
17968
19132
  */
17969
19133
  async markAsUsed(tokenHash) {
17970
- await this.db.update(this.passwordResetTokensTable).set({ usedAt: /* @__PURE__ */ new Date() }).where(eq(this.passwordResetTokensTable.tokenHash, tokenHash));
19134
+ const tableName = this.getQualifiedPasswordResetTokensTableName();
19135
+ return (await this.db.execute(sql`
19136
+ UPDATE ${sql.raw(tableName)}
19137
+ SET used_at = NOW()
19138
+ WHERE token_hash = ${tokenHash}
19139
+ AND used_at IS NULL
19140
+ AND expires_at > NOW()
19141
+ RETURNING token_hash
19142
+ `)).rows.length > 0;
17971
19143
  }
17972
19144
  /**
17973
19145
  * Delete all tokens for a user
@@ -18029,6 +19201,10 @@ var MagicLinkTokenService = class {
18029
19201
  expiresAt: new Date(row.expires_at)
18030
19202
  };
18031
19203
  }
19204
+ /** Every link and email code issued to a user, used or not. */
19205
+ async deleteAllForUser(uid) {
19206
+ await this.db.delete(this.magicLinkTokensTable).where(eq(this.magicLinkTokensTable.uid, uid));
19207
+ }
18032
19208
  /** Links and email codes past their expiry, used or not. */
18033
19209
  async deleteExpired() {
18034
19210
  const tableName = this.getQualifiedTableName();
@@ -18037,8 +19213,21 @@ var MagicLinkTokenService = class {
18037
19213
  WHERE expires_at < NOW()
18038
19214
  `);
18039
19215
  }
19216
+ /**
19217
+ * Mark the link or code used if it is still valid, and say whether this
19218
+ * call did — in one statement, so of two requests presenting it only one
19219
+ * gets `true`.
19220
+ */
18040
19221
  async markAsUsed(tokenHash) {
18041
- await this.db.update(this.magicLinkTokensTable).set({ usedAt: /* @__PURE__ */ new Date() }).where(eq(this.magicLinkTokensTable.tokenHash, tokenHash));
19222
+ const tableName = this.getQualifiedTableName();
19223
+ return (await this.db.execute(sql`
19224
+ UPDATE ${sql.raw(tableName)}
19225
+ SET used_at = NOW()
19226
+ WHERE token_hash = ${tokenHash}
19227
+ AND used_at IS NULL
19228
+ AND expires_at > NOW()
19229
+ RETURNING token_hash
19230
+ `)).rows.length > 0;
18042
19231
  }
18043
19232
  };
18044
19233
  /**
@@ -18096,7 +19285,7 @@ var PostgresTokenRepository = class {
18096
19285
  return this.passwordResetTokenService.findValidByHash(tokenHash);
18097
19286
  }
18098
19287
  async markPasswordResetTokenUsed(tokenHash) {
18099
- await this.passwordResetTokenService.markAsUsed(tokenHash);
19288
+ return this.passwordResetTokenService.markAsUsed(tokenHash);
18100
19289
  }
18101
19290
  async deleteAllPasswordResetTokensForUser(uid) {
18102
19291
  await this.passwordResetTokenService.deleteAllForUser(uid);
@@ -18117,7 +19306,10 @@ var PostgresTokenRepository = class {
18117
19306
  return this.magicLinkTokenService.findValidByHash(tokenHash);
18118
19307
  }
18119
19308
  async markMagicLinkTokenUsed(tokenHash) {
18120
- await this.magicLinkTokenService.markAsUsed(tokenHash);
19309
+ return this.magicLinkTokenService.markAsUsed(tokenHash);
19310
+ }
19311
+ async deleteAllMagicLinkTokensForUser(uid) {
19312
+ await this.magicLinkTokenService.deleteAllForUser(uid);
18121
19313
  }
18122
19314
  };
18123
19315
  /**
@@ -18258,7 +19450,7 @@ var PostgresAuthRepository = class {
18258
19450
  return this.tokenRepository.findValidPasswordResetToken(tokenHash);
18259
19451
  }
18260
19452
  async markPasswordResetTokenUsed(tokenHash) {
18261
- await this.tokenRepository.markPasswordResetTokenUsed(tokenHash);
19453
+ return this.tokenRepository.markPasswordResetTokenUsed(tokenHash);
18262
19454
  }
18263
19455
  async deleteAllPasswordResetTokensForUser(uid) {
18264
19456
  await this.tokenRepository.deleteAllPasswordResetTokensForUser(uid);
@@ -18274,7 +19466,10 @@ var PostgresAuthRepository = class {
18274
19466
  return this.tokenRepository.findValidMagicLinkToken(tokenHash);
18275
19467
  }
18276
19468
  async markMagicLinkTokenUsed(tokenHash) {
18277
- await this.tokenRepository.markMagicLinkTokenUsed(tokenHash);
19469
+ return this.tokenRepository.markMagicLinkTokenUsed(tokenHash);
19470
+ }
19471
+ async deleteAllMagicLinkTokensForUser(uid) {
19472
+ await this.tokenRepository.deleteAllMagicLinkTokensForUser(uid);
18278
19473
  }
18279
19474
  _mfaService = null;
18280
19475
  getMfaService() {
@@ -18680,8 +19875,8 @@ async function ensureHistoryTableExists(db, options = {}) {
18680
19875
  SET search_path = pg_catalog, pg_temp
18681
19876
  AS $fn$
18682
19877
  INSERT INTO rebase.entity_history
18683
- (table_name, entity_id, action, changed_fields, "values", previous_values, updated_by, format)
18684
- VALUES (p_table_name, p_entity_id, p_action, p_changed_fields, p_values, p_previous_values, p_updated_by, p_format)
19878
+ (table_name, entity_id, action, changed_fields, "values", previous_values, updated_by, format, updated_at)
19879
+ VALUES (p_table_name, p_entity_id, p_action, p_changed_fields, p_values, p_previous_values, p_updated_by, p_format, clock_timestamp())
18685
19880
  $fn$
18686
19881
  ` : `
18687
19882
  CREATE OR REPLACE FUNCTION ${RECORD_HISTORY_FUNCTION}(
@@ -18693,8 +19888,8 @@ async function ensureHistoryTableExists(db, options = {}) {
18693
19888
  SET search_path = pg_catalog, pg_temp
18694
19889
  AS $fn$
18695
19890
  INSERT INTO rebase.entity_history
18696
- (table_name, entity_id, action, changed_fields, "values", previous_values, updated_by)
18697
- VALUES (p_table_name, p_entity_id, p_action, p_changed_fields, p_values, p_previous_values, p_updated_by)
19891
+ (table_name, entity_id, action, changed_fields, "values", previous_values, updated_by, updated_at)
19892
+ VALUES (p_table_name, p_entity_id, p_action, p_changed_fields, p_values, p_previous_values, p_updated_by, clock_timestamp())
18698
19893
  $fn$
18699
19894
  `));
18700
19895
  await db.execute(sql.raw(`REVOKE ALL ON FUNCTION ${signature} FROM PUBLIC`));
@@ -18970,6 +20165,11 @@ var HistoryService = class {
18970
20165
  }
18971
20166
  /**
18972
20167
  * Fetch history entries for an row, ordered by most recent first.
20168
+ *
20169
+ * `id` breaks a tie in `updated_at`, so the order is the same on every
20170
+ * read and `offset` pages neither repeat nor skip an entry. Entries written
20171
+ * by `rebase.record_history` carry the moment they were written, so one
20172
+ * write's entries do not tie; the tiebreak covers those that do.
18973
20173
  */
18974
20174
  async fetchHistory(tableName, id, options = {}) {
18975
20175
  const limit = options.limit ?? 20;
@@ -18985,7 +20185,7 @@ var HistoryService = class {
18985
20185
  FROM rebase.entity_history
18986
20186
  WHERE table_name = ${tableName}
18987
20187
  AND entity_id = ${String(id)}
18988
- ORDER BY updated_at DESC
20188
+ ORDER BY updated_at DESC, id DESC
18989
20189
  LIMIT ${limit}
18990
20190
  OFFSET ${offset}
18991
20191
  `)]);
@@ -19049,7 +20249,7 @@ var HistoryService = class {
19049
20249
  SELECT id FROM rebase.entity_history
19050
20250
  WHERE table_name = ${tableName}
19051
20251
  AND entity_id = ${String(id)}
19052
- ORDER BY updated_at DESC
20252
+ ORDER BY updated_at DESC, id DESC
19053
20253
  OFFSET ${this.retention.maxEntries}
19054
20254
  )
19055
20255
  )
@@ -19750,22 +20950,57 @@ async function openPostgresDriver(pgConfig, config) {
19750
20950
  if (explicitCdc) logger.warn(`⚠️ [CDC] REALTIME_CDC=${cdcMode} but ${reason} — using app-level realtime.`);
19751
20951
  else logger.info(`ℹ️ [CDC] Using app-level realtime — ${reason}.`);
19752
20952
  } else if (wantsCdc && directUrl) {
19753
- if (cdcMode === "wal") logger.warn("⚠️ [CDC] REALTIME_CDC=wal: native WAL streaming is not bundled in this build; using trigger-based change capture instead (equivalent database-level coverage).");
19754
- try {
19755
- const cdcTables = collectionsOnThisSource().map((c) => ({
19756
- schema: c.schema ?? "public",
19757
- table: getTableName$1(c),
19758
- identityColumns: collectionKeyColumns(c, registry).map((k) => k.columnName)
19759
- })).filter((t) => Boolean(t.table) && registry.hasTableForCollection(t.table));
19760
- const ownSlugs = new Set(collectionsOnThisSource().map((c) => c.slug));
19761
- for (const link of collectJunctionLinks(registry)) {
19762
- if (!ownSlugs.has(link.parentCollection.slug)) continue;
19763
- cdcTables.push({
19764
- schema: link.schema,
19765
- table: link.table,
19766
- identityColumns: [link.sourceColumn, link.targetColumn]
19767
- });
20953
+ const cdcTables = collectionsOnThisSource().map((c) => ({
20954
+ schema: c.schema ?? "public",
20955
+ table: getTableName$1(c),
20956
+ identityColumns: collectionKeyColumns(c, registry).map((k) => k.columnName)
20957
+ })).filter((t) => Boolean(t.table) && registry.hasTableForCollection(t.table));
20958
+ const ownSlugs = new Set(collectionsOnThisSource().map((c) => c.slug));
20959
+ for (const link of collectJunctionLinks(registry)) {
20960
+ if (!ownSlugs.has(link.parentCollection.slug)) continue;
20961
+ cdcTables.push({
20962
+ schema: link.schema,
20963
+ table: link.table,
20964
+ identityColumns: [link.sourceColumn, link.targetColumn]
20965
+ });
20966
+ }
20967
+ let walActive = false;
20968
+ if (cdcMode === "wal") try {
20969
+ const blocker = await walCaptureBlocker(cdcRunSql);
20970
+ if (blocker) throw new Error(blocker);
20971
+ if (realtimeProvisions) reportUnpublishedTables(await provisionWalPublication(cdcRunSql, cdcTables));
20972
+ if (realtimeSubscribes) await realtimeService.enableWalCdc(directUrl, cdcTables);
20973
+ walActive = true;
20974
+ } catch (err) {
20975
+ logger.warn("⚠️ [CDC] REALTIME_CDC=wal, but changes cannot be streamed from the WAL — using trigger-based capture instead.", { detail: err instanceof Error ? err.message : String(err) });
20976
+ }
20977
+ if (walActive) {
20978
+ cdcEnabled = true;
20979
+ if (realtimeProvisions && realtimeSubscribes) {
20980
+ try {
20981
+ const removed = await removeTriggerCdc(cdcRunSql);
20982
+ cdcFunctionCurrent = true;
20983
+ if (removed > 0) logger.info(`ℹ️ [CDC] Removed ${removed} change-capture trigger(s): this process streams changes from the WAL instead.`);
20984
+ } catch (err) {
20985
+ logger.warn("⚠️ [CDC] Could not remove the change-capture triggers, so every write still runs one and a NOTIFY. The next boot tries again.", { detail: err instanceof Error ? err.message : String(err) });
20986
+ }
20987
+ provisionCdcForTables = async (tables) => {
20988
+ reportUnpublishedTables(await provisionWalPublication(cdcRunSql, tables));
20989
+ };
20990
+ } else if (realtimeProvisions) {
20991
+ try {
20992
+ await provisionTriggerCdc(cdcRunSql, cdcTables);
20993
+ cdcFunctionCurrent = true;
20994
+ } catch (err) {
20995
+ logger.warn("⚠️ [CDC] Could not attach the change-capture triggers a consumer that cannot stream the WAL would fall back to.", { detail: err instanceof Error ? err.message : String(err) });
20996
+ }
20997
+ provisionCdcForTables = async (tables) => {
20998
+ reportUnpublishedTables(await provisionWalPublication(cdcRunSql, tables));
20999
+ await provisionTriggerCdc(cdcRunSql, tables);
21000
+ };
19768
21001
  }
21002
+ logger.info("📡 [CDC] Realtime source = database-level change capture (mode: wal). All writes now emit realtime events regardless of origin." + (realtimeSubscribes ? "" : " This process sets up the capture but does not consume it."));
21003
+ } else try {
19769
21004
  if (realtimeProvisions) {
19770
21005
  await provisionTriggerCdc(cdcRunSql, cdcTables);
19771
21006
  cdcFunctionCurrent = true;
@@ -19937,7 +21172,7 @@ function createPostgresBootstrapper(pgConfig) {
19937
21172
  const openIndexBuildSession = (driverResult) => async () => {
19938
21173
  const db = requireProvisioningDb(driverResult);
19939
21174
  const pool = "$client" in db ? db.$client : void 0;
19940
- const { isPgPool, poolIndexBuildSession } = await import("./deferred-index-build-DRnoMq-O.js").then((n) => n.r);
21175
+ const { isPgPool, poolIndexBuildSession } = await import("./deferred-index-build-BcJ_oulu.js").then((n) => n.r);
19941
21176
  if (isPgPool(pool)) return poolIndexBuildSession(pool)();
19942
21177
  const queryable = provisioningQueryable(driverResult);
19943
21178
  return {
@@ -19963,7 +21198,7 @@ function createPostgresBootstrapper(pgConfig) {
19963
21198
  */
19964
21199
  const provisionRlsRuntime = async (driverResult) => {
19965
21200
  const db = requireProvisioningDb(driverResult);
19966
- const { RLS_BOOTSTRAP_STATEMENTS, rlsRuntimeIsCurrent } = await import("./migrate-baseline-Bfk1LGC6.js").then((n) => n.w);
21201
+ const { RLS_BOOTSTRAP_STATEMENTS, rlsRuntimeIsCurrent } = await import("./migrate-baseline-DPawvckr.js").then((n) => n.E);
19967
21202
  const run = async (text) => sqlRows(await db.execute(sql.raw(text)));
19968
21203
  if (await rlsRuntimeIsCurrent(run).catch(() => false)) return;
19969
21204
  await db.transaction(async (tx) => {
@@ -20076,11 +21311,11 @@ function createPostgresBootstrapper(pgConfig) {
20076
21311
  unavailable: historyUnavailableReason(state, { requireTransaction: false })
20077
21312
  }) };
20078
21313
  }
20079
- const { BOOT_LOCK_TIMEOUT_MS } = await import("./boot-ddl-yukSebLA.js").then((n) => n.i);
21314
+ const { BOOT_LOCK_TIMEOUT_MS } = await import("./boot-ddl-CIGoIGcC.js").then((n) => n.i);
20080
21315
  const historyTable = await ensureHistoryTableExists(db, { lockTimeoutMs: BOOT_LOCK_TIMEOUT_MS });
20081
21316
  const historyIndexes = await ensureHistoryIndexes(db, { lockTimeoutMs: BOOT_LOCK_TIMEOUT_MS });
20082
21317
  if (historyIndexes.length > 0) {
20083
- const { buildDeferredIndexes, scheduleDeferredIndexBuild } = await import("./deferred-index-build-DRnoMq-O.js").then((n) => n.r);
21318
+ const { buildDeferredIndexes, scheduleDeferredIndexBuild } = await import("./deferred-index-build-BcJ_oulu.js").then((n) => n.r);
20084
21319
  const indexOptions = {
20085
21320
  log: (message) => logger.info(`[history] ${message}`),
20086
21321
  lockTimeoutMs: BOOT_LOCK_TIMEOUT_MS
@@ -20136,9 +21371,9 @@ function createPostgresBootstrapper(pgConfig) {
20136
21371
  * proved it can bootstrap with.
20137
21372
  */
20138
21373
  async ensureCollectionSchema(collections, driverResult, log) {
20139
- const { ensureCollectionTables } = await import("./ensure-collection-tables-BeF1lArf.js").then((n) => n.n);
21374
+ const { ensureCollectionTables } = await import("./ensure-collection-tables-CSbgkTBx.js").then((n) => n.n);
20140
21375
  const queryable = provisioningQueryable(driverResult);
20141
- const { BOOT_LOCK_TIMEOUT_MS } = await import("./boot-ddl-yukSebLA.js").then((n) => n.i);
21376
+ const { BOOT_LOCK_TIMEOUT_MS } = await import("./boot-ddl-CIGoIGcC.js").then((n) => n.i);
20142
21377
  const provisionOnly = ["1", "true"].includes((process.env.REBASE_PROVISION_ONLY ?? "").trim().toLowerCase());
20143
21378
  const plan = await ensureCollectionTables(queryable, collections, log, {
20144
21379
  lockTimeoutMs: BOOT_LOCK_TIMEOUT_MS,
@@ -20150,7 +21385,7 @@ function createPostgresBootstrapper(pgConfig) {
20150
21385
  pendingIndexBuild?.cancel();
20151
21386
  pendingIndexBuild = void 0;
20152
21387
  if (!provisionOnly && plan.deferredIndexes.length > 0) {
20153
- const { scheduleDeferredIndexBuild } = await import("./deferred-index-build-DRnoMq-O.js").then((n) => n.r);
21388
+ const { scheduleDeferredIndexBuild } = await import("./deferred-index-build-BcJ_oulu.js").then((n) => n.r);
20154
21389
  logger.info(`[schema] ${plan.deferredIndexes.length} index(es) are missing, or were left INVALID, on tables that already exist. They are built in the background once the server is serving, one at a time, without blocking reads or writes.`);
20155
21390
  pendingIndexBuild = scheduleDeferredIndexBuild(openIndexBuildSession(driverResult), plan.deferredIndexes, {
20156
21391
  log: (message) => logger.info(`[schema] ${message}`),
@@ -20190,7 +21425,7 @@ function createPostgresBootstrapper(pgConfig) {
20190
21425
  await provisionRlsRuntime(driverResult);
20191
21426
  },
20192
21427
  async ensureCollectionPolicies(collections, driverResult, log) {
20193
- const { ensureCollectionPolicies, describeRulesDropped } = await import("./ensure-collection-policies-H8HN1Gbd.js");
21428
+ const { ensureCollectionPolicies, describeRulesDropped } = await import("./ensure-collection-policies-BNwSQfVx.js");
20194
21429
  const queryable = provisioningQueryable(driverResult);
20195
21430
  const { rows } = await queryable.query("SELECT to_regprocedure('rebase.roles()') IS NOT NULL AS present");
20196
21431
  if (!rows[0]?.present) await provisionRlsRuntime(driverResult);
@@ -20215,7 +21450,7 @@ function createPostgresBootstrapper(pgConfig) {
20215
21450
  for (const u of outcome.unsecured.filter((u) => u.grantWithdrawn)) logger.error(`🔐 [rls] Could not enable row-level security on "${u.table}": ${u.error}. Its privileges have been revoked from ${REBASE_USER_ROLE$1}, so the table is unreachable rather than unprotected. Reads and writes to that collection will fail until RLS can be enabled.`);
20216
21451
  if (unrevoked.length > 0) throw new Error("Refusing to start: row-level security could not be enabled on " + unrevoked.map((u) => `"${u.table}" (${u.error})`).join(", ") + `, and the privileges granted to ${REBASE_USER_ROLE$1} could not be revoked either. The table would be served with no row filtering. Fix the database permissions (the connection role must own these tables, or be able to ALTER them) and boot again.`);
20217
21452
  try {
20218
- const { dropLegacyAuthSchema } = await import("./migrate-baseline-Bfk1LGC6.js").then((n) => n.w);
21453
+ const { dropLegacyAuthSchema } = await import("./migrate-baseline-DPawvckr.js").then((n) => n.E);
20219
21454
  await dropLegacyAuthSchema(async (text) => (await queryable.query(text)).rows, {
20220
21455
  info: (m) => logger.info(m),
20221
21456
  warn: (m) => logger.warn(m)
@@ -20306,7 +21541,7 @@ function createPostgresBootstrapper(pgConfig) {
20306
21541
  return driverResult.internals.driver.admin;
20307
21542
  },
20308
21543
  async initializeWebsockets(server, realtimeService, driver, config, adapter, limits) {
20309
- const { createPostgresWebSocket, isSocketRealtimeService, socketRealtimeMissing } = await import("./websocket-C9Fx7iSx.js").then((n) => n.c);
21544
+ const { createPostgresWebSocket, isSocketRealtimeService, socketRealtimeMissing } = await import("./websocket-D4wrzF3b.js").then((n) => n.c);
20310
21545
  if (!isSocketRealtimeService(realtimeService)) throw new Error(`The realtime service handed to the Postgres socket cannot serve it — it has no ${socketRealtimeMissing(realtimeService).join(", ")}. Every socket would fail on its first frame.`);
20311
21546
  const realtimeConfig = effectiveConfig().realtime;
20312
21547
  const maxSocketsPerUser = realtimeConfig?.maxSocketsPerUser !== void 0 ? parseMaxSocketsPerUser(realtimeConfig.maxSocketsPerUser, "realtime.maxSocketsPerUser") : void 0;
@@ -20428,6 +21663,7 @@ async function openRebase(options) {
20428
21663
  collections,
20429
21664
  async sql(query, sqlOptions) {
20430
21665
  if (!isSQLAdmin(admin)) throw new Error("This driver cannot run raw SQL.");
21666
+ warnOnTableDdl(query, (message) => logger.warn(message));
20431
21667
  if (sqlOptions?.database !== void 0) {
20432
21668
  const asked = resourceKeyOf(sqlOptions.database);
20433
21669
  if (asked !== ownKey) throw new Error(`openRebase opened the "${ownKey}" data source only, so it cannot run SQL on "${asked}". Open that one with its own openRebase({ databaseUrl, dataSourceKey: "${asked}" }).`);
@@ -20450,4 +21686,4 @@ async function openRebase(options) {
20450
21686
  //#endregion
20451
21687
  export { AuthenticatedPostgresBackendDriver as $, budgetSendBuffers as A, mfaFactors as B, PG_NOTIFY_MAX_PAYLOAD_BYTES as C, MemoryChannelBus as D, parseChannelBusPayload as E, createAuthSchema as F, recoveryCodesRelations as G, passwordResetTokens as H, magicLinkTokens as I, userIdentities as J, refreshTokens as K, magicLinkTokensRelations as L, terminateIfBacklogged as M, assertReadRequestReadable as N, frameByteLength as O, appConfig as P, usersSchema as Q, mfaChallenges as R, DEFAULT_BATCH_WINDOW_MS as S, parseChannelBusFrame as T, passwordResetTokensRelations as U, mfaFactorsRelations as V, recoveryCodes as W, users as X, userIdentitiesRelations as Y, usersRelations as Z, parseRealtimeTuning as _, listedColumnKeys as _t, openPostgresDriver as a, isRoleSwitchingOptedOut as at, resolveChannelBusSetting as b, resolveSourceUrls as c, SqlResultTooLargeError as ct, DEFAULT_RECOUNT_MIN_ROWS as d, DrizzleConditionBuilder as dt, CONNECTION_OWNER as et, PostgresRealtimeProvider as f, PostgresConditionBuilder as ft, parseMaxSubscriptionsPerSocket as g, joinColumn as gt, parseMaxSocketsPerUser as h, getUnknownFilterFieldsMode as ht, isScaffoldedLocalDatabase as i, effectiveSqlRole as it, reapSilentSockets as j, isChannelBusInstance$1 as k, PostgresCollectionRegistry as l, isNestedPath as lt, packEntityChanges as m, escapeLikePattern as mt, createPostgresBootstrapper as n, RoleSwitchUnavailableError as nt, postgresDriverConfigFromEnv as o, sqlRoleRanAs as ot, RealtimeService as p, configureUnknownFilterFields as pt, refreshTokensRelations as q, describeSchemaDriftCause as r, branchParentDatabase as rt, resolveDriftCheckName as s, SQL_RESULT_BYTE_BUDGET as st, openRebase as t, PostgresBackendDriver as tt, DEFAULT_MAX_SUBSCRIPTIONS_PER_SOCKET as u, resolveNestedPath as ut, refetchConcurrencyFor as v, PostgresChannelBus as w, CHANNEL_BUS_NOTIFY_CHANNEL as x, createChannelBus as y, mfaChallengesRelations as z };
20452
21688
 
20453
- //# sourceMappingURL=open-rebase-DZ5JbZpg.js.map
21689
+ //# sourceMappingURL=open-rebase-D9n8idex.js.map