@almatar/branding 1.0.0-beta.3.7 → 1.0.0-beta.3.8
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.
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for tenant scope when schema uses 'brands' (array) instead of 'brand' (single).
|
|
3
|
+
*/
|
|
4
|
+
export interface IMongooseTenantOptions {
|
|
5
|
+
/** Column name: 'brand' (default) or 'brands'. Use 'brands' when schema has an array of brands. */
|
|
6
|
+
brandColumn?: 'brand' | 'brands';
|
|
7
|
+
}
|
|
1
8
|
/**
|
|
2
9
|
* Utility class for manually applying tenant scoping in Mongoose repositories
|
|
3
10
|
* This allows you to use regular MongooseModule and apply brand filters manually
|
|
4
11
|
* when needed, giving you more control over when tenant scoping is applied.
|
|
12
|
+
* Supports both 'brand' (single) and 'brands' (array) schema fields.
|
|
5
13
|
*/
|
|
6
14
|
export declare class MongooseTenantHelper {
|
|
7
15
|
/**
|
|
@@ -15,33 +23,35 @@ export declare class MongooseTenantHelper {
|
|
|
15
23
|
*/
|
|
16
24
|
static getBrandForDocument(): string | null;
|
|
17
25
|
/**
|
|
18
|
-
* Add brand filter to a query object
|
|
19
|
-
*
|
|
26
|
+
* Add brand filter to a query object.
|
|
27
|
+
* Supports both 'brand' (single) and 'brands' (array) - filter is always fieldName: { $in: contextBrands }.
|
|
20
28
|
* @param query - The query object to add brand filter to
|
|
29
|
+
* @param options - Optional. Use { brandColumn: 'brands' } when schema has brands array
|
|
21
30
|
* @returns The query object with brand filter added (if brand context exists)
|
|
22
31
|
*/
|
|
23
|
-
static addBrandFilter(query: any): any;
|
|
32
|
+
static addBrandFilter(query: any, options?: IMongooseTenantOptions): any;
|
|
24
33
|
/**
|
|
25
|
-
* Add brand filter to an aggregation pipeline
|
|
26
|
-
* Use this when building aggregate() queries manually
|
|
34
|
+
* Add brand filter to an aggregation pipeline.
|
|
27
35
|
* @param pipeline - The aggregation pipeline array
|
|
36
|
+
* @param options - Optional. Use { brandColumn: 'brands' } when schema has brands array
|
|
28
37
|
* @returns The pipeline with brand filter added (if brand context exists)
|
|
29
38
|
*/
|
|
30
|
-
static addBrandFilterToPipeline(pipeline
|
|
39
|
+
static addBrandFilterToPipeline(pipeline?: any[], options?: IMongooseTenantOptions): any[];
|
|
31
40
|
/**
|
|
32
|
-
* Set brand on a document before save
|
|
33
|
-
*
|
|
41
|
+
* Set brand on a document before save.
|
|
42
|
+
* For 'brand': sets document.brand. For 'brands': ensures context brand is in document.brands array.
|
|
34
43
|
* @param document - The document object to set brand on
|
|
35
|
-
* @
|
|
44
|
+
* @param options - Optional. Use { brandColumn: 'brands' } when schema has brands array
|
|
45
|
+
* @returns The document with brand set (if brand context exists)
|
|
36
46
|
*/
|
|
37
|
-
static setBrandOnDocument(document: any): any;
|
|
47
|
+
static setBrandOnDocument(document: any, options?: IMongooseTenantOptions): any;
|
|
38
48
|
/**
|
|
39
|
-
* Set brand on multiple documents before insertMany
|
|
40
|
-
* Use this when inserting multiple documents manually
|
|
49
|
+
* Set brand on multiple documents before insertMany.
|
|
41
50
|
* @param documents - Array of document objects to set brand on
|
|
51
|
+
* @param options - Optional. Use { brandColumn: 'brands' } when schema has brands array
|
|
42
52
|
* @returns The documents array with brand set (if brand context exists)
|
|
43
53
|
*/
|
|
44
|
-
static setBrandOnDocuments(documents: any[]): any[];
|
|
54
|
+
static setBrandOnDocuments(documents: any[], options?: IMongooseTenantOptions): any[];
|
|
45
55
|
/**
|
|
46
56
|
* Check if brand context exists
|
|
47
57
|
* @returns true if brand context is available, false otherwise
|
|
@@ -6,10 +6,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.MongooseTenantHelper = void 0;
|
|
7
7
|
const AlmatarBranding_1 = __importDefault(require("../AlmatarBranding"));
|
|
8
8
|
const Storage_1 = __importDefault(require("../Storage"));
|
|
9
|
+
const DEFAULT_BRAND_COLUMN = 'brand';
|
|
9
10
|
/**
|
|
10
11
|
* Utility class for manually applying tenant scoping in Mongoose repositories
|
|
11
12
|
* This allows you to use regular MongooseModule and apply brand filters manually
|
|
12
13
|
* when needed, giving you more control over when tenant scoping is applied.
|
|
14
|
+
* Supports both 'brand' (single) and 'brands' (array) schema fields.
|
|
13
15
|
*/
|
|
14
16
|
class MongooseTenantHelper {
|
|
15
17
|
/**
|
|
@@ -30,27 +32,28 @@ class MongooseTenantHelper {
|
|
|
30
32
|
return contextBrands && contextBrands.length > 0 ? contextBrands[0] : null;
|
|
31
33
|
}
|
|
32
34
|
/**
|
|
33
|
-
* Add brand filter to a query object
|
|
34
|
-
*
|
|
35
|
+
* Add brand filter to a query object.
|
|
36
|
+
* Supports both 'brand' (single) and 'brands' (array) - filter is always fieldName: { $in: contextBrands }.
|
|
35
37
|
* @param query - The query object to add brand filter to
|
|
38
|
+
* @param options - Optional. Use { brandColumn: 'brands' } when schema has brands array
|
|
36
39
|
* @returns The query object with brand filter added (if brand context exists)
|
|
37
40
|
*/
|
|
38
|
-
static addBrandFilter(query) {
|
|
41
|
+
static addBrandFilter(query, options) {
|
|
42
|
+
var _a;
|
|
39
43
|
const contextBrands = this.getBrandContext();
|
|
44
|
+
const column = (_a = options === null || options === void 0 ? void 0 : options.brandColumn) !== null && _a !== void 0 ? _a : DEFAULT_BRAND_COLUMN;
|
|
40
45
|
if (contextBrands && contextBrands.length > 0) {
|
|
41
|
-
|
|
46
|
+
const brandFilter = { [column]: { $in: contextBrands } };
|
|
42
47
|
if (query.$and) {
|
|
43
|
-
query.$and.push(
|
|
48
|
+
query.$and.push(brandFilter);
|
|
44
49
|
}
|
|
45
50
|
else if (Object.keys(query).length > 0) {
|
|
46
|
-
|
|
47
|
-
query = { $and: [query, { brand: { $in: contextBrands } }] };
|
|
51
|
+
query = { $and: [query, brandFilter] };
|
|
48
52
|
}
|
|
49
53
|
else {
|
|
50
|
-
|
|
51
|
-
query.brand = { $in: contextBrands };
|
|
54
|
+
query[column] = { $in: contextBrands };
|
|
52
55
|
}
|
|
53
|
-
AlmatarBranding_1.default.log('[MongooseTenantHelper] Added brand filter to query:',
|
|
56
|
+
AlmatarBranding_1.default.log('[MongooseTenantHelper] Added brand filter to query:', brandFilter);
|
|
54
57
|
}
|
|
55
58
|
else {
|
|
56
59
|
AlmatarBranding_1.default.log('[MongooseTenantHelper] No brand context, skipping brand filter');
|
|
@@ -58,27 +61,26 @@ class MongooseTenantHelper {
|
|
|
58
61
|
return query;
|
|
59
62
|
}
|
|
60
63
|
/**
|
|
61
|
-
* Add brand filter to an aggregation pipeline
|
|
62
|
-
* Use this when building aggregate() queries manually
|
|
64
|
+
* Add brand filter to an aggregation pipeline.
|
|
63
65
|
* @param pipeline - The aggregation pipeline array
|
|
66
|
+
* @param options - Optional. Use { brandColumn: 'brands' } when schema has brands array
|
|
64
67
|
* @returns The pipeline with brand filter added (if brand context exists)
|
|
65
68
|
*/
|
|
66
|
-
static addBrandFilterToPipeline(pipeline) {
|
|
69
|
+
static addBrandFilterToPipeline(pipeline = [], options) {
|
|
70
|
+
var _a;
|
|
67
71
|
const contextBrands = this.getBrandContext();
|
|
72
|
+
const column = (_a = options === null || options === void 0 ? void 0 : options.brandColumn) !== null && _a !== void 0 ? _a : DEFAULT_BRAND_COLUMN;
|
|
68
73
|
if (contextBrands && contextBrands.length > 0) {
|
|
69
|
-
const brandFilter = {
|
|
70
|
-
// Try to merge with existing $match stage if it exists
|
|
74
|
+
const brandFilter = { [column]: { $in: contextBrands } };
|
|
71
75
|
let mergedMatch = false;
|
|
72
76
|
for (const stage of pipeline) {
|
|
73
77
|
if (stage.$match) {
|
|
74
|
-
// Merge brand filter with existing $match
|
|
75
78
|
stage.$match = Object.assign(Object.assign({}, stage.$match), brandFilter);
|
|
76
79
|
mergedMatch = true;
|
|
77
80
|
AlmatarBranding_1.default.log('[MongooseTenantHelper] Merged brand filter with existing $match:', stage.$match);
|
|
78
81
|
break;
|
|
79
82
|
}
|
|
80
83
|
}
|
|
81
|
-
// If no $match stage exists, add brand filter at the beginning
|
|
82
84
|
if (!mergedMatch) {
|
|
83
85
|
pipeline.unshift({ $match: brandFilter });
|
|
84
86
|
AlmatarBranding_1.default.log('[MongooseTenantHelper] Added brand filter to pipeline:', brandFilter);
|
|
@@ -90,37 +92,57 @@ class MongooseTenantHelper {
|
|
|
90
92
|
return pipeline;
|
|
91
93
|
}
|
|
92
94
|
/**
|
|
93
|
-
* Set brand on a document before save
|
|
94
|
-
*
|
|
95
|
+
* Set brand on a document before save.
|
|
96
|
+
* For 'brand': sets document.brand. For 'brands': ensures context brand is in document.brands array.
|
|
95
97
|
* @param document - The document object to set brand on
|
|
96
|
-
* @
|
|
98
|
+
* @param options - Optional. Use { brandColumn: 'brands' } when schema has brands array
|
|
99
|
+
* @returns The document with brand set (if brand context exists)
|
|
97
100
|
*/
|
|
98
|
-
static setBrandOnDocument(document) {
|
|
101
|
+
static setBrandOnDocument(document, options) {
|
|
102
|
+
var _a;
|
|
99
103
|
const brand = this.getBrandForDocument();
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
104
|
+
const column = (_a = options === null || options === void 0 ? void 0 : options.brandColumn) !== null && _a !== void 0 ? _a : DEFAULT_BRAND_COLUMN;
|
|
105
|
+
if (brand) {
|
|
106
|
+
if (column === 'brands') {
|
|
107
|
+
document.brands = document.brands || [];
|
|
108
|
+
if (!document.brands.includes(brand)) {
|
|
109
|
+
document.brands.push(brand);
|
|
110
|
+
}
|
|
111
|
+
AlmatarBranding_1.default.log('[MongooseTenantHelper] Set brand on document.brands:', brand);
|
|
112
|
+
}
|
|
113
|
+
else if (!document.brand) {
|
|
114
|
+
document.brand = brand;
|
|
115
|
+
AlmatarBranding_1.default.log('[MongooseTenantHelper] Set brand on document:', brand);
|
|
116
|
+
}
|
|
103
117
|
}
|
|
104
|
-
else
|
|
118
|
+
else {
|
|
105
119
|
AlmatarBranding_1.default.log('[MongooseTenantHelper] No brand context, skipping brand on document');
|
|
106
120
|
}
|
|
107
121
|
return document;
|
|
108
122
|
}
|
|
109
123
|
/**
|
|
110
|
-
* Set brand on multiple documents before insertMany
|
|
111
|
-
* Use this when inserting multiple documents manually
|
|
124
|
+
* Set brand on multiple documents before insertMany.
|
|
112
125
|
* @param documents - Array of document objects to set brand on
|
|
126
|
+
* @param options - Optional. Use { brandColumn: 'brands' } when schema has brands array
|
|
113
127
|
* @returns The documents array with brand set (if brand context exists)
|
|
114
128
|
*/
|
|
115
|
-
static setBrandOnDocuments(documents) {
|
|
129
|
+
static setBrandOnDocuments(documents, options) {
|
|
130
|
+
var _a;
|
|
116
131
|
const brand = this.getBrandForDocument();
|
|
132
|
+
const column = (_a = options === null || options === void 0 ? void 0 : options.brandColumn) !== null && _a !== void 0 ? _a : DEFAULT_BRAND_COLUMN;
|
|
117
133
|
if (brand) {
|
|
118
134
|
documents.forEach((doc) => {
|
|
119
|
-
if (
|
|
135
|
+
if (column === 'brands') {
|
|
136
|
+
doc.brands = doc.brands || [];
|
|
137
|
+
if (!doc.brands.includes(brand)) {
|
|
138
|
+
doc.brands.push(brand);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else if (!doc.brand) {
|
|
120
142
|
doc.brand = brand;
|
|
121
143
|
}
|
|
122
144
|
});
|
|
123
|
-
AlmatarBranding_1.default.log('[MongooseTenantHelper] Set brand on documents:', brand);
|
|
145
|
+
AlmatarBranding_1.default.log('[MongooseTenantHelper] Set brand on documents:', { column, brand });
|
|
124
146
|
}
|
|
125
147
|
else {
|
|
126
148
|
AlmatarBranding_1.default.log('[MongooseTenantHelper] No brand context, skipping brand on documents');
|
|
@@ -1,33 +1,35 @@
|
|
|
1
1
|
import { Model, Document, FilterQuery, QueryOptions, UpdateQuery, UpdateWithAggregationPipeline } from 'mongoose';
|
|
2
|
+
import { IMongooseTenantOptions } from './MongooseTenantHelper';
|
|
3
|
+
/**
|
|
4
|
+
* Options for TenantMongooseRepository when schema uses 'brands' (array) instead of 'brand'.
|
|
5
|
+
*/
|
|
6
|
+
export interface ITenantMongooseRepositoryOptions {
|
|
7
|
+
/** Use 'brands' when your schema has an array of brands; default is 'brand'. */
|
|
8
|
+
brandColumn?: 'brand' | 'brands';
|
|
9
|
+
}
|
|
2
10
|
/**
|
|
3
11
|
* Base Repository class for Mongoose that AUTOMATICALLY applies brand filtering to ALL queries
|
|
4
12
|
* Similar to TenantRepository for TypeORM - just extend this class and everything works!
|
|
5
13
|
*
|
|
6
|
-
*
|
|
14
|
+
* Use the second constructor argument when your schema has a `brands` (array) field:
|
|
7
15
|
* ```typescript
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* export class BookingItemRepository extends TenantMongooseRepository<BookingItem> {
|
|
11
|
-
* constructor(@InjectModel(BookingItem.name) private readonly model: Model<BookingItem>) {
|
|
12
|
-
* super(model);
|
|
13
|
-
* }
|
|
14
|
-
* }
|
|
16
|
+
* super(model, { brandColumn: 'brands' });
|
|
15
17
|
* ```
|
|
16
18
|
*
|
|
17
19
|
* Brand filtering is automatically applied to:
|
|
18
|
-
* - find() - automatically filters by brand
|
|
19
|
-
* - findOne() - automatically filters by brand
|
|
20
|
-
* - findOneAndUpdate() - automatically filters by brand
|
|
21
|
-
* - findOneAndDelete() - automatically filters by brand
|
|
22
|
-
* - countDocuments() - automatically filters by brand
|
|
23
|
-
* - aggregate() - automatically filters by brand
|
|
24
|
-
* - save() - automatically sets brand on document
|
|
25
|
-
* - create() - automatically sets brand on document
|
|
20
|
+
* - find() - automatically filters by brand/brands
|
|
21
|
+
* - findOne() - automatically filters by brand/brands
|
|
22
|
+
* - findOneAndUpdate() - automatically filters by brand/brands
|
|
23
|
+
* - findOneAndDelete() - automatically filters by brand/brands
|
|
24
|
+
* - countDocuments() - automatically filters by brand/brands
|
|
25
|
+
* - aggregate() - automatically filters by brand/brands
|
|
26
|
+
* - save() - automatically sets brand/brands on document
|
|
27
|
+
* - create() - automatically sets brand/brands on document
|
|
26
28
|
*/
|
|
27
29
|
export declare class TenantMongooseRepository<T extends Document> {
|
|
28
30
|
protected readonly model: Model<T>;
|
|
29
|
-
protected readonly
|
|
30
|
-
constructor(model: Model<T
|
|
31
|
+
protected readonly tenantOptions: IMongooseTenantOptions;
|
|
32
|
+
constructor(model: Model<T>, options?: ITenantMongooseRepositoryOptions);
|
|
31
33
|
/**
|
|
32
34
|
* Override find to automatically apply brand filtering
|
|
33
35
|
*/
|
|
@@ -6,120 +6,114 @@ const MongooseTenantHelper_1 = require("./MongooseTenantHelper");
|
|
|
6
6
|
* Base Repository class for Mongoose that AUTOMATICALLY applies brand filtering to ALL queries
|
|
7
7
|
* Similar to TenantRepository for TypeORM - just extend this class and everything works!
|
|
8
8
|
*
|
|
9
|
-
*
|
|
9
|
+
* Use the second constructor argument when your schema has a `brands` (array) field:
|
|
10
10
|
* ```typescript
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* export class BookingItemRepository extends TenantMongooseRepository<BookingItem> {
|
|
14
|
-
* constructor(@InjectModel(BookingItem.name) private readonly model: Model<BookingItem>) {
|
|
15
|
-
* super(model);
|
|
16
|
-
* }
|
|
17
|
-
* }
|
|
11
|
+
* super(model, { brandColumn: 'brands' });
|
|
18
12
|
* ```
|
|
19
13
|
*
|
|
20
14
|
* Brand filtering is automatically applied to:
|
|
21
|
-
* - find() - automatically filters by brand
|
|
22
|
-
* - findOne() - automatically filters by brand
|
|
23
|
-
* - findOneAndUpdate() - automatically filters by brand
|
|
24
|
-
* - findOneAndDelete() - automatically filters by brand
|
|
25
|
-
* - countDocuments() - automatically filters by brand
|
|
26
|
-
* - aggregate() - automatically filters by brand
|
|
27
|
-
* - save() - automatically sets brand on document
|
|
28
|
-
* - create() - automatically sets brand on document
|
|
15
|
+
* - find() - automatically filters by brand/brands
|
|
16
|
+
* - findOne() - automatically filters by brand/brands
|
|
17
|
+
* - findOneAndUpdate() - automatically filters by brand/brands
|
|
18
|
+
* - findOneAndDelete() - automatically filters by brand/brands
|
|
19
|
+
* - countDocuments() - automatically filters by brand/brands
|
|
20
|
+
* - aggregate() - automatically filters by brand/brands
|
|
21
|
+
* - save() - automatically sets brand/brands on document
|
|
22
|
+
* - create() - automatically sets brand/brands on document
|
|
29
23
|
*/
|
|
30
24
|
class TenantMongooseRepository {
|
|
31
|
-
constructor(model) {
|
|
32
|
-
this.brandColumn = 'brand';
|
|
25
|
+
constructor(model, options) {
|
|
33
26
|
this.model = model;
|
|
27
|
+
this.tenantOptions = (options === null || options === void 0 ? void 0 : options.brandColumn) ? { brandColumn: options.brandColumn } : {};
|
|
34
28
|
}
|
|
35
29
|
/**
|
|
36
30
|
* Override find to automatically apply brand filtering
|
|
37
31
|
*/
|
|
38
32
|
find(filter = {}, projection, options) {
|
|
39
|
-
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter);
|
|
33
|
+
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter, this.tenantOptions);
|
|
40
34
|
return this.model.find(filteredQuery, projection, options);
|
|
41
35
|
}
|
|
42
36
|
/**
|
|
43
37
|
* Override findOne to automatically apply brand filtering
|
|
44
38
|
*/
|
|
45
39
|
findOne(filter = {}, projection, options) {
|
|
46
|
-
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter);
|
|
40
|
+
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter, this.tenantOptions);
|
|
47
41
|
return this.model.findOne(filteredQuery, projection, options);
|
|
48
42
|
}
|
|
49
43
|
/**
|
|
50
44
|
* Override findOneAndUpdate to automatically apply brand filtering
|
|
51
45
|
*/
|
|
52
46
|
findOneAndUpdate(filter, update, options) {
|
|
53
|
-
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter);
|
|
47
|
+
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter, this.tenantOptions);
|
|
54
48
|
return this.model.findOneAndUpdate(filteredQuery, update, options);
|
|
55
49
|
}
|
|
56
50
|
/**
|
|
57
51
|
* Override findOneAndDelete to automatically apply brand filtering
|
|
58
52
|
*/
|
|
59
53
|
findOneAndDelete(filter = {}, options) {
|
|
60
|
-
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter);
|
|
54
|
+
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter, this.tenantOptions);
|
|
61
55
|
return this.model.findOneAndDelete(filteredQuery, options);
|
|
62
56
|
}
|
|
63
57
|
/**
|
|
64
58
|
* Override findOneAndReplace to automatically apply brand filtering
|
|
65
59
|
*/
|
|
66
60
|
findOneAndReplace(filter, replacement, options) {
|
|
67
|
-
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter);
|
|
61
|
+
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter, this.tenantOptions);
|
|
68
62
|
return this.model.findOneAndReplace(filteredQuery, replacement, options);
|
|
69
63
|
}
|
|
70
64
|
/**
|
|
71
65
|
* Override countDocuments to automatically apply brand filtering
|
|
72
66
|
*/
|
|
73
67
|
countDocuments(filter = {}, options) {
|
|
74
|
-
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter);
|
|
68
|
+
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter, this.tenantOptions);
|
|
75
69
|
return this.model.countDocuments(filteredQuery, options);
|
|
76
70
|
}
|
|
77
71
|
/**
|
|
78
72
|
* Override deleteOne to automatically apply brand filtering
|
|
79
73
|
*/
|
|
80
74
|
deleteOne(filter = {}, options) {
|
|
81
|
-
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter);
|
|
75
|
+
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter, this.tenantOptions);
|
|
82
76
|
return this.model.deleteOne(filteredQuery, options);
|
|
83
77
|
}
|
|
84
78
|
/**
|
|
85
79
|
* Override deleteMany to automatically apply brand filtering
|
|
86
80
|
*/
|
|
87
81
|
deleteMany(filter = {}, options) {
|
|
88
|
-
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter);
|
|
82
|
+
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter, this.tenantOptions);
|
|
89
83
|
return this.model.deleteMany(filteredQuery, options);
|
|
90
84
|
}
|
|
91
85
|
/**
|
|
92
86
|
* Override updateOne to automatically apply brand filtering
|
|
93
87
|
*/
|
|
94
88
|
updateOne(filter, update, options) {
|
|
95
|
-
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter);
|
|
89
|
+
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter, this.tenantOptions);
|
|
96
90
|
return this.model.updateOne(filteredQuery, update, options);
|
|
97
91
|
}
|
|
98
92
|
/**
|
|
99
93
|
* Override updateMany to automatically apply brand filtering
|
|
100
94
|
*/
|
|
101
95
|
updateMany(filter, update, options) {
|
|
102
|
-
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter);
|
|
96
|
+
const filteredQuery = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter, this.tenantOptions);
|
|
103
97
|
return this.model.updateMany(filteredQuery, update, options);
|
|
104
98
|
}
|
|
105
99
|
/**
|
|
106
100
|
* Override aggregate to automatically apply brand filtering
|
|
107
101
|
*/
|
|
108
102
|
aggregate(pipeline = []) {
|
|
109
|
-
const filteredPipeline = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilterToPipeline([...pipeline]);
|
|
103
|
+
const filteredPipeline = MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilterToPipeline([...pipeline], this.tenantOptions);
|
|
110
104
|
return this.model.aggregate(filteredPipeline);
|
|
111
105
|
}
|
|
112
106
|
async create(doc, ...docs) {
|
|
113
107
|
if (Array.isArray(doc)) {
|
|
114
|
-
const documents = MongooseTenantHelper_1.MongooseTenantHelper.setBrandOnDocuments([...doc]);
|
|
108
|
+
const documents = MongooseTenantHelper_1.MongooseTenantHelper.setBrandOnDocuments([...doc], this.tenantOptions);
|
|
115
109
|
return this.model.create(documents);
|
|
116
110
|
}
|
|
117
111
|
else if (docs.length > 0) {
|
|
118
|
-
const documents = MongooseTenantHelper_1.MongooseTenantHelper.setBrandOnDocuments([doc, ...docs]);
|
|
112
|
+
const documents = MongooseTenantHelper_1.MongooseTenantHelper.setBrandOnDocuments([doc, ...docs], this.tenantOptions);
|
|
119
113
|
return this.model.create(documents);
|
|
120
114
|
}
|
|
121
115
|
else {
|
|
122
|
-
const document = MongooseTenantHelper_1.MongooseTenantHelper.setBrandOnDocument(Object.assign({}, doc));
|
|
116
|
+
const document = MongooseTenantHelper_1.MongooseTenantHelper.setBrandOnDocument(Object.assign({}, doc), this.tenantOptions);
|
|
123
117
|
return this.model.create(document);
|
|
124
118
|
}
|
|
125
119
|
}
|
|
@@ -127,14 +121,14 @@ class TenantMongooseRepository {
|
|
|
127
121
|
* Override insertMany to automatically set brand on documents
|
|
128
122
|
*/
|
|
129
123
|
insertMany(docs, options) {
|
|
130
|
-
const documents = MongooseTenantHelper_1.MongooseTenantHelper.setBrandOnDocuments([...docs]);
|
|
124
|
+
const documents = MongooseTenantHelper_1.MongooseTenantHelper.setBrandOnDocuments([...docs], this.tenantOptions);
|
|
131
125
|
return this.model.insertMany(documents, options);
|
|
132
126
|
}
|
|
133
127
|
/**
|
|
134
128
|
* Save method that automatically sets brand on document
|
|
135
129
|
*/
|
|
136
130
|
async save(document) {
|
|
137
|
-
MongooseTenantHelper_1.MongooseTenantHelper.setBrandOnDocument(document);
|
|
131
|
+
MongooseTenantHelper_1.MongooseTenantHelper.setBrandOnDocument(document, this.tenantOptions);
|
|
138
132
|
return document.save();
|
|
139
133
|
}
|
|
140
134
|
/**
|
|
@@ -148,14 +142,14 @@ class TenantMongooseRepository {
|
|
|
148
142
|
* Useful for building complex queries
|
|
149
143
|
*/
|
|
150
144
|
createQuery(filter = {}) {
|
|
151
|
-
return MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter);
|
|
145
|
+
return MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilter(filter, this.tenantOptions);
|
|
152
146
|
}
|
|
153
147
|
/**
|
|
154
148
|
* Create an aggregation pipeline with brand filter applied
|
|
155
149
|
* Useful for building complex aggregations
|
|
156
150
|
*/
|
|
157
151
|
createPipeline(pipeline = []) {
|
|
158
|
-
return MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilterToPipeline([...pipeline]);
|
|
152
|
+
return MongooseTenantHelper_1.MongooseTenantHelper.addBrandFilterToPipeline([...pipeline], this.tenantOptions);
|
|
159
153
|
}
|
|
160
154
|
}
|
|
161
155
|
exports.TenantMongooseRepository = TenantMongooseRepository;
|