@aphexcms/postgresql-adapter 14.4.0 → 15.0.0
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/agent-change-set-adapter.d.ts.map +1 -1
- package/dist/agent-change-set-adapter.js +8 -1
- package/dist/agent-change-set-adapter.js.map +1 -1
- package/dist/asset-adapter.d.ts +25 -0
- package/dist/asset-adapter.d.ts.map +1 -1
- package/dist/asset-adapter.js +103 -30
- package/dist/asset-adapter.js.map +1 -1
- package/dist/document-adapter.d.ts +47 -4
- package/dist/document-adapter.d.ts.map +1 -1
- package/dist/document-adapter.js +216 -21
- package/dist/document-adapter.js.map +1 -1
- package/dist/event-job-adapter.d.ts +8 -1
- package/dist/event-job-adapter.d.ts.map +1 -1
- package/dist/event-job-adapter.js +60 -4
- package/dist/event-job-adapter.js.map +1 -1
- package/dist/index.d.ts +71 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +161 -31
- package/dist/index.js.map +1 -1
- package/dist/plugin-storage-adapter.js +1 -1
- package/dist/plugin-storage-adapter.js.map +1 -1
- package/dist/reference-adapter.d.ts.map +1 -1
- package/dist/reference-adapter.js +24 -1
- package/dist/reference-adapter.js.map +1 -1
- package/dist/schema.d.ts +372 -4
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +111 -36
- package/dist/schema.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -23,7 +23,26 @@ export declare class PostgreSQLAdapter implements DatabaseAdapter {
|
|
|
23
23
|
readonly hierarchyEnabled: boolean;
|
|
24
24
|
private readonly singleConnection;
|
|
25
25
|
private opQueue;
|
|
26
|
-
|
|
26
|
+
/**
|
|
27
|
+
* The single-connection transaction the *current async call chain* is running
|
|
28
|
+
* inside, if any.
|
|
29
|
+
*
|
|
30
|
+
* This used to be a plain `inSingleConnTx` boolean on the instance, which
|
|
31
|
+
* couldn't distinguish the two callers it needs to: a sub-adapter re-entering
|
|
32
|
+
* from within the running `fn()` (must reuse the held tx — opening another
|
|
33
|
+
* would deadlock on the one connection) versus an unrelated *concurrent*
|
|
34
|
+
* caller that merely arrived while the flag was set. The second one skipped
|
|
35
|
+
* the mutex and rode a stranger's transaction, so the moment that transaction
|
|
36
|
+
* committed its `SET LOCAL app.organization_id` was discarded and the leaked
|
|
37
|
+
* caller's remaining queries evaluated the RLS policy against `''` —
|
|
38
|
+
* "invalid input syntax for type uuid". Worse, its writes committed on
|
|
39
|
+
* someone else's boundary, which can split a state change from the outbox row
|
|
40
|
+
* that `withTransaction` exists to keep atomic with it.
|
|
41
|
+
*
|
|
42
|
+
* AsyncLocalStorage scopes that answer to the call chain, so re-entrancy is
|
|
43
|
+
* detected and concurrency queues on `runExclusive` as intended.
|
|
44
|
+
*/
|
|
45
|
+
private readonly txContext;
|
|
27
46
|
constructor(config: {
|
|
28
47
|
db: ReturnType<typeof drizzle>;
|
|
29
48
|
tables: CMSSchema;
|
|
@@ -93,6 +112,7 @@ export declare class PostgreSQLAdapter implements DatabaseAdapter {
|
|
|
93
112
|
createDocument(data: any): Promise<import("@aphexcms/cms-core").Document>;
|
|
94
113
|
updateDocDraft(organizationId: string, id: string, data: any, updatedBy?: string, expectedRevision?: number): Promise<import("@aphexcms/cms-core").Document | null>;
|
|
95
114
|
deleteDocById(organizationId: string, id: string): Promise<boolean>;
|
|
115
|
+
updateSearchText(organizationId: string, id: string, searchText: string): Promise<void>;
|
|
96
116
|
publishDoc(organizationId: string, id: string, expectedRevision?: number): Promise<import("@aphexcms/cms-core").Document | null>;
|
|
97
117
|
unpublishDoc(organizationId: string, id: string, expectedRevision?: number): Promise<import("@aphexcms/cms-core").Document | null>;
|
|
98
118
|
countDocsByType(organizationId: string, type: string): Promise<number>;
|
|
@@ -100,7 +120,16 @@ export declare class PostgreSQLAdapter implements DatabaseAdapter {
|
|
|
100
120
|
createAsset(data: any): Promise<import("@aphexcms/cms-core").Asset>;
|
|
101
121
|
findAssetById(organizationId: string, id: string): Promise<import("@aphexcms/cms-core").Asset | null>;
|
|
102
122
|
findAssetByIdGlobal(id: string): Promise<import("@aphexcms/cms-core").Asset | null>;
|
|
103
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Turn the caller's `includeChildOrganizations` request into the explicit set
|
|
125
|
+
* of organizations to search.
|
|
126
|
+
*
|
|
127
|
+
* Shared by `findAssets` and `countAssets` for the same reason the adapter's
|
|
128
|
+
* clause builder is shared: a page widened to the subtree next to a total that
|
|
129
|
+
* wasn't reports "1–20 of 4" over twenty rows.
|
|
130
|
+
*/
|
|
131
|
+
private resolveAssetOrgScope;
|
|
132
|
+
findAssets(organizationId: string, filters?: AssetFilters): Promise<import("@aphexcms/cms-core").Asset[]>;
|
|
104
133
|
updateAsset(organizationId: string, id: string, data: any): Promise<import("@aphexcms/cms-core").Asset | null>;
|
|
105
134
|
deleteAsset(organizationId: string, id: string): Promise<boolean>;
|
|
106
135
|
countAssets(organizationId: string, filters?: AssetFilters): Promise<number>;
|
|
@@ -164,6 +193,15 @@ export declare class PostgreSQLAdapter implements DatabaseAdapter {
|
|
|
164
193
|
allowUserOrgCreation: boolean;
|
|
165
194
|
}>;
|
|
166
195
|
updateInstanceSettings(settings: Record<string, any>): Promise<Record<string, any>>;
|
|
196
|
+
/**
|
|
197
|
+
* Atomically take the one-time bootstrap claim.
|
|
198
|
+
*
|
|
199
|
+
* The whole race is decided by the primary key: the row either inserts or it
|
|
200
|
+
* conflicts, and only one concurrent caller can see a row come back. No
|
|
201
|
+
* transaction, so nothing holds SQLite's write lock while the auth provider is
|
|
202
|
+
* doing its own inserts.
|
|
203
|
+
*/
|
|
204
|
+
tryClaimBootstrap(): Promise<boolean>;
|
|
167
205
|
getPluginSettings(organizationId: string, pluginId: string): Promise<Record<string, unknown> | null>;
|
|
168
206
|
setPluginSettings(organizationId: string, pluginId: string, values: Record<string, unknown>): Promise<void>;
|
|
169
207
|
/**
|
|
@@ -240,7 +278,20 @@ export declare class PostgreSQLAdapter implements DatabaseAdapter {
|
|
|
240
278
|
status: string | null;
|
|
241
279
|
}[]>;
|
|
242
280
|
countDocumentReferencesForAssets(organizationId: string, assetIds: string[], knownTypes?: string[]): Promise<Record<string, number>>;
|
|
243
|
-
|
|
281
|
+
replaceAssetReferences(organizationId: string, documentId: string, documentType: string, references: Array<{
|
|
282
|
+
assetId: string;
|
|
283
|
+
fieldPath: string;
|
|
284
|
+
plane: 'draft' | 'published';
|
|
285
|
+
}>): Promise<void>;
|
|
286
|
+
hasAnyAssetReferences(organizationId: string): Promise<boolean>;
|
|
287
|
+
countAssetReferencesForAssets(organizationId: string, assetIds: string[]): Promise<Record<string, number>>;
|
|
288
|
+
listStoredDocumentTypes(organizationId: string): Promise<string[]>;
|
|
289
|
+
findAssetReferenceFieldPaths(organizationId: string, assetId: string): Promise<{
|
|
290
|
+
documentId: string;
|
|
291
|
+
fieldPath: string;
|
|
292
|
+
plane: string;
|
|
293
|
+
}[]>;
|
|
294
|
+
clearAssetReferences(organizationId: string, assetId: string): Promise<number>;
|
|
244
295
|
createDocumentVersion(data: {
|
|
245
296
|
documentId: string;
|
|
246
297
|
organizationId: string;
|
|
@@ -259,6 +310,18 @@ export declare class PostgreSQLAdapter implements DatabaseAdapter {
|
|
|
259
310
|
listEvents(options: ListEventsOptions): Promise<import("@aphexcms/cms-core").Page<import("@aphexcms/cms-core").DomainEvent>>;
|
|
260
311
|
listUnprocessedOutbox(options: ListUnprocessedOutboxOptions): Promise<import("@aphexcms/cms-core").OutboxRow[]>;
|
|
261
312
|
markOutboxProcessed(organizationId: string, id: string): Promise<void>;
|
|
313
|
+
outboxHealth(options: {
|
|
314
|
+
organizationId?: string;
|
|
315
|
+
}): Promise<import("@aphexcms/cms-core").OutboxHealth>;
|
|
316
|
+
/**
|
|
317
|
+
* Run a read that is either org-scoped or deliberately instance-wide.
|
|
318
|
+
*
|
|
319
|
+
* The instance-wide branch mirrors `claimDueJobs`/`listUnprocessedOutbox`: no GUC to set,
|
|
320
|
+
* so RLS has to be bypassed explicitly rather than by leaving the org unset (which would
|
|
321
|
+
* match nothing and silently look like an empty result). Only reach this branch for callers
|
|
322
|
+
* that have already been authorized instance-wide — the worker, or a super admin.
|
|
323
|
+
*/
|
|
324
|
+
private withInstanceOrOrgContext;
|
|
262
325
|
createChangeSet(input: CreateAgentChangeSetInput): Promise<import("@aphexcms/cms-core").AgentChangeSet>;
|
|
263
326
|
recordOperation(input: RecordAgentOperationInput): Promise<import("@aphexcms/cms-core").AgentOperation>;
|
|
264
327
|
completeChangeSet(organizationId: string, id: string, input: CompleteAgentChangeSetInput): Promise<void>;
|
|
@@ -278,8 +341,13 @@ export declare class PostgreSQLAdapter implements DatabaseAdapter {
|
|
|
278
341
|
error: string;
|
|
279
342
|
}): Promise<void>;
|
|
280
343
|
cancelJob(organizationId: string, id: string): Promise<void>;
|
|
344
|
+
getJob(organizationId: string, id: string): Promise<import("@aphexcms/cms-core").Job | null>;
|
|
345
|
+
requeueJob(organizationId: string, id: string, options: {
|
|
346
|
+
runAt: Date;
|
|
347
|
+
}): Promise<import("@aphexcms/cms-core").Job | null>;
|
|
281
348
|
listJobs(options: ListJobsOptions): Promise<import("@aphexcms/cms-core").Page<import("@aphexcms/cms-core").Job>>;
|
|
282
349
|
withTransaction<T>(fn: (adapter: any) => Promise<T>): Promise<T>;
|
|
350
|
+
private runTransaction;
|
|
283
351
|
disconnect(): Promise<void>;
|
|
284
352
|
isHealthy(): Promise<boolean>;
|
|
285
353
|
}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAKvD,OAAO,KAAK,EACX,eAAe,EACf,gBAAgB,EAChB,WAAW,EACX,YAAY,EACZ,qBAAqB,EAC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AAiE;QACjE,eAAe,CAAC,EAAE,OAAO,CAAC;KAC1B,CAAC;CACF;AAoDD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,gBAAgB,GAAG,gBAAgB,CAEnF"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
// PostgreSQL adapter - combines document and asset adapters
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
1
3
|
import postgres from 'postgres';
|
|
2
4
|
import { drizzle as drizzlePostgres } from 'drizzle-orm/postgres-js';
|
|
3
5
|
import { sql } from 'drizzle-orm';
|
|
4
6
|
import { eq, and } from 'drizzle-orm';
|
|
7
|
+
import { BOOTSTRAP_CLAIM_ID } from '@aphexcms/cms-core/server';
|
|
5
8
|
import { PostgreSQLDocumentAdapter } from './document-adapter.js';
|
|
6
9
|
import { PostgreSQLAssetAdapter } from './asset-adapter.js';
|
|
7
10
|
import { PostgreSQLUserProfileAdapter } from './user-adapter.js';
|
|
@@ -38,7 +41,26 @@ export class PostgreSQLAdapter {
|
|
|
38
41
|
// and serializes via a mutex so the rebind is safe. See withOrgContext below.
|
|
39
42
|
singleConnection;
|
|
40
43
|
opQueue = Promise.resolve();
|
|
41
|
-
|
|
44
|
+
/**
|
|
45
|
+
* The single-connection transaction the *current async call chain* is running
|
|
46
|
+
* inside, if any.
|
|
47
|
+
*
|
|
48
|
+
* This used to be a plain `inSingleConnTx` boolean on the instance, which
|
|
49
|
+
* couldn't distinguish the two callers it needs to: a sub-adapter re-entering
|
|
50
|
+
* from within the running `fn()` (must reuse the held tx — opening another
|
|
51
|
+
* would deadlock on the one connection) versus an unrelated *concurrent*
|
|
52
|
+
* caller that merely arrived while the flag was set. The second one skipped
|
|
53
|
+
* the mutex and rode a stranger's transaction, so the moment that transaction
|
|
54
|
+
* committed its `SET LOCAL app.organization_id` was discarded and the leaked
|
|
55
|
+
* caller's remaining queries evaluated the RLS policy against `''` —
|
|
56
|
+
* "invalid input syntax for type uuid". Worse, its writes committed on
|
|
57
|
+
* someone else's boundary, which can split a state change from the outbox row
|
|
58
|
+
* that `withTransaction` exists to keep atomic with it.
|
|
59
|
+
*
|
|
60
|
+
* AsyncLocalStorage scopes that answer to the call chain, so re-entrancy is
|
|
61
|
+
* detected and concurrency queues on `runExclusive` as intended.
|
|
62
|
+
*/
|
|
63
|
+
txContext = new AsyncLocalStorage();
|
|
42
64
|
constructor(config) {
|
|
43
65
|
this.db = config.db;
|
|
44
66
|
this.tables = config.tables;
|
|
@@ -144,7 +166,27 @@ export class PostgreSQLAdapter {
|
|
|
144
166
|
return result;
|
|
145
167
|
const childOrgIds = await this.getChildOrganizations(organizationId);
|
|
146
168
|
const found = await this.documentAdapter.findDocByIdInOrgs([organizationId, ...childOrgIds], id);
|
|
147
|
-
|
|
169
|
+
if (!found)
|
|
170
|
+
return result;
|
|
171
|
+
// Pooled Postgres connects as the table owner and bypasses RLS, so the retry
|
|
172
|
+
// can just run. Where RLS is live (pglite) it can't: the row belongs to a
|
|
173
|
+
// child org but the connection still carries the *parent's*
|
|
174
|
+
// app.organization_id, and while the USING policy admits children, withCheck
|
|
175
|
+
// demands an exact match — so every write leg of the hierarchy fallback
|
|
176
|
+
// (update / publish / unpublish / delete) died with 42501. Point the GUC at
|
|
177
|
+
// the org that actually owns the row for the retry, then put the caller's
|
|
178
|
+
// context back: SET LOCAL is transaction-scoped, not block-scoped, so
|
|
179
|
+
// otherwise it would leak to whatever the caller does next in the same
|
|
180
|
+
// transaction.
|
|
181
|
+
if (!this.rlsEnabled || !this.singleConnection)
|
|
182
|
+
return op(found.organizationId);
|
|
183
|
+
await this.db.execute(sql.raw(`SET LOCAL app.organization_id = '${found.organizationId}'`));
|
|
184
|
+
try {
|
|
185
|
+
return await op(found.organizationId);
|
|
186
|
+
}
|
|
187
|
+
finally {
|
|
188
|
+
await this.db.execute(sql.raw(`SET LOCAL app.organization_id = '${organizationId}'`));
|
|
189
|
+
}
|
|
148
190
|
}
|
|
149
191
|
// Document operations - delegate to document adapter with RLS context
|
|
150
192
|
async createDocument(data) {
|
|
@@ -156,6 +198,9 @@ export class PostgreSQLAdapter {
|
|
|
156
198
|
async deleteDocById(organizationId, id) {
|
|
157
199
|
return this.withOrgContext(organizationId, () => this.withHierarchyFallback(organizationId, id, (orgId) => this.documentAdapter.deleteDocById(orgId, id)));
|
|
158
200
|
}
|
|
201
|
+
async updateSearchText(organizationId, id, searchText) {
|
|
202
|
+
return this.withOrgContext(organizationId, () => this.withHierarchyFallback(organizationId, id, (orgId) => this.documentAdapter.updateSearchText(orgId, id, searchText)));
|
|
203
|
+
}
|
|
159
204
|
async publishDoc(organizationId, id, expectedRevision) {
|
|
160
205
|
return this.withOrgContext(organizationId, () => this.withHierarchyFallback(organizationId, id, (orgId) => this.documentAdapter.publishDoc(orgId, id, expectedRevision)));
|
|
161
206
|
}
|
|
@@ -200,21 +245,26 @@ export class PostgreSQLAdapter {
|
|
|
200
245
|
// Bypass RLS - call adapter directly without org context
|
|
201
246
|
return this.assetAdapter.findAssetByIdGlobal(id);
|
|
202
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* Turn the caller's `includeChildOrganizations` request into the explicit set
|
|
250
|
+
* of organizations to search.
|
|
251
|
+
*
|
|
252
|
+
* Shared by `findAssets` and `countAssets` for the same reason the adapter's
|
|
253
|
+
* clause builder is shared: a page widened to the subtree next to a total that
|
|
254
|
+
* wasn't reports "1–20 of 4" over twenty rows.
|
|
255
|
+
*/
|
|
256
|
+
async resolveAssetOrgScope(organizationId, filters) {
|
|
257
|
+
if (!this.hierarchyEnabled || !filters?.includeChildOrganizations)
|
|
258
|
+
return filters;
|
|
259
|
+
if (filters.filterOrganizationIds)
|
|
260
|
+
return filters;
|
|
261
|
+
const childOrgIds = await this.getChildOrganizations(organizationId);
|
|
262
|
+
if (childOrgIds.length === 0)
|
|
263
|
+
return filters;
|
|
264
|
+
return { ...filters, filterOrganizationIds: [organizationId, ...childOrgIds] };
|
|
265
|
+
}
|
|
203
266
|
async findAssets(organizationId, filters) {
|
|
204
|
-
return this.withOrgContext(organizationId, async () =>
|
|
205
|
-
// Only include child organizations if explicitly requested via includeChildOrganizations filter
|
|
206
|
-
if (this.hierarchyEnabled &&
|
|
207
|
-
filters?.includeChildOrganizations &&
|
|
208
|
-
!filters?.filterOrganizationIds) {
|
|
209
|
-
const childOrgIds = await this.getChildOrganizations(organizationId);
|
|
210
|
-
const orgIds = [organizationId, ...childOrgIds];
|
|
211
|
-
return this.assetAdapter.findAssets(organizationId, {
|
|
212
|
-
...filters,
|
|
213
|
-
filterOrganizationIds: orgIds
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
return this.assetAdapter.findAssets(organizationId, filters);
|
|
217
|
-
});
|
|
267
|
+
return this.withOrgContext(organizationId, async () => this.assetAdapter.findAssets(organizationId, await this.resolveAssetOrgScope(organizationId, filters)));
|
|
218
268
|
}
|
|
219
269
|
async updateAsset(organizationId, id, data) {
|
|
220
270
|
return this.withOrgContext(organizationId, async () => {
|
|
@@ -249,7 +299,7 @@ export class PostgreSQLAdapter {
|
|
|
249
299
|
});
|
|
250
300
|
}
|
|
251
301
|
async countAssets(organizationId, filters) {
|
|
252
|
-
return this.withOrgContext(organizationId, () => this.assetAdapter.countAssets(organizationId, filters));
|
|
302
|
+
return this.withOrgContext(organizationId, async () => this.assetAdapter.countAssets(organizationId, await this.resolveAssetOrgScope(organizationId, filters)));
|
|
253
303
|
}
|
|
254
304
|
async countAssetsByType(organizationId) {
|
|
255
305
|
return this.withOrgContext(organizationId, () => this.assetAdapter.countAssetsByType(organizationId));
|
|
@@ -468,6 +518,26 @@ export class PostgreSQLAdapter {
|
|
|
468
518
|
.returning();
|
|
469
519
|
return (rows[0]?.settings ?? merged);
|
|
470
520
|
}
|
|
521
|
+
/**
|
|
522
|
+
* Atomically take the one-time bootstrap claim.
|
|
523
|
+
*
|
|
524
|
+
* The whole race is decided by the primary key: the row either inserts or it
|
|
525
|
+
* conflicts, and only one concurrent caller can see a row come back. No
|
|
526
|
+
* transaction, so nothing holds SQLite's write lock while the auth provider is
|
|
527
|
+
* doing its own inserts.
|
|
528
|
+
*/
|
|
529
|
+
async tryClaimBootstrap() {
|
|
530
|
+
const claimed = await this.db
|
|
531
|
+
.insert(this.tables.instanceSettings)
|
|
532
|
+
.values({
|
|
533
|
+
id: BOOTSTRAP_CLAIM_ID,
|
|
534
|
+
settings: { claimedAt: new Date().toISOString() },
|
|
535
|
+
updatedAt: new Date()
|
|
536
|
+
})
|
|
537
|
+
.onConflictDoNothing()
|
|
538
|
+
.returning({ id: this.tables.instanceSettings.id });
|
|
539
|
+
return claimed.length > 0;
|
|
540
|
+
}
|
|
471
541
|
// Plugin settings operations — the generic per-(org, plugin) config store.
|
|
472
542
|
// Wrapped in withOrgContext so the RLS policy (pglite / RLS-enforced Postgres)
|
|
473
543
|
// admits the row; on SQLite/pooled-owner Postgres it's a passthrough and the
|
|
@@ -613,14 +683,16 @@ export class PostgreSQLAdapter {
|
|
|
613
683
|
if (options?.userRole)
|
|
614
684
|
await exec(sql.raw(`SET LOCAL app.user_role = '${options.userRole}'`));
|
|
615
685
|
};
|
|
616
|
-
// Already inside a single-connection transaction (a sub-adapter
|
|
617
|
-
//
|
|
618
|
-
// mutex (we already
|
|
619
|
-
|
|
620
|
-
|
|
686
|
+
// Already inside a single-connection transaction *on this call chain* (a sub-adapter
|
|
687
|
+
// re-entered withOrgContext): reuse the held tx — opening another would wait forever for
|
|
688
|
+
// the one connection. Re-apply the context and run inline; no new tx, no mutex (we already
|
|
689
|
+
// hold it). Concurrent callers see an empty store and queue below instead.
|
|
690
|
+
const activeTx = this.txContext.getStore();
|
|
691
|
+
if (activeTx) {
|
|
692
|
+
await applyContext((q) => activeTx.execute(q));
|
|
621
693
|
return fn();
|
|
622
694
|
}
|
|
623
|
-
return this.runExclusive(() => this.db.transaction(async (
|
|
695
|
+
return this.runExclusive(() => this.db.transaction((tx) => this.txContext.run(tx, async () => {
|
|
624
696
|
const restore = this.bindToTx(tx);
|
|
625
697
|
try {
|
|
626
698
|
await applyContext((q) => tx.execute(q));
|
|
@@ -629,7 +701,7 @@ export class PostgreSQLAdapter {
|
|
|
629
701
|
finally {
|
|
630
702
|
restore();
|
|
631
703
|
}
|
|
632
|
-
}));
|
|
704
|
+
})));
|
|
633
705
|
}
|
|
634
706
|
/** Serialize work on the single shared connection so tx-rebinds can't interleave. */
|
|
635
707
|
runExclusive(task) {
|
|
@@ -671,7 +743,6 @@ export class PostgreSQLAdapter {
|
|
|
671
743
|
this.referenceAdapter = new prev.referenceAdapter.constructor(txDb, this.tables);
|
|
672
744
|
this.eventJobAdapter = new prev.eventJobAdapter.constructor(txDb, this.tables);
|
|
673
745
|
this.pluginStorageAdapter = new prev.pluginStorageAdapter.constructor(txDb, this.tables);
|
|
674
|
-
this.inSingleConnTx = true;
|
|
675
746
|
return () => {
|
|
676
747
|
this.db = prevDb;
|
|
677
748
|
this.documentAdapter = prev.documentAdapter;
|
|
@@ -683,7 +754,6 @@ export class PostgreSQLAdapter {
|
|
|
683
754
|
this.referenceAdapter = prev.referenceAdapter;
|
|
684
755
|
this.eventJobAdapter = prev.eventJobAdapter;
|
|
685
756
|
this.pluginStorageAdapter = prev.pluginStorageAdapter;
|
|
686
|
-
this.inSingleConnTx = false;
|
|
687
757
|
};
|
|
688
758
|
}
|
|
689
759
|
/**
|
|
@@ -714,8 +784,23 @@ export class PostgreSQLAdapter {
|
|
|
714
784
|
async countDocumentReferencesForAssets(organizationId, assetIds, knownTypes) {
|
|
715
785
|
return this.withOrgContext(organizationId, () => this.documentAdapter.countDocumentReferencesForAssets(organizationId, assetIds, knownTypes));
|
|
716
786
|
}
|
|
717
|
-
async
|
|
718
|
-
return this.
|
|
787
|
+
async replaceAssetReferences(organizationId, documentId, documentType, references) {
|
|
788
|
+
return this.documentAdapter.replaceAssetReferences(organizationId, documentId, documentType, references);
|
|
789
|
+
}
|
|
790
|
+
async hasAnyAssetReferences(organizationId) {
|
|
791
|
+
return this.documentAdapter.hasAnyAssetReferences(organizationId);
|
|
792
|
+
}
|
|
793
|
+
async countAssetReferencesForAssets(organizationId, assetIds) {
|
|
794
|
+
return this.documentAdapter.countAssetReferencesForAssets(organizationId, assetIds);
|
|
795
|
+
}
|
|
796
|
+
async listStoredDocumentTypes(organizationId) {
|
|
797
|
+
return this.documentAdapter.listStoredDocumentTypes(organizationId);
|
|
798
|
+
}
|
|
799
|
+
async findAssetReferenceFieldPaths(organizationId, assetId) {
|
|
800
|
+
return this.documentAdapter.findAssetReferenceFieldPaths(organizationId, assetId);
|
|
801
|
+
}
|
|
802
|
+
async clearAssetReferences(organizationId, assetId) {
|
|
803
|
+
return this.withOrgContext(organizationId, () => this.documentAdapter.clearAssetReferences(organizationId, assetId));
|
|
719
804
|
}
|
|
720
805
|
// Version history — wrapped with org context for RLS
|
|
721
806
|
async createDocumentVersion(data) {
|
|
@@ -742,7 +827,7 @@ export class PostgreSQLAdapter {
|
|
|
742
827
|
return this.withOrgContext(organizationId, () => this.eventJobAdapter.getEvent(organizationId, id));
|
|
743
828
|
}
|
|
744
829
|
async listEvents(options) {
|
|
745
|
-
return this.
|
|
830
|
+
return this.withInstanceOrOrgContext(options.organizationId, () => this.eventJobAdapter.listEvents(options));
|
|
746
831
|
}
|
|
747
832
|
async listUnprocessedOutbox(options) {
|
|
748
833
|
// Worker-wide read (no org) runs with override access, mirroring claimDueJobs; an
|
|
@@ -756,6 +841,22 @@ export class PostgreSQLAdapter {
|
|
|
756
841
|
async markOutboxProcessed(organizationId, id) {
|
|
757
842
|
return this.withOrgContext(organizationId, () => this.eventJobAdapter.markOutboxProcessed(organizationId, id));
|
|
758
843
|
}
|
|
844
|
+
async outboxHealth(options) {
|
|
845
|
+
return this.withInstanceOrOrgContext(options.organizationId, () => this.eventJobAdapter.outboxHealth(options));
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Run a read that is either org-scoped or deliberately instance-wide.
|
|
849
|
+
*
|
|
850
|
+
* The instance-wide branch mirrors `claimDueJobs`/`listUnprocessedOutbox`: no GUC to set,
|
|
851
|
+
* so RLS has to be bypassed explicitly rather than by leaving the org unset (which would
|
|
852
|
+
* match nothing and silently look like an empty result). Only reach this branch for callers
|
|
853
|
+
* that have already been authorized instance-wide — the worker, or a super admin.
|
|
854
|
+
*/
|
|
855
|
+
withInstanceOrOrgContext(organizationId, fn) {
|
|
856
|
+
return organizationId
|
|
857
|
+
? this.withOrgContext(organizationId, fn)
|
|
858
|
+
: this.withOrgContext('', fn, { overrideAccess: true });
|
|
859
|
+
}
|
|
759
860
|
// --- Agent change-sets (audit/undo trail) — org context set for RLS, same as everything
|
|
760
861
|
// else. Not rebound inside withTransaction: recording is a best-effort side observation in
|
|
761
862
|
// the agent-chat route handler, never called from inside a document-write transaction.
|
|
@@ -807,11 +908,25 @@ export class PostgreSQLAdapter {
|
|
|
807
908
|
async cancelJob(organizationId, id) {
|
|
808
909
|
return this.withOrgContext(organizationId, () => this.eventJobAdapter.cancelJob(organizationId, id));
|
|
809
910
|
}
|
|
911
|
+
async getJob(organizationId, id) {
|
|
912
|
+
return this.withOrgContext(organizationId, () => this.eventJobAdapter.getJob(organizationId, id));
|
|
913
|
+
}
|
|
914
|
+
async requeueJob(organizationId, id, options) {
|
|
915
|
+
return this.withOrgContext(organizationId, () => this.eventJobAdapter.requeueJob(organizationId, id, options));
|
|
916
|
+
}
|
|
810
917
|
async listJobs(options) {
|
|
811
|
-
return this.
|
|
918
|
+
return this.withInstanceOrOrgContext(options.organizationId, () => this.eventJobAdapter.listJobs(options));
|
|
812
919
|
}
|
|
813
920
|
// Transaction support
|
|
814
921
|
async withTransaction(fn) {
|
|
922
|
+
// On a single connection this has to take the same mutex as withOrgContext,
|
|
923
|
+
// or a transaction opened here interleaves with one opened there on the one
|
|
924
|
+
// connection they share. Pooled Postgres gets its own connection per caller
|
|
925
|
+
// and is unaffected, so leave that path exactly as it was.
|
|
926
|
+
const run = () => this.runTransaction(fn);
|
|
927
|
+
return this.singleConnection ? this.runExclusive(run) : run();
|
|
928
|
+
}
|
|
929
|
+
async runTransaction(fn) {
|
|
815
930
|
return this.db.transaction(async (tx) => {
|
|
816
931
|
// Create a transactional adapter where all operations use tx
|
|
817
932
|
const txAdapter = Object.create(this);
|
|
@@ -820,6 +935,16 @@ export class PostgreSQLAdapter {
|
|
|
820
935
|
// Rebind the event/job adapter too so appendEvent/scheduleJob issued inside the
|
|
821
936
|
// callback run on the transaction (the outbox guarantee).
|
|
822
937
|
txAdapter.eventJobAdapter = new this.eventJobAdapter.constructor(tx, this.tables);
|
|
938
|
+
// The reference adapter must be rebound too, now that the back-reference
|
|
939
|
+
// index is written inside the caller's document-write transaction. Left
|
|
940
|
+
// on the root connection it wrote outside the transaction, and since
|
|
941
|
+
// `referencer_id` is a foreign key to a document row that had not
|
|
942
|
+
// committed yet, the insert failed the whole save.
|
|
943
|
+
//
|
|
944
|
+
// The general hazard: any sub-adapter NOT rebound here silently escapes
|
|
945
|
+
// the transaction. Add one to this list whenever its writes need to
|
|
946
|
+
// commit with the caller's.
|
|
947
|
+
txAdapter.referenceAdapter = new this.referenceAdapter.constructor(tx, this.tables);
|
|
823
948
|
txAdapter.pluginStorageAdapter = new this.pluginStorageAdapter.constructor(tx, this.tables);
|
|
824
949
|
// Override withOrgContext to use SET LOCAL within the existing transaction
|
|
825
950
|
// instead of opening a new one
|
|
@@ -837,7 +962,12 @@ export class PostgreSQLAdapter {
|
|
|
837
962
|
await tx.execute(sql.raw(`SET LOCAL app.organization_id = '${organizationId}'`));
|
|
838
963
|
return innerFn();
|
|
839
964
|
};
|
|
840
|
-
|
|
965
|
+
// Publish `tx` on the async context as well. `fn` gets `txAdapter`, but code
|
|
966
|
+
// inside it can still reach the outer adapter (a captured service, a
|
|
967
|
+
// sub-adapter closure); without this that call would try to open a second
|
|
968
|
+
// transaction — on one connection, while we hold the mutex, that deadlocks.
|
|
969
|
+
// Seeing the active tx, it reuses this one instead.
|
|
970
|
+
return this.txContext.run(tx, () => fn(txAdapter));
|
|
841
971
|
});
|
|
842
972
|
}
|
|
843
973
|
// Connection management
|