@rebasepro/server-postgres 0.9.1-canary.1d2d8b5 → 0.9.1-canary.29ed165
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 +43 -2
- package/dist/PostgresBootstrapper.d.ts +17 -1
- package/dist/auth/services.d.ts +68 -52
- package/dist/collections/buildRegistry.d.ts +27 -0
- package/dist/connection.d.ts +21 -0
- package/dist/data-transformer.d.ts +9 -2
- package/dist/index.es.js +2060 -762
- package/dist/index.es.js.map +1 -1
- package/dist/schema/auth-bootstrap-sql.d.ts +1 -1
- package/dist/schema/auth-schema.d.ts +24 -24
- package/dist/schema/doctor.d.ts +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/policy-drift.d.ts +54 -0
- package/dist/security/rls-enforcement.d.ts +2 -2
- package/dist/services/FetchService.d.ts +4 -24
- package/dist/services/PersistService.d.ts +9 -1
- package/dist/services/RelationService.d.ts +34 -1
- package/dist/services/channel-history.d.ts +118 -0
- package/dist/services/collection-helpers.d.ts +79 -14
- package/dist/services/dataService.d.ts +3 -1
- package/dist/services/index.d.ts +1 -1
- package/dist/services/realtimeService.d.ts +76 -2
- package/dist/services/row-pipeline.d.ts +63 -0
- package/package.json +12 -33
- package/src/PostgresBackendDriver.ts +183 -18
- package/src/PostgresBootstrapper.ts +80 -26
- package/src/auth/ensure-tables.ts +170 -28
- package/src/auth/services.ts +181 -150
- package/src/cli.ts +60 -0
- package/src/collections/buildRegistry.ts +59 -0
- package/src/connection.ts +61 -1
- package/src/data-transformer.ts +11 -9
- package/src/databasePoolManager.ts +2 -0
- package/src/schema/auth-bootstrap-sql.ts +7 -1
- package/src/schema/auth-schema.ts +13 -13
- package/src/schema/doctor.ts +45 -20
- package/src/schema/generate-drizzle-schema-logic.ts +24 -29
- package/src/schema/generate-postgres-ddl-logic.ts +76 -28
- 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-db.ts +19 -2
- package/src/schema/introspect-runtime.ts +1 -1
- package/src/security/policy-drift.test.ts +152 -1
- package/src/security/policy-drift.ts +126 -4
- package/src/security/rls-enforcement.ts +11 -5
- package/src/services/BranchService.ts +42 -10
- package/src/services/FetchService.ts +65 -270
- package/src/services/PersistService.ts +62 -9
- package/src/services/RelationService.ts +153 -94
- package/src/services/channel-history.ts +343 -0
- package/src/services/collection-helpers.ts +164 -47
- package/src/services/dataService.ts +3 -2
- package/src/services/index.ts +1 -0
- package/src/services/realtimeService.ts +238 -29
- package/src/services/row-pipeline.ts +239 -0
- package/src/utils/drizzle-conditions.ts +13 -0
- package/src/websocket.ts +34 -12
- package/dist/schema/auth-default-policies.d.ts +0 -10
- package/src/schema/auth-default-policies.ts +0 -132
|
@@ -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
|
}
|
|
@@ -200,7 +200,7 @@ export async function ensureAppRole(run: RawSqlRunner, schemas: string[]): Promi
|
|
|
200
200
|
* SECURITY: this function is only ever called on the **user** path (the server
|
|
201
201
|
* context uses the base/owner driver and never calls it). The default policies
|
|
202
202
|
* treat `auth.uid() IS NULL` as the trusted server context, and `auth.uid()`
|
|
203
|
-
* is `NULLIF(current_setting('app.
|
|
203
|
+
* is `NULLIF(current_setting('app.uid'), '')` — so an EMPTY user id would
|
|
204
204
|
* be read as NULL and silently escalate a user request to server privileges.
|
|
205
205
|
* Coerce empty/blank ids to `ANONYMOUS_USER_ID` here, at the single chokepoint,
|
|
206
206
|
* rather than trusting every caller (e.g. realtime subscription auth) to do it.
|
|
@@ -208,15 +208,21 @@ 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
|
+
// `app.user_id` is the pre-rename spelling, still written because policies
|
|
216
|
+
// are data: a database provisioned before the rename holds rules compiled
|
|
217
|
+
// to `current_setting('app.user_id')`, and those predicates would evaluate
|
|
218
|
+
// to NULL — failing open or locking out — if we stopped setting it. Drop
|
|
219
|
+
// the alias only once no live database carries a legacy policy.
|
|
215
220
|
await tx.execute(drizzleSql`
|
|
216
221
|
SELECT
|
|
217
|
-
set_config('app.
|
|
222
|
+
set_config('app.uid', ${uid}, true),
|
|
223
|
+
set_config('app.user_id', ${uid}, true),
|
|
218
224
|
set_config('app.user_roles', ${normalizedRoles.join(",")}, true),
|
|
219
|
-
set_config('app.jwt', ${JSON.stringify({ sub:
|
|
225
|
+
set_config('app.jwt', ${JSON.stringify({ sub: uid, roles: auth.roles })}, true)
|
|
220
226
|
`);
|
|
221
227
|
if (userRole) {
|
|
222
228
|
await tx.execute(drizzleSql.raw(`SET LOCAL ROLE ${quoteIdent(userRole)}`));
|
|
@@ -11,10 +11,45 @@ import { sql } from "drizzle-orm";
|
|
|
11
11
|
import { BranchInfo } from "@rebasepro/types";
|
|
12
12
|
import { DrizzleClient } from "../interfaces";
|
|
13
13
|
import { DatabasePoolManager } from "../databasePoolManager";
|
|
14
|
+
import { extractPgError, extractCauseMessage } from "../utils/pg-error-utils";
|
|
14
15
|
|
|
15
16
|
/** Internal prefix applied to branch database names to avoid collisions. */
|
|
16
17
|
const BRANCH_DB_PREFIX = "rb_";
|
|
17
18
|
|
|
19
|
+
/** `duplicate_database` — the target database name is already taken. */
|
|
20
|
+
const PG_DUPLICATE_DATABASE = "42P04";
|
|
21
|
+
|
|
22
|
+
/** `object_in_use` — the database still has connections attached. */
|
|
23
|
+
const PG_OBJECT_IN_USE = "55006";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Describe a failed branch DDL statement in terms a user can act on.
|
|
27
|
+
*
|
|
28
|
+
* Drizzle reports failures as `Failed query: <sql> params:` and hides the real
|
|
29
|
+
* PostgreSQL error in the `cause` chain, so matching on `err.message` never sees
|
|
30
|
+
* the actual problem. Match on the PG error code instead — it survives wrapping
|
|
31
|
+
* and, unlike the message text, is not locale-dependent.
|
|
32
|
+
*/
|
|
33
|
+
function describeBranchDdlError(err: unknown, fallbackContext: string): Error {
|
|
34
|
+
const pgError = extractPgError(err);
|
|
35
|
+
|
|
36
|
+
if (pgError?.code === PG_DUPLICATE_DATABASE) {
|
|
37
|
+
return new Error(`Database "${fallbackContext}" already exists on the server. Choose a different branch name.`);
|
|
38
|
+
}
|
|
39
|
+
if (pgError?.code === PG_OBJECT_IN_USE) {
|
|
40
|
+
return new Error(
|
|
41
|
+
`Cannot complete the operation: the database "${fallbackContext}" has active connections. ` +
|
|
42
|
+
"Close other clients or connections and try again."
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Unknown failure: surface the real PG message rather than the Drizzle
|
|
47
|
+
// wrapper, which would otherwise show the raw SQL and no reason at all.
|
|
48
|
+
const detail = pgError?.message ?? extractCauseMessage(err);
|
|
49
|
+
if (detail) return new Error(detail);
|
|
50
|
+
return err instanceof Error ? err : new Error(String(err));
|
|
51
|
+
}
|
|
52
|
+
|
|
18
53
|
/** Fully-qualified metadata table in the rebase schema. */
|
|
19
54
|
const BRANCHES_TABLE = "rebase.branches";
|
|
20
55
|
|
|
@@ -109,18 +144,15 @@ export class BranchService {
|
|
|
109
144
|
sql.raw(`CREATE DATABASE "${safeDbName}" TEMPLATE "${safeSourceDb}"`)
|
|
110
145
|
);
|
|
111
146
|
} catch (err) {
|
|
112
|
-
const
|
|
113
|
-
if (
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
// If template fails due to active connections, provide a helpful error
|
|
117
|
-
if (msg.includes("being accessed by other users")) {
|
|
147
|
+
const pgError = extractPgError(err);
|
|
148
|
+
if (pgError?.code === PG_OBJECT_IN_USE) {
|
|
149
|
+
// The template — not the new database — is the one still in use.
|
|
118
150
|
throw new Error(
|
|
119
151
|
`Cannot create branch: the source database "${sourceDb}" has active connections. ` +
|
|
120
152
|
"Close other clients or connections and try again."
|
|
121
153
|
);
|
|
122
154
|
}
|
|
123
|
-
throw err;
|
|
155
|
+
throw describeBranchDdlError(err, dbName);
|
|
124
156
|
}
|
|
125
157
|
|
|
126
158
|
// Record metadata in the default database
|
|
@@ -166,14 +198,14 @@ export class BranchService {
|
|
|
166
198
|
try {
|
|
167
199
|
await this.db.execute(sql.raw(`DROP DATABASE "${safeDbName}"`));
|
|
168
200
|
} catch (err) {
|
|
169
|
-
const
|
|
170
|
-
if (
|
|
201
|
+
const pgError = extractPgError(err);
|
|
202
|
+
if (pgError?.code === PG_OBJECT_IN_USE) {
|
|
171
203
|
throw new Error(
|
|
172
204
|
`Cannot delete branch "${sanitizedName}": the database has active connections. ` +
|
|
173
205
|
"Close other clients and try again."
|
|
174
206
|
);
|
|
175
207
|
}
|
|
176
|
-
throw err;
|
|
208
|
+
throw describeBranchDdlError(err, dbName);
|
|
177
209
|
}
|
|
178
210
|
|
|
179
211
|
// Remove metadata
|