@rebasepro/server-postgres 0.9.1-canary.73476f2 → 0.9.1-canary.7ba0e49
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 +25 -2
- package/dist/PostgresBootstrapper.d.ts +10 -0
- package/dist/auth/services.d.ts +16 -0
- 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 +1445 -571
- package/dist/index.es.js.map +1 -1
- package/dist/schema/doctor.d.ts +1 -1
- package/dist/services/FetchService.d.ts +4 -24
- package/dist/services/PersistService.d.ts +27 -1
- package/dist/services/RelationService.d.ts +34 -1
- 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 +7 -0
- package/dist/services/row-pipeline.d.ts +63 -0
- package/package.json +11 -9
- package/src/PostgresBackendDriver.ts +127 -13
- package/src/PostgresBootstrapper.ts +62 -25
- package/src/auth/ensure-tables.ts +73 -11
- package/src/auth/services.ts +49 -19
- 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/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.ts +19 -2
- package/src/services/BranchService.ts +42 -10
- package/src/services/FetchService.ts +65 -270
- package/src/services/PersistService.ts +130 -14
- package/src/services/RelationService.ts +153 -94
- 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 +40 -19
- package/src/services/row-pipeline.ts +215 -0
- package/src/utils/drizzle-conditions.ts +13 -0
- package/src/websocket.ts +4 -1
- package/dist/schema/auth-default-policies.d.ts +0 -10
- package/src/schema/auth-default-policies.ts +0 -132
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Implements the `BackendBootstrapper` interface for PostgreSQL.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { Relations, sql } from "drizzle-orm";
|
|
8
8
|
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
9
9
|
import { PgEnum, PgTable } from "drizzle-orm/pg-core";
|
|
10
10
|
import type { RebasePgTable } from "./types";
|
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
} from "@rebasepro/types";
|
|
22
22
|
import { PostgresBackendDriver } from "./PostgresBackendDriver";
|
|
23
23
|
import { RealtimeService } from "./services/realtimeService";
|
|
24
|
+
import { buildCollectionRegistry } from "./collections/buildRegistry";
|
|
24
25
|
import { DatabasePoolManager } from "./databasePoolManager";
|
|
25
26
|
import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
|
|
26
27
|
import { createEmailService, type EmailConfig, type EmailService, logger } from "@rebasepro/server";
|
|
@@ -64,6 +65,15 @@ export interface PostgresDriverInternals {
|
|
|
64
65
|
realtimeService: RealtimeService;
|
|
65
66
|
driver: PostgresBackendDriver;
|
|
66
67
|
poolManager?: DatabasePoolManager;
|
|
68
|
+
/**
|
|
69
|
+
* Attach CDC triggers to tables that did not exist when the driver
|
|
70
|
+
* bootstrapped. Only set when database-level capture is actually active.
|
|
71
|
+
*
|
|
72
|
+
* Auth owns its own tables and creates them later in boot, so at driver
|
|
73
|
+
* bootstrap they are legitimately missing and get skipped; without this
|
|
74
|
+
* they would stay uninstrumented until the next restart.
|
|
75
|
+
*/
|
|
76
|
+
provisionCdcForTables?: (tables: CdcTableRef[]) => Promise<void>;
|
|
67
77
|
}
|
|
68
78
|
|
|
69
79
|
// Re-export from shared CLI error utilities
|
|
@@ -161,30 +171,17 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
|
|
|
161
171
|
}
|
|
162
172
|
|
|
163
173
|
const activeCollections = introspectedCollections ?? collections;
|
|
164
|
-
|
|
165
|
-
// Create a fresh registry for this driver
|
|
166
|
-
const registry = new PostgresCollectionRegistry();
|
|
167
|
-
if (activeCollections) {
|
|
168
|
-
registry.registerMultiple(activeCollections);
|
|
169
|
-
logger.info(`📋 [PostgresRegistry] Registered ${registry.getCollections().length} collections: [${registry.getCollections().map(c => c.slug).join(", ")}]`);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
174
|
const schemaTables = introspectedTables ?? pgConfig.schema?.tables;
|
|
173
|
-
|
|
174
|
-
// Register tables
|
|
175
|
-
if (schemaTables) {
|
|
176
|
-
Object.values(schemaTables).forEach((table) => {
|
|
177
|
-
if (isTable(table)) {
|
|
178
|
-
const tableName = getTableName(table);
|
|
179
|
-
registry.registerTable(table as PgTable, tableName);
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (pgConfig.schema?.enums) registry.registerEnums(pgConfig.schema.enums as Record<string, PgEnum<[string, ...string[]]>>);
|
|
185
|
-
|
|
186
175
|
const schemaRelations = introspectedRelations ?? (pgConfig.schema?.relations as Record<string, Relations> | undefined);
|
|
187
|
-
|
|
176
|
+
|
|
177
|
+
// Create a fresh registry for this driver. Registration order is
|
|
178
|
+
// load-bearing, so it lives in one place — see `buildCollectionRegistry`.
|
|
179
|
+
const registry = buildCollectionRegistry({
|
|
180
|
+
collections: activeCollections,
|
|
181
|
+
tables: schemaTables,
|
|
182
|
+
enums: pgConfig.schema?.enums as Record<string, PgEnum<[string, ...string[]]>> | undefined,
|
|
183
|
+
relations: schemaRelations
|
|
184
|
+
});
|
|
188
185
|
|
|
189
186
|
// Patch Drizzle's PgArray columns to handle NULL values safely.
|
|
190
187
|
// Drizzle's mapFromDriverValue crashes with "value.map is not a function"
|
|
@@ -346,6 +343,7 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
|
|
|
346
343
|
const wantsCdc = cdcMode !== "off";
|
|
347
344
|
const explicitCdc = cdcMode === "trigger" || cdcMode === "wal";
|
|
348
345
|
let cdcEnabled = false;
|
|
346
|
+
let provisionCdcForTables: PostgresDriverInternals["provisionCdcForTables"];
|
|
349
347
|
|
|
350
348
|
if (wantsCdc && !directUrl) {
|
|
351
349
|
const reason = "no direct database connection is available for the realtime LISTEN client (set DATABASE_DIRECT_URL)";
|
|
@@ -379,6 +377,11 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
|
|
|
379
377
|
await provisionTriggerCdc(cdcRunSql, cdcTables);
|
|
380
378
|
await realtimeService.enableCdc(directUrl);
|
|
381
379
|
cdcEnabled = true;
|
|
380
|
+
// Boot steps that create their own tables (auth) run after
|
|
381
|
+
// this one and use it to instrument what they just created.
|
|
382
|
+
provisionCdcForTables = async (tables) => {
|
|
383
|
+
await provisionTriggerCdc(cdcRunSql, tables);
|
|
384
|
+
};
|
|
382
385
|
logger.info(
|
|
383
386
|
`📡 [CDC] Realtime source = database-level change capture (mode: ${cdcMode === "wal" ? "wal→trigger" : "trigger"}). ` +
|
|
384
387
|
`All writes now emit realtime events regardless of origin.`
|
|
@@ -429,6 +432,14 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
|
|
|
429
432
|
);
|
|
430
433
|
const missing: Array<{ slug: string; table: string }> = [];
|
|
431
434
|
for (const col of registeredCollections) {
|
|
435
|
+
// Auth owns its table and creates it later in this same
|
|
436
|
+
// boot (initializeAuth → ensureAuthTablesExist), so it is
|
|
437
|
+
// legitimately absent right now. Reporting it as drift
|
|
438
|
+
// tells the user to `db:push` a table that is about to
|
|
439
|
+
// exist — and on an introspected database, one that the
|
|
440
|
+
// database was never supposed to hold.
|
|
441
|
+
if ((col as { auth?: { enabled?: boolean } }).auth?.enabled) continue;
|
|
442
|
+
|
|
432
443
|
const schemaName = "schema" in col && col.schema ? col.schema : "public";
|
|
433
444
|
const tableName = registry.hasTableForCollection(
|
|
434
445
|
col.table ?? col.slug
|
|
@@ -443,8 +454,10 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
|
|
|
443
454
|
const checkName = resolvedTable ?? tableName;
|
|
444
455
|
const fullCheckName = schemaName === "public" ? checkName : `${schemaName}.${checkName}`;
|
|
445
456
|
if (!dbTables.has(fullCheckName)) {
|
|
457
|
+
// Report what was actually looked up: an unqualified
|
|
458
|
+
// "users" sends people hunting for public.users.
|
|
446
459
|
missing.push({ slug: col.slug,
|
|
447
|
-
table:
|
|
460
|
+
table: fullCheckName });
|
|
448
461
|
}
|
|
449
462
|
}
|
|
450
463
|
if (missing.length > 0) {
|
|
@@ -478,7 +491,8 @@ table: checkName });
|
|
|
478
491
|
registry,
|
|
479
492
|
realtimeService,
|
|
480
493
|
driver,
|
|
481
|
-
poolManager
|
|
494
|
+
poolManager,
|
|
495
|
+
provisionCdcForTables
|
|
482
496
|
};
|
|
483
497
|
|
|
484
498
|
return {
|
|
@@ -507,6 +521,29 @@ table: checkName });
|
|
|
507
521
|
// ensureAuthTablesExist works with the collection abstraction — no Drizzle leakage.
|
|
508
522
|
await ensureAuthTablesExist(db, authCollection);
|
|
509
523
|
|
|
524
|
+
// The driver bootstrapped before these tables existed, so CDC skipped
|
|
525
|
+
// them. Instrument them now, or writes to the user table emit no
|
|
526
|
+
// realtime events until the next restart.
|
|
527
|
+
if (authCollection && internals.provisionCdcForTables) {
|
|
528
|
+
const authSchema = "schema" in authCollection && typeof authCollection.schema === "string"
|
|
529
|
+
? authCollection.schema
|
|
530
|
+
: "rebase";
|
|
531
|
+
const authTable = "table" in authCollection && typeof authCollection.table === "string"
|
|
532
|
+
? authCollection.table
|
|
533
|
+
: authCollection.slug;
|
|
534
|
+
if (authTable) {
|
|
535
|
+
try {
|
|
536
|
+
await internals.provisionCdcForTables([{ schema: authSchema, table: authTable }]);
|
|
537
|
+
} catch (err) {
|
|
538
|
+
logger.warn(
|
|
539
|
+
`⚠️ [CDC] Could not attach change-capture to the auth table "${authSchema}.${authTable}" — ` +
|
|
540
|
+
"writes to it won't emit database-level events.",
|
|
541
|
+
{ detail: err instanceof Error ? err.message : String(err) }
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
510
547
|
let emailService: EmailService | undefined;
|
|
511
548
|
if (authConfig.email) {
|
|
512
549
|
emailService = createEmailService(authConfig.email as EmailConfig);
|
|
@@ -251,17 +251,32 @@ export async function ensureAuthTablesExist(db: NodePgDatabase, collection?: Col
|
|
|
251
251
|
// Seed default roles if none exist
|
|
252
252
|
// (no-op: roles are now stored inline on the users table)
|
|
253
253
|
|
|
254
|
-
// ── Migration:
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
//
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
254
|
+
// ── Migration: reconcile the full users column set (safe for existing tables) ──
|
|
255
|
+
// CREATE TABLE IF NOT EXISTS never revisits an existing table, so a
|
|
256
|
+
// database provisioned by an older framework era is missing every
|
|
257
|
+
// column added since. Each column the auth services read or write must
|
|
258
|
+
// be back-filled here, or upgraded deployments break on the first
|
|
259
|
+
// statement that references it. `email` is deliberately absent: it has
|
|
260
|
+
// existed since the first era and cannot be added NOT NULL safely.
|
|
261
|
+
const userColumnBackfills = [
|
|
262
|
+
"display_name VARCHAR(255)",
|
|
263
|
+
"photo_url VARCHAR(500)",
|
|
264
|
+
"roles TEXT[] DEFAULT '{}' NOT NULL",
|
|
265
|
+
"password_hash VARCHAR(255)",
|
|
266
|
+
"email_verified BOOLEAN DEFAULT FALSE NOT NULL",
|
|
267
|
+
"email_verification_token VARCHAR(255)",
|
|
268
|
+
"email_verification_sent_at TIMESTAMP WITH TIME ZONE",
|
|
269
|
+
"is_anonymous BOOLEAN DEFAULT FALSE NOT NULL",
|
|
270
|
+
"metadata JSONB DEFAULT '{}' NOT NULL",
|
|
271
|
+
"created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL",
|
|
272
|
+
"updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL"
|
|
273
|
+
];
|
|
274
|
+
for (const columnDef of userColumnBackfills) {
|
|
275
|
+
await db.execute(sql`
|
|
276
|
+
ALTER TABLE ${sql.raw(usersTableName)}
|
|
277
|
+
ADD COLUMN IF NOT EXISTS ${sql.raw(columnDef)}
|
|
278
|
+
`);
|
|
279
|
+
}
|
|
265
280
|
|
|
266
281
|
// ── Migration: Copy roles from legacy junction table to inline column ──
|
|
267
282
|
// If the old rebase.user_roles and rebase.roles tables exist, migrate
|
|
@@ -358,6 +373,53 @@ export async function ensureAuthTablesExist(db: NodePgDatabase, collection?: Col
|
|
|
358
373
|
ON ${sql.raw(recoveryCodesTableName)}(user_id)
|
|
359
374
|
`);
|
|
360
375
|
|
|
376
|
+
// ── Migration: clear stale FORCE ROW LEVEL SECURITY (older RLS model) ──
|
|
377
|
+
// The current model never emits FORCE: privileged auth writes run as
|
|
378
|
+
// the table owner and rely on the owner bypassing plain ENABLE RLS
|
|
379
|
+
// (see generate-postgres-ddl-logic). A table still carrying FORCE from
|
|
380
|
+
// an older framework era binds the owner too, so the first user
|
|
381
|
+
// registration after an upgrade fails with SQLSTATE 42501. Reconcile
|
|
382
|
+
// on boot; only tables actually flagged get the ALTER (and its lock).
|
|
383
|
+
try {
|
|
384
|
+
const authTablePairs: [string, string][] = [
|
|
385
|
+
[usersSchema, resolvedTable],
|
|
386
|
+
[authSchema, "user_identities"],
|
|
387
|
+
[authSchema, "refresh_tokens"],
|
|
388
|
+
[authSchema, "password_reset_tokens"],
|
|
389
|
+
[authSchema, "app_config"],
|
|
390
|
+
[authSchema, "mfa_factors"],
|
|
391
|
+
[authSchema, "mfa_challenges"],
|
|
392
|
+
[authSchema, "recovery_codes"]
|
|
393
|
+
];
|
|
394
|
+
for (const [schemaName, tableName] of authTablePairs) {
|
|
395
|
+
const forced = await db.execute(sql`
|
|
396
|
+
SELECT 1
|
|
397
|
+
FROM pg_class c
|
|
398
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
399
|
+
WHERE n.nspname = ${schemaName}
|
|
400
|
+
AND c.relname = ${tableName}
|
|
401
|
+
AND c.relforcerowsecurity
|
|
402
|
+
`);
|
|
403
|
+
if (forced.rows.length > 0) {
|
|
404
|
+
await db.execute(sql`
|
|
405
|
+
ALTER TABLE ${sql.raw(`"${schemaName}"."${tableName}"`)}
|
|
406
|
+
NO FORCE ROW LEVEL SECURITY
|
|
407
|
+
`);
|
|
408
|
+
logger.warn(
|
|
409
|
+
`🔧 Cleared stale FORCE ROW LEVEL SECURITY on "${schemaName}"."${tableName}" ` +
|
|
410
|
+
"(legacy RLS model — it binds the owner connection and breaks privileged auth writes)"
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
} catch (rlsReconcileError: unknown) {
|
|
415
|
+
// Non-fatal: the connection may lack ownership on a pre-provisioned
|
|
416
|
+
// table; registration will still fail loudly (42501) if FORCE remains.
|
|
417
|
+
logger.warn(
|
|
418
|
+
`⚠️ Could not reconcile FORCE ROW LEVEL SECURITY on auth tables: ` +
|
|
419
|
+
`${rlsReconcileError instanceof Error ? rlsReconcileError.message : String(rlsReconcileError)}`
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
|
|
361
423
|
logger.info("✅ Auth tables ready");
|
|
362
424
|
} catch (error) {
|
|
363
425
|
logger.error("❌ Failed to create auth tables", { error });
|
package/src/auth/services.ts
CHANGED
|
@@ -82,6 +82,32 @@ export class UserService implements UserRepository {
|
|
|
82
82
|
return `"${schema}"."${name}"`;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Run a privileged auth write with an explicitly cleared RLS context.
|
|
87
|
+
*
|
|
88
|
+
* The auth services run on the base/owner connection, which by design
|
|
89
|
+
* carries a NULL `app.user_id` so the `auth.uid() IS NULL` server-escape
|
|
90
|
+
* in the default policies applies. That NULL is normally guaranteed by
|
|
91
|
+
* `set_config(..., is_local = true)` resetting at transaction end — but a
|
|
92
|
+
* GUC that survives on a pooled connection (or a connection role that
|
|
93
|
+
* doesn't bypass RLS: FORCE ROW LEVEL SECURITY, or a non-owner role)
|
|
94
|
+
* turns the trusted write into an RLS-scoped one and denies it with
|
|
95
|
+
* SQLSTATE 42501. Clearing the GUCs here, transaction-locally at the
|
|
96
|
+
* single chokepoint, makes the server context deterministic instead of
|
|
97
|
+
* trusting whatever state the pool hands us. `auth.uid()` reads '' as
|
|
98
|
+
* NULL via NULLIF, so '' is the server context.
|
|
99
|
+
*/
|
|
100
|
+
private async withServerContext<T>(fn: (db: NodePgDatabase) => Promise<T>): Promise<T> {
|
|
101
|
+
return await this.db.transaction(async (tx) => {
|
|
102
|
+
await tx.execute(sql`
|
|
103
|
+
SELECT set_config('app.user_id', '', true),
|
|
104
|
+
set_config('app.user_roles', '', true),
|
|
105
|
+
set_config('app.jwt', '', true)
|
|
106
|
+
`);
|
|
107
|
+
return await fn(tx as unknown as NodePgDatabase);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
85
111
|
private mapRowToUser(row: Record<string, unknown>): UserData {
|
|
86
112
|
if (!row) return row as UserData;
|
|
87
113
|
|
|
@@ -200,7 +226,9 @@ export class UserService implements UserRepository {
|
|
|
200
226
|
|
|
201
227
|
async createUser(data: CreateUserData): Promise<UserData> {
|
|
202
228
|
const payload = this.mapPayload(data);
|
|
203
|
-
const [row] =
|
|
229
|
+
const [row] = await this.withServerContext(async (db) =>
|
|
230
|
+
(await db.insert(this.usersTable).values(payload).returning()) as Record<string, unknown>[]
|
|
231
|
+
);
|
|
204
232
|
return this.mapRowToUser(row);
|
|
205
233
|
}
|
|
206
234
|
|
|
@@ -255,12 +283,12 @@ export class UserService implements UserRepository {
|
|
|
255
283
|
}
|
|
256
284
|
|
|
257
285
|
async linkUserIdentity(userId: string, provider: string, providerId: string, profileData?: Record<string, unknown>): Promise<void> {
|
|
258
|
-
await this.db.insert(this.userIdentitiesTable).values({
|
|
286
|
+
await this.withServerContext(async (db) => db.insert(this.userIdentitiesTable).values({
|
|
259
287
|
userId,
|
|
260
288
|
provider,
|
|
261
289
|
providerId,
|
|
262
290
|
profileData: profileData || null
|
|
263
|
-
}).onConflictDoNothing({ target: [this.userIdentitiesTable.provider, this.userIdentitiesTable.providerId] });
|
|
291
|
+
}).onConflictDoNothing({ target: [this.userIdentitiesTable.provider, this.userIdentitiesTable.providerId] }));
|
|
264
292
|
}
|
|
265
293
|
|
|
266
294
|
async updateUser(id: string, data: Partial<Omit<CreateUserData, "id">>): Promise<UserData | null> {
|
|
@@ -270,18 +298,20 @@ export class UserService implements UserRepository {
|
|
|
270
298
|
const updatedAtKey = getColumnKey(this.usersTable, "updatedAt", "updated_at") || "updatedAt";
|
|
271
299
|
payload[updatedAtKey] = new Date();
|
|
272
300
|
|
|
273
|
-
const [row] =
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
301
|
+
const [row] = await this.withServerContext(async (db) =>
|
|
302
|
+
(await db
|
|
303
|
+
.update(this.usersTable)
|
|
304
|
+
.set(payload)
|
|
305
|
+
.where(eq(idCol, id))
|
|
306
|
+
.returning()) as Record<string, unknown>[]
|
|
307
|
+
);
|
|
278
308
|
return row ? this.mapRowToUser(row) : null;
|
|
279
309
|
}
|
|
280
310
|
|
|
281
311
|
async deleteUser(id: string): Promise<void> {
|
|
282
312
|
const idCol = getColumn(this.usersTable, "id");
|
|
283
313
|
if (!idCol) return;
|
|
284
|
-
await this.db.delete(this.usersTable).where(eq(idCol, id));
|
|
314
|
+
await this.withServerContext(async (db) => db.delete(this.usersTable).where(eq(idCol, id)));
|
|
285
315
|
}
|
|
286
316
|
|
|
287
317
|
async listUsers(): Promise<UserData[]> {
|
|
@@ -357,13 +387,13 @@ export class UserService implements UserRepository {
|
|
|
357
387
|
const passwordHashColKey = getColumnKey(this.usersTable, "passwordHash", "password_hash") || "passwordHash";
|
|
358
388
|
const updatedAtColKey = getColumnKey(this.usersTable, "updatedAt", "updated_at") || "updatedAt";
|
|
359
389
|
|
|
360
|
-
await this.db
|
|
390
|
+
await this.withServerContext(async (db) => db
|
|
361
391
|
.update(this.usersTable)
|
|
362
392
|
.set({
|
|
363
393
|
[passwordHashColKey]: passwordHash,
|
|
364
394
|
[updatedAtColKey]: new Date()
|
|
365
395
|
})
|
|
366
|
-
.where(eq(idCol, id));
|
|
396
|
+
.where(eq(idCol, id)));
|
|
367
397
|
}
|
|
368
398
|
|
|
369
399
|
/**
|
|
@@ -376,14 +406,14 @@ export class UserService implements UserRepository {
|
|
|
376
406
|
const emailVerificationTokenColKey = getColumnKey(this.usersTable, "emailVerificationToken", "email_verification_token") || "emailVerificationToken";
|
|
377
407
|
const updatedAtColKey = getColumnKey(this.usersTable, "updatedAt", "updated_at") || "updatedAt";
|
|
378
408
|
|
|
379
|
-
await this.db
|
|
409
|
+
await this.withServerContext(async (db) => db
|
|
380
410
|
.update(this.usersTable)
|
|
381
411
|
.set({
|
|
382
412
|
[emailVerifiedColKey]: verified,
|
|
383
413
|
[emailVerificationTokenColKey]: null,
|
|
384
414
|
[updatedAtColKey]: new Date()
|
|
385
415
|
})
|
|
386
|
-
.where(eq(idCol, id));
|
|
416
|
+
.where(eq(idCol, id)));
|
|
387
417
|
}
|
|
388
418
|
|
|
389
419
|
/**
|
|
@@ -396,14 +426,14 @@ export class UserService implements UserRepository {
|
|
|
396
426
|
const emailVerificationSentAtColKey = getColumnKey(this.usersTable, "emailVerificationSentAt", "email_verification_sent_at") || "emailVerificationSentAt";
|
|
397
427
|
const updatedAtColKey = getColumnKey(this.usersTable, "updatedAt", "updated_at") || "updatedAt";
|
|
398
428
|
|
|
399
|
-
await this.db
|
|
429
|
+
await this.withServerContext(async (db) => db
|
|
400
430
|
.update(this.usersTable)
|
|
401
431
|
.set({
|
|
402
432
|
[emailVerificationTokenColKey]: token,
|
|
403
433
|
[emailVerificationSentAtColKey]: token ? new Date() : null,
|
|
404
434
|
[updatedAtColKey]: new Date()
|
|
405
435
|
})
|
|
406
|
-
.where(eq(idCol, id));
|
|
436
|
+
.where(eq(idCol, id)));
|
|
407
437
|
}
|
|
408
438
|
|
|
409
439
|
/**
|
|
@@ -463,11 +493,11 @@ export class UserService implements UserRepository {
|
|
|
463
493
|
async setUserRoles(userId: string, roleIds: string[]): Promise<void> {
|
|
464
494
|
const usersTableName = this.getQualifiedUsersTableName();
|
|
465
495
|
const rolesArray = `{${roleIds.join(",")}}`;
|
|
466
|
-
await this.db.execute(sql`
|
|
496
|
+
await this.withServerContext(async (db) => db.execute(sql`
|
|
467
497
|
UPDATE ${sql.raw(usersTableName)}
|
|
468
498
|
SET roles = ${rolesArray}::text[], updated_at = NOW()
|
|
469
499
|
WHERE id = ${userId}
|
|
470
|
-
`);
|
|
500
|
+
`));
|
|
471
501
|
}
|
|
472
502
|
|
|
473
503
|
/**
|
|
@@ -475,11 +505,11 @@ export class UserService implements UserRepository {
|
|
|
475
505
|
*/
|
|
476
506
|
async assignDefaultRole(userId: string, roleId: string): Promise<void> {
|
|
477
507
|
const usersTableName = this.getQualifiedUsersTableName();
|
|
478
|
-
await this.db.execute(sql`
|
|
508
|
+
await this.withServerContext(async (db) => db.execute(sql`
|
|
479
509
|
UPDATE ${sql.raw(usersTableName)}
|
|
480
510
|
SET roles = array_append(roles, ${roleId}), updated_at = NOW()
|
|
481
511
|
WHERE id = ${userId} AND NOT (${roleId} = ANY(roles))
|
|
482
|
-
`);
|
|
512
|
+
`));
|
|
483
513
|
}
|
|
484
514
|
|
|
485
515
|
/**
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { isTable, getTableName, Relations } from "drizzle-orm";
|
|
2
|
+
import { PgEnum, PgTable } from "drizzle-orm/pg-core";
|
|
3
|
+
import { CollectionConfig } from "@rebasepro/types";
|
|
4
|
+
import { logger } from "@rebasepro/server";
|
|
5
|
+
import { PostgresCollectionRegistry } from "./PostgresCollectionRegistry";
|
|
6
|
+
import { warnOnKeysTheAdminCannotResolve } from "../services/collection-helpers";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Everything a registry is built from: the collections, and the drizzle schema
|
|
10
|
+
* they are backed by. In BaaS mode all of it is introspected from the live
|
|
11
|
+
* database; in CMS mode it comes from the config and the generated schema.
|
|
12
|
+
*/
|
|
13
|
+
export interface RegistrySchema {
|
|
14
|
+
collections?: CollectionConfig[];
|
|
15
|
+
tables?: Record<string, unknown>;
|
|
16
|
+
enums?: Record<string, PgEnum<[string, ...string[]]>>;
|
|
17
|
+
relations?: Record<string, Relations>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Build the collection registry for a driver.
|
|
22
|
+
*
|
|
23
|
+
* The order matters and is the reason this is one function rather than a run of
|
|
24
|
+
* statements in the bootstrapper. Keys are resolved from the drizzle schema, so
|
|
25
|
+
* anything that inspects them has to run *after* the tables are registered —
|
|
26
|
+
* and `warnOnKeysTheAdminCannotResolve` fails open if it does not, because a
|
|
27
|
+
* collection whose table it cannot look up is one it has nothing to say about.
|
|
28
|
+
* Warned too early, it would skip every collection and report nothing, which
|
|
29
|
+
* reads exactly like having nothing to report.
|
|
30
|
+
*/
|
|
31
|
+
export function buildCollectionRegistry(schema: RegistrySchema): PostgresCollectionRegistry {
|
|
32
|
+
const registry = new PostgresCollectionRegistry();
|
|
33
|
+
|
|
34
|
+
if (schema.collections) {
|
|
35
|
+
registry.registerMultiple(schema.collections);
|
|
36
|
+
logger.info(
|
|
37
|
+
`📋 [PostgresRegistry] Registered ${registry.getCollections().length} collections: ` +
|
|
38
|
+
`[${registry.getCollections().map(c => c.slug).join(", ")}]`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (schema.tables) {
|
|
43
|
+
Object.values(schema.tables).forEach((table) => {
|
|
44
|
+
if (isTable(table)) {
|
|
45
|
+
registry.registerTable(table as PgTable, getTableName(table));
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (schema.enums) registry.registerEnums(schema.enums);
|
|
51
|
+
if (schema.relations) registry.registerRelations(schema.relations);
|
|
52
|
+
|
|
53
|
+
// Now that the keys resolve: say which of them the admin cannot see. It
|
|
54
|
+
// compiles the same collection files into its bundle but never the drizzle
|
|
55
|
+
// schema, and nothing serves it one, so only an edit to the config fixes it.
|
|
56
|
+
warnOnKeysTheAdminCannotResolve(registry.getCollections(), registry);
|
|
57
|
+
|
|
58
|
+
return registry;
|
|
59
|
+
}
|
package/src/connection.ts
CHANGED
|
@@ -27,11 +27,68 @@ const DEFAULT_POOL: Required<PostgresPoolConfig> = {
|
|
|
27
27
|
max: 20,
|
|
28
28
|
idleTimeoutMillis: 30_000,
|
|
29
29
|
connectionTimeoutMillis: 10_000,
|
|
30
|
-
|
|
30
|
+
// The client-side read timeout MUST be comfortably above the server-side
|
|
31
|
+
// statement_timeout. When the client timer fires first, node-postgres
|
|
32
|
+
// abandons the in-flight statement but keeps the connection — inside a
|
|
33
|
+
// transaction that leaves the tx open (and any pending ROLLBACK is
|
|
34
|
+
// spliced out of the client queue before it ever reaches the wire), so
|
|
35
|
+
// the pooled connection is returned still in-transaction with its RLS
|
|
36
|
+
// GUCs set. The server abort (SQLSTATE 57014) is the clean path; the
|
|
37
|
+
// client timeout is only a backstop for a dead network.
|
|
38
|
+
queryTimeout: 60_000,
|
|
31
39
|
statementTimeout: 30_000,
|
|
32
40
|
keepAlive: true
|
|
33
41
|
};
|
|
34
42
|
|
|
43
|
+
/** ReadyForQuery status byte: `I` idle, `T` in transaction, `E` failed transaction. */
|
|
44
|
+
const TX_IDLE = "I";
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Destroy pool clients that are released while still inside a transaction.
|
|
48
|
+
*
|
|
49
|
+
* pg-pool returns a client to the idle list whenever `release()` is called
|
|
50
|
+
* without an error — even if the connection is still mid-transaction (status
|
|
51
|
+
* `T`/`E`). That happens in practice: drizzle's pool transaction releases in
|
|
52
|
+
* a `finally` after attempting ROLLBACK, and if the ROLLBACK itself fails
|
|
53
|
+
* (e.g. it was queued behind a statement that hit the client-side
|
|
54
|
+
* query_timeout), the client goes back dirty. The next checkout then runs
|
|
55
|
+
* its statements inside the zombie transaction — with the previous request's
|
|
56
|
+
* `app.*` RLS GUCs still applied, which turns unrelated queries into
|
|
57
|
+
* RLS-scoped ones (observed in production as registration failing with
|
|
58
|
+
* SQLSTATE 42501 under a leaked anonymous context).
|
|
59
|
+
*
|
|
60
|
+
* pg-pool emits `release` before it consults its private `_expired` set, so
|
|
61
|
+
* marking the client expired here makes `_release()` destroy it instead of
|
|
62
|
+
* pooling it. Both `client._txStatus` (pg ≥ 8.16) and `pool._expired` are
|
|
63
|
+
* private APIs — feature-detect and fall back to loud logging so an upstream
|
|
64
|
+
* change degrades to observability, never to silent corruption.
|
|
65
|
+
*/
|
|
66
|
+
export function guardPoolAgainstDirtyRelease(pool: Pool, label: string): void {
|
|
67
|
+
pool.on("release", (err: Error | undefined, client: unknown) => {
|
|
68
|
+
if (err) return; // errored clients are already destroyed by pg-pool
|
|
69
|
+
const txStatus = (client as { _txStatus?: string | null })?._txStatus;
|
|
70
|
+
if (typeof txStatus !== "string" || txStatus === TX_IDLE) return;
|
|
71
|
+
|
|
72
|
+
// pg-pool keeps expired clients in a WeakSet (a plain object works too
|
|
73
|
+
// if upstream ever changes it — duck-type on add/has).
|
|
74
|
+
const expired = (pool as unknown as { _expired?: { add(c: object): unknown; has(c: object): boolean } })._expired;
|
|
75
|
+
if (expired && typeof expired.add === "function" && typeof expired.has === "function" && client && typeof client === "object") {
|
|
76
|
+
expired.add(client);
|
|
77
|
+
logger.error(
|
|
78
|
+
`[${label}] Client released back to the pool while still in a transaction ` +
|
|
79
|
+
`(status '${txStatus}') — destroying it so the open transaction and its ` +
|
|
80
|
+
`session state (RLS GUCs) cannot leak into the next request.`
|
|
81
|
+
);
|
|
82
|
+
} else {
|
|
83
|
+
logger.error(
|
|
84
|
+
`[${label}] Client released mid-transaction (status '${txStatus}') but the ` +
|
|
85
|
+
`pool's internal expiry set is unavailable (pg-pool internals changed?). ` +
|
|
86
|
+
`The connection may leak its open transaction into subsequent requests.`
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
35
92
|
/**
|
|
36
93
|
* Create a Drizzle-backed Postgres connection with a production-grade
|
|
37
94
|
* connection pool.
|
|
@@ -75,6 +132,7 @@ export function createPostgresDatabaseConnection(
|
|
|
75
132
|
logger.warn("[pg-pool] Connection timeout detected — pool will auto-retry");
|
|
76
133
|
}
|
|
77
134
|
});
|
|
135
|
+
guardPoolAgainstDirtyRelease(pool, "pg-pool");
|
|
78
136
|
|
|
79
137
|
// Create drizzle instance — pass schema when available to enable db.query relational API
|
|
80
138
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
@@ -118,6 +176,7 @@ export function createDirectDatabaseConnection(
|
|
|
118
176
|
pool.on("error", (err) => {
|
|
119
177
|
logger.error("[pg-direct-pool] Unexpected pool error", { detail: err.message });
|
|
120
178
|
});
|
|
179
|
+
guardPoolAgainstDirtyRelease(pool, "pg-direct-pool");
|
|
121
180
|
|
|
122
181
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
123
182
|
|
|
@@ -157,6 +216,7 @@ export function createReadReplicaConnection(
|
|
|
157
216
|
pool.on("error", (err) => {
|
|
158
217
|
logger.error("[pg-replica-pool] Unexpected pool error", { detail: err.message });
|
|
159
218
|
});
|
|
219
|
+
guardPoolAgainstDirtyRelease(pool, "pg-replica-pool");
|
|
160
220
|
|
|
161
221
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
162
222
|
|
package/src/data-transformer.ts
CHANGED
|
@@ -20,12 +20,19 @@ import { logger } from "@rebasepro/server";
|
|
|
20
20
|
export interface SerializedEntityData {
|
|
21
21
|
/** Scalar column values ready for INSERT/UPDATE. */
|
|
22
22
|
scalarData: Record<string, unknown>;
|
|
23
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* Inverse relation updates that must be applied to target tables.
|
|
25
|
+
*
|
|
26
|
+
* No address here: the row being written does not know its own. These are
|
|
27
|
+
* applied by `PersistService`, which addresses them with the id it holds —
|
|
28
|
+
* the one it was given for an update, or the one the INSERT returned — and
|
|
29
|
+
* that is the authority. This item used to carry a `currentId` derived from
|
|
30
|
+
* the *input* values, which nothing ever read.
|
|
31
|
+
*/
|
|
24
32
|
inverseRelationUpdates: Array<{
|
|
25
33
|
relationKey: string;
|
|
26
34
|
relation: Relation;
|
|
27
35
|
newValue: unknown;
|
|
28
|
-
currentId?: string | number;
|
|
29
36
|
}>;
|
|
30
37
|
/** JoinPath relation updates that require multi-hop writes. */
|
|
31
38
|
joinPathRelationUpdates: Array<{
|
|
@@ -105,7 +112,6 @@ joinPathRelationUpdates: [] };
|
|
|
105
112
|
relationKey: string;
|
|
106
113
|
relation: Relation;
|
|
107
114
|
newValue: unknown;
|
|
108
|
-
currentId?: string | number;
|
|
109
115
|
}> = [];
|
|
110
116
|
const joinPathRelationUpdates: Array<{
|
|
111
117
|
relationKey: string;
|
|
@@ -146,12 +152,10 @@ joinPathRelationUpdates: [] };
|
|
|
146
152
|
} else if (relation.direction === "inverse" && relation.foreignKeyOnTarget) {
|
|
147
153
|
// Inverse relation: Need to update the target table's FK
|
|
148
154
|
const serializedValue = serializePropertyToServer(effectiveValue, property);
|
|
149
|
-
const pks = getPrimaryKeys(collection, registry!);
|
|
150
155
|
inverseRelationUpdates.push({
|
|
151
156
|
relationKey: key,
|
|
152
157
|
relation,
|
|
153
|
-
newValue: serializedValue
|
|
154
|
-
currentId: (row.id as string | number | undefined) || buildCompositeId(row, pks)
|
|
158
|
+
newValue: serializedValue
|
|
155
159
|
});
|
|
156
160
|
// Don't add the original relation property to the result
|
|
157
161
|
continue;
|
|
@@ -170,12 +174,10 @@ joinPathRelationUpdates: [] };
|
|
|
170
174
|
});
|
|
171
175
|
} else {
|
|
172
176
|
// Many inverse joinPath: capture as inverse relation update
|
|
173
|
-
const pks = getPrimaryKeys(collection, registry!);
|
|
174
177
|
inverseRelationUpdates.push({
|
|
175
178
|
relationKey: key,
|
|
176
179
|
relation,
|
|
177
|
-
newValue: serializedValue
|
|
178
|
-
currentId: (row.id as string | number | undefined) || buildCompositeId(row, pks)
|
|
180
|
+
newValue: serializedValue
|
|
179
181
|
});
|
|
180
182
|
}
|
|
181
183
|
// Don't add the original relation property to the result
|
|
@@ -2,6 +2,7 @@ import { Pool } from "pg";
|
|
|
2
2
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
3
3
|
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
4
4
|
import { logger } from "@rebasepro/server";
|
|
5
|
+
import { guardPoolAgainstDirtyRelease } from "./connection";
|
|
5
6
|
|
|
6
7
|
export class DatabasePoolManager {
|
|
7
8
|
private pools: Map<string, Pool> = new Map();
|
|
@@ -50,6 +51,7 @@ export class DatabasePoolManager {
|
|
|
50
51
|
pool.on("error", (err) => {
|
|
51
52
|
logger.error(`[DatabasePoolManager] Unexpected error on idle client for db ${databaseName}`, { error: err });
|
|
52
53
|
});
|
|
54
|
+
guardPoolAgainstDirtyRelease(pool, `pg-pool:${databaseName}`);
|
|
53
55
|
|
|
54
56
|
this.pools.set(databaseName, pool);
|
|
55
57
|
return pool;
|