@rebasepro/server-postgres 0.9.1-canary.742f831 → 0.9.1-canary.78ab3dc

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.
@@ -21,4 +21,4 @@
21
21
  * `DROP SCHEMA "rebase" CASCADE` on the next diff. The `auth` schema is safe
22
22
  * because the generated schema.sql declares it.
23
23
  */
24
- export declare const AUTH_BOOTSTRAP_SQL = "-- Auth schema + RLS helper functions (required by the policies below)\nCREATE SCHEMA IF NOT EXISTS auth;\n\nCREATE OR REPLACE FUNCTION auth.uid() RETURNS text AS $$\n SELECT NULLIF(current_setting('app.user_id', true), '');\n$$ LANGUAGE sql STABLE;\n\nCREATE OR REPLACE FUNCTION auth.jwt() RETURNS jsonb AS $$\n SELECT COALESCE(\n NULLIF(current_setting('app.jwt', true), ''),\n '{}'\n )::jsonb;\n$$ LANGUAGE sql STABLE;\n\nCREATE OR REPLACE FUNCTION auth.roles() RETURNS text AS $$\n SELECT COALESCE(NULLIF(current_setting('app.user_roles', true), ''), '');\n$$ LANGUAGE sql STABLE;\n";
24
+ export declare const AUTH_BOOTSTRAP_SQL = "-- Auth schema + RLS helper functions (required by the policies below)\nCREATE SCHEMA IF NOT EXISTS auth;\n\n-- Falls back to the pre-rename `app.user_id` so a database that has taken the\n-- new schema but is still served by an older backend keeps resolving the\n-- principal. Drop the COALESCE once no such deployment remains.\nCREATE OR REPLACE FUNCTION auth.uid() RETURNS text AS $$\n SELECT COALESCE(\n NULLIF(current_setting('app.uid', true), ''),\n NULLIF(current_setting('app.user_id', true), '')\n );\n$$ LANGUAGE sql STABLE;\n\nCREATE OR REPLACE FUNCTION auth.jwt() RETURNS jsonb AS $$\n SELECT COALESCE(\n NULLIF(current_setting('app.jwt', true), ''),\n '{}'\n )::jsonb;\n$$ LANGUAGE sql STABLE;\n\nCREATE OR REPLACE FUNCTION auth.roles() RETURNS text AS $$\n SELECT COALESCE(NULLIF(current_setting('app.user_roles', true), ''), '');\n$$ LANGUAGE sql STABLE;\n";
@@ -290,8 +290,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
290
290
  identity: undefined;
291
291
  generated: undefined;
292
292
  }, {}, {}>;
293
- userId: import("drizzle-orm/pg-core").PgColumn<{
294
- name: "user_id";
293
+ uid: import("drizzle-orm/pg-core").PgColumn<{
294
+ name: "uid";
295
295
  tableName: "refresh_tokens";
296
296
  dataType: "string";
297
297
  columnType: "PgUUID";
@@ -422,8 +422,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
422
422
  identity: undefined;
423
423
  generated: undefined;
424
424
  }, {}, {}>;
425
- userId: import("drizzle-orm/pg-core").PgColumn<{
426
- name: "user_id";
425
+ uid: import("drizzle-orm/pg-core").PgColumn<{
426
+ name: "uid";
427
427
  tableName: "password_reset_tokens";
428
428
  dataType: "string";
429
429
  columnType: "PgUUID";
@@ -593,8 +593,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
593
593
  identity: undefined;
594
594
  generated: undefined;
595
595
  }, {}, {}>;
596
- userId: import("drizzle-orm/pg-core").PgColumn<{
597
- name: "user_id";
596
+ uid: import("drizzle-orm/pg-core").PgColumn<{
597
+ name: "uid";
598
598
  tableName: "user_identities";
599
599
  dataType: "string";
600
600
  columnType: "PgUUID";
@@ -723,8 +723,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
723
723
  identity: undefined;
724
724
  generated: undefined;
725
725
  }, {}, {}>;
726
- userId: import("drizzle-orm/pg-core").PgColumn<{
727
- name: "user_id";
726
+ uid: import("drizzle-orm/pg-core").PgColumn<{
727
+ name: "uid";
728
728
  tableName: "mfa_factors";
729
729
  dataType: "string";
730
730
  columnType: "PgUUID";
@@ -983,8 +983,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
983
983
  identity: undefined;
984
984
  generated: undefined;
985
985
  }, {}, {}>;
986
- userId: import("drizzle-orm/pg-core").PgColumn<{
987
- name: "user_id";
986
+ uid: import("drizzle-orm/pg-core").PgColumn<{
987
+ name: "uid";
988
988
  tableName: "recovery_codes";
989
989
  dataType: "string";
990
990
  columnType: "PgUUID";
@@ -1077,8 +1077,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
1077
1077
  identity: undefined;
1078
1078
  generated: undefined;
1079
1079
  }, {}, {}>;
1080
- userId: import("drizzle-orm/pg-core").PgColumn<{
1081
- name: "user_id";
1080
+ uid: import("drizzle-orm/pg-core").PgColumn<{
1081
+ name: "uid";
1082
1082
  tableName: "magic_link_tokens";
1083
1083
  dataType: "string";
1084
1084
  columnType: "PgUUID";
@@ -1456,8 +1456,8 @@ export declare const refreshTokens: import("drizzle-orm/pg-core").PgTableWithCol
1456
1456
  identity: undefined;
1457
1457
  generated: undefined;
1458
1458
  }, {}, {}>;
1459
- userId: import("drizzle-orm/pg-core").PgColumn<{
1460
- name: "user_id";
1459
+ uid: import("drizzle-orm/pg-core").PgColumn<{
1460
+ name: "uid";
1461
1461
  tableName: "refresh_tokens";
1462
1462
  dataType: "string";
1463
1463
  columnType: "PgUUID";
@@ -1588,8 +1588,8 @@ export declare const passwordResetTokens: import("drizzle-orm/pg-core").PgTableW
1588
1588
  identity: undefined;
1589
1589
  generated: undefined;
1590
1590
  }, {}, {}>;
1591
- userId: import("drizzle-orm/pg-core").PgColumn<{
1592
- name: "user_id";
1591
+ uid: import("drizzle-orm/pg-core").PgColumn<{
1592
+ name: "uid";
1593
1593
  tableName: "password_reset_tokens";
1594
1594
  dataType: "string";
1595
1595
  columnType: "PgUUID";
@@ -1759,8 +1759,8 @@ export declare const userIdentities: import("drizzle-orm/pg-core").PgTableWithCo
1759
1759
  identity: undefined;
1760
1760
  generated: undefined;
1761
1761
  }, {}, {}>;
1762
- userId: import("drizzle-orm/pg-core").PgColumn<{
1763
- name: "user_id";
1762
+ uid: import("drizzle-orm/pg-core").PgColumn<{
1763
+ name: "uid";
1764
1764
  tableName: "user_identities";
1765
1765
  dataType: "string";
1766
1766
  columnType: "PgUUID";
@@ -1889,8 +1889,8 @@ export declare const mfaFactors: import("drizzle-orm/pg-core").PgTableWithColumn
1889
1889
  identity: undefined;
1890
1890
  generated: undefined;
1891
1891
  }, {}, {}>;
1892
- userId: import("drizzle-orm/pg-core").PgColumn<{
1893
- name: "user_id";
1892
+ uid: import("drizzle-orm/pg-core").PgColumn<{
1893
+ name: "uid";
1894
1894
  tableName: "mfa_factors";
1895
1895
  dataType: "string";
1896
1896
  columnType: "PgUUID";
@@ -2149,8 +2149,8 @@ export declare const recoveryCodes: import("drizzle-orm/pg-core").PgTableWithCol
2149
2149
  identity: undefined;
2150
2150
  generated: undefined;
2151
2151
  }, {}, {}>;
2152
- userId: import("drizzle-orm/pg-core").PgColumn<{
2153
- name: "user_id";
2152
+ uid: import("drizzle-orm/pg-core").PgColumn<{
2153
+ name: "uid";
2154
2154
  tableName: "recovery_codes";
2155
2155
  dataType: "string";
2156
2156
  columnType: "PgUUID";
@@ -2243,8 +2243,8 @@ export declare const magicLinkTokens: import("drizzle-orm/pg-core").PgTableWithC
2243
2243
  identity: undefined;
2244
2244
  generated: undefined;
2245
2245
  }, {}, {}>;
2246
- userId: import("drizzle-orm/pg-core").PgColumn<{
2247
- name: "user_id";
2246
+ uid: import("drizzle-orm/pg-core").PgColumn<{
2247
+ name: "uid";
2248
2248
  tableName: "magic_link_tokens";
2249
2249
  dataType: "string";
2250
2250
  columnType: "PgUUID";
@@ -32,11 +32,6 @@ export interface TableMeta {
32
32
  fks: ForeignKeyRow[];
33
33
  }
34
34
  export declare function singularize(word: string): string;
35
- /**
36
- * Convert a snake_case name to a human-readable Title Case label.
37
- * e.g. "created_at" -> "Created At", "customer_id" -> "Customer Id"
38
- */
39
- export declare function humanize(snakeName: string): string;
40
35
  /**
41
36
  * Convert a snake_case table name to a camelCase + "Collection" variable name.
42
37
  * e.g. "company_token" -> "companyTokenCollection"
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Naming helpers shared by the introspection modules. These live apart from
3
+ * `introspect-db-logic.ts` because the inference pass needs them too, and
4
+ * importing them from there would close a cycle back through this module.
5
+ */
6
+ /**
7
+ * Convert a snake_case name to a human-readable Title Case label.
8
+ * e.g. "created_at" -> "Created At", "customer_id" -> "Customer Id"
9
+ */
10
+ export declare function humanize(snakeName: string): string;
@@ -51,7 +51,7 @@ export interface ConnectionPosture {
51
51
  privileged: boolean;
52
52
  }
53
53
  export interface AuthContext {
54
- userId: string;
54
+ uid: string;
55
55
  /** Raw roles as carried on the user (strings or `{ id }` objects). */
56
56
  roles: unknown[];
57
57
  }
@@ -93,7 +93,7 @@ export declare function ensureAppRole(run: RawSqlRunner, schemas: string[]): Pro
93
93
  * SECURITY: this function is only ever called on the **user** path (the server
94
94
  * context uses the base/owner driver and never calls it). The default policies
95
95
  * treat `auth.uid() IS NULL` as the trusted server context, and `auth.uid()`
96
- * is `NULLIF(current_setting('app.user_id'), '')` — so an EMPTY user id would
96
+ * is `NULLIF(current_setting('app.uid'), '')` — so an EMPTY user id would
97
97
  * be read as NULL and silently escalate a user request to server privileges.
98
98
  * Coerce empty/blank ids to `ANONYMOUS_USER_ID` here, at the single chokepoint,
99
99
  * rather than trusting every caller (e.g. realtime subscription auth) to do it.
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Ordered, replayable per-channel message history.
3
+ *
4
+ * Broadcast on its own is fire-and-forget to whoever is connected at the
5
+ * instant it is sent: fine for presence and for "someone saved" notifications,
6
+ * not enough for op-based collaborative editing, where a client that blinks
7
+ * out for two seconds has to resync a whole document rather than catch up on
8
+ * the four operations it missed. This adds the missing half — every retained
9
+ * broadcast gets a per-channel sequence number, and a client can ask for
10
+ * everything after the last one it saw.
11
+ *
12
+ * Three decisions worth stating, because each rules out a simpler-looking one:
13
+ *
14
+ * - **Retention is server-side and opt-in.** A channel is created by whoever
15
+ * names it, so a client-supplied history depth would let any visitor commit
16
+ * the backend to unbounded storage. And presence channels — the common case
17
+ * — must not pay for this: with no rules configured nothing is written, no
18
+ * table is created, and `broadcast` runs exactly the code it ran before.
19
+ *
20
+ * - **Sequence numbers come from the database, not from a counter in this
21
+ * process.** They have to survive a restart and be shared across instances;
22
+ * an in-memory counter would restart at 1 after a deploy and hand a
23
+ * reconnecting client a replay from the wrong era, silently.
24
+ *
25
+ * - **The cursor row outlives the messages it numbered.** Pruning is what
26
+ * makes retention affordable, but pruning the cursor along with the messages
27
+ * would restart the sequence and make `sinceSeq` mean something different
28
+ * before and after — the worst kind of bug, because replay would still
29
+ * return rows and they would look plausible. Cursors are tiny and are kept
30
+ * forever; see {@link prune}, which touches only `channel_messages`.
31
+ */
32
+ import { NodePgDatabase } from "drizzle-orm/node-postgres";
33
+ import type { ChannelHistoryEntry, ChannelRetentionRule } from "@rebasepro/types";
34
+ /**
35
+ * Parse a retention TTL into milliseconds.
36
+ *
37
+ * Accepts a raw millisecond count or a short duration string (`"30s"`, `"15m"`,
38
+ * `"24h"`, `"7d"`). Returns undefined for anything unparseable, which the
39
+ * caller treats as "no TTL" — a misspelt duration must not silently become an
40
+ * aggressive one.
41
+ */
42
+ export declare function parseTtlMs(ttl: number | string | undefined): number | undefined;
43
+ /**
44
+ * Whether `channel` is covered by `rule`.
45
+ *
46
+ * Exact match, or a trailing `*` acting as a prefix. Not a general glob: this
47
+ * decides what reaches disk, and a pattern language whose reach is not obvious
48
+ * at a glance is the wrong tool for that job.
49
+ */
50
+ export declare function channelMatchesRule(channel: string, rule: ChannelRetentionRule): boolean;
51
+ /** A rule with its TTL already resolved to milliseconds. */
52
+ export interface ResolvedRetention {
53
+ limit?: number;
54
+ ttlMs?: number;
55
+ }
56
+ /**
57
+ * Persistence and replay for retained channels.
58
+ *
59
+ * Inert unless constructed with at least one rule: {@link enabled} is false,
60
+ * {@link ensureTables} does nothing, and {@link retentionFor} answers undefined
61
+ * for every channel, so the realtime service never reaches the SQL below.
62
+ */
63
+ export declare class ChannelHistoryStore {
64
+ private db;
65
+ private rules;
66
+ /** Resolved rule per channel name, so the match runs once per channel. */
67
+ private resolved;
68
+ /** Channel → timestamp of its last prune, for {@link PRUNE_THROTTLE_MS}. */
69
+ private lastPruned;
70
+ private tablesReady;
71
+ constructor(db: NodePgDatabase<Record<string, unknown>>, rules?: ChannelRetentionRule[]);
72
+ /** Whether any channel retains anything at all. */
73
+ get enabled(): boolean;
74
+ /**
75
+ * The retention that applies to `channel`, or undefined when none does.
76
+ *
77
+ * First matching rule wins, so callers order them most-specific first.
78
+ */
79
+ retentionFor(channel: string): ResolvedRetention | undefined;
80
+ /**
81
+ * Create the history tables. Idempotent, and a no-op when no rule is set —
82
+ * a deployment that never retains anything gets no schema for it.
83
+ */
84
+ ensureTables(): Promise<void>;
85
+ /**
86
+ * Append a broadcast and return the sequence number it was given.
87
+ *
88
+ * The sequence is allocated by the same statement that stores the message,
89
+ * so a crash between the two is not a possibility. `ON CONFLICT DO UPDATE`
90
+ * takes a row lock on the channel's cursor, which is what makes concurrent
91
+ * broadcasts to one channel line up in a single order — and what keeps
92
+ * different channels from contending with each other at all.
93
+ */
94
+ append(channel: string, event: string, payload: unknown, senderId?: string): Promise<{
95
+ seq: number;
96
+ at: string;
97
+ }>;
98
+ /**
99
+ * Everything retained for `channel` after `sinceSeq`, oldest first.
100
+ *
101
+ * `latestSeq` is reported whether or not the messages were capped, so a
102
+ * client that is further behind than one page can tell.
103
+ */
104
+ replay(channel: string, sinceSeq?: number, limit?: number): Promise<{
105
+ messages: ChannelHistoryEntry[];
106
+ latestSeq: number;
107
+ }>;
108
+ /**
109
+ * Enforce a channel's retention bounds.
110
+ *
111
+ * Throttled per channel, so a burst of operations prunes once rather than
112
+ * once per message — the cost then tracks elapsed time instead of write
113
+ * volume, which is what makes retention affordable on a hot channel.
114
+ */
115
+ prune(channel: string, retention: ResolvedRetention): Promise<number>;
116
+ /** Forget throttle and match caches. Called on shutdown. */
117
+ clear(): void;
118
+ }
@@ -4,12 +4,13 @@ import { DataDriver, WebSocketMessage } from "@rebasepro/types";
4
4
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
5
5
  import { RealtimeProvider, CollectionSubscriptionConfig, SingleSubscriptionConfig } from "../interfaces";
6
6
  import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
7
+ import type { ChannelRetentionRule } from "@rebasepro/types";
7
8
  /**
8
9
  * Auth context stored per-subscription so real-time refetches respect RLS.
9
10
  * Mirrors the session variables set by PostgresBackendDriver.withAuth().
10
11
  */
11
12
  export interface SubscriptionAuthContext {
12
- userId: string;
13
+ uid: string;
13
14
  roles: string[];
14
15
  }
15
16
  /**
@@ -24,6 +25,26 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
24
25
  private clients;
25
26
  private channels;
26
27
  private presence;
28
+ /**
29
+ * Ordered, replayable history for channels that opt into it.
30
+ *
31
+ * Undefined until {@link configureChannelHistory} is called, and inert even
32
+ * then unless retention rules were supplied — so presence and ephemeral
33
+ * notification channels never touch it. See `channel-history.ts`.
34
+ */
35
+ private channelHistory?;
36
+ /**
37
+ * One promise chain per retained channel, so that assigning a sequence
38
+ * number and fanning the message out happen in the same order for every
39
+ * message on that channel.
40
+ *
41
+ * Without it, two concurrent broadcasts can be numbered 4 and 5 by the
42
+ * database and still reach subscribers as 5 then 4 — live order and replay
43
+ * order would disagree, which is exactly the divergence sequence numbers
44
+ * are supposed to rule out. Keyed by channel, so unrelated channels never
45
+ * wait on each other.
46
+ */
47
+ private channelSendQueues;
27
48
  private presenceInterval?;
28
49
  private static readonly PRESENCE_TIMEOUT_MS;
29
50
  private dataService;
@@ -197,8 +218,54 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
197
218
  joinChannel(clientId: string, channel: string): void;
198
219
  /** Leave a broadcast channel */
199
220
  leaveChannel(clientId: string, channel: string): void;
200
- /** Broadcast a message to all clients in a channel except sender */
221
+ /**
222
+ * Broadcast a message to all clients in a channel except the sender.
223
+ *
224
+ * On a channel with no retention rule this is what it always was: a
225
+ * synchronous fan-out to whoever is connected, with no sequence number, no
226
+ * SQL and no await — the body below runs to completion before returning.
227
+ *
228
+ * On a retained channel the message is durably numbered first and only then
229
+ * delivered, through a per-channel queue so that delivery order matches
230
+ * sequence order. That ordering is the whole point: a client that catches up
231
+ * with `sinceSeq` has to arrive at the same state as one that never
232
+ * disconnected.
233
+ */
201
234
  broadcastToChannel(clientId: string, channel: string, event: string, payload: unknown): void;
235
+ /**
236
+ * Number a broadcast, store it, then deliver it.
237
+ *
238
+ * A message that cannot be stored is **not** delivered. Delivering it would
239
+ * put it in front of live subscribers while leaving it absent from every
240
+ * future replay — the two views of the channel would disagree permanently,
241
+ * and no later message could repair the gap. Failing loudly to the sender
242
+ * instead lets it retry, which for an operation stream is the only outcome
243
+ * that keeps clients convergent.
244
+ */
245
+ private persistAndFanOut;
246
+ /** Deliver a broadcast frame to every member of a channel but the sender. */
247
+ private fanOutBroadcast;
248
+ /**
249
+ * Install retention rules and create the tables they need.
250
+ *
251
+ * Safe to call with no rules (and safe not to call at all): the store stays
252
+ * inert, no schema is created, and broadcast keeps its original
253
+ * fire-and-forget path.
254
+ */
255
+ configureChannelHistory(rules: ChannelRetentionRule[] | undefined): Promise<void>;
256
+ /** Whether any channel is configured to retain messages. */
257
+ isChannelHistoryEnabled(): boolean;
258
+ /**
259
+ * Answer a client's catch-up request.
260
+ *
261
+ * A channel with no retention rule is answered with `retained: false`
262
+ * rather than an empty list, so the client can tell "you missed nothing"
263
+ * apart from "this channel never keeps anything" — the second means its
264
+ * reconnect strategy has to be a full resync, and silence would leave it
265
+ * guessing.
266
+ */
267
+ private handleChannelHistoryRequest;
268
+ private sendChannelHistory;
202
269
  /** Track presence in a channel */
203
270
  trackPresence(clientId: string, channel: string, state: Record<string, unknown>): void;
204
271
  /** Remove presence from a channel */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rebasepro/server-postgres",
3
3
  "type": "module",
4
- "version": "0.9.1-canary.742f831",
4
+ "version": "0.9.1-canary.78ab3dc",
5
5
  "description": "PostgreSQL data source backend implementation for Rebase with Drizzle ORM",
6
6
  "funding": {
7
7
  "url": "https://github.com/sponsors/rebaseco"
@@ -29,30 +29,6 @@
29
29
  "backend",
30
30
  "rebase"
31
31
  ],
32
- "jest": {
33
- "transform": {
34
- "^.+\\.tsx?$": "ts-jest"
35
- },
36
- "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
37
- "testPathIgnorePatterns": [
38
- "/node_modules/",
39
- "test/e2e/"
40
- ],
41
- "moduleFileExtensions": [
42
- "ts",
43
- "tsx",
44
- "js",
45
- "jsx",
46
- "json",
47
- "node"
48
- ],
49
- "moduleNameMapper": {
50
- "^chalk$": "<rootDir>/test/mocks/chalk.cjs",
51
- "^\\.{1,2}/module-dir$": "<rootDir>/test/mocks/module-dir.cjs",
52
- "^@rebasepro/([a-z0-9-]+)$": "<rootDir>/../$1/src/index.ts",
53
- "^(\\.{1,2}/.*)\\.js$": "$1"
54
- }
55
- },
56
32
  "exports": {
57
33
  ".": {
58
34
  "types": "./dist/index.d.ts",
@@ -71,14 +47,15 @@
71
47
  "execa": "^9.6.1",
72
48
  "pg": "^8.21.0",
73
49
  "ws": "^8.21.0",
74
- "@rebasepro/common": "0.9.1-canary.742f831",
75
- "@rebasepro/codegen": "0.9.1-canary.742f831",
76
- "@rebasepro/server": "0.9.1-canary.742f831",
77
- "@rebasepro/types": "0.9.1-canary.742f831",
78
- "@rebasepro/utils": "0.9.1-canary.742f831"
50
+ "@rebasepro/codegen": "0.9.1-canary.78ab3dc",
51
+ "@rebasepro/common": "0.9.1-canary.78ab3dc",
52
+ "@rebasepro/server": "0.9.1-canary.78ab3dc",
53
+ "@rebasepro/types": "0.9.1-canary.78ab3dc",
54
+ "@rebasepro/utils": "0.9.1-canary.78ab3dc"
79
55
  },
80
56
  "devDependencies": {
81
57
  "@hono/node-server": "^2.0.9",
58
+ "@jest/globals": "^30.4.1",
82
59
  "@types/jest": "^30.0.0",
83
60
  "@types/node": "^25.9.3",
84
61
  "@types/pg": "^8.20.0",
@@ -103,7 +80,7 @@
103
80
  "watch": "vite build --watch",
104
81
  "build": "vite build && tsc --emitDeclarationOnly -p tsconfig.prod.json && node ../../scripts/assert-build-output.mjs",
105
82
  "test:lint": "eslint \"src/**\" --quiet",
106
- "test": "jest --passWithNoTests",
83
+ "test": "jest",
107
84
  "test:e2e": "vitest run --config vitest.e2e.config.ts",
108
85
  "clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f",
109
86
  "smoke:baas": "tsx scripts/smoke-baas.ts"
@@ -111,6 +111,7 @@ export class PostgresBackendDriver implements DataDriver {
111
111
  executeSql: (...args: Parameters<NonNullable<DatabaseAdmin["executeSql"]>>) => this.executeSql(...args),
112
112
  fetchAvailableDatabases: () => this.fetchAvailableDatabases(),
113
113
  fetchAvailableRoles: () => this.fetchAvailableRoles(),
114
+ fetchApplicationRoles: () => this.fetchApplicationRoles(),
114
115
  fetchCurrentDatabase: () => this.fetchCurrentDatabase(),
115
116
  fetchUnmappedTables: (...args: Parameters<NonNullable<DatabaseAdmin["fetchUnmappedTables"]>>) => this.fetchUnmappedTables(...args),
116
117
  fetchTableMetadata: (...args: Parameters<NonNullable<DatabaseAdmin["fetchTableMetadata"]>>) => this.fetchTableMetadata(...args),
@@ -1171,6 +1172,56 @@ export class PostgresBackendDriver implements DataDriver {
1171
1172
  return result.map((r: Record<string, unknown>) => r.rolname as string);
1172
1173
  }
1173
1174
 
1175
+ /**
1176
+ * Application-level roles actually in use in this project.
1177
+ *
1178
+ * Distinct from {@link fetchAvailableRoles}, which returns native
1179
+ * PostgreSQL roles from `pg_roles` (`postgres`, `rebase_user`, …). Those
1180
+ * are the roles the SQL editor can `SET ROLE` to. *These* are the strings
1181
+ * held in the users table's `roles` column, injected per-transaction as
1182
+ * `auth.roles()` and matched by `SecurityRule.roles`. Feeding the pg roles
1183
+ * into a `SecurityRule.roles` field produces a condition no user can ever
1184
+ * satisfy, so the two must not be conflated.
1185
+ *
1186
+ * Roles have no registry table — they were migrated out of
1187
+ * `rebase.user_roles` onto an inline `roles TEXT[]` column — so the live
1188
+ * set is derived from what is assigned. A role that is declared in a policy
1189
+ * but held by nobody yet cannot be discovered here; callers that need it
1190
+ * should union in the roles they already know about.
1191
+ */
1192
+ async fetchApplicationRoles(): Promise<string[]> {
1193
+ // The users table lives in `rebase` for a default (public) setup, but
1194
+ // follows the configured schema otherwise — locate it rather than
1195
+ // assuming. The `roles` ARRAY column is what makes it the auth table.
1196
+ const located = await this.executeSql(`
1197
+ SELECT table_schema, table_name
1198
+ FROM information_schema.columns
1199
+ WHERE column_name = 'roles'
1200
+ AND data_type = 'ARRAY'
1201
+ AND table_name = 'users'
1202
+ AND table_schema NOT IN ('information_schema', 'pg_catalog')
1203
+ ORDER BY (table_schema = 'rebase') DESC, table_schema
1204
+ LIMIT 1;
1205
+ `);
1206
+ if (located.length === 0) return [];
1207
+
1208
+ const schema = located[0].table_schema as string;
1209
+ const table = located[0].table_name as string;
1210
+ // Identifiers come from information_schema, not user input, but they
1211
+ // are still interpolated — quote them so odd-but-legal names survive.
1212
+ const qualified = `"${schema.replace(/"/g, "\"\"")}"."${table.replace(/"/g, "\"\"")}"`;
1213
+
1214
+ const rows = await this.executeSql(`
1215
+ SELECT DISTINCT unnest(roles) AS role
1216
+ FROM ${qualified}
1217
+ WHERE roles IS NOT NULL
1218
+ ORDER BY role;
1219
+ `);
1220
+ return rows
1221
+ .map((r) => r.role as string)
1222
+ .filter((r): r is string => typeof r === "string" && r.length > 0);
1223
+ }
1224
+
1174
1225
  async fetchCurrentDatabase(): Promise<string | undefined> {
1175
1226
  return this.poolManager?.defaultDatabaseName;
1176
1227
  }
@@ -1374,10 +1425,10 @@ export class AuthenticatedPostgresBackendDriver implements DataDriver {
1374
1425
  const pendingNotifications: PostgresBackendDriver["_pendingNotifications"] = [];
1375
1426
 
1376
1427
  const result = await this.delegate.db.transaction(async (tx) => {
1377
- let userId = this.user?.uid;
1378
- if (!userId) {
1428
+ let uid = this.user?.uid;
1429
+ if (!uid) {
1379
1430
  logger.warn("[DataDriver] User ID (uid) is missing for authenticated delegate. Using 'anonymous'. User object", { detail: this.user });
1380
- userId = "anonymous";
1431
+ uid = "anonymous";
1381
1432
  }
1382
1433
 
1383
1434
  const userRoles = this.user?.roles ?? [];
@@ -1396,7 +1447,7 @@ export class AuthenticatedPostgresBackendDriver implements DataDriver {
1396
1447
  //
1397
1448
  // Fails closed: if the switch cannot be performed, the transaction
1398
1449
  // aborts rather than falling back to an RLS-bypassing connection.
1399
- await applyAuthContext(tx, { userId, roles: userRoles }, this.delegate.rlsUserRole);
1450
+ await applyAuthContext(tx, { uid, roles: userRoles }, this.delegate.rlsUserRole);
1400
1451
 
1401
1452
  const txEntityService = new DataService(tx, this.delegate.registry);
1402
1453
  const txDelegate = new PostgresBackendDriver(tx, this.delegate.realtimeService, this.delegate.registry, this.user, this.delegate.poolManager, this.delegate.historyService);
@@ -1435,7 +1486,7 @@ export class AuthenticatedPostgresBackendDriver implements DataDriver {
1435
1486
  */
1436
1487
  private injectAuthContext(unsubscribe: () => void): () => void {
1437
1488
  const authContext = {
1438
- userId: this.user?.uid || "anonymous",
1489
+ uid: this.user?.uid || "anonymous",
1439
1490
  roles: this.user?.roles ?? []
1440
1491
  };
1441
1492
  const entries = Array.from(this.delegate.realtimeService.subscriptions.entries());
@@ -17,7 +17,8 @@ import {
17
17
  CollectionConfig,
18
18
  type HistoryConfig,
19
19
  InitializedDriver,
20
- RealtimeProvider
20
+ RealtimeProvider,
21
+ type RealtimeChannelsConfig
21
22
  } from "@rebasepro/types";
22
23
  import { PostgresBackendDriver } from "./PostgresBackendDriver";
23
24
  import { RealtimeService } from "./services/realtimeService";
@@ -52,6 +53,12 @@ export interface PostgresDriverConfig {
52
53
  * (BaaS mode). Defaults to `public`.
53
54
  */
54
55
  introspectionSchema?: string;
56
+ /**
57
+ * Realtime options. Currently only channel retention, which is opt-in:
58
+ * without rules here no channel keeps any history and broadcast stays
59
+ * fire-and-forget. See {@link ChannelRetentionRule}.
60
+ */
61
+ realtime?: RealtimeChannelsConfig;
55
62
  }
56
63
 
57
64
  /**
@@ -316,6 +323,16 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
316
323
  }
317
324
  }
318
325
 
326
+ // ── Channel history ──────────────────────────────────────────────
327
+ // Opt-in per channel pattern. With no rules this creates no tables
328
+ // and leaves broadcast on its original fire-and-forget path, so
329
+ // presence-only apps pay nothing for it.
330
+ try {
331
+ await realtimeService.configureChannelHistory(pgConfig.realtime?.channels);
332
+ } catch (err) {
333
+ logger.warn("⚠️ Could not initialize channel history tables — retained channels will not replay", { error: err });
334
+ }
335
+
319
336
  // ── Realtime change source ───────────────────────────────────────
320
337
  // Prefer DATABASE_DIRECT_URL to bypass PgBouncer for LISTEN/NOTIFY.
321
338
  const directUrl = process.env.DATABASE_DIRECT_URL || pgConfig.connectionString;