@librechat/data-schemas 0.0.46 → 0.0.48
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/admin/capabilities.cjs +14 -0
- package/dist/admin/capabilities.cjs.map +1 -1
- package/dist/admin/capabilities.es.js +14 -1
- package/dist/admin/capabilities.es.js.map +1 -1
- package/dist/app/resolution.cjs +60 -1
- package/dist/app/resolution.cjs.map +1 -1
- package/dist/app/resolution.es.js +60 -1
- package/dist/app/resolution.es.js.map +1 -1
- package/dist/config/tenantContext.cjs +13 -0
- package/dist/config/tenantContext.cjs.map +1 -1
- package/dist/config/tenantContext.es.js +13 -1
- package/dist/config/tenantContext.es.js.map +1 -1
- package/dist/index.cjs +8 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.es.js +4 -2
- package/dist/index.es.js.map +1 -1
- package/dist/methods/aclEntry.cjs +2 -1
- package/dist/methods/aclEntry.cjs.map +1 -1
- package/dist/methods/aclEntry.es.js +2 -1
- package/dist/methods/aclEntry.es.js.map +1 -1
- package/dist/methods/agentCategory.cjs +4 -2
- package/dist/methods/agentCategory.cjs.map +1 -1
- package/dist/methods/agentCategory.es.js +4 -2
- package/dist/methods/agentCategory.es.js.map +1 -1
- package/dist/methods/conversation.cjs +3 -2
- package/dist/methods/conversation.cjs.map +1 -1
- package/dist/methods/conversation.es.js +3 -2
- package/dist/methods/conversation.es.js.map +1 -1
- package/dist/methods/conversationTag.cjs +3 -2
- package/dist/methods/conversationTag.cjs.map +1 -1
- package/dist/methods/conversationTag.es.js +3 -2
- package/dist/methods/conversationTag.es.js.map +1 -1
- package/dist/methods/file.cjs +2 -1
- package/dist/methods/file.cjs.map +1 -1
- package/dist/methods/file.es.js +2 -1
- package/dist/methods/file.es.js.map +1 -1
- package/dist/methods/index.cjs +1 -0
- package/dist/methods/index.cjs.map +1 -1
- package/dist/methods/index.es.js +1 -0
- package/dist/methods/index.es.js.map +1 -1
- package/dist/methods/message.cjs +2 -1
- package/dist/methods/message.cjs.map +1 -1
- package/dist/methods/message.es.js +2 -1
- package/dist/methods/message.es.js.map +1 -1
- package/dist/methods/prompt.cjs +25 -5
- package/dist/methods/prompt.cjs.map +1 -1
- package/dist/methods/prompt.es.js +25 -5
- package/dist/methods/prompt.es.js.map +1 -1
- package/dist/methods/role.cjs +165 -9
- package/dist/methods/role.cjs.map +1 -1
- package/dist/methods/role.es.js +166 -11
- package/dist/methods/role.es.js.map +1 -1
- package/dist/methods/systemGrant.cjs +155 -25
- package/dist/methods/systemGrant.cjs.map +1 -1
- package/dist/methods/systemGrant.es.js +155 -25
- package/dist/methods/systemGrant.es.js.map +1 -1
- package/dist/methods/tx.cjs.map +1 -1
- package/dist/methods/tx.es.js.map +1 -1
- package/dist/methods/user.cjs +19 -2
- package/dist/methods/user.cjs.map +1 -1
- package/dist/methods/user.es.js +19 -2
- package/dist/methods/user.es.js.map +1 -1
- package/dist/methods/userGroup.cjs +84 -12
- package/dist/methods/userGroup.cjs.map +1 -1
- package/dist/methods/userGroup.es.js +84 -12
- package/dist/methods/userGroup.es.js.map +1 -1
- package/dist/migrations/promptGroupIndexes.cjs.map +1 -1
- package/dist/migrations/promptGroupIndexes.es.js.map +1 -1
- package/dist/migrations/tenantIndexes.cjs +4 -2
- package/dist/migrations/tenantIndexes.cjs.map +1 -1
- package/dist/migrations/tenantIndexes.es.js +4 -2
- package/dist/migrations/tenantIndexes.es.js.map +1 -1
- package/dist/schema/agent.cjs +1 -2
- package/dist/schema/agent.cjs.map +1 -1
- package/dist/schema/agent.es.js +1 -2
- package/dist/schema/agent.es.js.map +1 -1
- package/dist/schema/convo.cjs +0 -1
- package/dist/schema/convo.cjs.map +1 -1
- package/dist/schema/convo.es.js +0 -1
- package/dist/schema/convo.es.js.map +1 -1
- package/dist/schema/message.cjs +0 -1
- package/dist/schema/message.cjs.map +1 -1
- package/dist/schema/message.es.js +0 -1
- package/dist/schema/message.es.js.map +1 -1
- package/dist/schema/preset.cjs +1 -1
- package/dist/schema/preset.cjs.map +1 -1
- package/dist/schema/preset.es.js +1 -1
- package/dist/schema/preset.es.js.map +1 -1
- package/dist/schema/role.cjs +1 -0
- package/dist/schema/role.cjs.map +1 -1
- package/dist/schema/role.es.js +1 -0
- package/dist/schema/role.es.js.map +1 -1
- package/dist/schema/systemGrant.cjs +2 -4
- package/dist/schema/systemGrant.cjs.map +1 -1
- package/dist/schema/systemGrant.es.js +3 -5
- package/dist/schema/systemGrant.es.js.map +1 -1
- package/dist/schema/user.cjs +1 -0
- package/dist/schema/user.cjs.map +1 -1
- package/dist/schema/user.es.js +1 -0
- package/dist/schema/user.es.js.map +1 -1
- package/dist/types/admin/capabilities.d.ts +5 -0
- package/dist/types/config/tenantContext.d.ts +6 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/methods/aclEntry.d.ts +2 -2
- package/dist/types/methods/index.d.ts +3 -2
- package/dist/types/methods/role.d.ts +19 -6
- package/dist/types/methods/systemGrant.d.ts +29 -0
- package/dist/types/methods/tx.d.ts +1 -1
- package/dist/types/methods/user.d.ts +5 -0
- package/dist/types/methods/userGroup.d.ts +15 -3
- package/dist/types/schema/user.d.ts +2 -2
- package/dist/types/types/admin.d.ts +17 -4
- package/dist/types/types/role.d.ts +3 -0
- package/dist/types/types/user.d.ts +9 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/tenantBulkWrite.d.ts +45 -0
- package/dist/utils/tenantBulkWrite.cjs +85 -0
- package/dist/utils/tenantBulkWrite.cjs.map +1 -0
- package/dist/utils/tenantBulkWrite.es.js +83 -0
- package/dist/utils/tenantBulkWrite.es.js.map +1 -0
- package/package.json +1 -1
|
@@ -28,6 +28,7 @@ import type { Document } from 'mongoose';
|
|
|
28
28
|
import { CursorPaginationParams } from '~/common';
|
|
29
29
|
export interface IRole extends Document {
|
|
30
30
|
name: string;
|
|
31
|
+
description?: string;
|
|
31
32
|
permissions: {};
|
|
32
33
|
tenantId?: string;
|
|
33
34
|
}
|
|
@@ -35,10 +36,12 @@ export type RolePermissions = IRole['permissions'];
|
|
|
35
36
|
export type RolePermissionsInput = DeepPartial<RolePermissions>;
|
|
36
37
|
export interface CreateRoleRequest {
|
|
37
38
|
name: string;
|
|
39
|
+
description?: string;
|
|
38
40
|
permissions: RolePermissionsInput;
|
|
39
41
|
}
|
|
40
42
|
export interface UpdateRoleRequest {
|
|
41
43
|
name?: string;
|
|
44
|
+
description?: string;
|
|
42
45
|
permissions?: RolePermissionsInput;
|
|
43
46
|
}
|
|
44
47
|
export interface RoleFilterOptions extends CursorPaginationParams {
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
import type { Document, Types } from 'mongoose';
|
|
27
27
|
import { CursorPaginationParams } from '~/common';
|
|
28
28
|
export interface IUser extends Document {
|
|
29
|
+
_id: Types.ObjectId;
|
|
29
30
|
name?: string;
|
|
30
31
|
username?: string;
|
|
31
32
|
email: string;
|
|
@@ -74,6 +75,14 @@ export interface IUser extends Document {
|
|
|
74
75
|
/** Field for external source identification (for consistency with TPrincipal schema) */
|
|
75
76
|
idOnTheSource?: string;
|
|
76
77
|
tenantId?: string;
|
|
78
|
+
federatedTokens?: OIDCTokens;
|
|
79
|
+
openidTokens?: OIDCTokens;
|
|
80
|
+
}
|
|
81
|
+
export interface OIDCTokens {
|
|
82
|
+
access_token?: string;
|
|
83
|
+
id_token?: string;
|
|
84
|
+
refresh_token?: string;
|
|
85
|
+
expires_at?: number;
|
|
77
86
|
}
|
|
78
87
|
export interface BalanceConfig {
|
|
79
88
|
enabled?: boolean;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
26
|
+
import type { AnyBulkWriteOperation, Model, MongooseBulkWriteOptions } from 'mongoose';
|
|
27
|
+
import type { BulkWriteResult } from 'mongodb';
|
|
28
|
+
/** Resets the cached strict-mode flag. Exposed for test teardown only. */
|
|
29
|
+
export declare function _resetBulkWriteStrictCache(): void;
|
|
30
|
+
/**
|
|
31
|
+
* Tenant-safe wrapper around Mongoose `Model.bulkWrite()`.
|
|
32
|
+
*
|
|
33
|
+
* Mongoose's `bulkWrite` does not trigger schema-level middleware hooks, so the
|
|
34
|
+
* `applyTenantIsolation` plugin cannot intercept it. This wrapper injects the
|
|
35
|
+
* current ALS tenant context into every operation's filter and/or document
|
|
36
|
+
* before delegating to the native `bulkWrite`.
|
|
37
|
+
*
|
|
38
|
+
* Behavior:
|
|
39
|
+
* - **tenantId present** (normal request): injects `{ tenantId }` into every
|
|
40
|
+
* operation filter (updateOne, deleteOne, replaceOne) and document (insertOne).
|
|
41
|
+
* - **SYSTEM_TENANT_ID**: skips injection (cross-tenant system operation).
|
|
42
|
+
* - **No tenantId + strict mode**: throws (fail-closed, same as the plugin).
|
|
43
|
+
* - **No tenantId + non-strict**: passes through without injection (backward compat).
|
|
44
|
+
*/
|
|
45
|
+
export declare function tenantSafeBulkWrite<T>(model: Model<T>, ops: AnyBulkWriteOperation[], options?: MongooseBulkWriteOptions): Promise<BulkWriteResult>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tenantContext = require('../config/tenantContext.cjs');
|
|
4
|
+
var winston = require('../config/winston.cjs');
|
|
5
|
+
|
|
6
|
+
let _strictMode;
|
|
7
|
+
function isStrict() {
|
|
8
|
+
return (_strictMode !== null && _strictMode !== void 0 ? _strictMode : (_strictMode = process.env.TENANT_ISOLATION_STRICT === 'true'));
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Tenant-safe wrapper around Mongoose `Model.bulkWrite()`.
|
|
12
|
+
*
|
|
13
|
+
* Mongoose's `bulkWrite` does not trigger schema-level middleware hooks, so the
|
|
14
|
+
* `applyTenantIsolation` plugin cannot intercept it. This wrapper injects the
|
|
15
|
+
* current ALS tenant context into every operation's filter and/or document
|
|
16
|
+
* before delegating to the native `bulkWrite`.
|
|
17
|
+
*
|
|
18
|
+
* Behavior:
|
|
19
|
+
* - **tenantId present** (normal request): injects `{ tenantId }` into every
|
|
20
|
+
* operation filter (updateOne, deleteOne, replaceOne) and document (insertOne).
|
|
21
|
+
* - **SYSTEM_TENANT_ID**: skips injection (cross-tenant system operation).
|
|
22
|
+
* - **No tenantId + strict mode**: throws (fail-closed, same as the plugin).
|
|
23
|
+
* - **No tenantId + non-strict**: passes through without injection (backward compat).
|
|
24
|
+
*/
|
|
25
|
+
async function tenantSafeBulkWrite(model, ops, options) {
|
|
26
|
+
const tenantId = tenantContext.getTenantId();
|
|
27
|
+
if (!tenantId) {
|
|
28
|
+
if (isStrict()) {
|
|
29
|
+
throw new Error(`[TenantIsolation] bulkWrite on ${model.modelName} attempted without tenant context in strict mode`);
|
|
30
|
+
}
|
|
31
|
+
return model.bulkWrite(ops, options);
|
|
32
|
+
}
|
|
33
|
+
if (tenantId === tenantContext.SYSTEM_TENANT_ID) {
|
|
34
|
+
return model.bulkWrite(ops, options);
|
|
35
|
+
}
|
|
36
|
+
const injected = ops.map((op) => injectTenantId(op, tenantId));
|
|
37
|
+
return model.bulkWrite(injected, options);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Injects `tenantId` into a single bulk-write operation.
|
|
41
|
+
* Returns a new operation object — does not mutate the original.
|
|
42
|
+
*/
|
|
43
|
+
function injectTenantId(op, tenantId) {
|
|
44
|
+
if ('insertOne' in op) {
|
|
45
|
+
return {
|
|
46
|
+
insertOne: {
|
|
47
|
+
document: { ...op.insertOne.document, tenantId },
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
if ('updateOne' in op) {
|
|
52
|
+
const { filter, ...rest } = op.updateOne;
|
|
53
|
+
return { updateOne: { ...rest, filter: { ...filter, tenantId } } };
|
|
54
|
+
}
|
|
55
|
+
if ('updateMany' in op) {
|
|
56
|
+
const { filter, ...rest } = op.updateMany;
|
|
57
|
+
return { updateMany: { ...rest, filter: { ...filter, tenantId } } };
|
|
58
|
+
}
|
|
59
|
+
if ('deleteOne' in op) {
|
|
60
|
+
const { filter, ...rest } = op.deleteOne;
|
|
61
|
+
return { deleteOne: { ...rest, filter: { ...filter, tenantId } } };
|
|
62
|
+
}
|
|
63
|
+
if ('deleteMany' in op) {
|
|
64
|
+
const { filter, ...rest } = op.deleteMany;
|
|
65
|
+
return { deleteMany: { ...rest, filter: { ...filter, tenantId } } };
|
|
66
|
+
}
|
|
67
|
+
if ('replaceOne' in op) {
|
|
68
|
+
const { filter, replacement, ...rest } = op.replaceOne;
|
|
69
|
+
return {
|
|
70
|
+
replaceOne: {
|
|
71
|
+
...rest,
|
|
72
|
+
filter: { ...filter, tenantId },
|
|
73
|
+
replacement: { ...replacement, tenantId },
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (isStrict()) {
|
|
78
|
+
throw new Error('[TenantIsolation] Unknown bulkWrite operation type in strict mode — refusing to pass through without tenant injection');
|
|
79
|
+
}
|
|
80
|
+
winston.warn('[tenantSafeBulkWrite] Unknown bulk op type, passing through without tenant injection');
|
|
81
|
+
return op;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
exports.tenantSafeBulkWrite = tenantSafeBulkWrite;
|
|
85
|
+
//# sourceMappingURL=tenantBulkWrite.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenantBulkWrite.cjs","sources":["../../src/utils/tenantBulkWrite.ts"],"sourcesContent":[null],"names":["getTenantId","SYSTEM_TENANT_ID","logger"],"mappings":";;;;;AAKA,IAAI,WAAgC;AAEpC,SAAS,QAAQ,GAAA;AACf,IAAA,QAAQ,WAAW,KAAA,IAAA,IAAX,WAAW,KAAA,MAAA,GAAX,WAAW,IAAX,WAAW,GAAK,OAAO,CAAC,GAAG,CAAC,uBAAuB,KAAK,MAAM;AACxE;AAOA;;;;;;;;;;;;;;AAcG;AACI,eAAe,mBAAmB,CACvC,KAAe,EACf,GAA4B,EAC5B,OAAkC,EAAA;AAElC,IAAA,MAAM,QAAQ,GAAGA,yBAAW,EAAE;IAE9B,IAAI,CAAC,QAAQ,EAAE;QACb,IAAI,QAAQ,EAAE,EAAE;YACd,MAAM,IAAI,KAAK,CACb,CAAA,+BAAA,EAAkC,KAAK,CAAC,SAAS,CAAA,gDAAA,CAAkD,CACpG;QACH;QACA,OAAO,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC;IACtC;AAEA,IAAA,IAAI,QAAQ,KAAKC,8BAAgB,EAAE;QACjC,OAAO,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC;IACtC;AAEA,IAAA,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E,GAAG,EAAE,CAAC,UAAU;AACzC,QAAA,OAAO,EAAE,UAAU,EAAE,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,EAAE,GAAG,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE;IACrE;AAEA,IAAA,IAAI,YAAY,IAAI,EAAE,EAAE;AACtB,QAAA,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,CAAC,UAAU;QACtD,OAAO;AACL,YAAA,UAAU,EAAE;AACV,gBAAA,GAAG,IAAI;AACP,gBAAA,MAAM,EAAE,EAAE,GAAG,MAAM,EAAE,QAAQ,EAAE;AAC/B,gBAAA,WAAW,EAAE,EAAE,GAAG,WAAW,EAAE,QAAQ,EAAE;AAC1C,aAAA;SACF;IACH;IAEA,IAAI,QAAQ,EAAE,EAAE;AACd,QAAA,MAAM,IAAI,KAAK,CACb,uHAAuH,CACxH;IACH;AACA,IAAAC,OAAM,CAAC,IAAI,CACT,sFAAsF,CACvF;AACD,IAAA,OAAO,EAAE;AACX;;;;"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { getTenantId, SYSTEM_TENANT_ID } from '../config/tenantContext.es.js';
|
|
2
|
+
import logger from '../config/winston.es.js';
|
|
3
|
+
|
|
4
|
+
let _strictMode;
|
|
5
|
+
function isStrict() {
|
|
6
|
+
return (_strictMode !== null && _strictMode !== void 0 ? _strictMode : (_strictMode = process.env.TENANT_ISOLATION_STRICT === 'true'));
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Tenant-safe wrapper around Mongoose `Model.bulkWrite()`.
|
|
10
|
+
*
|
|
11
|
+
* Mongoose's `bulkWrite` does not trigger schema-level middleware hooks, so the
|
|
12
|
+
* `applyTenantIsolation` plugin cannot intercept it. This wrapper injects the
|
|
13
|
+
* current ALS tenant context into every operation's filter and/or document
|
|
14
|
+
* before delegating to the native `bulkWrite`.
|
|
15
|
+
*
|
|
16
|
+
* Behavior:
|
|
17
|
+
* - **tenantId present** (normal request): injects `{ tenantId }` into every
|
|
18
|
+
* operation filter (updateOne, deleteOne, replaceOne) and document (insertOne).
|
|
19
|
+
* - **SYSTEM_TENANT_ID**: skips injection (cross-tenant system operation).
|
|
20
|
+
* - **No tenantId + strict mode**: throws (fail-closed, same as the plugin).
|
|
21
|
+
* - **No tenantId + non-strict**: passes through without injection (backward compat).
|
|
22
|
+
*/
|
|
23
|
+
async function tenantSafeBulkWrite(model, ops, options) {
|
|
24
|
+
const tenantId = getTenantId();
|
|
25
|
+
if (!tenantId) {
|
|
26
|
+
if (isStrict()) {
|
|
27
|
+
throw new Error(`[TenantIsolation] bulkWrite on ${model.modelName} attempted without tenant context in strict mode`);
|
|
28
|
+
}
|
|
29
|
+
return model.bulkWrite(ops, options);
|
|
30
|
+
}
|
|
31
|
+
if (tenantId === SYSTEM_TENANT_ID) {
|
|
32
|
+
return model.bulkWrite(ops, options);
|
|
33
|
+
}
|
|
34
|
+
const injected = ops.map((op) => injectTenantId(op, tenantId));
|
|
35
|
+
return model.bulkWrite(injected, options);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Injects `tenantId` into a single bulk-write operation.
|
|
39
|
+
* Returns a new operation object — does not mutate the original.
|
|
40
|
+
*/
|
|
41
|
+
function injectTenantId(op, tenantId) {
|
|
42
|
+
if ('insertOne' in op) {
|
|
43
|
+
return {
|
|
44
|
+
insertOne: {
|
|
45
|
+
document: { ...op.insertOne.document, tenantId },
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
if ('updateOne' in op) {
|
|
50
|
+
const { filter, ...rest } = op.updateOne;
|
|
51
|
+
return { updateOne: { ...rest, filter: { ...filter, tenantId } } };
|
|
52
|
+
}
|
|
53
|
+
if ('updateMany' in op) {
|
|
54
|
+
const { filter, ...rest } = op.updateMany;
|
|
55
|
+
return { updateMany: { ...rest, filter: { ...filter, tenantId } } };
|
|
56
|
+
}
|
|
57
|
+
if ('deleteOne' in op) {
|
|
58
|
+
const { filter, ...rest } = op.deleteOne;
|
|
59
|
+
return { deleteOne: { ...rest, filter: { ...filter, tenantId } } };
|
|
60
|
+
}
|
|
61
|
+
if ('deleteMany' in op) {
|
|
62
|
+
const { filter, ...rest } = op.deleteMany;
|
|
63
|
+
return { deleteMany: { ...rest, filter: { ...filter, tenantId } } };
|
|
64
|
+
}
|
|
65
|
+
if ('replaceOne' in op) {
|
|
66
|
+
const { filter, replacement, ...rest } = op.replaceOne;
|
|
67
|
+
return {
|
|
68
|
+
replaceOne: {
|
|
69
|
+
...rest,
|
|
70
|
+
filter: { ...filter, tenantId },
|
|
71
|
+
replacement: { ...replacement, tenantId },
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (isStrict()) {
|
|
76
|
+
throw new Error('[TenantIsolation] Unknown bulkWrite operation type in strict mode — refusing to pass through without tenant injection');
|
|
77
|
+
}
|
|
78
|
+
logger.warn('[tenantSafeBulkWrite] Unknown bulk op type, passing through without tenant injection');
|
|
79
|
+
return op;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { tenantSafeBulkWrite };
|
|
83
|
+
//# sourceMappingURL=tenantBulkWrite.es.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenantBulkWrite.es.js","sources":["../../src/utils/tenantBulkWrite.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAKA,IAAI,WAAgC;AAEpC,SAAS,QAAQ,GAAA;AACf,IAAA,QAAQ,WAAW,KAAA,IAAA,IAAX,WAAW,KAAA,MAAA,GAAX,WAAW,IAAX,WAAW,GAAK,OAAO,CAAC,GAAG,CAAC,uBAAuB,KAAK,MAAM;AACxE;AAOA;;;;;;;;;;;;;;AAcG;AACI,eAAe,mBAAmB,CACvC,KAAe,EACf,GAA4B,EAC5B,OAAkC,EAAA;AAElC,IAAA,MAAM,QAAQ,GAAG,WAAW,EAAE;IAE9B,IAAI,CAAC,QAAQ,EAA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