@rebasepro/server-postgres 0.26.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 (105) hide show
  1. package/README.md +1 -1
  2. package/dist/{BranchService-CWBnDhcv.js → BranchService-BPssiD8_.js} +37 -11
  3. package/dist/BranchService-BPssiD8_.js.map +1 -0
  4. package/dist/PostgresBackendDriver.d.ts +7 -0
  5. package/dist/auth/services.d.ts +34 -8
  6. package/dist/backup/pg-tools.d.ts +14 -0
  7. package/dist/backup/retention.d.ts +14 -1
  8. package/dist/{backup-cli-PoRG-zEq.js → backup-cli-C1wcj-rc.js} +60 -12
  9. package/dist/backup-cli-C1wcj-rc.js.map +1 -0
  10. package/dist/{boot-ddl-EsgqDkx0.js → boot-ddl-CIGoIGcC.js} +2 -2
  11. package/dist/{boot-ddl-EsgqDkx0.js.map → boot-ddl-CIGoIGcC.js.map} +1 -1
  12. package/dist/branch-argv.d.ts +20 -0
  13. package/dist/{cli-console-f0k6_TsJ.js → cli-console-C0FYkICX.js} +5 -5
  14. package/dist/{cli-console-f0k6_TsJ.js.map → cli-console-C0FYkICX.js.map} +1 -1
  15. package/dist/{cli-errors-DsYyNYzd.js → cli-errors-DYets7cp.js} +6 -6
  16. package/dist/{cli-errors-DsYyNYzd.js.map → cli-errors-DYets7cp.js.map} +1 -1
  17. package/dist/cli.js +43 -41
  18. package/dist/cli.js.map +1 -1
  19. package/dist/{column-plan-helpers-IG6uFb1s.js → column-plan-helpers-BeSztMor.js} +5 -1
  20. package/dist/{column-plan-helpers-IG6uFb1s.js.map → column-plan-helpers-BeSztMor.js.map} +1 -1
  21. package/dist/data-transformer.d.ts +11 -1
  22. package/dist/{databasePoolManager-3stMNFL-.js → databasePoolManager-BjoVvuAq.js} +2 -2
  23. package/dist/databasePoolManager-BjoVvuAq.js.map +1 -0
  24. package/dist/{deferred-index-build-CqaOv1Xx.js → deferred-index-build-BcJ_oulu.js} +5 -5
  25. package/dist/{deferred-index-build-CqaOv1Xx.js.map → deferred-index-build-BcJ_oulu.js.map} +1 -1
  26. package/dist/{doctor-DW_mlTzz.js → doctor-TJApVglQ.js} +6 -6
  27. package/dist/{doctor-DW_mlTzz.js.map → doctor-TJApVglQ.js.map} +1 -1
  28. package/dist/{ensure-collection-policies-DLWgabQ2.js → ensure-collection-policies-BNwSQfVx.js} +63 -25
  29. package/dist/ensure-collection-policies-BNwSQfVx.js.map +1 -0
  30. package/dist/{ensure-collection-tables-CFIRq8ni.js → ensure-collection-tables-CSbgkTBx.js} +8 -9
  31. package/dist/{ensure-collection-tables-CFIRq8ni.js.map → ensure-collection-tables-CSbgkTBx.js.map} +1 -1
  32. package/dist/{ensure-tables-4nA-tfwi.js → ensure-tables-CF-8zgYA.js} +3 -3
  33. package/dist/{ensure-tables-4nA-tfwi.js.map → ensure-tables-CF-8zgYA.js.map} +1 -1
  34. package/dist/{enum-labels-kept-PcPW65xy.js → enum-labels-kept-BCgFGAbJ.js} +3 -3
  35. package/dist/{enum-labels-kept-PcPW65xy.js.map → enum-labels-kept-BCgFGAbJ.js.map} +1 -1
  36. package/dist/{generate-drizzle-schema-DC0BOUpn.js → generate-drizzle-schema-BAIp9nKT.js} +2 -2
  37. package/dist/{generate-drizzle-schema-DC0BOUpn.js.map → generate-drizzle-schema-BAIp9nKT.js.map} +1 -1
  38. package/dist/{generate-drizzle-schema-logic-CPzCRD-8.js → generate-drizzle-schema-logic-CVRmZjP3.js} +2 -2
  39. package/dist/{generate-drizzle-schema-logic-CPzCRD-8.js.map → generate-drizzle-schema-logic-CVRmZjP3.js.map} +1 -1
  40. package/dist/{generate-files-BP-4W_gm.js → generate-files-DutUrsSS.js} +3 -3
  41. package/dist/{generate-files-BP-4W_gm.js.map → generate-files-DutUrsSS.js.map} +1 -1
  42. package/dist/{generate-postgres-ddl-logic-DPl1oH_z.js → generate-postgres-ddl-logic-0rbAqwYT.js} +22 -10
  43. package/dist/generate-postgres-ddl-logic-0rbAqwYT.js.map +1 -0
  44. package/dist/{generated-schema-diff-eZCklRZQ.js → generated-schema-diff-ChFg6UxI.js} +13 -4
  45. package/dist/{generated-schema-diff-eZCklRZQ.js.map → generated-schema-diff-ChFg6UxI.js.map} +1 -1
  46. package/dist/history/HistoryService.d.ts +5 -0
  47. package/dist/index.es.js +14 -13
  48. package/dist/index.es.js.map +1 -1
  49. package/dist/{migrate-baseline-B-2Ef4Mv.js → migrate-baseline-DPawvckr.js} +54 -11
  50. package/dist/{migrate-baseline-B-2Ef4Mv.js.map → migrate-baseline-DPawvckr.js.map} +1 -1
  51. package/dist/{migrate-policy-stamps-BQtFHApx.js → migrate-policy-stamps-B-8vTSIm.js} +6 -6
  52. package/dist/{migrate-policy-stamps-BQtFHApx.js.map → migrate-policy-stamps-B-8vTSIm.js.map} +1 -1
  53. package/dist/{open-rebase-C8jQ6s5a.js → open-rebase-D9n8idex.js} +1685 -201
  54. package/dist/open-rebase-D9n8idex.js.map +1 -0
  55. package/dist/{pg-error-utils-DhiUTlVx.js → pg-error-utils-BIHfsGZL.js} +2 -2
  56. package/dist/{pg-error-utils-DhiUTlVx.js.map → pg-error-utils-BIHfsGZL.js.map} +1 -1
  57. package/dist/{plan-schema-Czf3q4-Q.js → plan-schema-DMVh77yb.js} +29 -2
  58. package/dist/plan-schema-DMVh77yb.js.map +1 -0
  59. package/dist/{policy-drift-X4A-xAzv.js → policy-drift-0Cp4Lc0x.js} +13 -4
  60. package/dist/policy-drift-0Cp4Lc0x.js.map +1 -0
  61. package/dist/{push-index-prebuild-UTvB5pqH.js → push-index-prebuild-DR3P4yOw.js} +4 -4
  62. package/dist/{push-index-prebuild-UTvB5pqH.js.map → push-index-prebuild-DR3P4yOw.js.map} +1 -1
  63. package/dist/{push-kept-defaults-BGOlOqRQ.js → push-kept-defaults-BSqPzp0T.js} +3 -3
  64. package/dist/{push-kept-defaults-BGOlOqRQ.js.map → push-kept-defaults-BSqPzp0T.js.map} +1 -1
  65. package/dist/{push-legacy-renames-BwaKi8xF.js → push-legacy-renames-BIxBmVLl.js} +4 -4
  66. package/dist/{push-legacy-renames-BwaKi8xF.js.map → push-legacy-renames-BIxBmVLl.js.map} +1 -1
  67. package/dist/{push-policy-plan-DQ2tWceD.js → push-policy-plan-BHsasXa7.js} +4 -4
  68. package/dist/{push-policy-plan-DQ2tWceD.js.map → push-policy-plan-BHsasXa7.js.map} +1 -1
  69. package/dist/{render-ddl-4v88y-Gv.js → render-ddl-NpPPDACz.js} +20 -3
  70. package/dist/render-ddl-NpPPDACz.js.map +1 -0
  71. package/dist/schema/doctor-cli.js +3 -3
  72. package/dist/schema/ensure-collection-policies.d.ts +6 -4
  73. package/dist/schema/generate-drizzle-schema.js +1 -1
  74. package/dist/schema/generate-postgres-ddl-logic.d.ts +16 -0
  75. package/dist/schema/generate-postgres-ddl.js +1 -1
  76. package/dist/schema/generated-schema-diff.d.ts +6 -0
  77. package/dist/schema/introspect-db.js +1 -1
  78. package/dist/schema/plan/render-ddl.d.ts +11 -0
  79. package/dist/schema/reconcile-schema.d.ts +21 -0
  80. package/dist/security/policy-drift.d.ts +9 -1
  81. package/dist/services/BranchService.d.ts +6 -0
  82. package/dist/services/RelationService.d.ts +6 -6
  83. package/dist/services/cdc/CdcListener.d.ts +12 -0
  84. package/dist/services/cdc/WalCdcListener.d.ts +92 -0
  85. package/dist/services/cdc/pgoutput.d.ts +121 -0
  86. package/dist/services/cdc/wal-cdc.d.ts +29 -0
  87. package/dist/services/channel-bus/PostgresChannelBus.d.ts +1 -0
  88. package/dist/services/collection-helpers.d.ts +14 -0
  89. package/dist/services/junction-writes.d.ts +9 -1
  90. package/dist/services/nested-path.d.ts +2 -1
  91. package/dist/services/realtimeService.d.ts +77 -3
  92. package/dist/utils/drizzle-conditions.d.ts +11 -0
  93. package/dist/{websocket-CKYc3V4N.js → websocket-D4wrzF3b.js} +35 -8
  94. package/dist/websocket-D4wrzF3b.js.map +1 -0
  95. package/package.json +7 -7
  96. package/dist/BranchService-CWBnDhcv.js.map +0 -1
  97. package/dist/backup-cli-PoRG-zEq.js.map +0 -1
  98. package/dist/databasePoolManager-3stMNFL-.js.map +0 -1
  99. package/dist/ensure-collection-policies-DLWgabQ2.js.map +0 -1
  100. package/dist/generate-postgres-ddl-logic-DPl1oH_z.js.map +0 -1
  101. package/dist/open-rebase-C8jQ6s5a.js.map +0 -1
  102. package/dist/plan-schema-Czf3q4-Q.js.map +0 -1
  103. package/dist/policy-drift-X4A-xAzv.js.map +0 -1
  104. package/dist/render-ddl-4v88y-Gv.js.map +0 -1
  105. package/dist/websocket-CKYc3V4N.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-DsYyNYzd.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-DhiUTlVx.js";
7
- import { t as BranchService } from "./BranchService-CWBnDhcv.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
- import { u as resolveColumnName } from "./column-plan-helpers-IG6uFb1s.js";
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-Czf3q4-Q.js";
12
- import { t as generateSchema } from "./generate-drizzle-schema-logic-CPzCRD-8.js";
13
- import { r as renderPgType } from "./render-ddl-4v88y-Gv.js";
14
- import { u as typesAgree } from "./deferred-index-build-CqaOv1Xx.js";
15
- import { a as isLockTimeout, c as resolveBootLockOptions, l as sleep, n as BOOT_LOCK_TIMEOUT_MS } from "./boot-ddl-EsgqDkx0.js";
16
- import { a as readSchemaFactsFor, r as planCollectionSchemaEnsure } from "./ensure-collection-tables-CFIRq8ni.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-4nA-tfwi.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-eZCklRZQ.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
@@ -9523,6 +9731,7 @@ var PostgresBackendDriver = class PostgresBackendDriver {
9523
9731
  get admin() {
9524
9732
  return {
9525
9733
  executeSql: (...args) => this.executeSql(...args),
9734
+ probe: (timeoutMs) => this.probe(timeoutMs),
9526
9735
  runSqlScript: (...args) => this.runSqlScript(...args),
9527
9736
  openSqlCursor: (...args) => this.openSqlCursor(...args),
9528
9737
  fetchAvailableDatabases: () => this.fetchAvailableDatabases(),
@@ -9538,6 +9747,7 @@ var PostgresBackendDriver = class PostgresBackendDriver {
9538
9747
  sourceOnly: options?.sourceOnly,
9539
9748
  existing: await readSchemaFactsFor(this.schemaFactsQueryable(), after)
9540
9749
  }),
9750
+ planSchemaReconcile: async (collections, options) => readReconcileFacts(this.schemaFactsQueryable(), collections, options),
9541
9751
  listen: (...args) => this.listen(...args),
9542
9752
  ...this.branchService ? {
9543
9753
  createBranch: this.branchService.createBranch.bind(this.branchService),
@@ -10546,6 +10756,7 @@ var PostgresBackendDriver = class PostgresBackendDriver {
10546
10756
  const values = operation.values ? resolveBatchRefs(operation.values, named) : void 0;
10547
10757
  const rawId = operation.id !== void 0 ? resolveBatchRefs(operation.id, named) : void 0;
10548
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");
10549
10760
  let row = null;
10550
10761
  switch (operation.op) {
10551
10762
  case "create":
@@ -10615,6 +10826,7 @@ var PostgresBackendDriver = class PostgresBackendDriver {
10615
10826
  if (precondition) await precondition.check(await this.asRestServes(stored, targetPath));
10616
10827
  if (!stored) throw ApiError.notFound(`No row "${row.id}" in "${targetPath}" to delete.`);
10617
10828
  const targetRow = { ...stored };
10829
+ const address = (resolvedCollection ? deriveRowAddress(stored, resolvedCollection, this.registry) : "") || row.id.toString();
10618
10830
  const contextForCallback = this.buildCallContext();
10619
10831
  try {
10620
10832
  frame.stage = "beforeDelete";
@@ -10687,18 +10899,18 @@ var PostgresBackendDriver = class PostgresBackendDriver {
10687
10899
  }
10688
10900
  if (this.historyService && resolvedCollection?.history && !unlinking) await this.historyService.recordHistory({
10689
10901
  tableName: resolvedCollection.slug,
10690
- id: row.id.toString(),
10902
+ id: address,
10691
10903
  action: "delete",
10692
10904
  values: stored,
10693
10905
  updatedBy: this.user?.uid
10694
10906
  });
10695
10907
  if (this._deferNotifications) this._pendingNotifications.push({
10696
10908
  path: targetPath,
10697
- id: row.id.toString(),
10909
+ id: address,
10698
10910
  row: null,
10699
10911
  databaseId: resolvedCollection?.databaseId
10700
10912
  });
10701
- else await this.realtimeService.notifyUpdate(targetPath, row.id.toString(), null, resolvedCollection?.databaseId);
10913
+ else await this.realtimeService.notifyUpdate(targetPath, address, null, resolvedCollection?.databaseId);
10702
10914
  }
10703
10915
  /**
10704
10916
  * The child lists a save rewrote through the parent, as notifications —
@@ -10809,6 +11021,32 @@ var PostgresBackendDriver = class PostgresBackendDriver {
10809
11021
  return options?.maxResultBytes === void 0 ? rows : refuseOversizedRows(rows, options.maxResultBytes);
10810
11022
  }
10811
11023
  /**
11024
+ * `SQLAdmin.probe`: `SELECT 1` on a connection opened for it, outside the
11025
+ * pool, so a pool whose connections are all serving slow requests does not
11026
+ * read as a database that is down. Without a pg pool (PGlite) there is no
11027
+ * queue to bypass, and the query runs like any other.
11028
+ */
11029
+ async probe(timeoutMs) {
11030
+ const pool = "$client" in this.db ? this.db.$client : void 0;
11031
+ if (!(pool instanceof Pool)) {
11032
+ await this.db.execute(sql.raw("SELECT 1"));
11033
+ return;
11034
+ }
11035
+ const client = new Client({
11036
+ ...pool.options,
11037
+ connectionTimeoutMillis: timeoutMs,
11038
+ query_timeout: timeoutMs,
11039
+ statement_timeout: timeoutMs
11040
+ });
11041
+ client.on("error", () => {});
11042
+ try {
11043
+ await client.connect();
11044
+ await client.query("SELECT 1");
11045
+ } finally {
11046
+ await client.end().catch(() => {});
11047
+ }
11048
+ }
11049
+ /**
10812
11050
  * Run a script a person wrote — the Studio console — and describe its
10813
11051
  * result. See `SQLAdmin.runSqlScript` and `services/sql-script.ts`.
10814
11052
  */
@@ -11811,8 +12049,8 @@ var MAX_NOTIFY_BYTES = 7900;
11811
12049
  * notification.
11812
12050
  */
11813
12051
  var CHUNK_SLACK_BYTES = 64;
11814
- var quoteIdent = (name) => `"${name.replace(/"/g, "\"\"")}"`;
11815
- var quoteLiteral = (value) => `'${value.replace(/'/g, "''")}'`;
12052
+ var quoteIdent$1 = (name) => `"${name.replace(/"/g, "\"\"")}"`;
12053
+ var quoteLiteral$1 = (value) => `'${value.replace(/'/g, "''")}'`;
11816
12054
  /**
11817
12055
  * The row-level function: `{ schema, table, op, row }`, one per changed row.
11818
12056
  *
@@ -11895,7 +12133,7 @@ BEGIN
11895
12133
  )::text;
11896
12134
  END IF;
11897
12135
 
11898
- PERFORM pg_notify(${quoteLiteral(CDC_CHANNEL)}, payload);
12136
+ PERFORM pg_notify(${quoteLiteral$1(CDC_CHANNEL)}, payload);
11899
12137
  RETURN NULL;
11900
12138
  END;
11901
12139
  $rebase_cdc$;
@@ -11930,7 +12168,7 @@ var COMPAT_PATTERN = `${COMPAT_DECLARATION}'([^']*)'`;
11930
12168
  * document is fetched to name its row.
11931
12169
  */
11932
12170
  function buildCdcStatementFunctionSql(compatUntil) {
11933
- const channel = quoteLiteral(CDC_CHANNEL);
12171
+ const channel = quoteLiteral$1(CDC_CHANNEL);
11934
12172
  const idEnvelope = `,"cols":["id"],"keys":[]}`;
11935
12173
  const chunked = (source, keyExpr) => `SELECT string_agg(k, ',') AS keys
11936
12174
  FROM (SELECT k, (sum(octet_length(k) + 1) OVER (ROWS UNBOUNDED PRECEDING) - 1) / budget AS c
@@ -11946,7 +12184,7 @@ CREATE OR REPLACE FUNCTION ${CDC_STATEMENT_FUNCTION}() RETURNS trigger
11946
12184
  LANGUAGE plpgsql AS $rebase_cdc$
11947
12185
  DECLARE
11948
12186
  -- One row per notification until then: see CDC_ROW_COMPAT_MS in trigger-cdc.ts.
11949
- ${COMPAT_DECLARATION}${quoteLiteral(compatUntil)};
12187
+ ${COMPAT_DECLARATION}${quoteLiteral$1(compatUntil)};
11950
12188
  head text;
11951
12189
  cols text[];
11952
12190
  collist text;
@@ -12081,8 +12319,8 @@ $rebase_cdc$;
12081
12319
  * function serves every table. Empty means "the primary key".
12082
12320
  */
12083
12321
  function buildCdcTriggerSql(schema, table, identityColumns = [], level = "statement") {
12084
- const qualified = `${quoteIdent(schema)}.${quoteIdent(table)}`;
12085
- 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);
12086
12324
  }
12087
12325
  /**
12088
12326
  * `CREATE TRIGGER` alone, for a table that has no capture trigger yet.
@@ -12092,15 +12330,15 @@ function buildCdcTriggerSql(schema, table, identityColumns = [], level = "statem
12092
12330
  * `CREATE TRIGGER` takes SHARE ROW EXCLUSIVE, which readers never wait for.
12093
12331
  */
12094
12332
  var buildCdcTriggerCreateSql = (schema, table, identityColumns, level) => {
12095
- const qualified = `${quoteIdent(schema)}.${quoteIdent(table)}`;
12096
- const args = [...new Set(identityColumns)].map(quoteLiteral).join(", ");
12097
- 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");
12098
12336
  };
12099
12337
  /** `to_regprocedure` of each function, as a SQL expression. */
12100
- var ROW_FUNCTION_OID = `to_regprocedure(${quoteLiteral(`${CDC_TRIGGER_FUNCTION}()`)})`;
12101
- 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}()`)})`;
12102
12340
  /** The installed statement function's `compat_until` literal, or NULL when it is not installed. */
12103
- 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})`;
12104
12342
  /** The window end a `compat_until` literal names, in epoch ms; `-Infinity` for none or unreadable. */
12105
12343
  var compatMs = (literal) => {
12106
12344
  if (typeof literal !== "string" || literal === "" || literal === "-infinity") return -Infinity;
@@ -12124,7 +12362,7 @@ var functionBody = (sql) => sql.slice(sql.indexOf(DOLLAR_QUOTE) + 12, sql.lastIn
12124
12362
  var COMPAT_LINE_SQL_PATTERN = `${COMPAT_DECLARATION}'[^']*'`;
12125
12363
  var withoutCompat = (body) => body.replace(new RegExp(COMPAT_LINE_SQL_PATTERN), "");
12126
12364
  /** Whether each installed function is the current one, as SQL columns of one row. */
12127
- 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`;
12128
12366
  var readInstalledFunctions = (row) => ({
12129
12367
  rowCurrent: row?.row_function_current === true,
12130
12368
  statementCodeCurrent: row?.statement_code_current === true,
@@ -12216,11 +12454,11 @@ async function settleInstalledCapture(run, options) {
12216
12454
  */
12217
12455
  async function resolveIdentityColumns(run, ref) {
12218
12456
  const requested = ref.identityColumns ?? [];
12219
- const qualified = `${quoteIdent(ref.schema)}.${quoteIdent(ref.table)}`;
12457
+ const qualified = `${quoteIdent$1(ref.schema)}.${quoteIdent$1(ref.table)}`;
12220
12458
  let columns;
12221
12459
  let relkind = null;
12222
12460
  try {
12223
- 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`);
12224
12462
  columns = rows.map((row) => ({
12225
12463
  name: String(row.name),
12226
12464
  primary: row.is_primary === true
@@ -12260,7 +12498,7 @@ var decodeTriggerArgs = (hex) => {
12260
12498
  * 4.4 s while a report read the table.
12261
12499
  */
12262
12500
  async function readCaptureState(run, refs) {
12263
- 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(", ")})
12264
12502
  SELECT t.key,
12265
12503
  (SELECT c.relkind::text FROM pg_class c WHERE c.oid = t.rel) AS relkind,
12266
12504
  (SELECT coalesce(json_agg(json_build_object('name', a.attname,
@@ -12282,7 +12520,7 @@ async function readCaptureState(run, refs) {
12282
12520
  'newTable', tr.tgnewtable,
12283
12521
  'oldTable', tr.tgoldtable)), '[]'::json)
12284
12522
  FROM pg_trigger tr
12285
- 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,
12286
12524
  f.*
12287
12525
  FROM target t, (SELECT ${functionsCurrentSql()}) f`);
12288
12526
  const parseJson = (value) => typeof value === "string" ? JSON.parse(value) : value;
@@ -12572,6 +12810,853 @@ var CdcListener = class {
12572
12810
  }
12573
12811
  };
12574
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
12575
13660
  //#region src/services/socket-liveness.ts
12576
13661
  /**
12577
13662
  * Letting go of client sockets the server can no longer reach.
@@ -13313,6 +14398,7 @@ var PostgresChannelBus = class {
13313
14398
  connectionString;
13314
14399
  kind = "postgres";
13315
14400
  maxFrameBytes = PG_NOTIFY_MAX_PAYLOAD_BYTES;
14401
+ publishesOnDemand = true;
13316
14402
  listener;
13317
14403
  batchWindowMs;
13318
14404
  /**
@@ -13495,6 +14581,15 @@ function coerceFrame(value) {
13495
14581
  from: typeof obj.from === "string" ? obj.from : void 0,
13496
14582
  seq: obj.seq
13497
14583
  };
14584
+ case "channels": return {
14585
+ kind: "channels",
14586
+ sid,
14587
+ channel,
14588
+ add: Array.isArray(obj.add) ? obj.add.filter((c) => typeof c === "string") : void 0,
14589
+ list: Array.isArray(obj.list) ? obj.list.filter((c) => typeof c === "string") : void 0,
14590
+ done: obj.done === true,
14591
+ hello: obj.hello === true
14592
+ };
13498
14593
  case "presence_diff": return {
13499
14594
  kind: "presence_diff",
13500
14595
  sid,
@@ -13772,6 +14867,17 @@ var RealtimeService = class RealtimeService extends EventEmitter {
13772
14867
  */
13773
14868
  bus = new MemoryChannelBus();
13774
14869
  /**
14870
+ * The other instances on an on-demand bus, and the channels each serves
14871
+ * (`null`: it hasn't said, so assume all of them). See {@link hasRemoteMembers}.
14872
+ */
14873
+ busPeers = /* @__PURE__ */ new Map();
14874
+ /** Frames the bus took, by kind. See {@link counts}. */
14875
+ busFramesPublished = /* @__PURE__ */ new Map();
14876
+ /** Frames not sent because no other instance had members. See {@link counts}. */
14877
+ busFramesSkipped = 0;
14878
+ busStartedAt = 0;
14879
+ busGossipInterval;
14880
+ /**
13775
14881
  * The shared presence roster, present only when a real bus is active.
13776
14882
  *
13777
14883
  * Fan-out alone is not enough for presence: `presence_state` has to answer
@@ -13787,6 +14893,8 @@ var RealtimeService = class RealtimeService extends EventEmitter {
13787
14893
  * never track presence, and each of their closes was a `DELETE` too.
13788
14894
  */
13789
14895
  presenceTrackers = /* @__PURE__ */ new Set();
14896
+ /** Each client's roster writes still running, chained in order — see {@link presenceStoreOp}. */
14897
+ presenceStoreQueues = /* @__PURE__ */ new Map();
13790
14898
  /**
13791
14899
  * Channels whose oversized ephemeral broadcasts have already been reported,
13792
14900
  * so a hot channel logs the problem once rather than once per message.
@@ -13936,7 +15044,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
13936
15044
  * refetches once per this interval, which is the whole cost.
13937
15045
  */
13938
15046
  static REFETCH_MAX_WAIT_MS = DEFAULT_REFETCH_MAX_WAIT_MS;
13939
- /** 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). */
13940
15048
  cdcListener;
13941
15049
  /** Whether database-level CDC is the active cross-instance change source. */
13942
15050
  cdcActive = false;
@@ -14449,7 +15557,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
14449
15557
  this.clients.delete(clientId);
14450
15558
  this.clientOptIns.delete(clientId);
14451
15559
  for (const key of [...this.subscriptionsByClient.get(clientId) ?? []]) this.dropSubscription(key);
14452
- 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");
14453
15561
  for (const channel of [...this.channelsByClient.get(clientId) ?? []]) {
14454
15562
  const members = this.channels.get(channel);
14455
15563
  if (members?.has(clientId)) {
@@ -14499,6 +15607,21 @@ var RealtimeService = class RealtimeService extends EventEmitter {
14499
15607
  }
14500
15608
  }
14501
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
+ /**
14502
15625
  * Refuse a new subscription past the socket's ceiling, and say so.
14503
15626
  *
14504
15627
  * Every collection subscription is a refetch on every write to its
@@ -14520,6 +15643,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
14520
15643
  const subscriptionId = request.subscriptionId;
14521
15644
  const key = this.subscriptionKey(clientId, subscriptionId);
14522
15645
  let canDeliver;
15646
+ if (!this.socketIsOpen(clientId)) return;
14523
15647
  try {
14524
15648
  const collection = this.registry.getCollectionByPath(request.path);
14525
15649
  if (!collection) {
@@ -14617,6 +15741,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
14617
15741
  const subscriptionId = request.subscriptionId;
14618
15742
  const key = this.subscriptionKey(clientId, subscriptionId);
14619
15743
  let canDeliver;
15744
+ if (!this.socketIsOpen(clientId)) return;
14620
15745
  try {
14621
15746
  const collection = this.registry.getCollectionByPath(request.path);
14622
15747
  if (!collection) {
@@ -14702,16 +15827,18 @@ var RealtimeService = class RealtimeService extends EventEmitter {
14702
15827
  const subscription = this._subscriptions.get(key);
14703
15828
  if (!subscription) continue;
14704
15829
  const subscriptionId = subscription.subscriptionId;
14705
- 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) {
14706
15839
  const collection = this.registry.getCollectionByPath(subscription.path);
14707
- const request = subscription.collectionRequest;
14708
15840
  try {
14709
- if (collection) assertReadRequestReadable({
14710
- filter: request.filter,
14711
- logical: request.logical,
14712
- orderBy: request.orderBy,
14713
- fields: request.fields
14714
- }, collection, { roles: authContext.roles ?? ["anon"] });
15841
+ if (collection) assertReadRequestReadable(judged, collection, { roles: authContext.roles ?? ["anon"] });
14715
15842
  } catch (e) {
14716
15843
  if (!(e instanceof ApiError)) throw e;
14717
15844
  await this.handleUnsubscribe(clientId, subscriptionId);
@@ -15172,7 +16299,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15172
16299
  * themselves would keep a page of rows per subscriber in memory.
15173
16300
  */
15174
16301
  frameDigest(body) {
15175
- return createHash("sha256").update(body).digest("base64");
16302
+ return createHash$1("sha256").update(body).digest("base64");
15176
16303
  }
15177
16304
  /**
15178
16305
  * Send a data frame to a socket subscriber — unless it is exactly the one
@@ -15336,6 +16463,10 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15336
16463
  */
15337
16464
  handleChannelMessage(clientId, type, payload, authContext) {
15338
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
+ }
15339
16470
  if (type === "leave_channel") {
15340
16471
  this.leaveChannel(clientId, channel);
15341
16472
  return;
@@ -15354,6 +16485,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15354
16485
  }
15355
16486
  /** Perform an already-authorized channel frame. */
15356
16487
  dispatchChannelMessage(clientId, type, channel, payload) {
16488
+ if (!this.socketIsOpen(clientId)) return;
15357
16489
  switch (type) {
15358
16490
  case "join_channel":
15359
16491
  this.joinChannel(clientId, channel);
@@ -15427,7 +16559,10 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15427
16559
  }
15428
16560
  /** Join a broadcast channel */
15429
16561
  joinChannel(clientId, channel) {
15430
- if (!this.channels.has(channel)) this.channels.set(channel, /* @__PURE__ */ new Set());
16562
+ if (!this.channels.has(channel)) {
16563
+ this.channels.set(channel, /* @__PURE__ */ new Set());
16564
+ this.announceChannel(channel);
16565
+ }
15431
16566
  this.channels.get(channel).add(clientId);
15432
16567
  this.indexClientChannel(this.channelsByClient, clientId, channel);
15433
16568
  this.warnIfMemoryBusOnMultiplePods();
@@ -15608,6 +16743,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15608
16743
  return;
15609
16744
  }
15610
16745
  this.bus = bus;
16746
+ this.startBusGossip();
15611
16747
  try {
15612
16748
  const store = new ChannelPresenceStore(this.db, this.instanceId);
15613
16749
  if (!await store.ensureTables({ provision: options.provision }) && options.provision === false) throw new Error("rebase.channel_presence does not exist, and this backend changes nothing in its database. A provisioning run with the same channel bus configured creates it.");
@@ -15632,6 +16768,10 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15632
16768
  */
15633
16769
  publishBroadcast(clientId, channel, event, payload, seq) {
15634
16770
  if (this.bus.kind === "memory") return;
16771
+ if (seq === void 0 && !this.hasRemoteMembers(channel)) {
16772
+ this.busFramesSkipped++;
16773
+ return;
16774
+ }
15635
16775
  const frame = {
15636
16776
  kind: "broadcast",
15637
16777
  sid: this.instanceId,
@@ -15660,6 +16800,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15660
16800
  async publishFrame(frame) {
15661
16801
  try {
15662
16802
  await this.bus.publish(frame);
16803
+ this.busFramesPublished.set(frame.kind, (this.busFramesPublished.get(frame.kind) ?? 0) + 1);
15663
16804
  } catch (error) {
15664
16805
  logger.error("❌ [ChannelBus] Failed to publish frame — other instances did not receive it", {
15665
16806
  detail: `${frame.kind} on "${frame.channel}"`,
@@ -15692,6 +16833,11 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15692
16833
  * its own `sid`.
15693
16834
  */
15694
16835
  async handleBusFrame(frame) {
16836
+ if (frame.sid !== this.instanceId) this.notePeer(frame.sid);
16837
+ if (frame.kind === "channels") {
16838
+ if (frame.sid !== this.instanceId) this.readPeerChannels(frame);
16839
+ return;
16840
+ }
15695
16841
  const retained = (frame.kind === "broadcast" || frame.kind === "broadcast_ref") && frame.seq !== void 0;
15696
16842
  if (frame.sid === this.instanceId && !(retained && this.bus instanceof PostgresChannelBus)) return;
15697
16843
  if (!retained) return this.deliverBusFrame(frame);
@@ -15791,7 +16937,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15791
16937
  lastSeen: Date.now()
15792
16938
  });
15793
16939
  this.indexClientChannel(this.presenceByClient, clientId, channel);
15794
- this.presenceStoreOp(() => this.presenceStore.track(channel, clientId, state), "track");
16940
+ this.presenceStoreOp(clientId, (store) => store.track(channel, clientId, state), "track");
15795
16941
  if (changed) {
15796
16942
  this.deliverPresenceDiff(channel, { [clientId]: state }, {});
15797
16943
  this.publishPresenceDiff(channel, { [clientId]: state }, {});
@@ -15814,7 +16960,7 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15814
16960
  this.unindexClientChannel(this.presenceByClient, clientId, channel);
15815
16961
  this.deliverPresenceDiff(channel, {}, { [clientId]: entry.state });
15816
16962
  this.publishPresenceDiff(channel, {}, { [clientId]: entry.state });
15817
- if (!options?.skipStore) this.presenceStoreOp(() => this.presenceStore.remove(channel, clientId), "remove");
16963
+ if (!options?.skipStore) this.presenceStoreOp(clientId, (store) => store.remove(channel, clientId), "remove");
15818
16964
  }
15819
16965
  if (channelPresence.size === 0) this.presence.delete(channel);
15820
16966
  }
@@ -15868,6 +17014,10 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15868
17014
  /** Tell the other instances about a presence change. */
15869
17015
  publishPresenceDiff(channel, joins, leaves) {
15870
17016
  if (this.bus.kind === "memory") return;
17017
+ if (!this.hasRemoteMembers(channel)) {
17018
+ this.busFramesSkipped++;
17019
+ return;
17020
+ }
15871
17021
  this.publishFrame({
15872
17022
  kind: "presence_diff",
15873
17023
  sid: this.instanceId,
@@ -15876,14 +17026,137 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15876
17026
  leaves
15877
17027
  });
15878
17028
  }
15879
- /** Run a roster write when there is a roster, and never let it throw. */
15880
- async presenceStoreOp(op, label) {
15881
- if (!this.presenceStore) return;
15882
- try {
15883
- await op();
15884
- } catch (error) {
15885
- logger.warn(`⚠️ [Presence] Shared roster ${label} failed`, { error });
17029
+ static BUS_GOSSIP_INTERVAL_MS = 15e3;
17030
+ static BUS_PEER_TTL_MS = 45e3;
17031
+ static BUS_WARMUP_MS = 6e4;
17032
+ /** The frames' `channel`: a transport drops a frame without one. */
17033
+ static BUS_PEERS_CHANNEL = "__peers";
17034
+ /** Whether a frame on `channel` may have a reader on another instance. */
17035
+ hasRemoteMembers(channel) {
17036
+ if (!this.bus.publishesOnDemand) return true;
17037
+ const now = Date.now();
17038
+ if (now - this.busStartedAt < RealtimeService.BUS_WARMUP_MS) return true;
17039
+ for (const [sid, peer] of this.busPeers) {
17040
+ if (now - peer.heardAt > RealtimeService.BUS_PEER_TTL_MS) {
17041
+ this.busPeers.delete(sid);
17042
+ continue;
17043
+ }
17044
+ if (peer.channels === null || peer.channels.has(channel)) return true;
17045
+ }
17046
+ return false;
17047
+ }
17048
+ /** Tell the others about a channel's first member here. */
17049
+ announceChannel(channel) {
17050
+ if (!this.bus.publishesOnDemand) return;
17051
+ if (!this.busPeers.size && Date.now() - this.busStartedAt >= RealtimeService.BUS_WARMUP_MS) return;
17052
+ this.publishFrame({
17053
+ kind: "channels",
17054
+ sid: this.instanceId,
17055
+ channel: RealtimeService.BUS_PEERS_CHANNEL,
17056
+ add: [channel]
17057
+ });
17058
+ }
17059
+ /** Every channel served here, in as many parts as the transport needs. */
17060
+ announceChannels() {
17061
+ if (!this.bus.publishesOnDemand) return;
17062
+ const channel = RealtimeService.BUS_PEERS_CHANNEL;
17063
+ const limit = Math.min(this.bus.maxFrameBytes, 7e3) - 200;
17064
+ let list = [], bytes = 0;
17065
+ for (const name of this.channels.keys()) {
17066
+ const size = Buffer.byteLength(name, "utf8") + 3;
17067
+ if (bytes + size > limit) {
17068
+ this.publishFrame({
17069
+ kind: "channels",
17070
+ sid: this.instanceId,
17071
+ channel,
17072
+ list
17073
+ });
17074
+ list = [];
17075
+ bytes = 0;
17076
+ }
17077
+ list.push(name);
17078
+ bytes += size;
17079
+ }
17080
+ this.publishFrame({
17081
+ kind: "channels",
17082
+ sid: this.instanceId,
17083
+ channel,
17084
+ list,
17085
+ done: true
17086
+ });
17087
+ }
17088
+ /** Any frame from another instance: it's alive, and a new one needs to hear what's served here. */
17089
+ notePeer(sid) {
17090
+ const peer = this.busPeers.get(sid);
17091
+ if (peer) {
17092
+ peer.heardAt = Date.now();
17093
+ return;
15886
17094
  }
17095
+ this.busPeers.set(sid, {
17096
+ heardAt: Date.now(),
17097
+ channels: null
17098
+ });
17099
+ this.announceChannels();
17100
+ }
17101
+ readPeerChannels(frame) {
17102
+ const peer = this.busPeers.get(frame.sid);
17103
+ if (!peer) return;
17104
+ if (frame.hello) this.announceChannels();
17105
+ if (frame.add) {
17106
+ peer.channels ??= /* @__PURE__ */ new Set();
17107
+ for (const channel of frame.add) peer.channels.add(channel);
17108
+ }
17109
+ if (frame.list) {
17110
+ peer.incoming ??= /* @__PURE__ */ new Set();
17111
+ for (const channel of frame.list) peer.incoming.add(channel);
17112
+ if (frame.done) {
17113
+ peer.channels = peer.incoming;
17114
+ peer.incoming = void 0;
17115
+ }
17116
+ }
17117
+ }
17118
+ /** Start gossiping on a bus that publishes on demand. */
17119
+ startBusGossip() {
17120
+ if (!this.bus.publishesOnDemand) return;
17121
+ this.busStartedAt = Date.now();
17122
+ this.publishFrame({
17123
+ kind: "channels",
17124
+ sid: this.instanceId,
17125
+ channel: RealtimeService.BUS_PEERS_CHANNEL,
17126
+ hello: true
17127
+ });
17128
+ this.busGossipInterval = setInterval(() => {
17129
+ if (this.busPeers.size) this.announceChannels();
17130
+ }, RealtimeService.BUS_GOSSIP_INTERVAL_MS);
17131
+ unref(this.busGossipInterval);
17132
+ }
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;
15887
17160
  }
15888
17161
  /** Periodic cleanup for stale presences */
15889
17162
  ensurePresenceCleanup() {
@@ -15973,8 +17246,14 @@ var RealtimeService = class RealtimeService extends EventEmitter {
15973
17246
  clearInterval(this.presenceSweepInterval);
15974
17247
  this.presenceSweepInterval = void 0;
15975
17248
  }
17249
+ if (this.busGossipInterval) {
17250
+ clearInterval(this.busGossipInterval);
17251
+ this.busGossipInterval = void 0;
17252
+ }
15976
17253
  this.oversizedBroadcastWarned.clear();
15977
17254
  if (this.presenceStore) {
17255
+ await Promise.allSettled([...this.presenceStoreQueues.values()]);
17256
+ this.presenceStoreQueues.clear();
15978
17257
  try {
15979
17258
  await this.presenceStore.removeInstance();
15980
17259
  } catch (error) {
@@ -16006,6 +17285,15 @@ var RealtimeService = class RealtimeService extends EventEmitter {
16006
17285
  setScopeRunner(run) {
16007
17286
  this.runInScope = run;
16008
17287
  }
17288
+ /** What this instance is carrying — see `RealtimeProvider.counts`. */
17289
+ counts() {
17290
+ return {
17291
+ sockets: this.clients.size,
17292
+ channels: this.channels.size,
17293
+ busFramesPublished: Object.fromEntries(this.busFramesPublished),
17294
+ busFramesSkipped: this.busFramesSkipped
17295
+ };
17296
+ }
16009
17297
  /** The LISTEN connections this service depends on — see `RealtimeProvider.health`. */
16010
17298
  health() {
16011
17299
  const listeners = [];
@@ -16051,13 +17339,38 @@ var RealtimeService = class RealtimeService extends EventEmitter {
16051
17339
  * (bypass PgBouncer — LISTEN needs a session connection).
16052
17340
  */
16053
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) {
16054
17367
  if (this.cdcActive) {
16055
17368
  logger.warn("⚠️ [CDC] enableCdc called but CDC is already active. Ignoring.");
16056
17369
  return;
16057
17370
  }
16058
17371
  this.cdcTableMap = this.buildCdcTableMap();
16059
17372
  this.junctionLinkMap = buildJunctionLinkMap(this.registry);
16060
- this.cdcListener = new CdcListener(connectionString, (events) => this.handleCdcEvents(events), () => this.resyncSubscriptions(), this.listenerApplicationName);
17373
+ this.cdcListener = create();
16061
17374
  try {
16062
17375
  await this.cdcListener.start();
16063
17376
  } catch (err) {
@@ -16193,19 +17506,15 @@ var RealtimeService = class RealtimeService extends EventEmitter {
16193
17506
  const links = this.junctionLinkMap?.get(`${event.schema}.${event.table}`) ?? this.junctionLinkMap?.get(event.table);
16194
17507
  if (!links?.length) return false;
16195
17508
  if (event.op === "TRUNCATE") {
16196
- for (const link of links) {
16197
- const prefix = `${link.parentCollection.slug}/`;
16198
- const suffix = `/${link.relationKey}`;
16199
- const lists = [...this.subscribedPaths.keys()].filter((path) => path.startsWith(prefix) && path.endsWith(suffix) && path.split("/").length === 3);
16200
- for (const path of lists) await this.notifyUpdate(path, ANY_ROW, null, link.parentCollection.databaseId, false, "cdc");
16201
- }
17509
+ for (const link of links) await this.notifyEveryListOf(link, null);
16202
17510
  return true;
16203
17511
  }
16204
17512
  for (const link of links) {
16205
17513
  const sourceId = event.row?.[link.sourceColumn];
16206
17514
  const targetId = event.row?.[link.targetColumn];
16207
17515
  if (sourceId === void 0 || sourceId === null || targetId === void 0 || targetId === null) {
16208
- 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 });
16209
17518
  continue;
16210
17519
  }
16211
17520
  const path = `${link.parentCollection.slug}/${String(sourceId)}/${link.relationKey}`;
@@ -16215,6 +17524,17 @@ var RealtimeService = class RealtimeService extends EventEmitter {
16215
17524
  return true;
16216
17525
  }
16217
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
+ /**
16218
17538
  * Compute the canonical (possibly composite) id string from a captured row,
16219
17539
  * or {@link ANY_ROW} when it names none — a key too large to notify, a
16220
17540
  * table with no key.
@@ -16543,6 +17863,8 @@ function columnNames(table) {
16543
17863
  var quote$1 = (xs) => Array.from(xs).map((s) => `\`${s}\``).join(", ");
16544
17864
  /** `on.from` / `on.to` accept a single column or a composite tuple. */
16545
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;
16546
17868
  /**
16547
17869
  * A one-column link into a key of several columns, or `undefined`.
16548
17870
  *
@@ -16767,21 +18089,42 @@ function findRelationDefects(collections, registry, given) {
16767
18089
  break;
16768
18090
  }
16769
18091
  const stepColumns = columnNames(stepTable);
16770
- for (const column of asColumns(step.on.from)) if (!prevColumns.has(column)) defects.push({
18092
+ const alternatives = Array.isArray(step.on) ? step.on : [step.on];
18093
+ if (alternatives.length === 0) defects.push({
16771
18094
  ...at,
16772
- problem: `step ${i + 1} joins \`${prevName}.${column}\` → \`${step.table}\`, but \`${column}\` is not a column on \`${prevName}\``,
16773
- fix: `\`joinPath[${i}].on.from\` names columns on ${i === 0 ? "this collection's table" : `the previous step's table (\`${prevName}\`)`}: ${quote$1(prevColumns)}`
16774
- });
16775
- for (const column of asColumns(step.on.to)) if (!stepColumns.has(column)) defects.push({
16776
- ...at,
16777
- problem: `step ${i + 1} joins into \`${step.table}.${column}\`, but \`${column}\` is not a column on \`${step.table}\``,
16778
- fix: `\`joinPath[${i}].on.to\` names columns on \`${step.table}\`: ${quote$1(stepColumns)}`
16779
- });
16780
- if (asColumns(step.on.from).length !== asColumns(step.on.to).length) defects.push({
16781
- ...at,
16782
- problem: `step ${i + 1} compares ${asColumns(step.on.from).length} column(s) against ${asColumns(step.on.to).length}`,
16783
- 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`
16784
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
+ }
16785
18128
  prevName = step.table;
16786
18129
  prevColumns = stepColumns;
16787
18130
  }
@@ -17186,7 +18529,8 @@ var UserService = class {
17186
18529
  OR CAST(${table}.${sql.raw(idColumn)} AS text) ILIKE ${pattern})`);
17187
18530
  }
17188
18531
  const whereClause = conditions.length > 0 ? sql`WHERE ${sql.join(conditions, sql` AND `)}` : sql``;
17189
- 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}`;
17190
18534
  const total = (await this.db.execute(sql`
17191
18535
  SELECT count(*)::int as total FROM ${sql.raw(usersTableName)}
17192
18536
  ${whereClause}
@@ -17204,6 +18548,60 @@ var UserService = class {
17204
18548
  };
17205
18549
  }
17206
18550
  /**
18551
+ * @see UserRepository.listGuests — what a guest is, as SQL. The list and the
18552
+ * delete both read this, so nothing can be listed for deletion that the
18553
+ * delete would not call a guest. A column name falls back to its physical
18554
+ * name and never to no condition: a table without the column fails the
18555
+ * statement rather than widening it.
18556
+ */
18557
+ isGuest() {
18558
+ const users = sql.raw(this.getQualifiedUsersTableName());
18559
+ const column = (key, physical) => sql.identifier(getColumn(this.usersTable, key, physical)?.name ?? physical);
18560
+ const identities = sql.raw(qualifiedTableName(this.userIdentitiesTable));
18561
+ return sql`${users}.${column("isAnonymous", "is_anonymous")} = TRUE
18562
+ AND ${users}.${column("passwordHash", "password_hash")} IS NULL
18563
+ AND NOT EXISTS (SELECT 1 FROM ${identities} ui WHERE ui.uid = ${users}.${column("id", "id")})`;
18564
+ }
18565
+ /** @see UserRepository.listGuests */
18566
+ async listGuests(options) {
18567
+ const limit = options?.limit ?? 25;
18568
+ const offset = options?.offset ?? 0;
18569
+ const users = sql.raw(this.getQualifiedUsersTableName());
18570
+ const idColumn = sql.identifier(getColumn(this.usersTable, "id")?.name ?? "id");
18571
+ const createdAt = sql`${users}.${sql.identifier(getColumn(this.usersTable, "createdAt", "created_at")?.name ?? "created_at")}`;
18572
+ const lastSeenAt = sql`GREATEST(${createdAt}, (SELECT MAX(rt.created_at) FROM ${sql.raw(qualifiedTableName(this.refreshTokensTable))} rt WHERE rt.uid = ${users}.${idColumn}))`;
18573
+ const where = options?.lastSeenBefore ? sql`${this.isGuest()} AND ${lastSeenAt} < ${options.lastSeenBefore.toISOString()}::timestamptz` : this.isGuest();
18574
+ const counted = await this.db.execute(sql`SELECT count(*)::int AS total FROM ${users} WHERE ${where}`);
18575
+ return {
18576
+ guests: (await this.db.execute(sql`
18577
+ SELECT ${users}.${idColumn} AS id, ${createdAt} AS created_at, ${lastSeenAt} AS last_seen_at
18578
+ FROM ${users}
18579
+ WHERE ${where}
18580
+ ORDER BY last_seen_at ASC, id ASC
18581
+ LIMIT ${limit} OFFSET ${offset}
18582
+ `)).rows.map((row) => ({
18583
+ id: row.id,
18584
+ createdAt: new Date(row.created_at),
18585
+ lastSeenAt: new Date(row.last_seen_at)
18586
+ })),
18587
+ total: counted.rows[0].total
18588
+ };
18589
+ }
18590
+ /** @see UserRepository.deleteGuest */
18591
+ async deleteGuest(id) {
18592
+ const users = sql.raw(this.getQualifiedUsersTableName());
18593
+ const idColumn = sql.identifier(getColumn(this.usersTable, "id")?.name ?? "id");
18594
+ try {
18595
+ return (await this.withServerContext((db) => db.execute(sql`
18596
+ DELETE FROM ${users} WHERE ${users}.${idColumn} = ${id} AND ${this.isGuest()} RETURNING 1
18597
+ `))).rows.length > 0;
18598
+ } catch (error) {
18599
+ const code = extractPgError(error)?.code;
18600
+ if (code === "23503" || code === "23001") throw ApiError.conflict("A row of this project's own requires this guest, so it was not deleted.", "GUEST_REFERENCED");
18601
+ throw error;
18602
+ }
18603
+ }
18604
+ /**
17207
18605
  * Update user's password hash; `null` removes the password.
17208
18606
  */
17209
18607
  async updatePassword(id, passwordHash) {
@@ -17727,10 +19125,21 @@ var PasswordResetTokenService = class {
17727
19125
  };
17728
19126
  }
17729
19127
  /**
17730
- * 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`.
17731
19132
  */
17732
19133
  async markAsUsed(tokenHash) {
17733
- 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;
17734
19143
  }
17735
19144
  /**
17736
19145
  * Delete all tokens for a user
@@ -17792,6 +19201,10 @@ var MagicLinkTokenService = class {
17792
19201
  expiresAt: new Date(row.expires_at)
17793
19202
  };
17794
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
+ }
17795
19208
  /** Links and email codes past their expiry, used or not. */
17796
19209
  async deleteExpired() {
17797
19210
  const tableName = this.getQualifiedTableName();
@@ -17800,8 +19213,21 @@ var MagicLinkTokenService = class {
17800
19213
  WHERE expires_at < NOW()
17801
19214
  `);
17802
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
+ */
17803
19221
  async markAsUsed(tokenHash) {
17804
- 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;
17805
19231
  }
17806
19232
  };
17807
19233
  /**
@@ -17859,7 +19285,7 @@ var PostgresTokenRepository = class {
17859
19285
  return this.passwordResetTokenService.findValidByHash(tokenHash);
17860
19286
  }
17861
19287
  async markPasswordResetTokenUsed(tokenHash) {
17862
- await this.passwordResetTokenService.markAsUsed(tokenHash);
19288
+ return this.passwordResetTokenService.markAsUsed(tokenHash);
17863
19289
  }
17864
19290
  async deleteAllPasswordResetTokensForUser(uid) {
17865
19291
  await this.passwordResetTokenService.deleteAllForUser(uid);
@@ -17880,7 +19306,10 @@ var PostgresTokenRepository = class {
17880
19306
  return this.magicLinkTokenService.findValidByHash(tokenHash);
17881
19307
  }
17882
19308
  async markMagicLinkTokenUsed(tokenHash) {
17883
- await this.magicLinkTokenService.markAsUsed(tokenHash);
19309
+ return this.magicLinkTokenService.markAsUsed(tokenHash);
19310
+ }
19311
+ async deleteAllMagicLinkTokensForUser(uid) {
19312
+ await this.magicLinkTokenService.deleteAllForUser(uid);
17884
19313
  }
17885
19314
  };
17886
19315
  /**
@@ -17930,6 +19359,12 @@ var PostgresAuthRepository = class {
17930
19359
  async listUsersPaginated(options) {
17931
19360
  return this.userService.listUsersPaginated(options);
17932
19361
  }
19362
+ async listGuests(options) {
19363
+ return this.userService.listGuests(options);
19364
+ }
19365
+ async deleteGuest(id) {
19366
+ return this.userService.deleteGuest(id);
19367
+ }
17933
19368
  async firstUserIds(limit) {
17934
19369
  return this.userService.firstUserIds(limit);
17935
19370
  }
@@ -18015,7 +19450,7 @@ var PostgresAuthRepository = class {
18015
19450
  return this.tokenRepository.findValidPasswordResetToken(tokenHash);
18016
19451
  }
18017
19452
  async markPasswordResetTokenUsed(tokenHash) {
18018
- await this.tokenRepository.markPasswordResetTokenUsed(tokenHash);
19453
+ return this.tokenRepository.markPasswordResetTokenUsed(tokenHash);
18019
19454
  }
18020
19455
  async deleteAllPasswordResetTokensForUser(uid) {
18021
19456
  await this.tokenRepository.deleteAllPasswordResetTokensForUser(uid);
@@ -18031,7 +19466,10 @@ var PostgresAuthRepository = class {
18031
19466
  return this.tokenRepository.findValidMagicLinkToken(tokenHash);
18032
19467
  }
18033
19468
  async markMagicLinkTokenUsed(tokenHash) {
18034
- await this.tokenRepository.markMagicLinkTokenUsed(tokenHash);
19469
+ return this.tokenRepository.markMagicLinkTokenUsed(tokenHash);
19470
+ }
19471
+ async deleteAllMagicLinkTokensForUser(uid) {
19472
+ await this.tokenRepository.deleteAllMagicLinkTokensForUser(uid);
18035
19473
  }
18036
19474
  _mfaService = null;
18037
19475
  getMfaService() {
@@ -18437,8 +19875,8 @@ async function ensureHistoryTableExists(db, options = {}) {
18437
19875
  SET search_path = pg_catalog, pg_temp
18438
19876
  AS $fn$
18439
19877
  INSERT INTO rebase.entity_history
18440
- (table_name, entity_id, action, changed_fields, "values", previous_values, updated_by, format)
18441
- 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())
18442
19880
  $fn$
18443
19881
  ` : `
18444
19882
  CREATE OR REPLACE FUNCTION ${RECORD_HISTORY_FUNCTION}(
@@ -18450,8 +19888,8 @@ async function ensureHistoryTableExists(db, options = {}) {
18450
19888
  SET search_path = pg_catalog, pg_temp
18451
19889
  AS $fn$
18452
19890
  INSERT INTO rebase.entity_history
18453
- (table_name, entity_id, action, changed_fields, "values", previous_values, updated_by)
18454
- 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())
18455
19893
  $fn$
18456
19894
  `));
18457
19895
  await db.execute(sql.raw(`REVOKE ALL ON FUNCTION ${signature} FROM PUBLIC`));
@@ -18727,6 +20165,11 @@ var HistoryService = class {
18727
20165
  }
18728
20166
  /**
18729
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.
18730
20173
  */
18731
20174
  async fetchHistory(tableName, id, options = {}) {
18732
20175
  const limit = options.limit ?? 20;
@@ -18742,7 +20185,7 @@ var HistoryService = class {
18742
20185
  FROM rebase.entity_history
18743
20186
  WHERE table_name = ${tableName}
18744
20187
  AND entity_id = ${String(id)}
18745
- ORDER BY updated_at DESC
20188
+ ORDER BY updated_at DESC, id DESC
18746
20189
  LIMIT ${limit}
18747
20190
  OFFSET ${offset}
18748
20191
  `)]);
@@ -18806,7 +20249,7 @@ var HistoryService = class {
18806
20249
  SELECT id FROM rebase.entity_history
18807
20250
  WHERE table_name = ${tableName}
18808
20251
  AND entity_id = ${String(id)}
18809
- ORDER BY updated_at DESC
20252
+ ORDER BY updated_at DESC, id DESC
18810
20253
  OFFSET ${this.retention.maxEntries}
18811
20254
  )
18812
20255
  )
@@ -19321,6 +20764,7 @@ async function openPostgresDriver(pgConfig, config) {
19321
20764
  const rlsStatus = await readRlsStatus(rawClient, pgSchemaName);
19322
20765
  const unprotected = [...schema.tablesMap.keys()].filter((t) => !schema.joinTables.has(t) && !rlsStatus.get(t)?.rlsEnabled);
19323
20766
  const policyless = [...schema.tablesMap.keys()].filter((t) => !schema.joinTables.has(t) && rlsStatus.get(t)?.rlsEnabled && rlsStatus.get(t)?.policyCount === 0);
20767
+ const unprotectedJoinTables = [...schema.joinTables].filter((t) => !rlsStatus.get(t)?.rlsEnabled);
19324
20768
  if (unprotected.length > 0) if (unprotectedTables === "serve") logger.warn(`🔓 [rls] Serving ${unprotected.length} table(s) with row-level security DISABLED: ${unprotected.join(", ")}. Every authenticated request can read and write every row of these. This is baas.unprotectedTables: "serve".`);
19325
20769
  else {
19326
20770
  logger.warn(`🔒 [rls] Not serving ${unprotected.length} table(s) — row-level security is disabled, so they have no authorization model: ${unprotected.join(", ")}\n` + unprotected.map((t) => ` ALTER TABLE "${pgSchemaName}"."${t}" ENABLE ROW LEVEL SECURITY; -- then add a policy`).join("\n") + "\n To serve them without RLS anyway, pass baas: { unprotectedTables: \"serve\" } to the server entry: an option `rebase eject` gives you, which the managed runtime does not read from config/ or the environment.");
@@ -19330,6 +20774,10 @@ async function openPostgresDriver(pgConfig, config) {
19330
20774
  table
19331
20775
  }));
19332
20776
  }
20777
+ if (unprotectedJoinTables.length > 0) {
20778
+ logger.warn(`🔒 [rls] ${unprotectedJoinTables.length} join table(s) have row-level security disabled: ${unprotectedJoinTables.join(", ")}. The API does not serve a join table, with RLS or without, but ${REBASE_USER_ROLE$1} keeps its grants on these, so SQL your own code runs as a caller reaches every row.`);
20779
+ for (const t of unprotectedJoinTables) schema.tablesMap.delete(t);
20780
+ }
19333
20781
  if (policyless.length > 0) logger.warn(`🔒 [rls] ${policyless.length} table(s) have RLS enabled but no policies, so they will return no rows: ${policyless.join(", ")}`);
19334
20782
  introspectedCollections = buildCollectionsFromSchema(schema, pgSchemaName);
19335
20783
  ({tables: introspectedTables, relations: introspectedRelations} = buildBaasSchema(schema.tablesMap, pgSchemaName, introspectedCollections));
@@ -19502,22 +20950,57 @@ async function openPostgresDriver(pgConfig, config) {
19502
20950
  if (explicitCdc) logger.warn(`⚠️ [CDC] REALTIME_CDC=${cdcMode} but ${reason} — using app-level realtime.`);
19503
20951
  else logger.info(`ℹ️ [CDC] Using app-level realtime — ${reason}.`);
19504
20952
  } else if (wantsCdc && directUrl) {
19505
- 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).");
19506
- try {
19507
- const cdcTables = collectionsOnThisSource().map((c) => ({
19508
- schema: c.schema ?? "public",
19509
- table: getTableName$1(c),
19510
- identityColumns: collectionKeyColumns(c, registry).map((k) => k.columnName)
19511
- })).filter((t) => Boolean(t.table) && registry.hasTableForCollection(t.table));
19512
- const ownSlugs = new Set(collectionsOnThisSource().map((c) => c.slug));
19513
- for (const link of collectJunctionLinks(registry)) {
19514
- if (!ownSlugs.has(link.parentCollection.slug)) continue;
19515
- cdcTables.push({
19516
- schema: link.schema,
19517
- table: link.table,
19518
- identityColumns: [link.sourceColumn, link.targetColumn]
19519
- });
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
+ };
19520
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 {
19521
21004
  if (realtimeProvisions) {
19522
21005
  await provisionTriggerCdc(cdcRunSql, cdcTables);
19523
21006
  cdcFunctionCurrent = true;
@@ -19689,7 +21172,7 @@ function createPostgresBootstrapper(pgConfig) {
19689
21172
  const openIndexBuildSession = (driverResult) => async () => {
19690
21173
  const db = requireProvisioningDb(driverResult);
19691
21174
  const pool = "$client" in db ? db.$client : void 0;
19692
- const { isPgPool, poolIndexBuildSession } = await import("./deferred-index-build-CqaOv1Xx.js").then((n) => n.r);
21175
+ const { isPgPool, poolIndexBuildSession } = await import("./deferred-index-build-BcJ_oulu.js").then((n) => n.r);
19693
21176
  if (isPgPool(pool)) return poolIndexBuildSession(pool)();
19694
21177
  const queryable = provisioningQueryable(driverResult);
19695
21178
  return {
@@ -19715,7 +21198,7 @@ function createPostgresBootstrapper(pgConfig) {
19715
21198
  */
19716
21199
  const provisionRlsRuntime = async (driverResult) => {
19717
21200
  const db = requireProvisioningDb(driverResult);
19718
- const { RLS_BOOTSTRAP_STATEMENTS, rlsRuntimeIsCurrent } = await import("./migrate-baseline-B-2Ef4Mv.js").then((n) => n.w);
21201
+ const { RLS_BOOTSTRAP_STATEMENTS, rlsRuntimeIsCurrent } = await import("./migrate-baseline-DPawvckr.js").then((n) => n.E);
19719
21202
  const run = async (text) => sqlRows(await db.execute(sql.raw(text)));
19720
21203
  if (await rlsRuntimeIsCurrent(run).catch(() => false)) return;
19721
21204
  await db.transaction(async (tx) => {
@@ -19828,11 +21311,11 @@ function createPostgresBootstrapper(pgConfig) {
19828
21311
  unavailable: historyUnavailableReason(state, { requireTransaction: false })
19829
21312
  }) };
19830
21313
  }
19831
- const { BOOT_LOCK_TIMEOUT_MS } = await import("./boot-ddl-EsgqDkx0.js").then((n) => n.i);
21314
+ const { BOOT_LOCK_TIMEOUT_MS } = await import("./boot-ddl-CIGoIGcC.js").then((n) => n.i);
19832
21315
  const historyTable = await ensureHistoryTableExists(db, { lockTimeoutMs: BOOT_LOCK_TIMEOUT_MS });
19833
21316
  const historyIndexes = await ensureHistoryIndexes(db, { lockTimeoutMs: BOOT_LOCK_TIMEOUT_MS });
19834
21317
  if (historyIndexes.length > 0) {
19835
- const { buildDeferredIndexes, scheduleDeferredIndexBuild } = await import("./deferred-index-build-CqaOv1Xx.js").then((n) => n.r);
21318
+ const { buildDeferredIndexes, scheduleDeferredIndexBuild } = await import("./deferred-index-build-BcJ_oulu.js").then((n) => n.r);
19836
21319
  const indexOptions = {
19837
21320
  log: (message) => logger.info(`[history] ${message}`),
19838
21321
  lockTimeoutMs: BOOT_LOCK_TIMEOUT_MS
@@ -19888,9 +21371,9 @@ function createPostgresBootstrapper(pgConfig) {
19888
21371
  * proved it can bootstrap with.
19889
21372
  */
19890
21373
  async ensureCollectionSchema(collections, driverResult, log) {
19891
- const { ensureCollectionTables } = await import("./ensure-collection-tables-CFIRq8ni.js").then((n) => n.n);
21374
+ const { ensureCollectionTables } = await import("./ensure-collection-tables-CSbgkTBx.js").then((n) => n.n);
19892
21375
  const queryable = provisioningQueryable(driverResult);
19893
- const { BOOT_LOCK_TIMEOUT_MS } = await import("./boot-ddl-EsgqDkx0.js").then((n) => n.i);
21376
+ const { BOOT_LOCK_TIMEOUT_MS } = await import("./boot-ddl-CIGoIGcC.js").then((n) => n.i);
19894
21377
  const provisionOnly = ["1", "true"].includes((process.env.REBASE_PROVISION_ONLY ?? "").trim().toLowerCase());
19895
21378
  const plan = await ensureCollectionTables(queryable, collections, log, {
19896
21379
  lockTimeoutMs: BOOT_LOCK_TIMEOUT_MS,
@@ -19902,7 +21385,7 @@ function createPostgresBootstrapper(pgConfig) {
19902
21385
  pendingIndexBuild?.cancel();
19903
21386
  pendingIndexBuild = void 0;
19904
21387
  if (!provisionOnly && plan.deferredIndexes.length > 0) {
19905
- const { scheduleDeferredIndexBuild } = await import("./deferred-index-build-CqaOv1Xx.js").then((n) => n.r);
21388
+ const { scheduleDeferredIndexBuild } = await import("./deferred-index-build-BcJ_oulu.js").then((n) => n.r);
19906
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.`);
19907
21390
  pendingIndexBuild = scheduleDeferredIndexBuild(openIndexBuildSession(driverResult), plan.deferredIndexes, {
19908
21391
  log: (message) => logger.info(`[schema] ${message}`),
@@ -19942,7 +21425,7 @@ function createPostgresBootstrapper(pgConfig) {
19942
21425
  await provisionRlsRuntime(driverResult);
19943
21426
  },
19944
21427
  async ensureCollectionPolicies(collections, driverResult, log) {
19945
- const { ensureCollectionPolicies, describeRulesDropped } = await import("./ensure-collection-policies-DLWgabQ2.js");
21428
+ const { ensureCollectionPolicies, describeRulesDropped } = await import("./ensure-collection-policies-BNwSQfVx.js");
19946
21429
  const queryable = provisioningQueryable(driverResult);
19947
21430
  const { rows } = await queryable.query("SELECT to_regprocedure('rebase.roles()') IS NOT NULL AS present");
19948
21431
  if (!rows[0]?.present) await provisionRlsRuntime(driverResult);
@@ -19967,7 +21450,7 @@ function createPostgresBootstrapper(pgConfig) {
19967
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.`);
19968
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.`);
19969
21452
  try {
19970
- const { dropLegacyAuthSchema } = await import("./migrate-baseline-B-2Ef4Mv.js").then((n) => n.w);
21453
+ const { dropLegacyAuthSchema } = await import("./migrate-baseline-DPawvckr.js").then((n) => n.E);
19971
21454
  await dropLegacyAuthSchema(async (text) => (await queryable.query(text)).rows, {
19972
21455
  info: (m) => logger.info(m),
19973
21456
  warn: (m) => logger.warn(m)
@@ -20058,7 +21541,7 @@ function createPostgresBootstrapper(pgConfig) {
20058
21541
  return driverResult.internals.driver.admin;
20059
21542
  },
20060
21543
  async initializeWebsockets(server, realtimeService, driver, config, adapter, limits) {
20061
- const { createPostgresWebSocket, isSocketRealtimeService, socketRealtimeMissing } = await import("./websocket-CKYc3V4N.js").then((n) => n.c);
21544
+ const { createPostgresWebSocket, isSocketRealtimeService, socketRealtimeMissing } = await import("./websocket-D4wrzF3b.js").then((n) => n.c);
20062
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.`);
20063
21546
  const realtimeConfig = effectiveConfig().realtime;
20064
21547
  const maxSocketsPerUser = realtimeConfig?.maxSocketsPerUser !== void 0 ? parseMaxSocketsPerUser(realtimeConfig.maxSocketsPerUser, "realtime.maxSocketsPerUser") : void 0;
@@ -20180,6 +21663,7 @@ async function openRebase(options) {
20180
21663
  collections,
20181
21664
  async sql(query, sqlOptions) {
20182
21665
  if (!isSQLAdmin(admin)) throw new Error("This driver cannot run raw SQL.");
21666
+ warnOnTableDdl(query, (message) => logger.warn(message));
20183
21667
  if (sqlOptions?.database !== void 0) {
20184
21668
  const asked = resourceKeyOf(sqlOptions.database);
20185
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}" }).`);
@@ -20202,4 +21686,4 @@ async function openRebase(options) {
20202
21686
  //#endregion
20203
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 };
20204
21688
 
20205
- //# sourceMappingURL=open-rebase-C8jQ6s5a.js.map
21689
+ //# sourceMappingURL=open-rebase-D9n8idex.js.map