@rebasepro/server-postgres 0.9.1-canary.09aaf62 → 0.9.1-canary.0de22e0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -0
- package/dist/PostgresBackendDriver.d.ts +18 -0
- package/dist/PostgresBootstrapper.d.ts +7 -1
- package/dist/index.es.js +489 -33
- package/dist/index.es.js.map +1 -1
- package/dist/schema/introspect-db-logic.d.ts +0 -5
- package/dist/schema/introspect-db-naming.d.ts +10 -0
- package/dist/security/rls-enforcement.d.ts +1 -1
- package/dist/services/channel-history.d.ts +118 -0
- package/dist/services/realtimeService.d.ts +69 -2
- package/package.json +8 -31
- package/src/PostgresBackendDriver.ts +56 -5
- package/src/PostgresBootstrapper.ts +18 -1
- package/src/schema/introspect-db-inference.ts +1 -1
- package/src/schema/introspect-db-logic.ts +1 -10
- package/src/schema/introspect-db-naming.ts +15 -0
- package/src/schema/introspect-runtime.ts +1 -1
- package/src/security/rls-enforcement.ts +4 -4
- package/src/services/channel-history.ts +343 -0
- package/src/services/realtimeService.ts +197 -9
- package/src/websocket.ts +30 -11
|
@@ -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;
|
|
@@ -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
|
-
|
|
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
|
-
/**
|
|
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.
|
|
4
|
+
"version": "0.9.1-canary.0de22e0",
|
|
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/codegen": "0.9.1-canary.
|
|
75
|
-
"@rebasepro/server": "0.9.1-canary.
|
|
76
|
-
"@rebasepro/
|
|
77
|
-
"@rebasepro/utils": "0.9.1-canary.
|
|
78
|
-
"@rebasepro/
|
|
50
|
+
"@rebasepro/codegen": "0.9.1-canary.0de22e0",
|
|
51
|
+
"@rebasepro/server": "0.9.1-canary.0de22e0",
|
|
52
|
+
"@rebasepro/common": "0.9.1-canary.0de22e0",
|
|
53
|
+
"@rebasepro/utils": "0.9.1-canary.0de22e0",
|
|
54
|
+
"@rebasepro/types": "0.9.1-canary.0de22e0"
|
|
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
|
|
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
|
|
1378
|
-
if (!
|
|
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
|
-
|
|
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, {
|
|
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
|
-
|
|
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;
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* and consumed directly by tests.
|
|
8
8
|
*/
|
|
9
9
|
import { inferPropertyFromData } from "./introspect-db-inference";
|
|
10
|
+
import { humanize } from "./introspect-db-naming";
|
|
10
11
|
|
|
11
12
|
// ── Typed interfaces for SQL query results ────────────────────────────
|
|
12
13
|
|
|
@@ -117,16 +118,6 @@ export function singularize(word: string): string {
|
|
|
117
118
|
return word;
|
|
118
119
|
}
|
|
119
120
|
|
|
120
|
-
/**
|
|
121
|
-
* Convert a snake_case name to a human-readable Title Case label.
|
|
122
|
-
* e.g. "created_at" -> "Created At", "customer_id" -> "Customer Id"
|
|
123
|
-
*/
|
|
124
|
-
export function humanize(snakeName: string): string {
|
|
125
|
-
return snakeName
|
|
126
|
-
.replace(/_/g, " ")
|
|
127
|
-
.replace(/\b\w/g, (c) => c.toUpperCase());
|
|
128
|
-
}
|
|
129
|
-
|
|
130
121
|
/**
|
|
131
122
|
* Convert a snake_case table name to a camelCase + "Collection" variable name.
|
|
132
123
|
* e.g. "company_token" -> "companyTokenCollection"
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
/**
|
|
8
|
+
* Convert a snake_case name to a human-readable Title Case label.
|
|
9
|
+
* e.g. "created_at" -> "Created At", "customer_id" -> "Customer Id"
|
|
10
|
+
*/
|
|
11
|
+
export function humanize(snakeName: string): string {
|
|
12
|
+
return snakeName
|
|
13
|
+
.replace(/_/g, " ")
|
|
14
|
+
.replace(/\b\w/g, (c) => c.toUpperCase());
|
|
15
|
+
}
|
|
@@ -23,11 +23,11 @@ import {
|
|
|
23
23
|
buildTablesMap,
|
|
24
24
|
buildEnumMap,
|
|
25
25
|
identifyJoinTables,
|
|
26
|
-
humanize,
|
|
27
26
|
singularize,
|
|
28
27
|
mapPgType,
|
|
29
28
|
getIconForTable
|
|
30
29
|
} from "./introspect-db-logic";
|
|
30
|
+
import { humanize } from "./introspect-db-naming";
|
|
31
31
|
|
|
32
32
|
export interface IntrospectedSchema {
|
|
33
33
|
tablesMap: Map<string, TableMeta>;
|
|
@@ -59,7 +59,7 @@ export interface ConnectionPosture {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
export interface AuthContext {
|
|
62
|
-
|
|
62
|
+
uid: string;
|
|
63
63
|
/** Raw roles as carried on the user (strings or `{ id }` objects). */
|
|
64
64
|
roles: unknown[];
|
|
65
65
|
}
|
|
@@ -208,15 +208,15 @@ export async function ensureAppRole(run: RawSqlRunner, schemas: string[]): Promi
|
|
|
208
208
|
* semantics: it is why `auth.uid() IS NOT NULL` is true for anonymous requests.
|
|
209
209
|
*/
|
|
210
210
|
export async function applyAuthContext(tx: SqlTx, auth: AuthContext, userRole?: string): Promise<void> {
|
|
211
|
-
const
|
|
211
|
+
const uid = typeof auth.uid === "string" && auth.uid.trim() !== "" ? auth.uid : ANONYMOUS_USER_ID;
|
|
212
212
|
const normalizedRoles = auth.roles.map((r: unknown) =>
|
|
213
213
|
typeof r === "string" ? r : (r as Record<string, unknown>)?.id ?? String(r)
|
|
214
214
|
);
|
|
215
215
|
await tx.execute(drizzleSql`
|
|
216
216
|
SELECT
|
|
217
|
-
set_config('app.user_id', ${
|
|
217
|
+
set_config('app.user_id', ${uid}, true),
|
|
218
218
|
set_config('app.user_roles', ${normalizedRoles.join(",")}, true),
|
|
219
|
-
set_config('app.jwt', ${JSON.stringify({ sub:
|
|
219
|
+
set_config('app.jwt', ${JSON.stringify({ sub: uid, roles: auth.roles })}, true)
|
|
220
220
|
`);
|
|
221
221
|
if (userRole) {
|
|
222
222
|
await tx.execute(drizzleSql.raw(`SET LOCAL ROLE ${quoteIdent(userRole)}`));
|