@classytic/mongokit 1.0.1 → 2.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/README.md +564 -157
- package/package.json +20 -12
- package/src/Repository.js +296 -225
- package/src/actions/aggregate.js +266 -191
- package/src/actions/create.js +59 -58
- package/src/actions/delete.js +88 -88
- package/src/actions/index.js +11 -11
- package/src/actions/read.js +188 -155
- package/src/actions/update.js +176 -172
- package/src/hooks/lifecycle.js +146 -146
- package/src/index.js +71 -60
- package/src/pagination/PaginationEngine.js +348 -0
- package/src/pagination/utils/cursor.js +119 -0
- package/src/pagination/utils/filter.js +42 -0
- package/src/pagination/utils/limits.js +82 -0
- package/src/pagination/utils/sort.js +101 -0
- package/src/plugins/aggregate-helpers.plugin.js +71 -71
- package/src/plugins/audit-log.plugin.js +60 -60
- package/src/plugins/batch-operations.plugin.js +66 -66
- package/src/plugins/field-filter.plugin.js +27 -27
- package/src/plugins/index.js +19 -19
- package/src/plugins/method-registry.plugin.js +140 -140
- package/src/plugins/mongo-operations.plugin.js +317 -313
- package/src/plugins/soft-delete.plugin.js +46 -46
- package/src/plugins/subdocument.plugin.js +66 -66
- package/src/plugins/timestamp.plugin.js +19 -19
- package/src/plugins/validation-chain.plugin.js +145 -145
- package/src/types.d.ts +87 -0
- package/src/utils/error.js +12 -0
- package/src/utils/field-selection.js +156 -156
- package/src/utils/index.js +12 -12
- package/types/Repository.d.ts +95 -0
- package/types/Repository.d.ts.map +1 -0
- package/types/actions/aggregate.d.ts +112 -0
- package/types/actions/aggregate.d.ts.map +1 -0
- package/types/actions/create.d.ts +21 -0
- package/types/actions/create.d.ts.map +1 -0
- package/types/actions/delete.d.ts +37 -0
- package/types/actions/delete.d.ts.map +1 -0
- package/types/actions/index.d.ts +6 -113
- package/types/actions/index.d.ts.map +1 -0
- package/types/actions/read.d.ts +135 -0
- package/types/actions/read.d.ts.map +1 -0
- package/types/actions/update.d.ts +58 -0
- package/types/actions/update.d.ts.map +1 -0
- package/types/hooks/lifecycle.d.ts +44 -0
- package/types/hooks/lifecycle.d.ts.map +1 -0
- package/types/index.d.ts +25 -96
- package/types/index.d.ts.map +1 -0
- package/types/pagination/PaginationEngine.d.ts +386 -0
- package/types/pagination/PaginationEngine.d.ts.map +1 -0
- package/types/pagination/utils/cursor.d.ts +40 -0
- package/types/pagination/utils/cursor.d.ts.map +1 -0
- package/types/pagination/utils/filter.d.ts +28 -0
- package/types/pagination/utils/filter.d.ts.map +1 -0
- package/types/pagination/utils/limits.d.ts +64 -0
- package/types/pagination/utils/limits.d.ts.map +1 -0
- package/types/pagination/utils/sort.d.ts +41 -0
- package/types/pagination/utils/sort.d.ts.map +1 -0
- package/types/plugins/aggregate-helpers.plugin.d.ts +6 -0
- package/types/plugins/aggregate-helpers.plugin.d.ts.map +1 -0
- package/types/plugins/audit-log.plugin.d.ts +6 -0
- package/types/plugins/audit-log.plugin.d.ts.map +1 -0
- package/types/plugins/batch-operations.plugin.d.ts +6 -0
- package/types/plugins/batch-operations.plugin.d.ts.map +1 -0
- package/types/plugins/field-filter.plugin.d.ts +6 -0
- package/types/plugins/field-filter.plugin.d.ts.map +1 -0
- package/types/plugins/index.d.ts +11 -88
- package/types/plugins/index.d.ts.map +1 -0
- package/types/plugins/method-registry.plugin.d.ts +3 -0
- package/types/plugins/method-registry.plugin.d.ts.map +1 -0
- package/types/plugins/mongo-operations.plugin.d.ts +4 -0
- package/types/plugins/mongo-operations.plugin.d.ts.map +1 -0
- package/types/plugins/soft-delete.plugin.d.ts +6 -0
- package/types/plugins/soft-delete.plugin.d.ts.map +1 -0
- package/types/plugins/subdocument.plugin.d.ts +6 -0
- package/types/plugins/subdocument.plugin.d.ts.map +1 -0
- package/types/plugins/timestamp.plugin.d.ts +6 -0
- package/types/plugins/timestamp.plugin.d.ts.map +1 -0
- package/types/plugins/validation-chain.plugin.d.ts +31 -0
- package/types/plugins/validation-chain.plugin.d.ts.map +1 -0
- package/types/utils/error.d.ts +11 -0
- package/types/utils/error.d.ts.map +1 -0
- package/types/utils/field-selection.d.ts +9 -0
- package/types/utils/field-selection.d.ts.map +1 -0
- package/types/utils/index.d.ts +2 -24
- package/types/utils/index.d.ts.map +1 -0
|
@@ -1,313 +1,317 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* -
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* -
|
|
13
|
-
* -
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
* import {
|
|
28
|
-
*
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
*
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* @
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* @
|
|
103
|
-
*
|
|
104
|
-
* @
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* @example
|
|
109
|
-
* // Increment
|
|
110
|
-
* await productRepo.increment(
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
* @
|
|
124
|
-
*
|
|
125
|
-
* @
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
* @
|
|
146
|
-
*
|
|
147
|
-
* @
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
* @example
|
|
152
|
-
* // Add
|
|
153
|
-
* await productRepo.pushToArray(
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
* @
|
|
170
|
-
*
|
|
171
|
-
* @
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* @example
|
|
176
|
-
* // Remove
|
|
177
|
-
* await productRepo.pullFromArray(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
* @
|
|
191
|
-
*
|
|
192
|
-
* @
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
* @
|
|
208
|
-
*
|
|
209
|
-
* @
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
* @
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
* @example
|
|
230
|
-
* // Remove
|
|
231
|
-
* await userRepo.unsetField(userId,
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
* @
|
|
251
|
-
*
|
|
252
|
-
* @
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
* @
|
|
268
|
-
*
|
|
269
|
-
* @
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
* @
|
|
285
|
-
*
|
|
286
|
-
* @
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
* @
|
|
302
|
-
*
|
|
303
|
-
* @
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
})
|
|
312
|
-
|
|
313
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('../types.js').ObjectId} ObjectId
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* MongoDB Operations Plugin
|
|
7
|
+
*
|
|
8
|
+
* Adds MongoDB-specific operations to repositories.
|
|
9
|
+
* Requires method-registry.plugin.js to be loaded first.
|
|
10
|
+
*
|
|
11
|
+
* **Operations Added:**
|
|
12
|
+
* - upsert(query, data, options) - Update or insert
|
|
13
|
+
* - increment(id, field, value, options) - Atomic increment
|
|
14
|
+
* - decrement(id, field, value, options) - Atomic decrement
|
|
15
|
+
* - pushToArray(id, field, value, options) - Add to array
|
|
16
|
+
* - pullFromArray(id, field, value, options) - Remove from array
|
|
17
|
+
* - addToSet(id, field, value, options) - Add unique to array
|
|
18
|
+
*
|
|
19
|
+
* **Pattern:** Opt-in MongoDB features
|
|
20
|
+
* **Philosophy:** Keep core pure, add database-specific features via plugins
|
|
21
|
+
*
|
|
22
|
+
* @module common/repositories/plugins/mongo-operations
|
|
23
|
+
* @requires method-registry.plugin
|
|
24
|
+
*
|
|
25
|
+
* @example Basic Usage
|
|
26
|
+
* ```js
|
|
27
|
+
* import { Repository } from '../Repository.js';
|
|
28
|
+
* import { method
|
|
29
|
+
|
|
30
|
+
RegistryPlugin } from './method-registry.plugin.js';
|
|
31
|
+
* import { mongoOperationsPlugin } from './mongo-operations.plugin.js';
|
|
32
|
+
*
|
|
33
|
+
* class ProductRepository extends Repository {
|
|
34
|
+
* constructor() {
|
|
35
|
+
* super(Product, [
|
|
36
|
+
* methodRegistryPlugin(),
|
|
37
|
+
* mongoOperationsPlugin(),
|
|
38
|
+
* ]);
|
|
39
|
+
* }
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* // Now you can use MongoDB operations
|
|
43
|
+
* await productRepo.increment(productId, 'views', 1);
|
|
44
|
+
* await productRepo.pushToArray(productId, 'tags', 'featured');
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
import { createError } from '../utils/error.js';
|
|
49
|
+
import * as createActions from '../actions/create.js';
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* MongoDB Operations Plugin
|
|
53
|
+
*
|
|
54
|
+
* Adds common MongoDB atomic operations to repository.
|
|
55
|
+
* All operations use repository's update method internally (events/plugins run).
|
|
56
|
+
*
|
|
57
|
+
* @returns {Object} Plugin configuration
|
|
58
|
+
*/
|
|
59
|
+
export const mongoOperationsPlugin = () => ({
|
|
60
|
+
name: 'mongo-operations',
|
|
61
|
+
|
|
62
|
+
apply(repo) {
|
|
63
|
+
// Check if method-registry is available
|
|
64
|
+
if (!repo.registerMethod) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
'mongoOperationsPlugin requires methodRegistryPlugin. ' +
|
|
67
|
+
'Add methodRegistryPlugin() before mongoOperationsPlugin() in plugins array.'
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Update existing document or insert new one
|
|
73
|
+
*
|
|
74
|
+
* @param {Object} query - Query to find existing document
|
|
75
|
+
* @param {Object} data - Data to insert/update
|
|
76
|
+
* @param {Object} [options={}] - Options
|
|
77
|
+
* @returns {Promise<Object>} Upserted document
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* // Create or update user session
|
|
81
|
+
* await repo.upsert(
|
|
82
|
+
* { userId, deviceId },
|
|
83
|
+
* { lastActive: new Date(), ipAddress },
|
|
84
|
+
* { lean: true }
|
|
85
|
+
* );
|
|
86
|
+
*/
|
|
87
|
+
repo.registerMethod('upsert', async function (query, data, options = {}) {
|
|
88
|
+
return createActions.upsert(this.Model, query, data, options);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Helper: Validate and perform numeric operation
|
|
92
|
+
const validateAndUpdateNumeric = function (id, field, value, operator, operationName, options) {
|
|
93
|
+
if (typeof value !== 'number') {
|
|
94
|
+
throw createError(400, `${operationName} value must be a number`);
|
|
95
|
+
}
|
|
96
|
+
return this.update(id, { [operator]: { [field]: value } }, options);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Atomically increment numeric field
|
|
101
|
+
*
|
|
102
|
+
* @param {string|ObjectId} id - Document ID
|
|
103
|
+
* @param {string} field - Field name to increment
|
|
104
|
+
* @param {number} [value=1] - Amount to increment by
|
|
105
|
+
* @param {Object} [options={}] - Options
|
|
106
|
+
* @returns {Promise<Object>} Updated document
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* // Increment product views
|
|
110
|
+
* await productRepo.increment(productId, 'views', 1);
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* // Increment multiple times
|
|
114
|
+
* await productRepo.increment(userId, 'points', 100);
|
|
115
|
+
*/
|
|
116
|
+
repo.registerMethod('increment', async function (id, field, value = 1, options = {}) {
|
|
117
|
+
return validateAndUpdateNumeric.call(this, id, field, value, '$inc', 'Increment', options);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Atomically decrement numeric field
|
|
122
|
+
*
|
|
123
|
+
* @param {string|ObjectId} id - Document ID
|
|
124
|
+
* @param {string} field - Field name to decrement
|
|
125
|
+
* @param {number} [value=1] - Amount to decrement by
|
|
126
|
+
* @param {Object} [options={}] - Options
|
|
127
|
+
* @returns {Promise<Object>} Updated document
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* // Decrement product stock
|
|
131
|
+
* await productRepo.decrement(productId, 'stock', 1);
|
|
132
|
+
*/
|
|
133
|
+
repo.registerMethod('decrement', async function (id, field, value = 1, options = {}) {
|
|
134
|
+
return validateAndUpdateNumeric.call(this, id, field, -value, '$inc', 'Decrement', options);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Helper: Generic MongoDB operator update
|
|
138
|
+
const applyOperator = function (id, field, value, operator, options) {
|
|
139
|
+
return this.update(id, { [operator]: { [field]: value } }, options);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Push value to array field
|
|
144
|
+
*
|
|
145
|
+
* @param {string|ObjectId} id - Document ID
|
|
146
|
+
* @param {string} field - Array field name
|
|
147
|
+
* @param {*} value - Value to push
|
|
148
|
+
* @param {Object} [options={}] - Options
|
|
149
|
+
* @returns {Promise<Object>} Updated document
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* // Add tag to product
|
|
153
|
+
* await productRepo.pushToArray(productId, 'tags', 'new-arrival');
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* // Add multiple items
|
|
157
|
+
* await productRepo.pushToArray(userId, 'notifications', {
|
|
158
|
+
* message: 'Welcome!',
|
|
159
|
+
* createdAt: new Date()
|
|
160
|
+
* });
|
|
161
|
+
*/
|
|
162
|
+
repo.registerMethod('pushToArray', async function (id, field, value, options = {}) {
|
|
163
|
+
return applyOperator.call(this, id, field, value, '$push', options);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Remove value from array field
|
|
168
|
+
*
|
|
169
|
+
* @param {string|ObjectId} id - Document ID
|
|
170
|
+
* @param {string} field - Array field name
|
|
171
|
+
* @param {*} value - Value to remove (can be query object)
|
|
172
|
+
* @param {Object} [options={}] - Options
|
|
173
|
+
* @returns {Promise<Object>} Updated document
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* // Remove tag from product
|
|
177
|
+
* await productRepo.pullFromArray(productId, 'tags', 'old-tag');
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* // Remove by query
|
|
181
|
+
* await productRepo.pullFromArray(userId, 'notifications', { read: true });
|
|
182
|
+
*/
|
|
183
|
+
repo.registerMethod('pullFromArray', async function (id, field, value, options = {}) {
|
|
184
|
+
return applyOperator.call(this, id, field, value, '$pull', options);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Add value to array only if not already present (unique)
|
|
189
|
+
*
|
|
190
|
+
* @param {string|ObjectId} id - Document ID
|
|
191
|
+
* @param {string} field - Array field name
|
|
192
|
+
* @param {*} value - Value to add
|
|
193
|
+
* @param {Object} [options={}] - Options
|
|
194
|
+
* @returns {Promise<Object>} Updated document
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* // Add unique follower
|
|
198
|
+
* await userRepo.addToSet(userId, 'followers', followerId);
|
|
199
|
+
*/
|
|
200
|
+
repo.registerMethod('addToSet', async function (id, field, value, options = {}) {
|
|
201
|
+
return applyOperator.call(this, id, field, value, '$addToSet', options);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Set field value (alias for update with $set)
|
|
206
|
+
*
|
|
207
|
+
* @param {string|ObjectId} id - Document ID
|
|
208
|
+
* @param {string} field - Field name
|
|
209
|
+
* @param {*} value - New value
|
|
210
|
+
* @param {Object} [options={}] - Options
|
|
211
|
+
* @returns {Promise<Object>} Updated document
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* // Set last login
|
|
215
|
+
* await userRepo.setField(userId, 'lastLogin', new Date());
|
|
216
|
+
*/
|
|
217
|
+
repo.registerMethod('setField', async function (id, field, value, options = {}) {
|
|
218
|
+
return applyOperator.call(this, id, field, value, '$set', options);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Unset (remove) field from document
|
|
223
|
+
*
|
|
224
|
+
* @param {string|ObjectId} id - Document ID
|
|
225
|
+
* @param {string|string[]} fields - Field name(s) to remove
|
|
226
|
+
* @param {Object} [options={}] - Options
|
|
227
|
+
* @returns {Promise<Object>} Updated document
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* // Remove temporary field
|
|
231
|
+
* await userRepo.unsetField(userId, 'tempToken');
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* // Remove multiple fields
|
|
235
|
+
* await userRepo.unsetField(userId, ['tempToken', 'tempData']);
|
|
236
|
+
*/
|
|
237
|
+
repo.registerMethod('unsetField', async function (id, fields, options = {}) {
|
|
238
|
+
const fieldArray = Array.isArray(fields) ? fields : [fields];
|
|
239
|
+
const unsetObj = fieldArray.reduce((acc, field) => {
|
|
240
|
+
acc[field] = '';
|
|
241
|
+
return acc;
|
|
242
|
+
}, {});
|
|
243
|
+
|
|
244
|
+
return this.update(id, { $unset: unsetObj }, options);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Rename field in document
|
|
249
|
+
*
|
|
250
|
+
* @param {string|ObjectId} id - Document ID
|
|
251
|
+
* @param {string} oldName - Current field name
|
|
252
|
+
* @param {string} newName - New field name
|
|
253
|
+
* @param {Object} [options={}] - Options
|
|
254
|
+
* @returns {Promise<Object>} Updated document
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* // Rename field
|
|
258
|
+
* await userRepo.renameField(userId, 'username', 'displayName');
|
|
259
|
+
*/
|
|
260
|
+
repo.registerMethod('renameField', async function (id, oldName, newName, options = {}) {
|
|
261
|
+
return this.update(id, { $rename: { [oldName]: newName } }, options);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Multiply numeric field by value
|
|
266
|
+
*
|
|
267
|
+
* @param {string|ObjectId} id - Document ID
|
|
268
|
+
* @param {string} field - Field name
|
|
269
|
+
* @param {number} multiplier - Multiplier value
|
|
270
|
+
* @param {Object} [options={}] - Options
|
|
271
|
+
* @returns {Promise<Object>} Updated document
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* // Double points
|
|
275
|
+
* await userRepo.multiplyField(userId, 'points', 2);
|
|
276
|
+
*/
|
|
277
|
+
repo.registerMethod('multiplyField', async function (id, field, multiplier, options = {}) {
|
|
278
|
+
return validateAndUpdateNumeric.call(this, id, field, multiplier, '$mul', 'Multiplier', options);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Set field to minimum value (only if current value is greater)
|
|
283
|
+
*
|
|
284
|
+
* @param {string|ObjectId} id - Document ID
|
|
285
|
+
* @param {string} field - Field name
|
|
286
|
+
* @param {number} value - Minimum value
|
|
287
|
+
* @param {Object} [options={}] - Options
|
|
288
|
+
* @returns {Promise<Object>} Updated document
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* // Set minimum price
|
|
292
|
+
* await productRepo.setMin(productId, 'price', 999);
|
|
293
|
+
*/
|
|
294
|
+
repo.registerMethod('setMin', async function (id, field, value, options = {}) {
|
|
295
|
+
return applyOperator.call(this, id, field, value, '$min', options);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Set field to maximum value (only if current value is less)
|
|
300
|
+
*
|
|
301
|
+
* @param {string|ObjectId} id - Document ID
|
|
302
|
+
* @param {string} field - Field name
|
|
303
|
+
* @param {number} value - Maximum value
|
|
304
|
+
* @param {Object} [options={}] - Options
|
|
305
|
+
* @returns {Promise<Object>} Updated document
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* // Set maximum score
|
|
309
|
+
* await gameRepo.setMax(gameId, 'highScore', newScore);
|
|
310
|
+
*/
|
|
311
|
+
repo.registerMethod('setMax', async function (id, field, value, options = {}) {
|
|
312
|
+
return applyOperator.call(this, id, field, value, '$max', options);
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
export default mongoOperationsPlugin;
|