@rebasepro/server-postgres 0.12.1-canary.gdfba2a1 → 0.12.1-canary.ge1bb178
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/dist/PostgresBackendDriver.d.ts +1 -1
- package/dist/PostgresBootstrapper.d.ts +25 -1
- package/dist/auth/services.d.ts +6 -1
- package/dist/backup/backup-service.d.ts +10 -1
- package/dist/backup/pg-tools.d.ts +47 -0
- package/dist/{backup-service-DH9kPg-E.js → backup-service-CD8o_1Sl.js} +144 -11
- package/dist/backup-service-CD8o_1Sl.js.map +1 -0
- package/dist/cli-helpers.d.ts +39 -0
- package/dist/{connection-B5Wndbr1.js → connection-BuZ97wsr.js} +57 -3
- package/dist/connection-BuZ97wsr.js.map +1 -0
- package/dist/connection.d.ts +42 -0
- package/dist/ensure-collection-policies-BrUVgjz3.js +57 -0
- package/dist/ensure-collection-policies-BrUVgjz3.js.map +1 -0
- package/dist/ensure-collection-tables-Da2oGkX2.js +650 -0
- package/dist/ensure-collection-tables-Da2oGkX2.js.map +1 -0
- package/dist/index.es.js +529 -142
- package/dist/index.es.js.map +1 -1
- package/dist/policy-CeA1JcxP.js +105 -0
- package/dist/policy-CeA1JcxP.js.map +1 -0
- package/dist/schema/ensure-collection-policies.d.ts +60 -0
- package/dist/schema/ensure-collection-tables.d.ts +44 -2
- package/dist/schema/generate-postgres-ddl-logic.d.ts +135 -1
- package/dist/schema/introspect-db-constraints.d.ts +57 -0
- package/dist/schema/introspect-db-logic.d.ts +94 -5
- package/dist/schema/introspect-db-queries.d.ts +119 -0
- package/dist/schema/introspect-db-structure.d.ts +263 -0
- package/dist/schema/introspect-db-types.d.ts +11 -0
- package/dist/services/FetchService.d.ts +4 -1
- package/dist/services/RelationService.d.ts +24 -1
- package/dist/services/channel-bus/index.d.ts +1 -7
- package/dist/services/collection-helpers.d.ts +24 -1
- package/dist/services/dataService.d.ts +3 -1
- package/dist/services/row-pipeline.d.ts +1 -1
- package/dist/{src-DihrDFuP.js → src-CzbghKwf.js} +189 -101
- package/dist/src-CzbghKwf.js.map +1 -0
- package/dist/utils/connection-string.d.ts +29 -0
- package/dist/utils/drizzle-conditions.d.ts +5 -4
- package/dist/utils/pg-error-utils.d.ts +19 -0
- package/dist/{websocket-BKcGvILX.js → websocket-B2LsrINK.js} +8 -6
- package/dist/websocket-B2LsrINK.js.map +1 -0
- package/package.json +6 -6
- package/src/PostgresAdapter.ts +21 -2
- package/src/PostgresBackendDriver.ts +4 -0
- package/src/PostgresBootstrapper.ts +192 -33
- package/src/auth/services.ts +6 -3
- package/src/backup/backup-cli.ts +41 -2
- package/src/backup/backup-service.ts +38 -5
- package/src/backup/pg-tools.ts +96 -3
- package/src/cli-helpers.ts +70 -0
- package/src/cli.ts +44 -26
- package/src/collections/validate-relations.ts +15 -0
- package/src/connection.ts +73 -0
- package/src/data-transformer.ts +9 -3
- package/src/databasePoolManager.ts +5 -2
- package/src/schema/ensure-collection-policies.ts +105 -0
- package/src/schema/ensure-collection-tables.test.ts +105 -9
- package/src/schema/ensure-collection-tables.ts +220 -32
- package/src/schema/generate-drizzle-schema-logic.ts +16 -3
- package/src/schema/generate-postgres-ddl-logic.ts +375 -16
- package/src/schema/introspect-db-constraints.ts +385 -0
- package/src/schema/introspect-db-inference.ts +18 -8
- package/src/schema/introspect-db-logic.ts +385 -71
- package/src/schema/introspect-db-queries.ts +326 -0
- package/src/schema/introspect-db-structure.ts +670 -0
- package/src/schema/introspect-db-types.ts +56 -0
- package/src/schema/introspect-db.ts +37 -80
- package/src/schema/introspect-runtime.test.ts +56 -8
- package/src/schema/introspect-runtime.ts +31 -9
- package/src/security/policy-drift.test.ts +11 -3
- package/src/services/FetchService.ts +76 -14
- package/src/services/PersistService.ts +20 -6
- package/src/services/RelationService.ts +249 -48
- package/src/services/channel-bus/index.ts +0 -9
- package/src/services/collection-helpers.ts +40 -1
- package/src/services/dataService.ts +3 -1
- package/src/services/realtimeService.ts +3 -3
- package/src/services/row-pipeline.ts +1 -1
- package/src/utils/connection-string.ts +58 -0
- package/src/utils/drizzle-conditions.ts +31 -6
- package/src/utils/pg-error-utils.ts +46 -0
- package/src/websocket.ts +18 -9
- package/dist/backup-service-DH9kPg-E.js.map +0 -1
- package/dist/connection-B5Wndbr1.js.map +0 -1
- package/dist/ensure-collection-tables-CIHSH962.js +0 -304
- package/dist/ensure-collection-tables-CIHSH962.js.map +0 -1
- package/dist/src-DihrDFuP.js.map +0 -1
- package/dist/websocket-BKcGvILX.js.map +0 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connection-string rewrites for the tools Rebase shells out to.
|
|
3
|
+
*
|
|
4
|
+
* Pure and dependency-free: `pg-tools` builds argument vectors without a live
|
|
5
|
+
* server, and these have to be usable from there.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The `sslmode` values libpq accepts. `no-verify` is conspicuously not one.
|
|
10
|
+
*/
|
|
11
|
+
const LIBPQ_SSLMODES = new Set(["disable", "allow", "prefer", "require", "verify-ca", "verify-full"]);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Make a connection string safe to hand to a libpq program.
|
|
15
|
+
*
|
|
16
|
+
* `sslmode=no-verify` is a node-postgres convention — encrypt, but do not check
|
|
17
|
+
* the certificate — and libpq does not have it. It does not degrade or warn:
|
|
18
|
+
*
|
|
19
|
+
* psql: error: invalid sslmode value: "no-verify"
|
|
20
|
+
*
|
|
21
|
+
* which means a `DATABASE_URL` that works perfectly for the app makes `psql`,
|
|
22
|
+
* `pg_dump`, `pg_restore` and Atlas all refuse to start, with an error that
|
|
23
|
+
* points at the value rather than at the convention it comes from. It has cost
|
|
24
|
+
* this project time more than once.
|
|
25
|
+
*
|
|
26
|
+
* `require` is the honest translation: libpq's `require` encrypts and does not
|
|
27
|
+
* verify the certificate either, which is exactly what `no-verify` asks for.
|
|
28
|
+
* Nothing is relaxed by the rewrite — `verify-ca` and `verify-full` are left
|
|
29
|
+
* alone, so a connection string that asked for verification still gets it.
|
|
30
|
+
*
|
|
31
|
+
* Only `sslmode` is touched, and only when it is a value libpq would reject.
|
|
32
|
+
* Anything unparseable is returned as given: a connection that works
|
|
33
|
+
* unverified beats one this corrupted.
|
|
34
|
+
*/
|
|
35
|
+
export function forLibpq(connectionString: string): string {
|
|
36
|
+
let url: URL;
|
|
37
|
+
try {
|
|
38
|
+
url = new URL(connectionString);
|
|
39
|
+
} catch {
|
|
40
|
+
// Key/value DSNs and anything else we cannot parse. A `sslmode` in one
|
|
41
|
+
// of those is space-separated rather than a query parameter, and
|
|
42
|
+
// rewriting it with a regex is how a password containing "sslmode="
|
|
43
|
+
// gets mangled.
|
|
44
|
+
return connectionString;
|
|
45
|
+
}
|
|
46
|
+
if (url.protocol !== "postgres:" && url.protocol !== "postgresql:") return connectionString;
|
|
47
|
+
|
|
48
|
+
const sslmode = url.searchParams.get("sslmode");
|
|
49
|
+
if (sslmode === null || LIBPQ_SSLMODES.has(sslmode)) return connectionString;
|
|
50
|
+
|
|
51
|
+
url.searchParams.set("sslmode", "require");
|
|
52
|
+
// Re-serialize by hand for the reason `pinSearchPath` does: URLSearchParams
|
|
53
|
+
// writes a space as `+`, which node-postgres decodes and libpq does not.
|
|
54
|
+
url.search = Array.from(url.searchParams.entries())
|
|
55
|
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
56
|
+
.join("&");
|
|
57
|
+
return url.toString();
|
|
58
|
+
}
|
|
@@ -175,10 +175,11 @@ export class DrizzleConditionBuilder {
|
|
|
175
175
|
static buildRelationScopeCondition(
|
|
176
176
|
relation: ResolvedRelation,
|
|
177
177
|
/**
|
|
178
|
-
* Lazy:
|
|
179
|
-
*
|
|
180
|
-
* the parent's *id* alone, and
|
|
181
|
-
* a child listing fail on a
|
|
178
|
+
* Lazy: `via`, `belongsTo`, and a foreign key that points at a
|
|
179
|
+
* `sourceKey` need the parent's own table. A junction and a plain
|
|
180
|
+
* foreign key are expressible from the parent's *id* alone, and
|
|
181
|
+
* requiring the table for them would make a child listing fail on a
|
|
182
|
+
* parent whose table isn't registered.
|
|
182
183
|
*/
|
|
183
184
|
parent: () => { table: PgTable<any>; idColumn: AnyPgColumn },
|
|
184
185
|
parentId: string | number,
|
|
@@ -239,7 +240,16 @@ export class DrizzleConditionBuilder {
|
|
|
239
240
|
`relation '${relation.relationName}'.`
|
|
240
241
|
);
|
|
241
242
|
}
|
|
242
|
-
return eq(fkColumn, parentId);
|
|
243
|
+
if (!relation.sourceKey) return eq(fkColumn, parentId);
|
|
244
|
+
|
|
245
|
+
// A link on a natural key: the foreign key holds a column of the
|
|
246
|
+
// parent row, not its id, so the parent has to be read. As a
|
|
247
|
+
// subquery rather than a prior SELECT, for the same reason
|
|
248
|
+
// `belongsTo` below is — one statement sees one snapshot, and a
|
|
249
|
+
// scope condition that read the key separately could be built
|
|
250
|
+
// from a value the very next statement no longer agrees with.
|
|
251
|
+
const { table, idColumn } = parent();
|
|
252
|
+
return sql`${fkColumn} = (SELECT ${sql.identifier(relation.sourceKey)} FROM ${table} WHERE ${idColumn} = ${parentId})`;
|
|
243
253
|
}
|
|
244
254
|
|
|
245
255
|
case "belongsTo": {
|
|
@@ -385,7 +395,22 @@ export class DrizzleConditionBuilder {
|
|
|
385
395
|
// inverse, so reversing it into a filter is a different problem
|
|
386
396
|
// from the two shapes below rather than a third case of them.
|
|
387
397
|
if (relation && (hasForeignKeyOnTarget(relation) || isManyToMany(relation)) && registry && sourceIdColumn) {
|
|
388
|
-
|
|
398
|
+
// The `EXISTS` correlates the target's foreign key with a column
|
|
399
|
+
// on *this* table, and that is the primary key only when the
|
|
400
|
+
// link joins on it. A `sourceKey` names a different one, and
|
|
401
|
+
// correlating on the id anyway silently matches nothing —
|
|
402
|
+
// "filter by this relation" would quietly return zero rows.
|
|
403
|
+
const correlationColumn = hasForeignKeyOnTarget(relation) && relation.sourceKey
|
|
404
|
+
? columnAt(relation.sourceKey)
|
|
405
|
+
: sourceIdColumn;
|
|
406
|
+
if (!correlationColumn) {
|
|
407
|
+
throw new Error(
|
|
408
|
+
`\`sourceKey: "${(relation as ResolvedForeignKeyOnTarget).sourceKey}"\` on relation ` +
|
|
409
|
+
`'${relation.relationName}' is not a column on '${collectionPath}', so a filter on ` +
|
|
410
|
+
"that relation has nothing to correlate against."
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
return { kind: "relation", relation, registry, sourceIdColumn: correlationColumn };
|
|
389
414
|
}
|
|
390
415
|
}
|
|
391
416
|
|
|
@@ -97,6 +97,52 @@ export function extractCauseMessage(error: unknown): string | null {
|
|
|
97
97
|
return null;
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Codes that mean "this connection will never work as configured".
|
|
102
|
+
*
|
|
103
|
+
* A wrong password or a database that does not exist is a settled fact about
|
|
104
|
+
* the connection string, not a transient fault — retrying produces the same
|
|
105
|
+
* answer forever.
|
|
106
|
+
*/
|
|
107
|
+
const UNRECOVERABLE_CONNECT_CODES = new Set([
|
|
108
|
+
"28P01", // invalid_password
|
|
109
|
+
"28000", // invalid_authorization_specification
|
|
110
|
+
"3D000", // invalid_catalog_name — the database does not exist
|
|
111
|
+
"42501" // insufficient_privilege
|
|
112
|
+
]);
|
|
113
|
+
|
|
114
|
+
export interface ConnectFailure {
|
|
115
|
+
/** True when retrying cannot help: the connection string itself is wrong. */
|
|
116
|
+
fatal: boolean;
|
|
117
|
+
/** The deepest message available — the Postgres one where there is one. */
|
|
118
|
+
reason: string;
|
|
119
|
+
/** The `SQLSTATE`, when the failure came from Postgres rather than the socket. */
|
|
120
|
+
code?: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Describe a failed connection attempt in terms a developer can act on.
|
|
125
|
+
*
|
|
126
|
+
* The error a caller catches is Drizzle's wrapper: its message is
|
|
127
|
+
* `Failed query: SELECT 1` and its stack runs through drizzle internals, while
|
|
128
|
+
* the sentence that says what is actually wrong — "password authentication
|
|
129
|
+
* failed for user …", "database … does not exist" — sits in `.cause`. Logging
|
|
130
|
+
* the wrapper, as the bootstrapper used to, tells a developer with a typo in
|
|
131
|
+
* their `DATABASE_URL` nothing at all.
|
|
132
|
+
*/
|
|
133
|
+
export function classifyConnectFailure(error: unknown): ConnectFailure {
|
|
134
|
+
const pgError = extractPgError(error);
|
|
135
|
+
const reason =
|
|
136
|
+
pgError?.message ??
|
|
137
|
+
extractCauseMessage(error) ??
|
|
138
|
+
(error instanceof Error ? error.message : String(error));
|
|
139
|
+
return {
|
|
140
|
+
fatal: Boolean(pgError?.code && UNRECOVERABLE_CONNECT_CODES.has(pgError.code)),
|
|
141
|
+
reason,
|
|
142
|
+
code: pgError?.code
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
100
146
|
/**
|
|
101
147
|
* Detect whether an error is specifically a role-switching permission failure
|
|
102
148
|
* (e.g. "permission denied to set role" or "must be member of role"),
|
package/src/websocket.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { RealtimeService } from "./services/realtimeService";
|
|
2
2
|
import { PostgresBackendDriver } from "./PostgresBackendDriver";
|
|
3
3
|
import type { DataDriver, DeleteProps, FetchCollectionProps, FetchOneProps, SaveProps, TableMetadata, BranchInfo, AuthAdapter } from "@rebasepro/types";
|
|
4
|
-
import { isSQLAdmin, isSchemaAdmin } from "@rebasepro/types";
|
|
4
|
+
import { ANONYMOUS_USER_ID, isSQLAdmin, isSchemaAdmin } from "@rebasepro/types";
|
|
5
5
|
import type { User } from "@rebasepro/types";
|
|
6
6
|
|
|
7
7
|
import { WebSocketServer, WebSocket } from "ws";
|
|
8
8
|
import { Server } from "http";
|
|
9
9
|
import { inspect } from "util";
|
|
10
|
-
import { extractUserFromToken, AccessTokenPayload, safeCompare } from "@rebasepro/server";
|
|
10
|
+
import { extractUserFromToken, AccessTokenPayload, safeCompare, resolveRequireAuth } from "@rebasepro/server";
|
|
11
11
|
import { logger } from "@rebasepro/server";
|
|
12
12
|
|
|
13
13
|
/** Minimal subset of RebaseAuthConfig used by the WebSocket layer. */
|
|
@@ -116,11 +116,20 @@ export function createPostgresWebSocket(
|
|
|
116
116
|
logger.error("❌ [WebSocket Server] Error", { error: err });
|
|
117
117
|
});
|
|
118
118
|
|
|
119
|
-
//
|
|
120
|
-
//
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
119
|
+
// The same predicate the HTTP data routes use, from the same function —
|
|
120
|
+
// this socket is the other enforcement point for one product decision, and
|
|
121
|
+
// while it computed the answer itself it computed a different one. See
|
|
122
|
+
// `resolveRequireAuth` for what its local copy got wrong and why a `false`
|
|
123
|
+
// here grants access rather than skipping a check.
|
|
124
|
+
const requireAuth = !!authAdapter || resolveRequireAuth(authConfig as never);
|
|
125
|
+
|
|
126
|
+
if (requireAuth && !authAdapter && !authConfig?.jwtSecret && !authConfig?.serviceKey) {
|
|
127
|
+
logger.warn(
|
|
128
|
+
"🔐 [WebSocket Server] Authentication is required but no adapter, jwtSecret or " +
|
|
129
|
+
"serviceKey is configured — no client can complete AUTH, so every realtime " +
|
|
130
|
+
"message will be refused with UNAUTHORIZED."
|
|
131
|
+
);
|
|
132
|
+
}
|
|
124
133
|
|
|
125
134
|
wss.on("connection", (ws) => {
|
|
126
135
|
const clientId = `client_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
|
@@ -281,7 +290,7 @@ roles: verifiedUser.roles }
|
|
|
281
290
|
roles: session.user.roles ?? []
|
|
282
291
|
}
|
|
283
292
|
: {
|
|
284
|
-
uid:
|
|
293
|
+
uid: ANONYMOUS_USER_ID,
|
|
285
294
|
displayName: null,
|
|
286
295
|
email: null,
|
|
287
296
|
photoURL: null,
|
|
@@ -620,7 +629,7 @@ colors: true }));
|
|
|
620
629
|
const authContext = session?.user
|
|
621
630
|
? { uid: session.user.uid,
|
|
622
631
|
roles: session.user.roles ?? [] }
|
|
623
|
-
: { uid:
|
|
632
|
+
: { uid: ANONYMOUS_USER_ID,
|
|
624
633
|
roles: ["anon"] };
|
|
625
634
|
// Let RealtimeService handle these messages
|
|
626
635
|
await realtimeService.handleClientMessage(clientId, {
|