@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
@@ -216,6 +216,12 @@ export declare class BranchService {
216
216
  * so the *disagreements* between the two are visible — a row whose database
217
217
  * is gone and a database with no row are the two things prune exists to
218
218
  * find, and a join hides both.
219
+ *
220
+ * A database whose comment marks it as another main database's branch
221
+ * (see {@link BRANCH_HOME_COMMENT_PREFIX}) is left out, unless this
222
+ * registry names it: it is another project's branch on the same server,
223
+ * which has no row here because it is registered there, and dropping it as
224
+ * an orphan would destroy that project's work.
219
225
  */
220
226
  pruneCandidates(): Promise<{
221
227
  rows: BranchRow[];
@@ -2,6 +2,7 @@ import { SQL } from "drizzle-orm";
2
2
  import { AnyPgColumn, PgTable } from "drizzle-orm/pg-core";
3
3
  import { DrizzleClient } from "../interfaces.js";
4
4
  import { CollectionConfig, FilterValues, OrderByTuple, ResolvedRelation, ResolvedHasMany, ResolvedHasOne } from "@rebasepro/types";
5
+ import { type JoinStep } from "@rebasepro/types";
5
6
  import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry.js";
6
7
  import type { NestedPathHop } from "./nested-path.js";
7
8
  import { type ReadCallContextProvider } from "./read-scope.js";
@@ -23,13 +24,12 @@ export declare function applyDynamicJoin<T>(query: T, joinTable: PgTable, condit
23
24
  * `on.from`/`on.to` are a column or a tuple, and a tuple is how a step joins on
24
25
  * a composite key. Comparing the first pair alone joined each row to every row
25
26
  * sharing that column — every locale of a translation, not the one addressed.
27
+ *
28
+ * A step listing several conditions joins on any one of them, OR-ed: a row
29
+ * that meets two of them is still one row of an inner join, so a link held in
30
+ * either of two columns is read once either way.
26
31
  */
27
- export declare function joinStepCondition(currentTable: PgTable, joinTable: PgTable, step: {
28
- on: {
29
- from: string | string[];
30
- to: string | string[];
31
- };
32
- }): {
32
+ export declare function joinStepCondition(currentTable: PgTable, joinTable: PgTable, step: Pick<JoinStep, "on">): {
33
33
  condition: SQL;
34
34
  } | {
35
35
  missing: string;
@@ -18,6 +18,18 @@ export interface CdcChangeEvent {
18
18
  /** True when even the key was too large to notify, so `row` is empty. */
19
19
  truncated?: boolean;
20
20
  }
21
+ /**
22
+ * Where committed changes come from: this listener (capture triggers and
23
+ * NOTIFY) or `WalCdcListener` (logical replication). `RealtimeService` reads
24
+ * the same events from either.
25
+ */
26
+ export interface CdcSource {
27
+ /** Rejects when the first connection cannot be made, so the caller can fall back. */
28
+ start(): Promise<void>;
29
+ stop(): Promise<void>;
30
+ readonly connected: boolean;
31
+ status(): PgNotifyListenerStatus;
32
+ }
21
33
  /**
22
34
  * Parse a `rebase_cdc` NOTIFY payload into the changes it names, one per row.
23
35
  * Returns `null` for anything malformed so a single bad message can never
@@ -0,0 +1,92 @@
1
+ import type { CdcChangeEvent } from "./CdcListener.js";
2
+ import type { PgNotifyListenerStatus } from "../pg-notify-listener.js";
3
+ export interface WalCdcListenerOptions {
4
+ /** Direct Postgres connection string. Not a pooler: it carries the replication protocol. */
5
+ connectionString: string;
6
+ /** The publication to stream. Must be a plain identifier — it is interpolated. */
7
+ publication: string;
8
+ onEvents: (events: CdcChangeEvent[]) => void | Promise<void>;
9
+ /**
10
+ * Called once the stream is open again after a drop — never after the
11
+ * first connect. The new slot starts from the present: what committed
12
+ * while the connection was down is gone, so subscribers resync here.
13
+ */
14
+ onReconnect?: () => void;
15
+ /** The columns a table's rows are addressed by; `undefined` for its replica identity. */
16
+ identityColumns: (schema: string, table: string) => readonly string[] | undefined;
17
+ applicationName?: string;
18
+ reconnectDelayMs?: number;
19
+ /** How often the stream must prove it is alive. `0` turns the check off. */
20
+ heartbeatIntervalMs?: number;
21
+ /** How long the server may take to answer a heartbeat. */
22
+ heartbeatTimeoutMs?: number;
23
+ /** How often the position read so far is reported, so the server can recycle WAL. */
24
+ statusIntervalMs?: number;
25
+ /**
26
+ * How long creating the slot may take. It waits for every transaction
27
+ * already running to finish, so one long transaction holds it up.
28
+ */
29
+ slotTimeoutMs?: number;
30
+ }
31
+ /**
32
+ * A logical replication consumer that repairs itself, shaped like
33
+ * {@link PgNotifyListener}: the first connect is validated and rethrown, so a
34
+ * caller can fall back to the trigger listener; every later drop is repaired in
35
+ * the background.
36
+ */
37
+ export declare class WalCdcListener {
38
+ private readonly options;
39
+ private client?;
40
+ /** The connection being opened: its frames count before it is adopted, an abandoned one's never do. */
41
+ private connecting?;
42
+ private stream?;
43
+ private running;
44
+ private reconnectTimer?;
45
+ private heartbeatTimer?;
46
+ private heartbeatDeadline?;
47
+ private statusTimer?;
48
+ private downSince?;
49
+ private lastHeartbeatAt?;
50
+ private lastFrameAt;
51
+ private outageWarned;
52
+ /** The end of the last transaction handed on: what is reported as read. */
53
+ private confirmedLsn;
54
+ private inTransaction;
55
+ private pending;
56
+ private slotName?;
57
+ constructor(options: WalCdcListenerOptions);
58
+ get active(): boolean;
59
+ /** Streaming on a connection that answered its last heartbeat. */
60
+ get connected(): boolean;
61
+ status(): PgNotifyListenerStatus;
62
+ /**
63
+ * Open the stream. Rejects if the first connection, the slot or
64
+ * `START_REPLICATION` fails — `wal_level` is not `logical`, the role may
65
+ * not replicate, the publication is missing, no slot is free — leaving
66
+ * the listener stopped so the caller can use triggers instead.
67
+ */
68
+ start(): Promise<void>;
69
+ /** Close the stream; the server drops the temporary slot with it. Idempotent. */
70
+ stop(): Promise<void>;
71
+ private connect;
72
+ private onFrame;
73
+ /** Hand on what has been read. Every change in the stream is committed, so this needs no commit to wait for. */
74
+ private flush;
75
+ private abandon;
76
+ private startTimers;
77
+ /**
78
+ * Ask the server to answer at once. A half-open connection — packets
79
+ * stopped, no FIN, no RST — fires no error, so a stream that went quiet
80
+ * this way would look idle forever while every change went unseen.
81
+ */
82
+ private heartbeat;
83
+ private clearHeartbeatDeadline;
84
+ private stopTimers;
85
+ private scheduleReconnect;
86
+ private withTimeout;
87
+ /** For logs and tests: the slot of the current stream, and how far it has read. */
88
+ position(): {
89
+ slot?: string;
90
+ confirmedLsn: string;
91
+ };
92
+ }
@@ -0,0 +1,121 @@
1
+ /**
2
+ * The logical replication wire format, as far as change capture needs it.
3
+ *
4
+ * A `START_REPLICATION … LOGICAL` stream is a COPY in both directions. The
5
+ * server sends CopyData frames that are either a keepalive or a chunk of WAL;
6
+ * with the built-in `pgoutput` plugin each WAL chunk is one message — a
7
+ * transaction's begin and commit, a table's description, a row inserted,
8
+ * updated or deleted, a truncate. The client answers with standby status
9
+ * updates naming how far it has read, which is what lets the server recycle
10
+ * the WAL behind it.
11
+ *
12
+ * Everything here is a pure function of bytes, so it is tested against frames
13
+ * built by hand; the connection that carries them is `WalCdcListener`.
14
+ *
15
+ * Formats: https://www.postgresql.org/docs/current/protocol-replication.html
16
+ * and https://www.postgresql.org/docs/current/protocol-logicalrep-message-formats.html
17
+ * (protocol version 1, the only one this asks for).
18
+ */
19
+ import type { CdcChangeEvent } from "./CdcListener.js";
20
+ /** A column of a table as `pgoutput` describes it. */
21
+ export interface PgoutputColumn {
22
+ name: string;
23
+ /** Part of the replica identity: present in the old tuple of an update or delete. */
24
+ isKey: boolean;
25
+ typeOid: number;
26
+ }
27
+ export interface PgoutputRelation {
28
+ id: number;
29
+ schema: string;
30
+ table: string;
31
+ /** `d` default (the primary key), `n` nothing, `f` full, `i` index. */
32
+ replicaIdentity: string;
33
+ columns: PgoutputColumn[];
34
+ }
35
+ /**
36
+ * One column of a row. `undefined` is an unchanged TOASTed value: the server
37
+ * did not send it because the update did not touch it.
38
+ */
39
+ export type TupleValue = string | null | undefined;
40
+ export type PgoutputMessage = {
41
+ tag: "begin";
42
+ finalLsn: bigint;
43
+ xid: number;
44
+ } | {
45
+ tag: "commit";
46
+ commitLsn: bigint;
47
+ endLsn: bigint;
48
+ } | {
49
+ tag: "relation";
50
+ relation: PgoutputRelation;
51
+ } | {
52
+ tag: "insert";
53
+ relationId: number;
54
+ newTuple: TupleValue[];
55
+ } | {
56
+ tag: "update";
57
+ relationId: number;
58
+ oldTuple?: TupleValue[];
59
+ oldIsKeyOnly?: boolean;
60
+ newTuple: TupleValue[];
61
+ } | {
62
+ tag: "delete";
63
+ relationId: number;
64
+ oldTuple: TupleValue[];
65
+ oldIsKeyOnly: boolean;
66
+ } | {
67
+ tag: "truncate";
68
+ relationIds: number[];
69
+ }
70
+ /** Origin, type and logical messages: nothing capture acts on. */
71
+ | {
72
+ tag: "other";
73
+ type: string;
74
+ };
75
+ export type ReplicationFrame = {
76
+ kind: "xlog";
77
+ walStart: bigint;
78
+ walEnd: bigint;
79
+ data: Buffer;
80
+ } | {
81
+ kind: "keepalive";
82
+ walEnd: bigint;
83
+ replyRequested: boolean;
84
+ };
85
+ /** The payload of one CopyData frame from a replication stream. */
86
+ export declare function parseReplicationFrame(payload: Buffer): ReplicationFrame;
87
+ /** One `pgoutput` message: the data of an `xlog` frame. */
88
+ export declare function parsePgoutputMessage(data: Buffer): PgoutputMessage;
89
+ /**
90
+ * A standby status update: everything up to `lsn` is written, flushed and
91
+ * applied. Capture keeps nothing, so the three positions are the same — the
92
+ * end of the last transaction handed on.
93
+ *
94
+ * @param replyRequested Ask the server to answer at once with a keepalive,
95
+ * which is how the connection proves it is still alive.
96
+ */
97
+ export declare function encodeStandbyStatus(lsn: bigint, replyRequested: boolean, nowMs?: number): Buffer;
98
+ /** A frontend CopyData message around `payload`: `d`, a length that counts itself, the bytes. */
99
+ export declare function copyDataFrame(payload: Buffer): Buffer;
100
+ /** `16/B374D848`, the way Postgres writes an LSN. */
101
+ export declare function formatLsn(lsn: bigint): string;
102
+ /**
103
+ * Turns a `pgoutput` stream into the change events the trigger listener emits,
104
+ * so `RealtimeService` cannot tell which one it is reading.
105
+ *
106
+ * Holds the relation descriptions the stream sends ahead of a table's first
107
+ * row (and again after its schema changes). The stream only ever carries
108
+ * committed transactions, so a change can be handed on as soon as it arrives.
109
+ */
110
+ export declare class PgoutputChanges {
111
+ private readonly identityColumns;
112
+ private readonly relations;
113
+ /**
114
+ * @param identityColumns The columns a table's rows are addressed by, as
115
+ * provisioning attaches them; `undefined` for the replica identity.
116
+ */
117
+ constructor(identityColumns: (schema: string, table: string) => readonly string[] | undefined);
118
+ /** The changes one message describes — none for anything but a row change. */
119
+ apply(message: PgoutputMessage): CdcChangeEvent[];
120
+ private change;
121
+ }
@@ -0,0 +1,29 @@
1
+ import type { RawSqlRunner } from "../../security/rls-enforcement.js";
2
+ import type { CdcTableRef } from "./trigger-cdc.js";
3
+ /** The publication WAL capture streams. A plain identifier: it is interpolated into replication commands. */
4
+ export declare const CDC_PUBLICATION = "rebase_cdc";
5
+ /**
6
+ * Why this database cannot do WAL capture, or `undefined` when it can as far
7
+ * as the server's settings go. Whether the streaming role may replicate is
8
+ * only known when it tries — a host can grant it through role membership
9
+ * (`rds_replication`, `cloudsqlsuperuser`) that no attribute shows.
10
+ */
11
+ export declare function walCaptureBlocker(run: RawSqlRunner): Promise<string | undefined>;
12
+ export interface WalPublicationResult {
13
+ /** Tables in the publication, including ones an earlier boot added. */
14
+ published: CdcTableRef[];
15
+ skipped: Array<CdcTableRef & {
16
+ reason: string;
17
+ }>;
18
+ }
19
+ /**
20
+ * Create the publication if it is missing and add every table it lacks.
21
+ *
22
+ * Throws when the publication cannot be created or read at all (no
23
+ * privilege), which the caller treats as "WAL capture is unavailable". A
24
+ * single table that cannot be added is reported in `skipped` instead, so one
25
+ * of them cannot turn capture off for the rest.
26
+ */
27
+ export declare function provisionWalPublication(run: RawSqlRunner, tables: CdcTableRef[]): Promise<WalPublicationResult>;
28
+ /** One line per table the publication could not take: its writes reach realtime only through this server. */
29
+ export declare function reportUnpublishedTables(result: WalPublicationResult): void;
@@ -51,6 +51,7 @@ export declare class PostgresChannelBus implements ChannelBus {
51
51
  private readonly connectionString;
52
52
  readonly kind: "postgres";
53
53
  readonly maxFrameBytes = 7500;
54
+ readonly publishesOnDemand = true;
54
55
  private listener?;
55
56
  private readonly batchWindowMs;
56
57
  /**
@@ -143,6 +143,20 @@ export declare function requirePrimaryKeys(collection: CollectionConfig, registr
143
143
  * @param collectionPath named in the error.
144
144
  */
145
145
  export declare function primaryKeyColumns(table: PgTable, primaryKeys: PrimaryKeyInfo[], collectionPath: string): AnyPgColumn[];
146
+ /**
147
+ * The column a table holds under the property key `key`, or `undefined`.
148
+ *
149
+ * A Drizzle table is an object whose own properties are its columns — and
150
+ * also everything any object answers to. `key in table` is true of
151
+ * `constructor`, `toString` and `hasOwnProperty`, inherited from the
152
+ * prototype, and of `enableRLS`, a method Drizzle sets on the table itself;
153
+ * reading one of those as a column put a function into the statement as a
154
+ * bound parameter. A filter on `constructor` then compiled to a comparison
155
+ * between two parameters — "a filter that names nothing" without the 400 —
156
+ * and `__proto__` crashed the builder into a 500. Only an own, object-valued
157
+ * property is a column.
158
+ */
159
+ export declare function columnAtKey(table: PgTable<any>, key: string): AnyPgColumn | undefined;
146
160
  /**
147
161
  * The column a `joinPath` step names, on a table that may key it either way.
148
162
  *
@@ -1,6 +1,6 @@
1
1
  import { type SQL } from "drizzle-orm";
2
2
  import { AnyPgColumn, PgTable } from "drizzle-orm/pg-core";
3
- import type { Properties, ResolvedVia } from "@rebasepro/types";
3
+ import { type Properties, type ResolvedVia } from "@rebasepro/types";
4
4
  import { DrizzleClient } from "../interfaces.js";
5
5
  import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry.js";
6
6
  /**
@@ -27,6 +27,14 @@ export interface JunctionBinding {
27
27
  parentColumn: AnyPgColumn;
28
28
  /** The junction column holding the id of the row on the far side. */
29
29
  targetColumn: AnyPgColumn;
30
+ /**
31
+ * The keys the junction's drizzle table holds those two columns under —
32
+ * what an insert's values are keyed by. A table may key a column by
33
+ * another name than the column's own (`postId: integer("post_id")`), and
34
+ * drizzle leaves a value under any other key out of the statement.
35
+ */
36
+ parentKey: string;
37
+ targetKey: string;
30
38
  /** `<collection>.<relation>`, for error messages. */
31
39
  label: string;
32
40
  /**
@@ -48,7 +48,8 @@ export declare function resolveNestedPath(path: string, registry: PostgresCollec
48
48
  export declare function isJunctionBackedRelation(relation: ResolvedRelation): boolean;
49
49
  /**
50
50
  * Reject a create or update whose body names a relation Rebase does not write
51
- * — a multi-hop `via` (`writable: false`) — before anything is read or written.
51
+ * — a multi-hop `via`, or one whose step joins on either of several columns
52
+ * (`writable: false`) — before anything is read or written.
52
53
  *
53
54
  * A single-hop `via` is written through this door as it shipped (to-one: the
54
55
  * target's key; to-many: one junction's rows). A longer chain used to get as
@@ -4,8 +4,9 @@ import { DataDriver, IncludeSpec, WebSocketMessage, LogicalCondition, OrderByTup
4
4
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
5
5
  import { RealtimeProvider, CollectionSubscriptionConfig, SingleSubscriptionConfig } from "../interfaces.js";
6
6
  import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry.js";
7
+ import type { CdcTableRef } from "./cdc/trigger-cdc.js";
7
8
  import { ChannelBus } from "./channel-bus/index.js";
8
- import type { ChannelRetentionRule, RealtimeListenerHealth } from "@rebasepro/types";
9
+ import type { ChannelRetentionRule, RealtimeCounts, RealtimeListenerHealth } from "@rebasepro/types";
9
10
  /**
10
11
  * Auth context stored per-subscription so real-time refetches respect RLS.
11
12
  * Mirrors the session variables set by PostgresBackendDriver.withAuth().
@@ -286,6 +287,17 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
286
287
  * per broadcast for company. See `channel-bus/ChannelBus.ts`.
287
288
  */
288
289
  private bus;
290
+ /**
291
+ * The other instances on an on-demand bus, and the channels each serves
292
+ * (`null`: it hasn't said, so assume all of them). See {@link hasRemoteMembers}.
293
+ */
294
+ private busPeers;
295
+ /** Frames the bus took, by kind. See {@link counts}. */
296
+ private busFramesPublished;
297
+ /** Frames not sent because no other instance had members. See {@link counts}. */
298
+ private busFramesSkipped;
299
+ private busStartedAt;
300
+ private busGossipInterval?;
289
301
  /**
290
302
  * The shared presence roster, present only when a real bus is active.
291
303
  *
@@ -302,6 +314,8 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
302
314
  * never track presence, and each of their closes was a `DELETE` too.
303
315
  */
304
316
  private presenceTrackers;
317
+ /** Each client's roster writes still running, chained in order — see {@link presenceStoreOp}. */
318
+ private presenceStoreQueues;
305
319
  /**
306
320
  * Channels whose oversized ephemeral broadcasts have already been reported,
307
321
  * so a hot channel logs the problem once rather than once per message.
@@ -451,7 +465,7 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
451
465
  * refetches once per this interval, which is the whole cost.
452
466
  */
453
467
  private static readonly REFETCH_MAX_WAIT_MS;
454
- /** Dedicated LISTEN client for DB-level change events (undefined unless CDC is enabled). */
468
+ /** Where DB-level change events come from — capture triggers or the WAL (undefined unless CDC is enabled). */
455
469
  private cdcListener?;
456
470
  /** Whether database-level CDC is the active cross-instance change source. */
457
471
  private cdcActive;
@@ -645,6 +659,19 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
645
659
  private indexClientChannel;
646
660
  private unindexClientChannel;
647
661
  private handleMessage;
662
+ /**
663
+ * Whether `clientId` is a socket this service still holds.
664
+ *
665
+ * A frame reaches this service after the socket door has awaited — the
666
+ * identity recheck, the data rate limiter — and a channel frame may await
667
+ * again for its authorizer. A client that closed inside one of those awaits
668
+ * was already removed by `removeClient`, which runs on `close`, so whatever
669
+ * its frame registers now is never cleaned up: a subscription that keeps
670
+ * its group and its refetches for the life of the process, or a membership
671
+ * that keeps a channel alive with nobody in it. Checked synchronously
672
+ * before the registration, so nothing can close the socket in between.
673
+ */
674
+ private socketIsOpen;
648
675
  /**
649
676
  * Refuse a new subscription past the socket's ceiling, and say so.
650
677
  *
@@ -1094,7 +1121,33 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
1094
1121
  private deliverPresenceDiff;
1095
1122
  /** Tell the other instances about a presence change. */
1096
1123
  private publishPresenceDiff;
1097
- /** Run a roster write when there is a roster, and never let it throw. */
1124
+ private static readonly BUS_GOSSIP_INTERVAL_MS;
1125
+ private static readonly BUS_PEER_TTL_MS;
1126
+ private static readonly BUS_WARMUP_MS;
1127
+ /** The frames' `channel`: a transport drops a frame without one. */
1128
+ private static readonly BUS_PEERS_CHANNEL;
1129
+ /** Whether a frame on `channel` may have a reader on another instance. */
1130
+ private hasRemoteMembers;
1131
+ /** Tell the others about a channel's first member here. */
1132
+ private announceChannel;
1133
+ /** Every channel served here, in as many parts as the transport needs. */
1134
+ private announceChannels;
1135
+ /** Any frame from another instance: it's alive, and a new one needs to hear what's served here. */
1136
+ private notePeer;
1137
+ private readPeerChannels;
1138
+ /** Start gossiping on a bus that publishes on demand. */
1139
+ private startBusGossip;
1140
+ /**
1141
+ * Run one of `clientId`'s roster writes when there is a roster, after the
1142
+ * ones it made before, and never let it throw.
1143
+ *
1144
+ * In order, because each is its own statement on the pool: a removal sent
1145
+ * while the client's upsert was still on its way could run first, and the
1146
+ * upsert then put the row back. That row is this instance's, so the
1147
+ * stale-roster sweep leaves it alone, and the in-process sweep no longer
1148
+ * knows the client — the departed client stayed in every roster for as
1149
+ * long as the instance ran.
1150
+ */
1098
1151
  private presenceStoreOp;
1099
1152
  /** Periodic cleanup for stale presences */
1100
1153
  private ensurePresenceCleanup;
@@ -1133,6 +1186,8 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
1133
1186
  isCdcActive(): boolean;
1134
1187
  /** See `RealtimeProvider.setScopeRunner`. */
1135
1188
  setScopeRunner(run: <T>(fn: () => T) => T): void;
1189
+ /** What this instance is carrying — see `RealtimeProvider.counts`. */
1190
+ counts(): RealtimeCounts;
1136
1191
  /** The LISTEN connections this service depends on — see `RealtimeProvider.health`. */
1137
1192
  health(): RealtimeListenerHealth[];
1138
1193
  /**
@@ -1153,6 +1208,19 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
1153
1208
  * (bypass PgBouncer — LISTEN needs a session connection).
1154
1209
  */
1155
1210
  enableCdc(connectionString: string): Promise<void>;
1211
+ /**
1212
+ * Enable change capture read from the write-ahead log instead of NOTIFY —
1213
+ * `REALTIME_CDC=wal`. Same events, same pipeline as {@link enableCdc};
1214
+ * writers pay for no trigger and no NOTIFY. Rejects when the stream cannot
1215
+ * be opened (see {@link WalCdcListener.start}), leaving CDC inactive so the
1216
+ * caller can enable the trigger listener instead.
1217
+ *
1218
+ * @param tables What provisioning captures, with the columns each table's
1219
+ * rows are addressed by. A table it does not name is addressed by
1220
+ * its replica identity.
1221
+ */
1222
+ enableWalCdc(connectionString: string, tables: readonly CdcTableRef[]): Promise<void>;
1223
+ private activateCdc;
1156
1224
  /**
1157
1225
  * Refetch every live subscription, as a change to its path would.
1158
1226
  *
@@ -1216,6 +1284,12 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
1216
1284
  * Returns whether the table was recognised as a junction.
1217
1285
  */
1218
1286
  private handleJunctionCdcEvent;
1287
+ /**
1288
+ * Notify every subscribed child list of one many-to-many relation,
1289
+ * whichever parent it belongs to — for a junction change that names no
1290
+ * single list.
1291
+ */
1292
+ private notifyEveryListOf;
1219
1293
  /**
1220
1294
  * Compute the canonical (possibly composite) id string from a captured row,
1221
1295
  * or {@link ANY_ROW} when it names none — a key too large to notify, a
@@ -519,6 +519,17 @@ export declare class DrizzleConditionBuilder {
519
519
  * Build conditions for join path relations
520
520
  */
521
521
  private static buildJoinPathConditions;
522
+ /**
523
+ * One step of a join path walked target → parent: the table it brings in
524
+ * and the condition it joins on.
525
+ *
526
+ * A step listing alternatives joins on any of them, OR-ed. Each alternative
527
+ * is built the way a plain step is, and they must all bring in the same
528
+ * table — they are several ways to match one pair of tables, not several
529
+ * paths. The junction fallback is only tried for a plain step: guessing a
530
+ * junction for one alternative of several is guessing twice.
531
+ */
532
+ private static buildReverseJoinStep;
522
533
  /**
523
534
  * Build a single join condition between tables
524
535
  */
@@ -1,8 +1,8 @@
1
1
  import { createRequire as __createRequire } from "module";
2
2
  __createRequire(import.meta.url);
3
3
  import { i as __exportAll } from "./cli-prompt-DXG0yk-h.js";
4
- import { A as budgetSendBuffers, M as terminateIfBacklogged, N as assertReadRequestReadable, ct as SqlResultTooLargeError, it as effectiveSqlRole, j as reapSilentSockets, lt as isNestedPath, ot as sqlRoleRanAs, st as SQL_RESULT_BYTE_BUDGET, ut as resolveNestedPath } from "./open-rebase-C8jQ6s5a.js";
5
- import { r as BranchingUnsupportedError } from "./BranchService-CWBnDhcv.js";
4
+ import { A as budgetSendBuffers, M as terminateIfBacklogged, N as assertReadRequestReadable, ct as SqlResultTooLargeError, it as effectiveSqlRole, j as reapSilentSockets, lt as isNestedPath, ot as sqlRoleRanAs, st as SQL_RESULT_BYTE_BUDGET, ut as resolveNestedPath } from "./open-rebase-D9n8idex.js";
5
+ import { r as BranchingUnsupportedError } from "./BranchService-BPssiD8_.js";
6
6
  import { ApiError, DATABASE_UNAVAILABLE_MESSAGE, RUNTIME_DEFAULT_MAX_BODY_SIZE, assertFieldOpsValid, assertNestedWriteAllowed, assertNoFieldOpsOnCreate, assertNoUserUpsert, assertUserCreationBodyValid, assertWriteRequestValid, createUserThroughAuthCollection, decideImpersonation, declaredErrorAnswer, extractUserFromToken, getAccessModel, isDatabaseUnreachable, logger, resolveConflictTarget, resolveRequireAuth, safeCompare } from "@rebasepro/server";
7
7
  import { ANONYMOUS_USER_ID, ListLimitError, hasAdminRole, isSQLAdmin, isSchemaAdmin, resolveClientListLimit, scopeGrants, scopesForRoles } from "@rebasepro/types";
8
8
  import { UnknownFilterOperatorError, deserializeFilter, redactSqlLiterals } from "@rebasepro/common";
@@ -226,8 +226,28 @@ var IDENTITY_SWEEP_CONCURRENCY = 4;
226
226
  * high-rate traffic (see `WS_CHANNEL_RATE_LIMIT`): a database read for every
227
227
  * cursor position would make realtime presence the busiest query a backend
228
228
  * runs, for a window no wider than this.
229
+ *
230
+ * At one second a busy backend's channel traffic was its busiest query all the
231
+ * same: a socket that answers a ping every few seconds and sends a state now
232
+ * and then is checked on nearly every frame, so 800 game sockets were several
233
+ * hundred auth lookups a second on the shared pool, and the lookups were what
234
+ * starved it (a load test of 100 rooms). Five seconds keeps a revocation's reach
235
+ * well inside the idle sweep's 30 s while taking that traffic down fivefold.
229
236
  */
230
- var CHANNEL_RECHECK_MIN_MS = 1e3;
237
+ var CHANNEL_RECHECK_MIN_MS = 5e3;
238
+ /**
239
+ * How long a channel frame is still honoured on the strength of a successful
240
+ * check, in ms, while the auth store cannot answer.
241
+ *
242
+ * A lookup that fails because the database is busy (the pool's acquire
243
+ * timeout, a statement timeout) says nothing about the identity, yet dropping
244
+ * the frame lost a join or a broadcast without the client knowing to resend it,
245
+ * and every following frame asked again, adding to the load that made it fail.
246
+ * Within this window of the last confirmed check, a channel frame goes through
247
+ * and the next check waits {@link CHANNEL_RECHECK_MIN_MS}; past it, the frame
248
+ * is refused as before. Data frames get no grace: they read the database anyway.
249
+ */
250
+ var CHANNEL_STALE_GRACE_MS = 6e4;
231
251
  /**
232
252
  * The close code for a socket whose identity ended: signed out, revoked,
233
253
  * demoted out of existence, deleted or expired. In the 4000–4999 range the
@@ -746,13 +766,14 @@ function createPostgresWebSocket(server, realtimeService, driver, authConfig, au
746
766
  const session = clientSessions.get(clientId);
747
767
  const credential = session?.credential;
748
768
  if (!session?.user || !credential) return "live";
769
+ const impersonate = session.impersonate;
749
770
  let outcome;
750
771
  try {
751
- outcome = await resolve(credential, session.impersonate);
772
+ outcome = await resolve(credential, impersonate);
752
773
  } catch (error) {
753
774
  return { unavailable: storeUnavailableAnswer(error) };
754
775
  }
755
- if (session.credential !== credential || !session.user) return session.user ? "live" : "ended";
776
+ if (session.credential !== credential || session.impersonate !== impersonate || !session.user) return session.user ? "live" : "ended";
756
777
  session.checkedAt = Date.now();
757
778
  if (!("user" in outcome)) {
758
779
  if (outcome.code) endSession(clientId, outcome.code, outcome.refused);
@@ -1045,10 +1066,16 @@ function createPostgresWebSocket(server, realtimeService, driver, authConfig, au
1045
1066
  }
1046
1067
  {
1047
1068
  const session = clientSessions.get(clientId);
1048
- if (session?.user && session.credential && Date.now() - session.checkedAt >= (CHANNEL_MESSAGE_TYPES.has(type) ? CHANNEL_RECHECK_MIN_MS : 0)) {
1069
+ const channelFrame = CHANNEL_MESSAGE_TYPES.has(type);
1070
+ const now = Date.now();
1071
+ if (session?.user && session.credential && now - session.checkedAt >= (channelFrame ? CHANNEL_RECHECK_MIN_MS : 0) && !(channelFrame && session.recheckAfter !== void 0 && now < session.recheckAfter)) {
1049
1072
  const outcome = await recheckIdentityForFrame(clientId);
1050
1073
  if (outcome === "ended") return;
1051
- if (outcome !== "live") {
1074
+ if (outcome === "live") session.recheckAfter = void 0;
1075
+ else if (channelFrame && Date.now() - session.checkedAt < CHANNEL_STALE_GRACE_MS) {
1076
+ session.recheckAfter = Date.now() + CHANNEL_RECHECK_MIN_MS;
1077
+ wsDebug(`[WebSocket Server] Auth store unavailable (${outcome.unavailable.code}); honouring ${clientId}'s last check for a channel frame`);
1078
+ } else {
1052
1079
  sendError("ERROR", outcome.unavailable.code, outcome.unavailable.message);
1053
1080
  return;
1054
1081
  }
@@ -1671,4 +1698,4 @@ function createPostgresWebSocket(server, realtimeService, driver, authConfig, au
1671
1698
  //#endregion
1672
1699
  export { createPostgresWebSocket as a, websocket_exports as c, TOO_MANY_SOCKETS_CLOSE_CODE as i, PUBLIC_TYPES as n, isSocketRealtimeService as o, SESSION_ENDED_CLOSE_CODE as r, socketRealtimeMissing as s, ADMIN_PLANE_TYPES as t };
1673
1700
 
1674
- //# sourceMappingURL=websocket-CKYc3V4N.js.map
1701
+ //# sourceMappingURL=websocket-D4wrzF3b.js.map