@rebasepro/server-postgres 0.24.0 → 0.24.1-canary.gcb918fc
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 +16 -0
- package/dist/{BranchService-BRt78gfa.js → BranchService-Bsn2wfaj.js} +3 -2
- package/dist/{BranchService-BRt78gfa.js.map → BranchService-Bsn2wfaj.js.map} +1 -1
- package/dist/PostgresBackendDriver.d.ts +91 -2
- package/dist/PostgresBootstrapper.d.ts +101 -2
- package/dist/auth/ensure-tables.d.ts +12 -0
- package/dist/auth/services.d.ts +6 -0
- package/dist/backup/backup-cli.d.ts +14 -0
- package/dist/backup/backup-service.d.ts +2 -2
- package/dist/backup/pg-binary.d.ts +5 -0
- package/dist/{backup-cli-DW5p9_zv.js → backup-cli-DnpJi6I7.js} +37 -29
- package/dist/backup-cli-DnpJi6I7.js.map +1 -0
- package/dist/{backup-service-EwcDVG-8.js → backup-service-BTBB7RXn.js} +55 -21
- package/dist/backup-service-BTBB7RXn.js.map +1 -0
- package/dist/boot-ddl-DVpsFN6r.js +105 -0
- package/dist/boot-ddl-DVpsFN6r.js.map +1 -0
- package/dist/cli-console-Ca6ZlAVU.js +154 -0
- package/dist/cli-console-Ca6ZlAVU.js.map +1 -0
- package/dist/cli-console.d.ts +39 -0
- package/dist/cli-errors-CaI4ntyD.js +1919 -0
- package/dist/cli-errors-CaI4ntyD.js.map +1 -0
- package/dist/cli-errors.d.ts +51 -5
- package/dist/{cli-flags-BglvpjHv.js → cli-flags-JNI-pwSf.js} +6 -3
- package/dist/cli-flags-JNI-pwSf.js.map +1 -0
- package/dist/cli-helpers.d.ts +40 -0
- package/dist/cli-output.d.ts +16 -28
- package/dist/cli-push.d.ts +87 -0
- package/dist/cli.js +566 -182
- package/dist/cli.js.map +1 -1
- package/dist/{column-plan-helpers-1-LQD0yI.js → column-plan-helpers-drZIsSNf.js} +10 -4
- package/dist/{column-plan-helpers-1-LQD0yI.js.map → column-plan-helpers-drZIsSNf.js.map} +1 -1
- package/dist/{databasePoolManager-Bj5FbeAs.js → connection-C9mT8bgf.js} +24 -112
- package/dist/connection-C9mT8bgf.js.map +1 -0
- package/dist/connection.d.ts +13 -2
- package/dist/databasePoolManager-BIuewidb.js +119 -0
- package/dist/databasePoolManager-BIuewidb.js.map +1 -0
- package/dist/{ensure-collection-tables-kkkHk8oo.js → deferred-index-build-DP1_SNFo.js} +484 -346
- package/dist/deferred-index-build-DP1_SNFo.js.map +1 -0
- package/dist/{doctor-C-sYWbmt.js → doctor-C4WiftHm.js} +63 -52
- package/dist/doctor-C4WiftHm.js.map +1 -0
- package/dist/ensure-collection-policies-CXpbeNag.js +541 -0
- package/dist/ensure-collection-policies-CXpbeNag.js.map +1 -0
- package/dist/ensure-collection-tables-BhsU6qUE.js +532 -0
- package/dist/ensure-collection-tables-BhsU6qUE.js.map +1 -0
- package/dist/{ensure-tables-BmI_tRxc.js → ensure-tables-CVm_yvzb.js} +173 -77
- package/dist/ensure-tables-CVm_yvzb.js.map +1 -0
- package/dist/enum-labels-kept-D8fI1MoX.js +56 -0
- package/dist/enum-labels-kept-D8fI1MoX.js.map +1 -0
- package/dist/generate-drizzle-schema-Bz52jH2h.js +57 -0
- package/dist/generate-drizzle-schema-Bz52jH2h.js.map +1 -0
- package/dist/{generate-drizzle-schema-logic-sSFDp6LR.js → generate-drizzle-schema-logic-B74WN37S.js} +5 -5
- package/dist/generate-drizzle-schema-logic-B74WN37S.js.map +1 -0
- package/dist/generate-files-CdEckX6c.js +199 -0
- package/dist/generate-files-CdEckX6c.js.map +1 -0
- package/dist/{generate-postgres-ddl-logic-BJsLaVNX.js → generate-postgres-ddl-logic-B64O2oZF.js} +3 -3
- package/dist/{generate-postgres-ddl-logic-BJsLaVNX.js.map → generate-postgres-ddl-logic-B64O2oZF.js.map} +1 -1
- package/dist/generated-sql.d.ts +8 -2
- package/dist/history/HistoryService.d.ts +64 -5
- package/dist/history/ensure-history-table.d.ts +63 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.es.js +139 -19236
- package/dist/index.es.js.map +1 -1
- package/dist/{introspect-db-logic-kCETE8TY.js → introspect-db-logic-C-Di8dA9.js} +44 -6
- package/dist/introspect-db-logic-C-Di8dA9.js.map +1 -0
- package/dist/{introspect-db-queries-C_Q5VgQw.js → introspect-db-queries-D6kAqReI.js} +23 -1
- package/dist/introspect-db-queries-D6kAqReI.js.map +1 -0
- package/dist/open-rebase-CbOur0TU.js +20858 -0
- package/dist/open-rebase-CbOur0TU.js.map +1 -0
- package/dist/open-rebase.d.ts +86 -0
- package/dist/pg-error-utils-vJezlXdI.js +388 -0
- package/dist/pg-error-utils-vJezlXdI.js.map +1 -0
- package/dist/{plan-schema-DU9exq6C.js → plan-schema-B1YF_tYq.js} +151 -41
- package/dist/plan-schema-B1YF_tYq.js.map +1 -0
- package/dist/{policy-drift-B-J2hhm0.js → policy-drift-Dm8i-s1J.js} +23 -6
- package/dist/policy-drift-Dm8i-s1J.js.map +1 -0
- package/dist/push-index-prebuild-Db4AQF4i.js +45 -0
- package/dist/push-index-prebuild-Db4AQF4i.js.map +1 -0
- package/dist/push-legacy-renames-BK6ilQq2.js +34 -0
- package/dist/push-legacy-renames-BK6ilQq2.js.map +1 -0
- package/dist/{render-ddl-Ds2t_d9V.js → render-ddl-CtC3y9Ol.js} +8 -4
- package/dist/render-ddl-CtC3y9Ol.js.map +1 -0
- package/dist/{rls-bootstrap-sql-_KNnjanK.js → rls-bootstrap-sql-RQUljnJ1.js} +125 -1270
- package/dist/rls-bootstrap-sql-RQUljnJ1.js.map +1 -0
- package/dist/{rls-enforcement-CfXOJJaW.js → rls-enforcement-DvzqlaL1.js} +129 -17
- package/dist/rls-enforcement-DvzqlaL1.js.map +1 -0
- package/dist/schema/auth-schema.d.ts +2 -2
- package/dist/schema/boot-ddl.d.ts +47 -0
- package/dist/schema/collection-index.d.ts +45 -0
- package/dist/schema/column-plan-helpers.d.ts +5 -0
- package/dist/schema/deferred-index-build.d.ts +131 -0
- package/dist/schema/destructive-sql.d.ts +7 -0
- package/dist/schema/doctor-cli.js +110 -6
- package/dist/schema/doctor-cli.js.map +1 -1
- package/dist/schema/doctor-search-scale.d.ts +45 -0
- package/dist/schema/doctor.d.ts +20 -1
- package/dist/schema/ensure-collection-policies.d.ts +73 -35
- package/dist/schema/ensure-collection-tables.d.ts +56 -3
- package/dist/schema/enum-labels-kept.d.ts +26 -0
- package/dist/schema/generate-drizzle-schema.js +1 -1
- package/dist/schema/generate-files.d.ts +44 -0
- package/dist/schema/generate-next-step.d.ts +0 -13
- package/dist/schema/generate-postgres-ddl.js +10 -81
- package/dist/schema/generate-postgres-ddl.js.map +1 -1
- package/dist/schema/introspect-db-logic.d.ts +12 -0
- package/dist/schema/introspect-db-queries.d.ts +11 -1
- package/dist/schema/introspect-db-structure.d.ts +10 -0
- package/dist/schema/introspect-db.js +4 -5
- package/dist/schema/introspect-db.js.map +1 -1
- package/dist/schema/migration-post-process.d.ts +34 -0
- package/dist/schema/plan/diff-plan.d.ts +126 -0
- package/dist/schema/plan/types.d.ts +8 -0
- package/dist/schema/push-index-prebuild.d.ts +25 -0
- package/dist/schema/push-legacy-renames.d.ts +20 -0
- package/dist/schema/rls-bootstrap-sql.d.ts +15 -0
- package/dist/security/rls-enforcement.d.ts +32 -4
- package/dist/services/FetchService.d.ts +58 -8
- package/dist/services/PersistService.d.ts +15 -0
- package/dist/services/RelationService.d.ts +88 -3
- package/dist/services/cdc/CdcListener.d.ts +17 -6
- package/dist/services/cdc/trigger-cdc.d.ts +151 -27
- package/dist/services/channel-bus/index.d.ts +1 -1
- package/dist/services/channel-history.d.ts +15 -4
- package/dist/services/collection-helpers.d.ts +15 -0
- package/dist/services/field-viewer.d.ts +15 -1
- package/dist/services/pg-notify-listener.d.ts +7 -0
- package/dist/services/realtimeService.d.ts +314 -14
- package/dist/services/row-pipeline.d.ts +16 -9
- package/dist/services/socket-liveness.d.ts +42 -2
- package/dist/services/sql-script.d.ts +207 -3
- package/dist/services/vector-iterative-scan.d.ts +40 -0
- package/dist/services/write-depth.d.ts +6 -0
- package/dist/services/write-transaction-scope.d.ts +9 -0
- package/dist/socket-opt-ins.d.ts +64 -0
- package/dist/source-DBDbBTL4.js +811 -0
- package/dist/source-DBDbBTL4.js.map +1 -0
- package/dist/tsx-loader.d.ts +14 -0
- package/dist/utils/drizzle-conditions.d.ts +21 -1
- package/dist/utils/error-cause.d.ts +7 -0
- package/dist/utils/pg-error-utils.d.ts +2 -4
- package/dist/utils/result-rows.d.ts +10 -0
- package/dist/websocket-BLGc5yhs.js +1604 -0
- package/dist/websocket-BLGc5yhs.js.map +1 -0
- package/dist/websocket.d.ts +13 -0
- package/package.json +7 -7
- package/dist/backup-cli-DW5p9_zv.js.map +0 -1
- package/dist/backup-service-EwcDVG-8.js.map +0 -1
- package/dist/cli-errors-DsA-K9uP.js +0 -893
- package/dist/cli-errors-DsA-K9uP.js.map +0 -1
- package/dist/cli-flags-BglvpjHv.js.map +0 -1
- package/dist/cli-output-CNdMql-L.js +0 -47
- package/dist/cli-output-CNdMql-L.js.map +0 -1
- package/dist/collection-index-DdnsxiJ_.js +0 -286
- package/dist/collection-index-DdnsxiJ_.js.map +0 -1
- package/dist/databasePoolManager-Bj5FbeAs.js.map +0 -1
- package/dist/doctor-C-sYWbmt.js.map +0 -1
- package/dist/ensure-collection-policies-B1ureSIV.js +0 -160
- package/dist/ensure-collection-policies-B1ureSIV.js.map +0 -1
- package/dist/ensure-collection-tables-kkkHk8oo.js.map +0 -1
- package/dist/ensure-tables-BmI_tRxc.js.map +0 -1
- package/dist/generate-drizzle-schema-93M0lUxK.js +0 -123
- package/dist/generate-drizzle-schema-93M0lUxK.js.map +0 -1
- package/dist/generate-drizzle-schema-logic-sSFDp6LR.js.map +0 -1
- package/dist/introspect-db-logic-kCETE8TY.js.map +0 -1
- package/dist/introspect-db-queries-C_Q5VgQw.js.map +0 -1
- package/dist/plan-schema-DU9exq6C.js.map +0 -1
- package/dist/policy-drift-B-J2hhm0.js.map +0 -1
- package/dist/render-ddl-Ds2t_d9V.js.map +0 -1
- package/dist/rls-bootstrap-sql-_KNnjanK.js.map +0 -1
- package/dist/rls-enforcement-CfXOJJaW.js.map +0 -1
- package/dist/source-Br7L7GOI.js +0 -410
- package/dist/source-Br7L7GOI.js.map +0 -1
- package/dist/utils/sql-redaction.d.ts +0 -21
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `openRebase` — a project's data from a script, a REPL or a test, with no
|
|
3
|
+
* server around it.
|
|
4
|
+
*
|
|
5
|
+
* The same vocabulary a function gets from the server (`dataAsAdmin`, `sql`),
|
|
6
|
+
* one call to act as a user under row-level security, and `close()`. Nothing
|
|
7
|
+
* else: no HTTP, no schema changes, no environment variables read, no realtime,
|
|
8
|
+
* no timers and no process listeners. What the server's boot adds on top of
|
|
9
|
+
* this is the boot's business.
|
|
10
|
+
*/
|
|
11
|
+
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
12
|
+
import type { Pool } from "pg";
|
|
13
|
+
import { type CollectionConfig, type RebaseSdkData, type RebaseServerClient } from "@rebasepro/types";
|
|
14
|
+
import { type PostgresPoolConfig } from "./connection.js";
|
|
15
|
+
/** Who a request runs as: what row-level security policies read. */
|
|
16
|
+
export interface RebaseIdentity {
|
|
17
|
+
uid: string;
|
|
18
|
+
roles?: string[];
|
|
19
|
+
/** A guest — an anonymous sign-in — rather than an account. */
|
|
20
|
+
isAnonymous?: boolean;
|
|
21
|
+
/** Custom token claims, as `rebase.jwt()` reads them in a policy. */
|
|
22
|
+
claims?: Record<string, unknown>;
|
|
23
|
+
}
|
|
24
|
+
export interface OpenRebaseOptions {
|
|
25
|
+
/**
|
|
26
|
+
* The database to open. `openRebase` creates the pool and `close()` ends
|
|
27
|
+
* it. One of this and `connection`.
|
|
28
|
+
*/
|
|
29
|
+
databaseUrl?: string;
|
|
30
|
+
/**
|
|
31
|
+
* A connection you already hold — `createPostgresDatabaseConnection(…).db`.
|
|
32
|
+
* It stays yours: `close()` leaves it open.
|
|
33
|
+
*/
|
|
34
|
+
connection?: NodePgDatabase<Record<string, unknown>> & {
|
|
35
|
+
$client: Pool;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* The project's collections — what the server is configured with. Empty
|
|
39
|
+
* reads them from the database instead, the way a server with no
|
|
40
|
+
* collection files does.
|
|
41
|
+
*/
|
|
42
|
+
collections: CollectionConfig[];
|
|
43
|
+
/** Pool tuning, when `databaseUrl` is given. */
|
|
44
|
+
pool?: PostgresPoolConfig;
|
|
45
|
+
/** The data source these collections are served from. Defaults to `(default)`. */
|
|
46
|
+
dataSourceKey?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* An open project. `dataAsAdmin` and `sql` are the accessors of the same name
|
|
50
|
+
* a server-side function gets (`RebaseServerClient`).
|
|
51
|
+
*/
|
|
52
|
+
export interface OpenedRebase extends Pick<RebaseServerClient<unknown>, "dataAsAdmin" | "sql"> {
|
|
53
|
+
/**
|
|
54
|
+
* The data as `user` sees it: every read and write runs as them, filtered
|
|
55
|
+
* by the collections' row-level security policies — the scoping an HTTP
|
|
56
|
+
* request from that user gets. Named after the driver method it uses.
|
|
57
|
+
*/
|
|
58
|
+
withAuth(user: RebaseIdentity): Promise<{
|
|
59
|
+
data: RebaseSdkData;
|
|
60
|
+
}>;
|
|
61
|
+
/** The collections this was opened with, or the ones the database described. */
|
|
62
|
+
readonly collections: CollectionConfig[];
|
|
63
|
+
/** End the pool `openRebase` created. Safe to call twice; a `connection` you passed stays open. */
|
|
64
|
+
close(): Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Open a project's database the way its server would serve it, from code.
|
|
68
|
+
*
|
|
69
|
+
* ```ts
|
|
70
|
+
* const rebase = await openRebase({ databaseUrl, collections });
|
|
71
|
+
* const posts = await rebase.dataAsAdmin.posts.find({ limit: 10 });
|
|
72
|
+
* const ada = await rebase.withAuth({ uid: "ada", roles: ["user"] });
|
|
73
|
+
* const hers = await ada.data.posts.find();
|
|
74
|
+
* await rebase.close();
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* Opening changes nothing in the database: no table, role, trigger or
|
|
78
|
+
* function is created. A write to a `history: true` collection records its
|
|
79
|
+
* entry as a server write does, on the write's transaction; on a database no
|
|
80
|
+
* backend with history has provisioned, such writes are refused with
|
|
81
|
+
* `HISTORY_UNAVAILABLE` (reads are not) rather than made without their audit
|
|
82
|
+
* entry. A `withAuth` request on a connection that bypasses row-level
|
|
83
|
+
* security runs as the restricted role a server boot provisions; against a
|
|
84
|
+
* database no server has booted on, it fails rather than run unfiltered.
|
|
85
|
+
*/
|
|
86
|
+
export declare function openRebase(options: OpenRebaseOptions): Promise<OpenedRebase>;
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "module";
|
|
2
|
+
__createRequire(import.meta.url);
|
|
3
|
+
import { K as extractCauseMessage } from "./cli-errors-CaI4ntyD.js";
|
|
4
|
+
import { declaredErrorAnswer, logger } from "@rebasepro/server";
|
|
5
|
+
import { fieldKeyForColumn, getTableName } from "@rebasepro/common";
|
|
6
|
+
//#region src/utils/pg-error-utils.ts
|
|
7
|
+
/**
|
|
8
|
+
* Shared PostgreSQL error extraction and user-friendly message formatting.
|
|
9
|
+
*
|
|
10
|
+
* Drizzle wraps native PG errors in a `.cause` chain. These utilities
|
|
11
|
+
* unwrap that chain to get the real PostgreSQL error (identified by a
|
|
12
|
+
* 5-character alphanumeric `code` such as `42P01`) and translate it into
|
|
13
|
+
* a message that is safe and helpful to show to end-users.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Return the error when it is a deliberate 4xx, otherwise null.
|
|
17
|
+
*
|
|
18
|
+
* A thrown `ApiError` is a decision the server made about the request, not a
|
|
19
|
+
* database failure: its message and code are already written for the client.
|
|
20
|
+
*
|
|
21
|
+
* So is a `RebaseApiError` — what a collection callback throws, since a
|
|
22
|
+
* collection file cannot import the server package. It spells its status
|
|
23
|
+
* `status`, and reading only `statusCode` here turned an `afterRead` that
|
|
24
|
+
* refused with a 403 into "Could not load data … Check server logs" on a
|
|
25
|
+
* subscription, where REST and the socket's own requests answer the 403.
|
|
26
|
+
* Recognised by the predicate those doors use, `declaredErrorAnswer`.
|
|
27
|
+
*/
|
|
28
|
+
function asClientFacingError(error) {
|
|
29
|
+
const answer = declaredErrorAnswer(error);
|
|
30
|
+
if (answer) {
|
|
31
|
+
if (answer.status < 400 || answer.status >= 500) return null;
|
|
32
|
+
return {
|
|
33
|
+
statusCode: answer.status,
|
|
34
|
+
code: answer.code,
|
|
35
|
+
message: answer.message,
|
|
36
|
+
expected: answer.expected
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (!(error instanceof Error)) return null;
|
|
40
|
+
const e = error;
|
|
41
|
+
if (typeof e.statusCode !== "number" || e.statusCode < 400 || e.statusCode >= 500) return null;
|
|
42
|
+
return e;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Extract the underlying PostgreSQL error from a Drizzle wrapper.
|
|
46
|
+
* Drizzle wraps PG errors in a `cause` property — this function
|
|
47
|
+
* recursively walks the chain until it finds an object with a PG
|
|
48
|
+
* error code (5-char alphanumeric, e.g. `42P01`).
|
|
49
|
+
*/
|
|
50
|
+
function extractPgError(error) {
|
|
51
|
+
if (!error || typeof error !== "object") return null;
|
|
52
|
+
if (!(error instanceof Error)) {
|
|
53
|
+
if ("cause" in error && error.cause && typeof error.cause === "object") return extractPgError(error.cause);
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
if ("code" in error && typeof error.code === "string" && /^[0-9A-Z]{5}$/.test(error.code)) return error;
|
|
57
|
+
if (error.cause && typeof error.cause === "object") return extractPgError(error.cause);
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Whether the failure came back from Postgres rather than from building the
|
|
62
|
+
* query — which decides whether a fallback query is worth issuing.
|
|
63
|
+
*
|
|
64
|
+
* Reads here run inside a transaction (that is where `SET LOCAL ROLE` binds
|
|
65
|
+
* RLS). Once a statement raises, that transaction is aborted, and every later
|
|
66
|
+
* statement on it returns `25P02` — "current transaction is aborted, commands
|
|
67
|
+
* ignored until end of transaction block". So a retry after a database error
|
|
68
|
+
* cannot succeed, and it replaces a precise diagnosis ("invalid input syntax
|
|
69
|
+
* for type uuid") with a generic one. Rethrow instead.
|
|
70
|
+
*
|
|
71
|
+
* A query the driver could not even build — a missing reciprocal relation, say
|
|
72
|
+
* — never reached Postgres, leaves the transaction usable, and is exactly what
|
|
73
|
+
* the fallback paths exist for.
|
|
74
|
+
*/
|
|
75
|
+
function reachedDatabase(error) {
|
|
76
|
+
return extractPgError(error) !== null;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Codes that mean "this connection will never work as configured".
|
|
80
|
+
*
|
|
81
|
+
* A wrong password or a database that does not exist is a settled fact about
|
|
82
|
+
* the connection string, not a transient fault — retrying produces the same
|
|
83
|
+
* answer forever.
|
|
84
|
+
*/
|
|
85
|
+
var UNRECOVERABLE_CONNECT_CODES = /* @__PURE__ */ new Set([
|
|
86
|
+
"28P01",
|
|
87
|
+
"28000",
|
|
88
|
+
"3D000",
|
|
89
|
+
"42501"
|
|
90
|
+
]);
|
|
91
|
+
/**
|
|
92
|
+
* Describe a failed connection attempt in terms a developer can act on.
|
|
93
|
+
*
|
|
94
|
+
* The error a caller catches is Drizzle's wrapper: its message is
|
|
95
|
+
* `Failed query: SELECT 1` and its stack runs through drizzle internals, while
|
|
96
|
+
* the sentence that says what is actually wrong — "password authentication
|
|
97
|
+
* failed for user …", "database … does not exist" — sits in `.cause`. Logging
|
|
98
|
+
* the wrapper, as the bootstrapper used to, tells a developer with a typo in
|
|
99
|
+
* their `DATABASE_URL` nothing at all.
|
|
100
|
+
*/
|
|
101
|
+
function classifyConnectFailure(error) {
|
|
102
|
+
const pgError = extractPgError(error);
|
|
103
|
+
const reason = pgError?.message ?? extractCauseMessage(error) ?? (error instanceof Error ? error.message : String(error));
|
|
104
|
+
return {
|
|
105
|
+
fatal: Boolean(pgError?.code && UNRECOVERABLE_CONNECT_CODES.has(pgError.code)),
|
|
106
|
+
reason,
|
|
107
|
+
code: pgError?.code
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Detect whether an error is specifically a role-switching permission failure
|
|
112
|
+
* (e.g. "permission denied to set role" or "must be member of role"),
|
|
113
|
+
* as opposed to a table-level permission denial.
|
|
114
|
+
*
|
|
115
|
+
* This is used by the backend driver to auto-disable role switching when the
|
|
116
|
+
* connection user lacks SET ROLE privileges, rather than surfacing a confusing
|
|
117
|
+
* error to the Studio SQL Editor user.
|
|
118
|
+
*/
|
|
119
|
+
function isRoleSwitchingPermissionError(error) {
|
|
120
|
+
const pgError = extractPgError(error);
|
|
121
|
+
if (!pgError || pgError.code !== "42501") return false;
|
|
122
|
+
const msg = pgError.message.toLowerCase();
|
|
123
|
+
return msg.includes("set role") || msg.includes("member of role");
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Was this `42501` the *caller* being refused by a policy, rather than the
|
|
127
|
+
* server lacking a privilege?
|
|
128
|
+
*
|
|
129
|
+
* Both arrive as `insufficient_privilege`, and they are opposite kinds of
|
|
130
|
+
* problem. A row-level-security refusal is a working access-control system
|
|
131
|
+
* doing its job: the caller asked for something their policies do not permit,
|
|
132
|
+
* which is a 403 and nobody's bug. A missing `GRANT` is the deployment being
|
|
133
|
+
* wrong — the connection role cannot touch the table at all, no policy is
|
|
134
|
+
* involved, and nothing the caller changes about the request will help.
|
|
135
|
+
*
|
|
136
|
+
* Postgres distinguishes them in the message, so this does too:
|
|
137
|
+
*
|
|
138
|
+
* new row violates row-level security policy for table "notes" → the caller
|
|
139
|
+
* permission denied for table notes → the server
|
|
140
|
+
*
|
|
141
|
+
* Only writes reach this. A read that RLS excludes is not an error — the rows
|
|
142
|
+
* are filtered and the caller gets an empty page — so the erroring case is
|
|
143
|
+
* specifically an `INSERT`/`UPDATE` whose row fails a policy's `WITH CHECK`.
|
|
144
|
+
*
|
|
145
|
+
* Matched on the message because that is the only thing carrying the
|
|
146
|
+
* distinction; the SQLSTATE is identical either way. Narrow by design: anything
|
|
147
|
+
* not naming row-level security stays the server's problem, since reporting a
|
|
148
|
+
* genuine privilege misconfiguration as "forbidden" would send an operator
|
|
149
|
+
* hunting for a policy bug that does not exist.
|
|
150
|
+
*/
|
|
151
|
+
function isRowLevelSecurityDenial(error) {
|
|
152
|
+
const pgError = extractPgError(error);
|
|
153
|
+
if (!pgError || pgError.code !== "42501") return false;
|
|
154
|
+
return pgError.message.toLowerCase().includes("row-level security policy");
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The physical column names a Postgres error is about.
|
|
158
|
+
*
|
|
159
|
+
* Postgres puts one in `column` for `23502` and nowhere else, so the rest are
|
|
160
|
+
* read out of the two places that do carry them:
|
|
161
|
+
*
|
|
162
|
+
* - `detail` — `Key (author_id)=(9) is not present in table "authors".` and
|
|
163
|
+
* `Key (email, tenant_id)=(…) already exists.` Only the parenthesised column
|
|
164
|
+
* list is read; the values after `=` are never touched.
|
|
165
|
+
* - `constraint` — `users_email_key`, `posts_author_id_fkey`. The table prefix
|
|
166
|
+
* and the constraint suffix are stripped, leaving the columns joined by `_`,
|
|
167
|
+
* which is ambiguous for multi-column constraints and is therefore only used
|
|
168
|
+
* when `detail` gave nothing.
|
|
169
|
+
*
|
|
170
|
+
* `22P02` carries neither, because an invalid literal fails during parse rather
|
|
171
|
+
* than against a constraint. Its message names the *type*, and an enum type is
|
|
172
|
+
* generated as `<table>_<column>` — which is the one case worth decoding,
|
|
173
|
+
* because a value outside an enum's labels is the most common 22P02 anyone
|
|
174
|
+
* hits and the least legible one.
|
|
175
|
+
*/
|
|
176
|
+
function offendingColumns(pgError, collection) {
|
|
177
|
+
if (pgError.column) return [pgError.column];
|
|
178
|
+
const detail = pgError.detail;
|
|
179
|
+
if (detail) {
|
|
180
|
+
const keyed = /Key \(([^)]+)\)=/.exec(detail);
|
|
181
|
+
if (keyed) return keyed[1].split(",").map((part) => part.trim().replace(/^"|"$/g, "")).filter(Boolean);
|
|
182
|
+
}
|
|
183
|
+
if (pgError.code === "22P02") {
|
|
184
|
+
const enumType = /invalid input value for enum ([^:\s]+)/i.exec(pgError.message ?? "");
|
|
185
|
+
if (enumType && collection) {
|
|
186
|
+
const typeName = enumType[1].replace(/^.*\./, "").replace(/"/g, "");
|
|
187
|
+
const table = getTableName(collection);
|
|
188
|
+
if (typeName.startsWith(`${table}_`)) return [typeName.slice(table.length + 1)];
|
|
189
|
+
}
|
|
190
|
+
return [];
|
|
191
|
+
}
|
|
192
|
+
const constraint = pgError.constraint;
|
|
193
|
+
if (constraint && collection) {
|
|
194
|
+
const table = getTableName(collection);
|
|
195
|
+
const stripped = constraint.replace(new RegExp(`^${table}_`), "").replace(/_(pkey|key|fkey|unique|check|excl|idx)$/, "");
|
|
196
|
+
if (stripped && stripped !== constraint) {
|
|
197
|
+
const columns = /* @__PURE__ */ new Set();
|
|
198
|
+
for (const [key, prop] of Object.entries(collection.properties ?? {})) columns.add(prop?.columnName ?? key);
|
|
199
|
+
if (columns.has(stripped)) return [stripped];
|
|
200
|
+
const wire = fieldKeyForColumn(collection, stripped);
|
|
201
|
+
if (collection.properties && wire in collection.properties) return [stripped];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The fields a Postgres error is about, for `details.violations`.
|
|
208
|
+
*
|
|
209
|
+
* Only the four SQLSTATEs that describe *the caller's data*: a missing value, a
|
|
210
|
+
* duplicate, a bad literal, a foreign key pointing at nothing. Everything else
|
|
211
|
+
* — a dropped connection, a missing table, a privilege problem — is the
|
|
212
|
+
* deployment's, and naming a field for it would send the reader to the wrong
|
|
213
|
+
* place.
|
|
214
|
+
*
|
|
215
|
+
* Empty when the collection is unknown or the column cannot be recovered. An
|
|
216
|
+
* empty list is the honest answer; inventing a field is worse than saying
|
|
217
|
+
* nothing, because a form will mark the wrong input red.
|
|
218
|
+
*/
|
|
219
|
+
function pgFieldViolations(pgError, collection) {
|
|
220
|
+
const code = pgError.code;
|
|
221
|
+
if (code !== "23502" && code !== "23505" && code !== "22P02" && code !== "23503") return [];
|
|
222
|
+
if (!collection) return [];
|
|
223
|
+
const columns = offendingColumns(pgError, collection);
|
|
224
|
+
if (columns.length === 0) return [];
|
|
225
|
+
return columns.map((column) => ({
|
|
226
|
+
field: fieldKeyForColumn(collection, column),
|
|
227
|
+
code
|
|
228
|
+
}));
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Translate a raw PostgreSQL error into a user-friendly message.
|
|
232
|
+
*
|
|
233
|
+
* @param pgError - The extracted PostgreSQL error (from {@link extractPgError})
|
|
234
|
+
* @param context - A human-readable context string (e.g. collection slug or path)
|
|
235
|
+
* @returns An object with a `message` safe for the client and the PG `code`.
|
|
236
|
+
*/
|
|
237
|
+
function pgErrorToFriendlyMessage(pgError, context, options = {}) {
|
|
238
|
+
const verbose = options.verbose ?? process.env.NODE_ENV !== "production";
|
|
239
|
+
const detail = verbose ? pgError.detail : void 0;
|
|
240
|
+
const hint = verbose ? pgError.hint : void 0;
|
|
241
|
+
const constraint = pgError.constraint;
|
|
242
|
+
const column = pgError.column;
|
|
243
|
+
const table = pgError.table;
|
|
244
|
+
const dataType = verbose ? pgError.dataType : void 0;
|
|
245
|
+
const rawMessage = pgError.message || "Unknown database error";
|
|
246
|
+
const pgMessage = verbose ? rawMessage : "";
|
|
247
|
+
const code = pgError.code || "UNKNOWN";
|
|
248
|
+
const violations = pgFieldViolations(pgError, options.collection);
|
|
249
|
+
const suffix = hint ? ` Hint: ${hint}` : "";
|
|
250
|
+
const tableRef = table ?? context;
|
|
251
|
+
/** `": <postgres said>"`, or nothing when the details are withheld. */
|
|
252
|
+
const said = pgMessage ? `: ${pgMessage}` : "";
|
|
253
|
+
switch (pgError.code) {
|
|
254
|
+
case "23503": return {
|
|
255
|
+
message: detail ? `Foreign key constraint violated: ${detail}${suffix}` : `Cannot complete operation: a foreign key constraint${constraint ? ` (${constraint})` : ""} was violated in "${context}".${suffix}`,
|
|
256
|
+
code,
|
|
257
|
+
violations
|
|
258
|
+
};
|
|
259
|
+
case "23001": return {
|
|
260
|
+
message: `Cannot complete operation: rows in "${table ?? "another table"}" still reference this row in "${context}", and their foreign key${constraint ? ` (${constraint})` : ""} does not allow it to be deleted or its key changed. Delete or reassign those rows first.${detail ? ` ${detail}` : ""}${suffix}`,
|
|
261
|
+
code,
|
|
262
|
+
violations
|
|
263
|
+
};
|
|
264
|
+
case "23505": return {
|
|
265
|
+
message: detail ? `Duplicate value: ${detail}${suffix}` : `Cannot complete operation: a unique constraint${constraint ? ` (${constraint})` : ""} was violated in "${context}".${suffix}`,
|
|
266
|
+
code,
|
|
267
|
+
violations
|
|
268
|
+
};
|
|
269
|
+
case "23502": return {
|
|
270
|
+
message: `Missing required field: "${violations[0]?.field ?? column ?? "unknown"}" in "${tableRef}" cannot be empty.${suffix}`,
|
|
271
|
+
code,
|
|
272
|
+
violations
|
|
273
|
+
};
|
|
274
|
+
case "23514": return {
|
|
275
|
+
message: `Validation failed: a check constraint${constraint ? ` (${constraint})` : ""} was violated in "${context}".${suffix}`,
|
|
276
|
+
code,
|
|
277
|
+
violations
|
|
278
|
+
};
|
|
279
|
+
case "22P02": return {
|
|
280
|
+
message: `Invalid data format in "${context}"${said}.${suffix}`,
|
|
281
|
+
code,
|
|
282
|
+
violations
|
|
283
|
+
};
|
|
284
|
+
case "22001": return {
|
|
285
|
+
message: `Value too long for column "${column ?? "unknown"}" in "${tableRef}"${said}.${suffix}`,
|
|
286
|
+
code,
|
|
287
|
+
violations
|
|
288
|
+
};
|
|
289
|
+
case "22003": return {
|
|
290
|
+
message: `Numeric value out of range for column "${column ?? "unknown"}" in "${tableRef}"${said}.${suffix}`,
|
|
291
|
+
code,
|
|
292
|
+
violations
|
|
293
|
+
};
|
|
294
|
+
case "42703": return {
|
|
295
|
+
message: `Unknown column in "${tableRef}"${said}. Check if your schema is up to date (run migrations).${suffix}`,
|
|
296
|
+
code,
|
|
297
|
+
violations
|
|
298
|
+
};
|
|
299
|
+
case "42P01": return {
|
|
300
|
+
message: `Table not found for "${context}"${said}. Check if your schema is up to date (run migrations).${suffix}`,
|
|
301
|
+
code,
|
|
302
|
+
violations
|
|
303
|
+
};
|
|
304
|
+
case "42501": return rawMessage.toLowerCase().includes("row-level security policy") ? {
|
|
305
|
+
message: `Not permitted to write this row in "${tableRef}": it does not satisfy the row-level security policy.${suffix}`,
|
|
306
|
+
code,
|
|
307
|
+
violations
|
|
308
|
+
} : {
|
|
309
|
+
message: `Permission denied on "${tableRef}": the database role this server connects as lacks privileges on it.${suffix}`,
|
|
310
|
+
code,
|
|
311
|
+
violations
|
|
312
|
+
};
|
|
313
|
+
case "28000": return {
|
|
314
|
+
message: `Authorization failed for "${context}". Check your database credentials.${suffix}`,
|
|
315
|
+
code,
|
|
316
|
+
violations
|
|
317
|
+
};
|
|
318
|
+
default: {
|
|
319
|
+
const parts = [`Database error in "${context}" [${code}]${said}`];
|
|
320
|
+
if (detail) parts.push(`Detail: ${detail}`);
|
|
321
|
+
if (column) parts.push(`Column: ${column}`);
|
|
322
|
+
if (dataType) parts.push(`Data type: ${dataType}`);
|
|
323
|
+
if (constraint) parts.push(`Constraint: ${constraint}`);
|
|
324
|
+
if (hint) parts.push(`Hint: ${hint}`);
|
|
325
|
+
return {
|
|
326
|
+
message: parts.join(". "),
|
|
327
|
+
code,
|
|
328
|
+
violations
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Sanitize any error into a message safe and helpful for the client.
|
|
335
|
+
*
|
|
336
|
+
* A deliberate 4xx (`ApiError`) passes through untouched — the server already
|
|
337
|
+
* decided what the client should read. Otherwise the PG error is extracted
|
|
338
|
+
* from the Drizzle cause chain, falling back to a generic message that
|
|
339
|
+
* doesn't leak SQL.
|
|
340
|
+
*
|
|
341
|
+
* @param error - The raw caught error
|
|
342
|
+
* @param context - A human-readable context string (e.g. collection path)
|
|
343
|
+
* @returns An object with `message` (user-friendly) and optional `code`
|
|
344
|
+
* (the `ApiError` code, or the PG SQLSTATE).
|
|
345
|
+
*/
|
|
346
|
+
function sanitizeErrorForClient(error, context, options = {}) {
|
|
347
|
+
const clientError = asClientFacingError(error);
|
|
348
|
+
if (clientError) {
|
|
349
|
+
const line = `[API ${clientError.statusCode} ${clientError.code ?? "BAD_REQUEST"}] in "${context}": ${clientError.message}`;
|
|
350
|
+
if (clientError.expected) logger.debug(line);
|
|
351
|
+
else logger.warn(`⚠️ ${line}`);
|
|
352
|
+
return {
|
|
353
|
+
message: clientError.message,
|
|
354
|
+
...clientError.code && { code: clientError.code }
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
const pgError = extractPgError(error);
|
|
358
|
+
if (pgError) {
|
|
359
|
+
logger.error(`[PG ${pgError.code}] Error in "${context}"`, {
|
|
360
|
+
code: pgError.code,
|
|
361
|
+
message: pgError.message,
|
|
362
|
+
detail: pgError.detail,
|
|
363
|
+
hint: pgError.hint,
|
|
364
|
+
column: pgError.column,
|
|
365
|
+
table: pgError.table,
|
|
366
|
+
constraint: pgError.constraint,
|
|
367
|
+
dataType: pgError.dataType
|
|
368
|
+
});
|
|
369
|
+
const { message, code, violations } = pgErrorToFriendlyMessage(pgError, context, { collection: options.collection });
|
|
370
|
+
return {
|
|
371
|
+
message,
|
|
372
|
+
code,
|
|
373
|
+
...violations.length > 0 && { violations }
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
logger.error(`Database error in "${context}" (no PG error extracted)`, {
|
|
377
|
+
error: error instanceof Error ? error.message : String(error),
|
|
378
|
+
stack: error instanceof Error ? error.stack : void 0,
|
|
379
|
+
cause: error instanceof Error && error.cause ? error.cause instanceof Error ? error.cause.message : String(error.cause) : void 0
|
|
380
|
+
});
|
|
381
|
+
const causeMessage = extractCauseMessage(error);
|
|
382
|
+
if (causeMessage) return { message: `Database error in "${context}": ${causeMessage}` };
|
|
383
|
+
return { message: `Could not load data for "${context}". Check server logs for details.` };
|
|
384
|
+
}
|
|
385
|
+
//#endregion
|
|
386
|
+
export { pgErrorToFriendlyMessage as a, isRowLevelSecurityDenial as i, extractPgError as n, reachedDatabase as o, isRoleSwitchingPermissionError as r, sanitizeErrorForClient as s, classifyConnectFailure as t };
|
|
387
|
+
|
|
388
|
+
//# sourceMappingURL=pg-error-utils-vJezlXdI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pg-error-utils-vJezlXdI.js","names":[],"sources":["../src/utils/pg-error-utils.ts"],"sourcesContent":["/**\n * Shared PostgreSQL error extraction and user-friendly message formatting.\n *\n * Drizzle wraps native PG errors in a `.cause` chain. These utilities\n * unwrap that chain to get the real PostgreSQL error (identified by a\n * 5-character alphanumeric `code` such as `42P01`) and translate it into\n * a message that is safe and helpful to show to end-users.\n */\n\nimport { declaredErrorAnswer, logger } from \"@rebasepro/server\";\nimport type { CollectionConfig } from \"@rebasepro/types\";\nimport { fieldKeyForColumn, getTableName } from \"@rebasepro/common\";\nimport { extractCauseMessage } from \"./error-cause\";\n\n/**\n * Shape of a deliberate client-facing error — `ApiError` from\n * `@rebasepro/server`, `RebaseApiError` from `@rebasepro/types`, or anything\n * else carrying a 4xx `statusCode`.\n *\n * Matched structurally rather than with `instanceof`: `@rebasepro/server` can\n * be loaded twice (published dist vs. workspace source), which breaks class\n * identity — `PersistService` hedges against the same thing by also accepting\n * `name === \"ApiError\"`.\n */\ninterface ClientFacingError extends Error {\n statusCode?: number;\n code?: string;\n /** See `ApiError.expected` — routine outcomes log at debug, not warn. */\n expected?: boolean;\n}\n\n/**\n * Return the error when it is a deliberate 4xx, otherwise null.\n *\n * A thrown `ApiError` is a decision the server made about the request, not a\n * database failure: its message and code are already written for the client.\n *\n * So is a `RebaseApiError` — what a collection callback throws, since a\n * collection file cannot import the server package. It spells its status\n * `status`, and reading only `statusCode` here turned an `afterRead` that\n * refused with a 403 into \"Could not load data … Check server logs\" on a\n * subscription, where REST and the socket's own requests answer the 403.\n * Recognised by the predicate those doors use, `declaredErrorAnswer`.\n */\nfunction asClientFacingError(error: unknown): Pick<ClientFacingError, \"statusCode\" | \"code\" | \"message\" | \"expected\"> | null {\n const answer = declaredErrorAnswer(error);\n if (answer) {\n if (answer.status < 400 || answer.status >= 500) return null;\n return { statusCode: answer.status, code: answer.code, message: answer.message, expected: answer.expected };\n }\n if (!(error instanceof Error)) return null;\n const e = error as ClientFacingError;\n if (typeof e.statusCode !== \"number\" || e.statusCode < 400 || e.statusCode >= 500) return null;\n return e;\n}\n\n/** Shape of PostgreSQL errors with diagnostic metadata. */\nexport interface PostgresError extends Error {\n code?: string;\n detail?: string;\n hint?: string;\n constraint?: string;\n column?: string;\n table?: string;\n dataType?: string;\n cause?: unknown;\n}\n\n/**\n * Extract the underlying PostgreSQL error from a Drizzle wrapper.\n * Drizzle wraps PG errors in a `cause` property — this function\n * recursively walks the chain until it finds an object with a PG\n * error code (5-char alphanumeric, e.g. `42P01`).\n */\nexport function extractPgError(error: unknown): PostgresError | null {\n if (!error || typeof error !== \"object\") return null;\n if (!(error instanceof Error)) {\n // Check non-Error objects for a cause chain (Drizzle sometimes wraps oddly)\n if (\"cause\" in error && (error as Record<string, unknown>).cause && typeof (error as Record<string, unknown>).cause === \"object\") {\n return extractPgError((error as Record<string, unknown>).cause);\n }\n return null;\n }\n\n // Check if the error itself has a PG error code\n if (\"code\" in error && typeof (error as PostgresError).code === \"string\" && /^[0-9A-Z]{5}$/.test((error as PostgresError).code!)) {\n return error as PostgresError;\n }\n\n // Check the cause chain (Drizzle wraps PG errors)\n if (error.cause && typeof error.cause === \"object\") {\n return extractPgError(error.cause);\n }\n\n return null;\n}\n\n/**\n * Whether the failure came back from Postgres rather than from building the\n * query — which decides whether a fallback query is worth issuing.\n *\n * Reads here run inside a transaction (that is where `SET LOCAL ROLE` binds\n * RLS). Once a statement raises, that transaction is aborted, and every later\n * statement on it returns `25P02` — \"current transaction is aborted, commands\n * ignored until end of transaction block\". So a retry after a database error\n * cannot succeed, and it replaces a precise diagnosis (\"invalid input syntax\n * for type uuid\") with a generic one. Rethrow instead.\n *\n * A query the driver could not even build — a missing reciprocal relation, say\n * — never reached Postgres, leaves the transaction usable, and is exactly what\n * the fallback paths exist for.\n */\nexport function reachedDatabase(error: unknown): boolean {\n return extractPgError(error) !== null;\n}\n\n// In its own module so the driver CLI can use it without loading the server\n// (this file imports `@rebasepro/server`).\nexport { extractCauseMessage };\n\n/**\n * Codes that mean \"this connection will never work as configured\".\n *\n * A wrong password or a database that does not exist is a settled fact about\n * the connection string, not a transient fault — retrying produces the same\n * answer forever.\n */\nconst UNRECOVERABLE_CONNECT_CODES = new Set([\n \"28P01\", // invalid_password\n \"28000\", // invalid_authorization_specification\n \"3D000\", // invalid_catalog_name — the database does not exist\n \"42501\" // insufficient_privilege\n]);\n\nexport interface ConnectFailure {\n /** True when retrying cannot help: the connection string itself is wrong. */\n fatal: boolean;\n /** The deepest message available — the Postgres one where there is one. */\n reason: string;\n /** The `SQLSTATE`, when the failure came from Postgres rather than the socket. */\n code?: string;\n}\n\n/**\n * Describe a failed connection attempt in terms a developer can act on.\n *\n * The error a caller catches is Drizzle's wrapper: its message is\n * `Failed query: SELECT 1` and its stack runs through drizzle internals, while\n * the sentence that says what is actually wrong — \"password authentication\n * failed for user …\", \"database … does not exist\" — sits in `.cause`. Logging\n * the wrapper, as the bootstrapper used to, tells a developer with a typo in\n * their `DATABASE_URL` nothing at all.\n */\nexport function classifyConnectFailure(error: unknown): ConnectFailure {\n const pgError = extractPgError(error);\n const reason =\n pgError?.message ??\n extractCauseMessage(error) ??\n (error instanceof Error ? error.message : String(error));\n return {\n fatal: Boolean(pgError?.code && UNRECOVERABLE_CONNECT_CODES.has(pgError.code)),\n reason,\n code: pgError?.code\n };\n}\n\n/**\n * Detect whether an error is specifically a role-switching permission failure\n * (e.g. \"permission denied to set role\" or \"must be member of role\"),\n * as opposed to a table-level permission denial.\n *\n * This is used by the backend driver to auto-disable role switching when the\n * connection user lacks SET ROLE privileges, rather than surfacing a confusing\n * error to the Studio SQL Editor user.\n */\nexport function isRoleSwitchingPermissionError(error: unknown): boolean {\n const pgError = extractPgError(error);\n if (!pgError || pgError.code !== \"42501\") return false;\n const msg = pgError.message.toLowerCase();\n return msg.includes(\"set role\") || msg.includes(\"member of role\");\n}\n\n/**\n * Was this `42501` the *caller* being refused by a policy, rather than the\n * server lacking a privilege?\n *\n * Both arrive as `insufficient_privilege`, and they are opposite kinds of\n * problem. A row-level-security refusal is a working access-control system\n * doing its job: the caller asked for something their policies do not permit,\n * which is a 403 and nobody's bug. A missing `GRANT` is the deployment being\n * wrong — the connection role cannot touch the table at all, no policy is\n * involved, and nothing the caller changes about the request will help.\n *\n * Postgres distinguishes them in the message, so this does too:\n *\n * new row violates row-level security policy for table \"notes\" → the caller\n * permission denied for table notes → the server\n *\n * Only writes reach this. A read that RLS excludes is not an error — the rows\n * are filtered and the caller gets an empty page — so the erroring case is\n * specifically an `INSERT`/`UPDATE` whose row fails a policy's `WITH CHECK`.\n *\n * Matched on the message because that is the only thing carrying the\n * distinction; the SQLSTATE is identical either way. Narrow by design: anything\n * not naming row-level security stays the server's problem, since reporting a\n * genuine privilege misconfiguration as \"forbidden\" would send an operator\n * hunting for a policy bug that does not exist.\n */\nexport function isRowLevelSecurityDenial(error: unknown): boolean {\n const pgError = extractPgError(error);\n if (!pgError || pgError.code !== \"42501\") return false;\n return pgError.message.toLowerCase().includes(\"row-level security policy\");\n}\n\n/**\n * One constraint the database refused, named in the caller's vocabulary.\n *\n * `field` is the **wire** name, derived from the physical column: `author_id`\n * on the table is `authorId` on the row and in the request body, and telling a\n * caller their `author_id` is missing sends them looking for a key their client\n * has never seen. `code` is the SQLSTATE, which is stable, documented by\n * Postgres and the same in every environment — unlike the message, which is\n * deliberately thinner in production.\n *\n * Deliberately no `value`. The column name is safe to publish: it is already in\n * the OpenAPI, the generated SDK and the caller's own request. The *value* is\n * not — `23505`'s detail reads `Key (email)=(a@b.c) already exists.`, which\n * answers \"is this person registered?\" for any address on a table whose rows\n * RLS is otherwise hiding completely.\n */\nexport interface PgFieldViolation {\n field: string;\n code: string;\n}\n\n/**\n * The physical column names a Postgres error is about.\n *\n * Postgres puts one in `column` for `23502` and nowhere else, so the rest are\n * read out of the two places that do carry them:\n *\n * - `detail` — `Key (author_id)=(9) is not present in table \"authors\".` and\n * `Key (email, tenant_id)=(…) already exists.` Only the parenthesised column\n * list is read; the values after `=` are never touched.\n * - `constraint` — `users_email_key`, `posts_author_id_fkey`. The table prefix\n * and the constraint suffix are stripped, leaving the columns joined by `_`,\n * which is ambiguous for multi-column constraints and is therefore only used\n * when `detail` gave nothing.\n *\n * `22P02` carries neither, because an invalid literal fails during parse rather\n * than against a constraint. Its message names the *type*, and an enum type is\n * generated as `<table>_<column>` — which is the one case worth decoding,\n * because a value outside an enum's labels is the most common 22P02 anyone\n * hits and the least legible one.\n */\nfunction offendingColumns(pgError: PostgresError, collection?: CollectionConfig): string[] {\n if (pgError.column) return [pgError.column];\n\n const detail = pgError.detail;\n if (detail) {\n const keyed = /Key \\(([^)]+)\\)=/.exec(detail);\n if (keyed) {\n return keyed[1].split(\",\").map(part => part.trim().replace(/^\"|\"$/g, \"\")).filter(Boolean);\n }\n }\n\n if (pgError.code === \"22P02\") {\n // `invalid input value for enum posts_status: \"archived\"`\n const enumType = /invalid input value for enum ([^:\\s]+)/i.exec(pgError.message ?? \"\");\n if (enumType && collection) {\n const typeName = enumType[1].replace(/^.*\\./, \"\").replace(/\"/g, \"\");\n const table = getTableName(collection);\n if (typeName.startsWith(`${table}_`)) return [typeName.slice(table.length + 1)];\n }\n return [];\n }\n\n const constraint = pgError.constraint;\n if (constraint && collection) {\n const table = getTableName(collection);\n const stripped = constraint\n .replace(new RegExp(`^${table}_`), \"\")\n .replace(/_(pkey|key|fkey|unique|check|excl|idx)$/, \"\");\n // Only when it names something the collection actually has: a\n // hand-written constraint name is not a column list, and guessing at one\n // would put a field in the response that does not exist.\n if (stripped && stripped !== constraint) {\n const columns = new Set<string>();\n for (const [key, prop] of Object.entries(collection.properties ?? {})) {\n columns.add((prop as { columnName?: string })?.columnName ?? key);\n }\n if (columns.has(stripped)) return [stripped];\n const wire = fieldKeyForColumn(collection, stripped);\n if (collection.properties && wire in collection.properties) return [stripped];\n }\n }\n\n return [];\n}\n\n/**\n * The fields a Postgres error is about, for `details.violations`.\n *\n * Only the four SQLSTATEs that describe *the caller's data*: a missing value, a\n * duplicate, a bad literal, a foreign key pointing at nothing. Everything else\n * — a dropped connection, a missing table, a privilege problem — is the\n * deployment's, and naming a field for it would send the reader to the wrong\n * place.\n *\n * Empty when the collection is unknown or the column cannot be recovered. An\n * empty list is the honest answer; inventing a field is worse than saying\n * nothing, because a form will mark the wrong input red.\n */\nexport function pgFieldViolations(\n pgError: PostgresError,\n collection?: CollectionConfig\n): PgFieldViolation[] {\n const code = pgError.code;\n if (code !== \"23502\" && code !== \"23505\" && code !== \"22P02\" && code !== \"23503\") return [];\n // Without the collection there is no mapping, only a guess. `toWireKey`\n // would get the default case right and silently invent a field for any\n // property carrying an explicit `columnName` — and a form pointed at an\n // input that does not exist is worse off than one shown the raw column.\n if (!collection) return [];\n const columns = offendingColumns(pgError, collection);\n if (columns.length === 0) return [];\n return columns.map(column => ({\n field: fieldKeyForColumn(collection, column),\n code\n }));\n}\n\n/**\n * Translate a raw PostgreSQL error into a user-friendly message.\n *\n * @param pgError - The extracted PostgreSQL error (from {@link extractPgError})\n * @param context - A human-readable context string (e.g. collection slug or path)\n * @returns An object with a `message` safe for the client and the PG `code`.\n */\nexport function pgErrorToFriendlyMessage(\n pgError: PostgresError,\n context: string,\n options: { verbose?: boolean; collection?: CollectionConfig } = {}\n): { message: string; code: string; violations: PgFieldViolation[] } {\n // Postgres's own `DETAIL` and `HINT`, and the raw driver message, are what\n // make these errors useful while you are building — and what makes them an\n // oracle once the server is answering strangers.\n //\n // The sharp one is `23505`: its detail reads `Key (email)=(a@b.c) already\n // exists.`, which answers \"is this person registered?\" for any address, on\n // a table whose rows RLS is otherwise hiding completely. The rest leak\n // physical column and constraint names, which are the map for the next\n // question. So in production the shape of the failure is served — what\n // rule was broken, and where — and the values are not.\n //\n // The full text is not lost: the caller logs the original error. This\n // decides what crosses the wire, not what is recorded.\n const verbose = options.verbose ?? process.env.NODE_ENV !== \"production\";\n\n const detail = verbose ? (pgError.detail as string | undefined) : undefined;\n const hint = verbose ? (pgError.hint as string | undefined) : undefined;\n const constraint = pgError.constraint as string | undefined;\n const column = pgError.column as string | undefined;\n const table = pgError.table as string | undefined;\n const dataType = verbose ? (pgError.dataType as string | undefined) : undefined;\n const rawMessage = pgError.message || \"Unknown database error\";\n const pgMessage = verbose ? rawMessage : \"\";\n const code = pgError.code || \"UNKNOWN\";\n\n // Which *field* the caller has to fix, derived from the column. Safe in\n // production — a column name is already published in the OpenAPI and the\n // generated SDK — where the value that broke the rule is not, and is never\n // included. See `pgFieldViolations`.\n const violations = pgFieldViolations(pgError, options.collection);\n\n const suffix = hint ? ` Hint: ${hint}` : \"\";\n const tableRef = table ?? context;\n\n /** `\": <postgres said>\"`, or nothing when the details are withheld. */\n const said = pgMessage ? `: ${pgMessage}` : \"\";\n\n switch (pgError.code) {\n case \"23503\": // foreign_key_violation\n return {\n message: detail\n ? `Foreign key constraint violated: ${detail}${suffix}`\n : `Cannot complete operation: a foreign key constraint${constraint ? ` (${constraint})` : \"\"} was violated in \"${context}\".${suffix}`,\n code,\n violations\n };\n case \"23001\": // restrict_violation\n // A row other rows still point at, under `ON DELETE RESTRICT` — the\n // default for a required link. `table` is the *referencing* table,\n // which is the one the caller has to clear or reassign first.\n return {\n message: `Cannot complete operation: rows in \"${table ?? \"another table\"}\" still reference this row in ` +\n `\"${context}\", and their foreign key${constraint ? ` (${constraint})` : \"\"} does not allow it to be ` +\n `deleted or its key changed. Delete or reassign those rows first.${detail ? ` ${detail}` : \"\"}${suffix}`,\n code,\n violations\n };\n case \"23505\": // unique_violation\n return {\n message: detail\n ? `Duplicate value: ${detail}${suffix}`\n : `Cannot complete operation: a unique constraint${constraint ? ` (${constraint})` : \"\"} was violated in \"${context}\".${suffix}`,\n code,\n violations\n };\n case \"23502\": {\n // not_null_violation. The message names the *field* when the\n // collection is known, because that is the key the caller sent and\n // the only one they can act on: told `author_id` is missing, they go\n // looking for a key their generated client has never mentioned.\n const named = violations[0]?.field ?? column ?? \"unknown\";\n return {\n message: `Missing required field: \"${named}\" in \"${tableRef}\" cannot be empty.${suffix}`,\n code,\n violations\n };\n }\n case \"23514\": // check_violation\n return {\n message: `Validation failed: a check constraint${constraint ? ` (${constraint})` : \"\"} was violated in \"${context}\".${suffix}`,\n code,\n violations\n };\n case \"22P02\": // invalid_text_representation (e.g. invalid UUID, wrong enum value)\n return {\n message: `Invalid data format in \"${context}\"${said}.${suffix}`,\n code,\n violations\n };\n case \"22001\": // string_data_right_truncation (value too long)\n return {\n message: `Value too long for column \"${column ?? \"unknown\"}\" in \"${tableRef}\"${said}.${suffix}`,\n code,\n violations\n };\n case \"22003\": // numeric_value_out_of_range\n return {\n message: `Numeric value out of range for column \"${column ?? \"unknown\"}\" in \"${tableRef}\"${said}.${suffix}`,\n code,\n violations\n };\n case \"42703\": // undefined_column\n return {\n message: `Unknown column in \"${tableRef}\"${said}. Check if your schema is up to date (run migrations).${suffix}`,\n code,\n violations\n };\n case \"42P01\": // undefined_table\n return {\n message: `Table not found for \"${context}\"${said}. Check if your schema is up to date (run migrations).${suffix}`,\n code,\n violations\n };\n case \"42501\": // insufficient_privilege\n // Two unrelated failures share this SQLSTATE, and the old message\n // named both causes because it could not tell them apart — which\n // meant it was half wrong whichever one had happened, and sent the\n // reader to check the other. Postgres says which in its own message.\n return rawMessage.toLowerCase().includes(\"row-level security policy\")\n ? {\n // The caller. Their policies do not permit this row; the\n // deployment is working exactly as configured.\n message: `Not permitted to write this row in \"${tableRef}\": it does not satisfy the row-level security policy.${suffix}`,\n code,\n violations\n }\n : {\n // The deployment. No policy is involved — the connecting\n // role cannot touch the table at all.\n message: `Permission denied on \"${tableRef}\": the database role this server connects as lacks privileges on it.${suffix}`,\n code,\n violations\n };\n case \"28000\": // invalid_authorization_specification\n return {\n message: `Authorization failed for \"${context}\". Check your database credentials.${suffix}`,\n code,\n violations\n };\n default: {\n // Unhandled PG code — still surface the actual database message\n const parts = [`Database error in \"${context}\" [${code}]${said}`];\n if (detail) parts.push(`Detail: ${detail}`);\n if (column) parts.push(`Column: ${column}`);\n if (dataType) parts.push(`Data type: ${dataType}`);\n if (constraint) parts.push(`Constraint: ${constraint}`);\n if (hint) parts.push(`Hint: ${hint}`);\n return { message: parts.join(\". \"), code, violations };\n }\n }\n}\n\n/**\n * Sanitize any error into a message safe and helpful for the client.\n *\n * A deliberate 4xx (`ApiError`) passes through untouched — the server already\n * decided what the client should read. Otherwise the PG error is extracted\n * from the Drizzle cause chain, falling back to a generic message that\n * doesn't leak SQL.\n *\n * @param error - The raw caught error\n * @param context - A human-readable context string (e.g. collection path)\n * @returns An object with `message` (user-friendly) and optional `code`\n * (the `ApiError` code, or the PG SQLSTATE).\n */\nexport function sanitizeErrorForClient(\n error: unknown,\n context: string,\n options: { collection?: CollectionConfig } = {}\n): { message: string; code?: string; violations?: PgFieldViolation[] } {\n // ── A deliberate 4xx is not a database failure ──────────────────\n // Its message and code are written for the client; replacing them with\n // \"Check server logs\" would discard the only diagnosis the caller gets\n // (e.g. the offending filter field and the collection's valid ones).\n // Log level follows the same convention as the HTTP error handler in\n // @rebasepro/server: routine outcomes at debug, everything else at warn.\n const clientError = asClientFacingError(error);\n if (clientError) {\n const line = `[API ${clientError.statusCode} ${clientError.code ?? \"BAD_REQUEST\"}] in \"${context}\": ${clientError.message}`;\n if (clientError.expected) {\n logger.debug(line);\n } else {\n logger.warn(`⚠️ ${line}`);\n }\n return { message: clientError.message, ...(clientError.code && { code: clientError.code }) };\n }\n\n // ── Always log the full, unsanitized error server-side ──────────\n const pgError = extractPgError(error);\n\n if (pgError) {\n logger.error(`[PG ${pgError.code}] Error in \"${context}\"`, {\n code: pgError.code,\n message: pgError.message,\n detail: pgError.detail,\n hint: pgError.hint,\n column: pgError.column,\n table: pgError.table,\n constraint: pgError.constraint,\n dataType: pgError.dataType\n // The outer Drizzle wrapper message used to be logged here \"for\n // full context\": it is `Failed query: <sql>\\nparams: <values>`, so\n // it published the statement and every bound value (an email, a\n // password hash) on every realtime data failure. The SQLSTATE,\n // detail, table, column and constraint above are the diagnostic\n // value; the wrapper added only the leak. `logger` strips the\n // wrapper as well, but the field itself carried nothing else.\n });\n const { message, code, violations } = pgErrorToFriendlyMessage(pgError, context, { collection: options.collection });\n return { message, code, ...(violations.length > 0 && { violations }) };\n }\n\n // No PG error found — log the raw error as-is\n logger.error(`Database error in \"${context}\" (no PG error extracted)`, {\n error: error instanceof Error ? error.message : String(error),\n stack: error instanceof Error ? error.stack : undefined,\n cause: error instanceof Error && error.cause\n ? (error.cause instanceof Error ? error.cause.message : String(error.cause))\n : undefined\n });\n\n // Try to get the deepest cause message\n const causeMessage = extractCauseMessage(error);\n if (causeMessage) {\n return { message: `Database error in \"${context}\": ${causeMessage}` };\n }\n\n // Last resort — generic message, never leak raw SQL\n return { message: `Could not load data for \"${context}\". Check server logs for details.` };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,SAAS,oBAAoB,OAAgG;CACzH,MAAM,SAAS,oBAAoB,KAAK;CACxC,IAAI,QAAQ;EACR,IAAI,OAAO,SAAS,OAAO,OAAO,UAAU,KAAK,OAAO;EACxD,OAAO;GAAE,YAAY,OAAO;GAAQ,MAAM,OAAO;GAAM,SAAS,OAAO;GAAS,UAAU,OAAO;EAAS;CAC9G;CACA,IAAI,EAAE,iBAAiB,QAAQ,OAAO;CACtC,MAAM,IAAI;CACV,IA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