@burdenoff/be-sdk 2026.1127.2 → 2026.1128.1
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/auth/index.d.ts +1 -1
- package/dist/feature-flags/index.d.ts +1 -1
- package/dist/{index-CkQieKSy.d.ts → index-12NbzOYy.d.ts} +1 -1
- package/dist/{index-DFEKBKBE.d.ts → index-BQ5O967l.d.ts} +1 -1
- package/dist/{index-BwgBHUuX.d.ts → index-CkB68nQA.d.ts} +2 -2
- package/dist/{index-CUiL43SY.d.ts → index-tLd9b-nS.d.ts} +2 -2
- package/dist/index.d.ts +4 -4
- package/dist/index.js +2 -2
- package/dist/prisma/index.d.ts +3 -3
- package/dist/prisma/index.js +3 -3
- package/dist/{prisma-J-ZUmVWJ.js → prisma-1J9WTyAF.js} +11 -2
- package/dist/{prisma-J-ZUmVWJ.js.map → prisma-1J9WTyAF.js.map} +1 -1
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.js +24 -10
- package/dist/server/index.js.map +1 -1
- package/dist/{stub-BUnfMiAS.js → stub-DZ332ZHG.js} +2 -2
- package/dist/{stub-BUnfMiAS.js.map → stub-DZ332ZHG.js.map} +1 -1
- package/dist/tracker/index.js +2 -2
- package/dist/{tracker-B1fEAM3i.js → tracker-DE4MDVTr.js} +2 -2
- package/dist/{tracker-B1fEAM3i.js.map → tracker-DE4MDVTr.js.map} +1 -1
- package/dist/{types-BWN8tMdF.d.ts → types-oV9uAkMA.d.ts} +1 -1
- package/dist/{workspace-extension-COrC0dYi.js → workspace-extension-ByopOwtK.js} +260 -62
- package/dist/workspace-extension-ByopOwtK.js.map +1 -0
- package/dist/{workspace-extension-DTFTdGVF.d.ts → workspace-extension-DKxDI4H6.d.ts} +14 -1
- package/package.json +1 -1
- package/dist/workspace-extension-COrC0dYi.js.map +0 -1
|
@@ -96,6 +96,13 @@ function bindTransactionContext(client, scope, getWorkspaceContext) {
|
|
|
96
96
|
* **What gets auto-injected on reads/updates/deletes:**
|
|
97
97
|
* - `workspaceId` in the `where` clause - ensures queries only access data within the workspace
|
|
98
98
|
*
|
|
99
|
+
* **No bound context (fail closed):**
|
|
100
|
+
* The context is read from AsyncLocalStorage only. Work that runs outside a request scope (stream
|
|
101
|
+
* consumers, timers, workers, startup jobs) has no context and never inherits another request's:
|
|
102
|
+
* with `strictMode` (the default) such operations throw until the work binds a scope with
|
|
103
|
+
* `withWorkspaceContext()` (one workspace) or `withSystemScope(reason, fn)` (explicitly
|
|
104
|
+
* cross-workspace). `contextFallback: 'warn'` is a deprecated, time-limited migration aid.
|
|
105
|
+
*
|
|
99
106
|
* **Opt-out:**
|
|
100
107
|
* - Explicitly providing `workspaceId` in the where/data will override auto-injection
|
|
101
108
|
* - Pass `_skipWorkspaceInjection: true` in the query args to skip entirely
|
|
@@ -111,83 +118,147 @@ function bindTransactionContext(client, scope, getWorkspaceContext) {
|
|
|
111
118
|
* fine-grained control over workspace-specific context propagation.
|
|
112
119
|
*/
|
|
113
120
|
const workspaceContextStore = new AsyncLocalStorage();
|
|
121
|
+
/** Date after which the `'warn'` fallback mode is removed and `'deny'` is the only behaviour. */
|
|
122
|
+
const WORKSPACE_CONTEXT_WARN_MODE_REMOVAL_DATE = "2027-01-08";
|
|
123
|
+
/** Minimum interval between two warn-mode log lines (each line carries the count since the last). */
|
|
124
|
+
const FALLBACK_WARNING_INTERVAL_MS = 6e4;
|
|
125
|
+
/** Upper bound on distinct system-scope reasons logged, so a computed reason cannot grow memory. */
|
|
126
|
+
const MAX_LOGGED_SYSTEM_SCOPE_REASONS = 256;
|
|
127
|
+
const fallbackState = {
|
|
128
|
+
mode: "deny",
|
|
129
|
+
lastContext: void 0,
|
|
130
|
+
suppressedWarnings: 0,
|
|
131
|
+
lastWarningAt: 0
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Contexts created by {@link withSystemScope}. Membership, not a field, marks a system scope, so
|
|
135
|
+
* a context built or spread from request data can never claim to be one.
|
|
136
|
+
*/
|
|
137
|
+
const systemScopeContexts = /* @__PURE__ */ new WeakSet();
|
|
138
|
+
const systemScopeReasons = /* @__PURE__ */ new WeakMap();
|
|
139
|
+
const loggedSystemScopeReasons = /* @__PURE__ */ new Set();
|
|
140
|
+
let systemScopeReasonCapLogged = false;
|
|
114
141
|
/**
|
|
115
|
-
*
|
|
116
|
-
*
|
|
142
|
+
* Select the fallback mode for this process. Called by {@link addWorkspaceExtension} from its
|
|
143
|
+
* `contextFallback` option. `'warn'` is sticky for the life of the process: once any client asks
|
|
144
|
+
* for it, a second client created later with the default cannot silently switch it back mid-flight.
|
|
117
145
|
*
|
|
118
|
-
*
|
|
119
|
-
* It works for single-request-at-a-time scenarios but could race under
|
|
120
|
-
* concurrent requests. The ALS is the primary mechanism; this is a fallback.
|
|
146
|
+
* @internal
|
|
121
147
|
*/
|
|
122
|
-
|
|
148
|
+
function configureWorkspaceContextFallback(mode) {
|
|
149
|
+
if (mode !== "deny" && mode !== "warn") throw new Error(`[WorkspaceExtension] Unknown contextFallback mode: ${String(mode)}`);
|
|
150
|
+
if (mode === "warn" && fallbackState.mode !== "warn") {
|
|
151
|
+
fallbackState.mode = "warn";
|
|
152
|
+
logger.error(`[WorkspaceExtension] contextFallback 'warn' is enabled. Operations outside a bound workspace context will use the most recent request's context. This mode is deprecated and will be removed after ${WORKSPACE_CONTEXT_WARN_MODE_REMOVAL_DATE}; bind context with withWorkspaceContext() or withSystemScope() instead.`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** The fallback mode currently in effect for this process. */
|
|
156
|
+
function getWorkspaceContextFallbackMode() {
|
|
157
|
+
return fallbackState.mode;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Reset module-level context state (fallback mode, retained context, log dedupe).
|
|
161
|
+
*
|
|
162
|
+
* @internal For unit tests only.
|
|
163
|
+
*/
|
|
164
|
+
function resetWorkspaceContextStateForTests() {
|
|
165
|
+
fallbackState.mode = "deny";
|
|
166
|
+
fallbackState.lastContext = void 0;
|
|
167
|
+
fallbackState.suppressedWarnings = 0;
|
|
168
|
+
fallbackState.lastWarningAt = 0;
|
|
169
|
+
loggedSystemScopeReasons.clear();
|
|
170
|
+
systemScopeReasonCapLogged = false;
|
|
171
|
+
}
|
|
172
|
+
function warnFallbackUse(context) {
|
|
173
|
+
const now = Date.now();
|
|
174
|
+
if (now - fallbackState.lastWarningAt < FALLBACK_WARNING_INTERVAL_MS) {
|
|
175
|
+
fallbackState.suppressedWarnings += 1;
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const suppressed = fallbackState.suppressedWarnings;
|
|
179
|
+
fallbackState.lastWarningAt = now;
|
|
180
|
+
fallbackState.suppressedWarnings = 0;
|
|
181
|
+
const callSite = (/* @__PURE__ */ new Error()).stack?.split("\n").slice(2, 8).join("\n");
|
|
182
|
+
logger.warn(`[WorkspaceExtension] Workspace context resolved from the shared fallback (no context bound to this async scope). This will fail closed after ${WORKSPACE_CONTEXT_WARN_MODE_REMOVAL_DATE}.`, {
|
|
183
|
+
fallbackWorkspaceId: context.workspaceId ?? "NONE",
|
|
184
|
+
suppressedSinceLastWarning: suppressed,
|
|
185
|
+
callSite
|
|
186
|
+
});
|
|
187
|
+
}
|
|
123
188
|
/**
|
|
124
189
|
* Set workspace context for the current async scope.
|
|
125
|
-
* Call this in your GraphQL context builder or middleware.
|
|
126
190
|
*
|
|
127
|
-
* Uses AsyncLocalStorage.enterWith()
|
|
128
|
-
*
|
|
129
|
-
*
|
|
191
|
+
* Uses `AsyncLocalStorage.enterWith()`, which binds the context to the CURRENT synchronous
|
|
192
|
+
* execution and everything it schedules afterwards. It does not reach a caller that awaited the
|
|
193
|
+
* function that called it, so request handlers should prefer {@link withWorkspaceContext}, which
|
|
194
|
+
* binds a scope explicitly (the SDK server does this for GraphQL execution and REST endpoints).
|
|
130
195
|
*
|
|
131
196
|
* @param context - The workspace context for this request
|
|
132
|
-
*
|
|
133
|
-
* @example
|
|
134
|
-
* ```typescript
|
|
135
|
-
* // In your server's buildContext:
|
|
136
|
-
* import { setWorkspaceContext } from '@burdenoff/be-sdk/prisma';
|
|
137
|
-
*
|
|
138
|
-
* buildContext: async (defaultContext) => {
|
|
139
|
-
* setWorkspaceContext({
|
|
140
|
-
* workspaceId: defaultContext.workspace?.id,
|
|
141
|
-
* tenantId: defaultContext.tenant?.id,
|
|
142
|
-
* actorId: defaultContext.actor?.id,
|
|
143
|
-
* actorType: defaultContext.actor?.type,
|
|
144
|
-
* organizationId: defaultContext.organization?.id,
|
|
145
|
-
* });
|
|
146
|
-
* return defaultContext;
|
|
147
|
-
* }
|
|
148
|
-
* ```
|
|
149
197
|
*/
|
|
150
198
|
function setWorkspaceContext(context) {
|
|
151
199
|
workspaceContextStore.enterWith(context);
|
|
152
|
-
|
|
200
|
+
fallbackState.lastContext = fallbackState.mode === "warn" ? context : void 0;
|
|
153
201
|
}
|
|
154
202
|
/**
|
|
155
|
-
* Get the
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
203
|
+
* Get the workspace context bound to the current async scope.
|
|
204
|
+
*
|
|
205
|
+
* Fails closed: outside a bound scope (a background job, a stream consumer, a timer, or code
|
|
206
|
+
* that lost its request scope) this returns `undefined`, unless the process opted into the
|
|
207
|
+
* deprecated `contextFallback: 'warn'` mode.
|
|
159
208
|
*
|
|
160
|
-
* @returns The current workspace context, or undefined if
|
|
209
|
+
* @returns The current workspace context, or undefined if none is bound
|
|
161
210
|
*/
|
|
162
211
|
function getWorkspaceContext() {
|
|
163
|
-
|
|
212
|
+
const scoped = workspaceContextStore.getStore();
|
|
213
|
+
if (scoped) return scoped;
|
|
214
|
+
if (fallbackState.mode === "warn" && fallbackState.lastContext) {
|
|
215
|
+
warnFallbackUse(fallbackState.lastContext);
|
|
216
|
+
return fallbackState.lastContext;
|
|
217
|
+
}
|
|
164
218
|
}
|
|
165
219
|
/**
|
|
166
|
-
* The workspace context bound to the CURRENT async scope only
|
|
220
|
+
* The workspace context bound to the CURRENT async scope only. Never consults the deprecated
|
|
221
|
+
* warn-mode fallback, whatever the process mode.
|
|
167
222
|
*
|
|
168
223
|
* Use it for values that must never leak between requests (for example the assistant-session
|
|
169
|
-
* attribution on activity rows)
|
|
170
|
-
* background job or a concurrent request could otherwise read another request's values.
|
|
224
|
+
* attribution on activity rows).
|
|
171
225
|
*/
|
|
172
226
|
function getScopedWorkspaceContext() {
|
|
173
227
|
return workspaceContextStore.getStore();
|
|
174
228
|
}
|
|
175
229
|
/**
|
|
230
|
+
* Get the bound workspace context and its workspaceId, or throw.
|
|
231
|
+
*
|
|
232
|
+
* Use in code that must only ever run for one workspace (resolvers, consumers that already
|
|
233
|
+
* wrapped themselves in {@link withWorkspaceContext}) so a missing scope fails loudly at the
|
|
234
|
+
* call site instead of deep inside a query. Reads only the scope bound to the current async
|
|
235
|
+
* chain: the deprecated warn-mode fallback never satisfies it.
|
|
236
|
+
*
|
|
237
|
+
* @throws Error when no workspace is bound to the current async scope
|
|
238
|
+
*/
|
|
239
|
+
function requireWorkspaceContext() {
|
|
240
|
+
const context = workspaceContextStore.getStore();
|
|
241
|
+
if (!context?.workspaceId || isSystemScope(context)) throw new Error("[WorkspaceExtension] No workspace is bound to the current async scope. Wrap the work in withWorkspaceContext({ workspaceId, ... }, fn).");
|
|
242
|
+
return context;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
176
245
|
* Run a function with a specific workspace context.
|
|
177
246
|
* The context is isolated to this execution and its children.
|
|
178
247
|
*
|
|
248
|
+
* This is the explicit API for background work that acts for ONE workspace: a stream consumer
|
|
249
|
+
* handling an event that names its workspace, a job row that stores its workspaceId, a
|
|
250
|
+
* per-workspace loop inside a sweep. Pass `actorType: 'service'` (and the service as `actorId`)
|
|
251
|
+
* when no user is behind the work, so audit columns say who acted.
|
|
252
|
+
*
|
|
179
253
|
* @param context - The workspace context
|
|
180
254
|
* @param fn - The function to run
|
|
181
255
|
* @returns The result of the function
|
|
182
256
|
*
|
|
183
257
|
* @example
|
|
184
258
|
* ```typescript
|
|
185
|
-
*
|
|
186
|
-
* { workspaceId: '
|
|
187
|
-
* async () => {
|
|
188
|
-
* // All Prisma operations in here will auto-inject workspaceId
|
|
189
|
-
* return await prisma.tag.create({ data: { name: 'test' } });
|
|
190
|
-
* }
|
|
259
|
+
* await withWorkspaceContext(
|
|
260
|
+
* { workspaceId: event.workspaceId, actorId: 'wspace-tags-svc', actorType: 'service' },
|
|
261
|
+
* async () => prisma.tag.updateMany({ where: { name }, data: { archived: true } })
|
|
191
262
|
* );
|
|
192
263
|
* ```
|
|
193
264
|
*/
|
|
@@ -201,6 +272,69 @@ function withWorkspaceContextSync(context, fn) {
|
|
|
201
272
|
return workspaceContextStore.run(context, fn);
|
|
202
273
|
}
|
|
203
274
|
/**
|
|
275
|
+
* Run a function in an explicit, audited system scope that is not tied to any workspace.
|
|
276
|
+
*
|
|
277
|
+
* Inside the scope the workspace extension injects nothing and does not apply `strictMode`:
|
|
278
|
+
* reads see every workspace's rows and writes must set `workspaceId` themselves. Use it only for
|
|
279
|
+
* genuinely cross-workspace work (retention sweeps, outbox relays, startup backfills), never to
|
|
280
|
+
* silence a missing request context. Prefer one {@link withWorkspaceContext} per workspace when
|
|
281
|
+
* the work can be split that way.
|
|
282
|
+
*
|
|
283
|
+
* Each distinct reason is logged once per process so the system scopes in use stay visible.
|
|
284
|
+
* A {@link withWorkspaceContext} nested inside the scope rebinds to that workspace.
|
|
285
|
+
*
|
|
286
|
+
* @param reason - Short, static description of the work, e.g. `'retention-sweep'`. Required.
|
|
287
|
+
* @param fn - The function to run
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```typescript
|
|
291
|
+
* await withSystemScope('expired-session-sweep', () =>
|
|
292
|
+
* prisma.session.deleteMany({ where: { expiresAt: { lt: new Date() } } })
|
|
293
|
+
* );
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
async function withSystemScope(reason, fn) {
|
|
297
|
+
return workspaceContextStore.run(createSystemScopeContext(reason), fn);
|
|
298
|
+
}
|
|
299
|
+
/** Synchronous form of {@link withSystemScope}. */
|
|
300
|
+
function withSystemScopeSync(reason, fn) {
|
|
301
|
+
return workspaceContextStore.run(createSystemScopeContext(reason), fn);
|
|
302
|
+
}
|
|
303
|
+
/** Whether the given (or current) context is a system scope created by {@link withSystemScope}. */
|
|
304
|
+
function isSystemScope(context = workspaceContextStore.getStore()) {
|
|
305
|
+
return context !== void 0 && systemScopeContexts.has(context);
|
|
306
|
+
}
|
|
307
|
+
function createSystemScopeContext(reason) {
|
|
308
|
+
const trimmed = typeof reason === "string" ? reason.trim() : "";
|
|
309
|
+
if (!trimmed) throw new Error("[WorkspaceExtension] withSystemScope requires a non-empty reason.");
|
|
310
|
+
const context = Object.freeze({ actorType: "service" });
|
|
311
|
+
systemScopeContexts.add(context);
|
|
312
|
+
systemScopeReasons.set(context, trimmed);
|
|
313
|
+
return context;
|
|
314
|
+
}
|
|
315
|
+
/** Log the first operation run under each distinct system-scope reason. */
|
|
316
|
+
function noteSystemScopeOperation(context, model, operation) {
|
|
317
|
+
const reason = systemScopeReasons.get(context) ?? "unknown";
|
|
318
|
+
if (loggedSystemScopeReasons.has(reason)) return;
|
|
319
|
+
if (loggedSystemScopeReasons.size >= MAX_LOGGED_SYSTEM_SCOPE_REASONS) {
|
|
320
|
+
if (!systemScopeReasonCapLogged) {
|
|
321
|
+
systemScopeReasonCapLogged = true;
|
|
322
|
+
logger.warn(`[WorkspaceExtension] More than ${MAX_LOGGED_SYSTEM_SCOPE_REASONS} distinct system-scope reasons; further new reasons are not logged. Reasons should be short static strings.`, {
|
|
323
|
+
reason,
|
|
324
|
+
model,
|
|
325
|
+
operation
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
loggedSystemScopeReasons.add(reason);
|
|
331
|
+
logger.info("[WorkspaceExtension] Operation running in an explicit system scope (no workspace injection)", {
|
|
332
|
+
reason,
|
|
333
|
+
model,
|
|
334
|
+
operation
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
204
338
|
* Default configuration. Exported so consumers/tests can spread over the
|
|
205
339
|
* real defaults (`{ ...defaultWorkspaceExtensionConfig, populateOnBehalfOf: false }`)
|
|
206
340
|
* instead of re-declaring every field.
|
|
@@ -219,6 +353,7 @@ const defaultWorkspaceExtensionConfig = {
|
|
|
219
353
|
excludeModelsFromReadIsolation: [],
|
|
220
354
|
strictMode: true,
|
|
221
355
|
allowedUnguardedOperations: [],
|
|
356
|
+
contextFallback: "deny",
|
|
222
357
|
debug: false,
|
|
223
358
|
enableApplicationNameTagging: false,
|
|
224
359
|
enableRlsGucTagging: false
|
|
@@ -331,6 +466,63 @@ function stripSkipFlag(args) {
|
|
|
331
466
|
function isExcludedFromReadIsolation(model, config) {
|
|
332
467
|
return config.excludeModels.includes(model) || config.excludeModelsFromReadIsolation.includes(model);
|
|
333
468
|
}
|
|
469
|
+
/** Read a non-empty string `workspaceId` set directly on a where/data object. */
|
|
470
|
+
function readExplicitWorkspaceId(value) {
|
|
471
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
|
|
472
|
+
const workspaceId = value["workspaceId"];
|
|
473
|
+
return typeof workspaceId === "string" && workspaceId.length > 0 ? workspaceId : void 0;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* The single workspace an operation's own arguments pin it to, if any.
|
|
477
|
+
*
|
|
478
|
+
* Only a plain string `workspaceId` at the top level of `where` (reads, updates, deletes) or
|
|
479
|
+
* `data` (creates) counts. Filters such as `{ in: [...] }`, relation paths and compound unique
|
|
480
|
+
* keys do not, and a `createMany` whose rows name different workspaces does not either: those
|
|
481
|
+
* stay unresolved so strict mode still applies.
|
|
482
|
+
*
|
|
483
|
+
* @internal Exported for unit tests.
|
|
484
|
+
*/
|
|
485
|
+
function explicitWorkspaceIdFromArgs(operation, args) {
|
|
486
|
+
switch (operation) {
|
|
487
|
+
case "create": return readExplicitWorkspaceId(args["data"]);
|
|
488
|
+
case "createMany":
|
|
489
|
+
case "createManyAndReturn": {
|
|
490
|
+
const data = args["data"];
|
|
491
|
+
const rows = Array.isArray(data) ? data : [data];
|
|
492
|
+
if (rows.length === 0) return void 0;
|
|
493
|
+
const ids = rows.map(readExplicitWorkspaceId);
|
|
494
|
+
const first = ids[0];
|
|
495
|
+
return first !== void 0 && ids.every((id) => id === first) ? first : void 0;
|
|
496
|
+
}
|
|
497
|
+
case "upsert": {
|
|
498
|
+
const fromWhere = readExplicitWorkspaceId(args["where"]);
|
|
499
|
+
if (!fromWhere) return void 0;
|
|
500
|
+
const fromCreate = readExplicitWorkspaceId(args["create"]);
|
|
501
|
+
if (args["create"] && typeof args["create"] === "object" && "workspaceId" in args["create"]) return fromCreate === fromWhere ? fromWhere : void 0;
|
|
502
|
+
return fromWhere;
|
|
503
|
+
}
|
|
504
|
+
default: return readExplicitWorkspaceId(args["where"]);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Resolve the workspace context for one model operation.
|
|
509
|
+
*
|
|
510
|
+
* 1. A context bound to the async scope always wins (request scopes, withWorkspaceContext,
|
|
511
|
+
* withSystemScope). A bound request scope with no workspace stays without one, so strict
|
|
512
|
+
* mode keeps rejecting workspace-less requests exactly as before.
|
|
513
|
+
* 2. With NO scope bound (background work), an operation whose own arguments pin one
|
|
514
|
+
* workspace (`where.workspaceId` / `data.workspaceId`) runs scoped to that workspace, with
|
|
515
|
+
* no actor attributed and the RLS GUC set to it. This is the explicit-argument path.
|
|
516
|
+
* 3. Otherwise the deprecated warn-mode fallback, if the process opted in.
|
|
517
|
+
* 4. Otherwise nothing: strict mode throws, non-strict runs without injection.
|
|
518
|
+
*/
|
|
519
|
+
function resolveOperationContext(operation, args) {
|
|
520
|
+
const scoped = workspaceContextStore.getStore();
|
|
521
|
+
if (scoped) return scoped;
|
|
522
|
+
const explicitWorkspaceId = explicitWorkspaceIdFromArgs(operation, args);
|
|
523
|
+
if (explicitWorkspaceId) return { workspaceId: explicitWorkspaceId };
|
|
524
|
+
return getWorkspaceContext();
|
|
525
|
+
}
|
|
334
526
|
/**
|
|
335
527
|
* Enforce strict mode: throw if workspace context is missing and the operation
|
|
336
528
|
* is not explicitly allowed to run without it.
|
|
@@ -340,10 +532,14 @@ function isExcludedFromReadIsolation(model, config) {
|
|
|
340
532
|
*/
|
|
341
533
|
function enforceStrictMode(model, operation, wsCtx, config) {
|
|
342
534
|
if (wsCtx?.workspaceId) return;
|
|
535
|
+
if (isSystemScope(wsCtx)) {
|
|
536
|
+
noteSystemScopeOperation(wsCtx, model, operation);
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
343
539
|
if (!config.strictMode) return;
|
|
344
540
|
const opKey = `${model}.${operation}`;
|
|
345
541
|
if (config.allowedUnguardedOperations.includes(opKey)) return;
|
|
346
|
-
throw new Error(`[WorkspaceExtension] Workspace context required for ${model}.${operation}. No workspaceId
|
|
542
|
+
throw new Error(`[WorkspaceExtension] Workspace context required for ${model}.${operation}. No workspaceId is bound to the current async scope` + (wsCtx ? "" : " (background work must bind one explicitly)") + `. Either: (1) run the work inside withWorkspaceContext({ workspaceId, ... }, fn), (2) run genuinely cross-workspace work inside withSystemScope(reason, fn), (3) add "${model}" to excludeModels or "${opKey}" to allowedUnguardedOperations, or (4) pass _skipWorkspaceInjection: true in the query args.`);
|
|
347
543
|
}
|
|
348
544
|
/**
|
|
349
545
|
* WeakSet of contexts that have already had the connection tagged this request.
|
|
@@ -595,6 +791,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
595
791
|
logger.info("[WorkspaceExtension] Extension disabled by configuration");
|
|
596
792
|
return baseClient;
|
|
597
793
|
}
|
|
794
|
+
configureWorkspaceContextFallback(fullConfig.contextFallback);
|
|
598
795
|
const rawPrisma = baseClient;
|
|
599
796
|
const rawPrismaForContinuations = baseClient;
|
|
600
797
|
const transactionDelegate = baseClient.$transaction;
|
|
@@ -646,6 +843,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
646
843
|
excludeModels: fullConfig.excludeModels,
|
|
647
844
|
excludeModelsFromReadIsolation: fullConfig.excludeModelsFromReadIsolation,
|
|
648
845
|
allowedUnguardedOperations: fullConfig.allowedUnguardedOperations,
|
|
846
|
+
contextFallback: getWorkspaceContextFallbackMode(),
|
|
649
847
|
enableApplicationNameTagging: fullConfig.enableApplicationNameTagging,
|
|
650
848
|
enableRlsGucTagging: fullConfig.enableRlsGucTagging
|
|
651
849
|
});
|
|
@@ -661,7 +859,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
661
859
|
$allModels: {
|
|
662
860
|
async create({ model, args, query, __internalParams }) {
|
|
663
861
|
if (fullConfig.excludeModels.includes(model) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
664
|
-
const wsCtx =
|
|
862
|
+
const wsCtx = resolveOperationContext("create", args);
|
|
665
863
|
enforceStrictMode(model, "create", wsCtx, fullConfig);
|
|
666
864
|
if (!wsCtx?.workspaceId) return query(args);
|
|
667
865
|
const injectedData = injectCreateFields(args.data, wsCtx, fullConfig);
|
|
@@ -684,7 +882,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
684
882
|
},
|
|
685
883
|
async createMany({ model, args, query, __internalParams }) {
|
|
686
884
|
if (fullConfig.excludeModels.includes(model) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
687
|
-
const wsCtx =
|
|
885
|
+
const wsCtx = resolveOperationContext("createMany", args);
|
|
688
886
|
enforceStrictMode(model, "createMany", wsCtx, fullConfig);
|
|
689
887
|
if (!wsCtx?.workspaceId) return query(args);
|
|
690
888
|
const injectedData = (Array.isArray(args.data) ? args.data : [args.data]).map((item) => injectCreateFields(item, wsCtx, fullConfig));
|
|
@@ -700,7 +898,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
700
898
|
},
|
|
701
899
|
async createManyAndReturn({ model, args, query, __internalParams }) {
|
|
702
900
|
if (fullConfig.excludeModels.includes(model) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
703
|
-
const wsCtx =
|
|
901
|
+
const wsCtx = resolveOperationContext("createManyAndReturn", args);
|
|
704
902
|
enforceStrictMode(model, "createManyAndReturn", wsCtx, fullConfig);
|
|
705
903
|
if (!wsCtx?.workspaceId) return query(args);
|
|
706
904
|
const injectedData = (Array.isArray(args.data) ? args.data : [args.data]).map((item) => injectCreateFields(item, wsCtx, fullConfig));
|
|
@@ -716,7 +914,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
716
914
|
},
|
|
717
915
|
async update({ model, args, query, __internalParams }) {
|
|
718
916
|
if (fullConfig.excludeModels.includes(model) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
719
|
-
const wsCtx =
|
|
917
|
+
const wsCtx = resolveOperationContext("update", args);
|
|
720
918
|
enforceStrictMode(model, "update", wsCtx, fullConfig);
|
|
721
919
|
if (!wsCtx?.workspaceId) return query(args);
|
|
722
920
|
const injectedData = injectUpdateFields(args.data, wsCtx, fullConfig);
|
|
@@ -740,7 +938,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
740
938
|
},
|
|
741
939
|
async updateMany({ model, args, query, __internalParams }) {
|
|
742
940
|
if (fullConfig.excludeModels.includes(model) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
743
|
-
const wsCtx =
|
|
941
|
+
const wsCtx = resolveOperationContext("updateMany", args);
|
|
744
942
|
enforceStrictMode(model, "updateMany", wsCtx, fullConfig);
|
|
745
943
|
if (!wsCtx?.workspaceId) return query(args);
|
|
746
944
|
const injectedData = injectUpdateFields(args.data, wsCtx, fullConfig);
|
|
@@ -759,7 +957,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
759
957
|
},
|
|
760
958
|
async upsert({ model, args, query, __internalParams }) {
|
|
761
959
|
if (fullConfig.excludeModels.includes(model) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
762
|
-
const wsCtx =
|
|
960
|
+
const wsCtx = resolveOperationContext("upsert", args);
|
|
763
961
|
enforceStrictMode(model, "upsert", wsCtx, fullConfig);
|
|
764
962
|
if (!wsCtx?.workspaceId) return query(args);
|
|
765
963
|
const injectedCreate = injectCreateFields(args.create, wsCtx, fullConfig);
|
|
@@ -780,7 +978,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
780
978
|
},
|
|
781
979
|
async findMany({ model, args, query, __internalParams }) {
|
|
782
980
|
if (!fullConfig.enableReadIsolation || isExcludedFromReadIsolation(model, fullConfig) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
783
|
-
const wsCtx =
|
|
981
|
+
const wsCtx = resolveOperationContext("findMany", args);
|
|
784
982
|
enforceStrictMode(model, "findMany", wsCtx, fullConfig);
|
|
785
983
|
if (!wsCtx?.workspaceId) return query(args);
|
|
786
984
|
const injectedWhere = injectWhereClause(args.where, wsCtx);
|
|
@@ -797,7 +995,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
797
995
|
},
|
|
798
996
|
async findFirst({ model, args, query, __internalParams }) {
|
|
799
997
|
if (!fullConfig.enableReadIsolation || isExcludedFromReadIsolation(model, fullConfig) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
800
|
-
const wsCtx =
|
|
998
|
+
const wsCtx = resolveOperationContext("findFirst", args);
|
|
801
999
|
enforceStrictMode(model, "findFirst", wsCtx, fullConfig);
|
|
802
1000
|
if (!wsCtx?.workspaceId) return query(args);
|
|
803
1001
|
const injectedWhere = injectWhereClause(args.where, wsCtx);
|
|
@@ -814,7 +1012,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
814
1012
|
},
|
|
815
1013
|
async findFirstOrThrow({ model, args, query, __internalParams }) {
|
|
816
1014
|
if (!fullConfig.enableReadIsolation || isExcludedFromReadIsolation(model, fullConfig) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
817
|
-
const wsCtx =
|
|
1015
|
+
const wsCtx = resolveOperationContext("findFirstOrThrow", args);
|
|
818
1016
|
enforceStrictMode(model, "findFirstOrThrow", wsCtx, fullConfig);
|
|
819
1017
|
if (!wsCtx?.workspaceId) return query(args);
|
|
820
1018
|
const injectedWhere = injectWhereClause(args.where, wsCtx);
|
|
@@ -831,7 +1029,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
831
1029
|
},
|
|
832
1030
|
async findUnique({ model, args, query, __internalParams }) {
|
|
833
1031
|
if (!fullConfig.enableReadIsolation || isExcludedFromReadIsolation(model, fullConfig) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
834
|
-
const wsCtx =
|
|
1032
|
+
const wsCtx = resolveOperationContext("findUnique", args);
|
|
835
1033
|
enforceStrictMode(model, "findUnique", wsCtx, fullConfig);
|
|
836
1034
|
if (!wsCtx?.workspaceId) return query(args);
|
|
837
1035
|
const injectedWhere = injectWhereClause(args.where, wsCtx);
|
|
@@ -848,7 +1046,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
848
1046
|
},
|
|
849
1047
|
async findUniqueOrThrow({ model, args, query, __internalParams }) {
|
|
850
1048
|
if (!fullConfig.enableReadIsolation || isExcludedFromReadIsolation(model, fullConfig) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
851
|
-
const wsCtx =
|
|
1049
|
+
const wsCtx = resolveOperationContext("findUniqueOrThrow", args);
|
|
852
1050
|
enforceStrictMode(model, "findUniqueOrThrow", wsCtx, fullConfig);
|
|
853
1051
|
if (!wsCtx?.workspaceId) return query(args);
|
|
854
1052
|
const injectedWhere = injectWhereClause(args.where, wsCtx);
|
|
@@ -865,7 +1063,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
865
1063
|
},
|
|
866
1064
|
async count({ model, args, query, __internalParams }) {
|
|
867
1065
|
if (!fullConfig.enableReadIsolation || isExcludedFromReadIsolation(model, fullConfig) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
868
|
-
const wsCtx =
|
|
1066
|
+
const wsCtx = resolveOperationContext("count", args);
|
|
869
1067
|
enforceStrictMode(model, "count", wsCtx, fullConfig);
|
|
870
1068
|
if (!wsCtx?.workspaceId) return query(args);
|
|
871
1069
|
const injectedWhere = injectWhereClause(args.where, wsCtx);
|
|
@@ -882,7 +1080,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
882
1080
|
},
|
|
883
1081
|
async aggregate({ model, args, query, __internalParams }) {
|
|
884
1082
|
if (!fullConfig.enableReadIsolation || isExcludedFromReadIsolation(model, fullConfig) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
885
|
-
const wsCtx =
|
|
1083
|
+
const wsCtx = resolveOperationContext("aggregate", args);
|
|
886
1084
|
enforceStrictMode(model, "aggregate", wsCtx, fullConfig);
|
|
887
1085
|
if (!wsCtx?.workspaceId) return query(args);
|
|
888
1086
|
const injectedWhere = injectWhereClause(args.where, wsCtx);
|
|
@@ -899,7 +1097,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
899
1097
|
},
|
|
900
1098
|
async groupBy({ model, args, query, __internalParams }) {
|
|
901
1099
|
if (!fullConfig.enableReadIsolation || isExcludedFromReadIsolation(model, fullConfig) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
902
|
-
const wsCtx =
|
|
1100
|
+
const wsCtx = resolveOperationContext("groupBy", args);
|
|
903
1101
|
enforceStrictMode(model, "groupBy", wsCtx, fullConfig);
|
|
904
1102
|
if (!wsCtx?.workspaceId) return query(args);
|
|
905
1103
|
const injectedWhere = injectWhereClause(args.where, wsCtx);
|
|
@@ -916,7 +1114,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
916
1114
|
},
|
|
917
1115
|
async delete({ model, args, query, __internalParams }) {
|
|
918
1116
|
if (!fullConfig.enableDeleteIsolation || isExcludedFromReadIsolation(model, fullConfig) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
919
|
-
const wsCtx =
|
|
1117
|
+
const wsCtx = resolveOperationContext("delete", args);
|
|
920
1118
|
enforceStrictMode(model, "delete", wsCtx, fullConfig);
|
|
921
1119
|
if (!wsCtx?.workspaceId) return query(args);
|
|
922
1120
|
const injectedWhere = injectWhereClause(args.where, wsCtx);
|
|
@@ -933,7 +1131,7 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
933
1131
|
},
|
|
934
1132
|
async deleteMany({ model, args, query, __internalParams }) {
|
|
935
1133
|
if (!fullConfig.enableDeleteIsolation || isExcludedFromReadIsolation(model, fullConfig) || args._skipWorkspaceInjection) return query(stripSkipFlag(args));
|
|
936
|
-
const wsCtx =
|
|
1134
|
+
const wsCtx = resolveOperationContext("deleteMany", args);
|
|
937
1135
|
enforceStrictMode(model, "deleteMany", wsCtx, fullConfig);
|
|
938
1136
|
if (!wsCtx?.workspaceId) return query(args);
|
|
939
1137
|
const injectedWhere = injectWhereClause(args.where, wsCtx);
|
|
@@ -975,5 +1173,5 @@ function addWorkspaceExtension(baseClient, config) {
|
|
|
975
1173
|
} } });
|
|
976
1174
|
}
|
|
977
1175
|
|
|
978
|
-
export {
|
|
979
|
-
//# sourceMappingURL=workspace-extension-
|
|
1176
|
+
export { withSystemScopeSync as C, withSystemScope as S, withWorkspaceContextSync as T, resetCallerTransactionStateForTests as _, defaultWorkspaceExtensionConfig as a, runOperationWithRlsGuc as b, getScopedWorkspaceContext as c, injectCreateFields as d, injectWhereWorkspaceId as f, requireWorkspaceContext as g, maybeTagConnection as h, configureWorkspaceContextFallback as i, getWorkspaceContext as l, maybeSetRlsGuc as m, addWorkspaceExtension as n, explicitWorkspaceIdFromArgs as o, isSystemScope as p, buildTransactionContextStatement as r, getCallerTransaction as s, WORKSPACE_CONTEXT_WARN_MODE_REMOVAL_DATE as t, getWorkspaceContextFallbackMode as u, resetWorkspaceContextStateForTests as v, withWorkspaceContext as w, setWorkspaceContext as x, runOperationInCallerTransaction as y };
|
|
1177
|
+
//# sourceMappingURL=workspace-extension-ByopOwtK.js.map
|