@opra/mongodb 1.5.7 → 1.7.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/cjs/adapter/mongo-patch-generator.js +3 -0
- package/cjs/services/mongo-entity-service.js +39 -3
- package/cjs/services/mongo-service.js +1 -0
- package/esm/adapter/mongo-patch-generator.js +3 -0
- package/esm/services/mongo-entity-service.js +39 -3
- package/esm/services/mongo-service.js +1 -0
- package/package.json +4 -4
- package/types/adapter/mongo-patch-generator.d.ts +2 -0
- package/types/services/mongo-entity-service.d.ts +3 -0
|
@@ -19,6 +19,7 @@ class MongoPatchGenerator {
|
|
|
19
19
|
return {
|
|
20
20
|
update,
|
|
21
21
|
arrayFilters: ctx.arrayFilters,
|
|
22
|
+
initArrayFields: ctx.initArrayFields,
|
|
22
23
|
};
|
|
23
24
|
}
|
|
24
25
|
_processComplexType(ctx, dataType, path, input, scope) {
|
|
@@ -84,6 +85,8 @@ class MongoPatchGenerator {
|
|
|
84
85
|
if (!value)
|
|
85
86
|
continue;
|
|
86
87
|
if (field.isArray) {
|
|
88
|
+
ctx.initArrayFields = ctx.initArrayFields || [];
|
|
89
|
+
ctx.initArrayFields.push(pathDot + field.name);
|
|
87
90
|
if (!value.length)
|
|
88
91
|
continue;
|
|
89
92
|
keyField = field.keyField || field.type.keyField;
|
|
@@ -201,7 +201,10 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
201
201
|
});
|
|
202
202
|
/** Execute db command */
|
|
203
203
|
try {
|
|
204
|
-
/** Fetch the cursor
|
|
204
|
+
/** Fetch the cursor */
|
|
205
|
+
if (options?.noDecode)
|
|
206
|
+
return cursor.toArray();
|
|
207
|
+
/** Decode result objects */
|
|
205
208
|
const outputCodec = this._getOutputCodec('find');
|
|
206
209
|
return (await cursor.toArray()).map((r) => outputCodec(r));
|
|
207
210
|
}
|
|
@@ -269,7 +272,9 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
269
272
|
const facetResult = await cursor.toArray();
|
|
270
273
|
return {
|
|
271
274
|
count: facetResult[0].count[0]?.totalMatches || 0,
|
|
272
|
-
items:
|
|
275
|
+
items: options?.noDecode
|
|
276
|
+
? facetResult[0].data
|
|
277
|
+
: facetResult[0].data?.map((r) => outputCodec(r)),
|
|
273
278
|
};
|
|
274
279
|
}
|
|
275
280
|
finally {
|
|
@@ -329,6 +334,21 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
329
334
|
]);
|
|
330
335
|
const db = this.getDatabase();
|
|
331
336
|
const collection = await this.getCollection(db);
|
|
337
|
+
/** Create array fields if not exists */
|
|
338
|
+
if (options?.initArrayFields) {
|
|
339
|
+
const $set = options.initArrayFields.reduce((a, k) => {
|
|
340
|
+
a[k] = { $ifNull: ['$' + k, []] };
|
|
341
|
+
return a;
|
|
342
|
+
}, {});
|
|
343
|
+
await collection.updateOne(filter || {}, [{ $set }], {
|
|
344
|
+
...options,
|
|
345
|
+
session: options?.session ?? this.getSession(),
|
|
346
|
+
arrayFilters: undefined,
|
|
347
|
+
upsert: false,
|
|
348
|
+
});
|
|
349
|
+
delete options.initArrayFields;
|
|
350
|
+
}
|
|
351
|
+
/** Execute update operation */
|
|
332
352
|
return (await collection.updateOne(filter || {}, update, {
|
|
333
353
|
...options,
|
|
334
354
|
session: options?.session ?? this.getSession(),
|
|
@@ -352,6 +372,21 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
352
372
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
|
|
353
373
|
const db = this.getDatabase();
|
|
354
374
|
const collection = await this.getCollection(db);
|
|
375
|
+
/** Create array fields if not exists */
|
|
376
|
+
if (options?.initArrayFields) {
|
|
377
|
+
const $set = options.initArrayFields.reduce((a, k) => {
|
|
378
|
+
a[k] = { $ifNull: ['$' + k, []] };
|
|
379
|
+
return a;
|
|
380
|
+
}, {});
|
|
381
|
+
await collection.updateMany(filter || {}, [{ $set }], {
|
|
382
|
+
...(0, objects_1.omit)(options, ['filter']),
|
|
383
|
+
session: options?.session ?? this.getSession(),
|
|
384
|
+
arrayFilters: undefined,
|
|
385
|
+
upsert: false,
|
|
386
|
+
});
|
|
387
|
+
delete options.initArrayFields;
|
|
388
|
+
}
|
|
389
|
+
/** Execute update operation */
|
|
355
390
|
return (await collection.updateMany(filter || {}, update, {
|
|
356
391
|
...(0, objects_1.omit)(options, ['filter']),
|
|
357
392
|
session: options?.session ?? this.getSession(),
|
|
@@ -406,13 +441,14 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
406
441
|
}
|
|
407
442
|
_generatePatch(command, doc) {
|
|
408
443
|
const patchGenerator = new mongo_patch_generator_js_1.MongoPatchGenerator();
|
|
409
|
-
const { update, arrayFilters } = patchGenerator.generatePatch(this.dataType, doc, {
|
|
444
|
+
const { update, arrayFilters, initArrayFields } = patchGenerator.generatePatch(this.dataType, doc, {
|
|
410
445
|
scope: this._dataTypeScope,
|
|
411
446
|
});
|
|
412
447
|
command.options = command.options || {};
|
|
413
448
|
if (arrayFilters) {
|
|
414
449
|
command.options.arrayFilters = command.options.arrayFilters || [];
|
|
415
450
|
command.options.arrayFilters.push(...arrayFilters);
|
|
451
|
+
command.options.initArrayFields = initArrayFields;
|
|
416
452
|
}
|
|
417
453
|
return update;
|
|
418
454
|
}
|
|
@@ -253,6 +253,7 @@ class MongoService extends core_1.ServiceBase {
|
|
|
253
253
|
if (operation === 'update') {
|
|
254
254
|
options.partial = 'deep';
|
|
255
255
|
options.allowPatchOperators = true;
|
|
256
|
+
options.keepKeyFields = true;
|
|
256
257
|
}
|
|
257
258
|
const dataType = this.dataType;
|
|
258
259
|
validator = dataType.generateCodec('decode', options);
|
|
@@ -16,6 +16,7 @@ export class MongoPatchGenerator {
|
|
|
16
16
|
return {
|
|
17
17
|
update,
|
|
18
18
|
arrayFilters: ctx.arrayFilters,
|
|
19
|
+
initArrayFields: ctx.initArrayFields,
|
|
19
20
|
};
|
|
20
21
|
}
|
|
21
22
|
_processComplexType(ctx, dataType, path, input, scope) {
|
|
@@ -81,6 +82,8 @@ export class MongoPatchGenerator {
|
|
|
81
82
|
if (!value)
|
|
82
83
|
continue;
|
|
83
84
|
if (field.isArray) {
|
|
85
|
+
ctx.initArrayFields = ctx.initArrayFields || [];
|
|
86
|
+
ctx.initArrayFields.push(pathDot + field.name);
|
|
84
87
|
if (!value.length)
|
|
85
88
|
continue;
|
|
86
89
|
keyField = field.keyField || field.type.keyField;
|
|
@@ -198,7 +198,10 @@ export class MongoEntityService extends MongoService {
|
|
|
198
198
|
});
|
|
199
199
|
/** Execute db command */
|
|
200
200
|
try {
|
|
201
|
-
/** Fetch the cursor
|
|
201
|
+
/** Fetch the cursor */
|
|
202
|
+
if (options?.noDecode)
|
|
203
|
+
return cursor.toArray();
|
|
204
|
+
/** Decode result objects */
|
|
202
205
|
const outputCodec = this._getOutputCodec('find');
|
|
203
206
|
return (await cursor.toArray()).map((r) => outputCodec(r));
|
|
204
207
|
}
|
|
@@ -266,7 +269,9 @@ export class MongoEntityService extends MongoService {
|
|
|
266
269
|
const facetResult = await cursor.toArray();
|
|
267
270
|
return {
|
|
268
271
|
count: facetResult[0].count[0]?.totalMatches || 0,
|
|
269
|
-
items:
|
|
272
|
+
items: options?.noDecode
|
|
273
|
+
? facetResult[0].data
|
|
274
|
+
: facetResult[0].data?.map((r) => outputCodec(r)),
|
|
270
275
|
};
|
|
271
276
|
}
|
|
272
277
|
finally {
|
|
@@ -326,6 +331,21 @@ export class MongoEntityService extends MongoService {
|
|
|
326
331
|
]);
|
|
327
332
|
const db = this.getDatabase();
|
|
328
333
|
const collection = await this.getCollection(db);
|
|
334
|
+
/** Create array fields if not exists */
|
|
335
|
+
if (options?.initArrayFields) {
|
|
336
|
+
const $set = options.initArrayFields.reduce((a, k) => {
|
|
337
|
+
a[k] = { $ifNull: ['$' + k, []] };
|
|
338
|
+
return a;
|
|
339
|
+
}, {});
|
|
340
|
+
await collection.updateOne(filter || {}, [{ $set }], {
|
|
341
|
+
...options,
|
|
342
|
+
session: options?.session ?? this.getSession(),
|
|
343
|
+
arrayFilters: undefined,
|
|
344
|
+
upsert: false,
|
|
345
|
+
});
|
|
346
|
+
delete options.initArrayFields;
|
|
347
|
+
}
|
|
348
|
+
/** Execute update operation */
|
|
329
349
|
return (await collection.updateOne(filter || {}, update, {
|
|
330
350
|
...options,
|
|
331
351
|
session: options?.session ?? this.getSession(),
|
|
@@ -349,6 +369,21 @@ export class MongoEntityService extends MongoService {
|
|
|
349
369
|
const filter = MongoAdapter.prepareFilter(options?.filter);
|
|
350
370
|
const db = this.getDatabase();
|
|
351
371
|
const collection = await this.getCollection(db);
|
|
372
|
+
/** Create array fields if not exists */
|
|
373
|
+
if (options?.initArrayFields) {
|
|
374
|
+
const $set = options.initArrayFields.reduce((a, k) => {
|
|
375
|
+
a[k] = { $ifNull: ['$' + k, []] };
|
|
376
|
+
return a;
|
|
377
|
+
}, {});
|
|
378
|
+
await collection.updateMany(filter || {}, [{ $set }], {
|
|
379
|
+
...omit(options, ['filter']),
|
|
380
|
+
session: options?.session ?? this.getSession(),
|
|
381
|
+
arrayFilters: undefined,
|
|
382
|
+
upsert: false,
|
|
383
|
+
});
|
|
384
|
+
delete options.initArrayFields;
|
|
385
|
+
}
|
|
386
|
+
/** Execute update operation */
|
|
352
387
|
return (await collection.updateMany(filter || {}, update, {
|
|
353
388
|
...omit(options, ['filter']),
|
|
354
389
|
session: options?.session ?? this.getSession(),
|
|
@@ -403,13 +438,14 @@ export class MongoEntityService extends MongoService {
|
|
|
403
438
|
}
|
|
404
439
|
_generatePatch(command, doc) {
|
|
405
440
|
const patchGenerator = new MongoPatchGenerator();
|
|
406
|
-
const { update, arrayFilters } = patchGenerator.generatePatch(this.dataType, doc, {
|
|
441
|
+
const { update, arrayFilters, initArrayFields } = patchGenerator.generatePatch(this.dataType, doc, {
|
|
407
442
|
scope: this._dataTypeScope,
|
|
408
443
|
});
|
|
409
444
|
command.options = command.options || {};
|
|
410
445
|
if (arrayFilters) {
|
|
411
446
|
command.options.arrayFilters = command.options.arrayFilters || [];
|
|
412
447
|
command.options.arrayFilters.push(...arrayFilters);
|
|
448
|
+
command.options.initArrayFields = initArrayFields;
|
|
413
449
|
}
|
|
414
450
|
return update;
|
|
415
451
|
}
|
|
@@ -250,6 +250,7 @@ export class MongoService extends ServiceBase {
|
|
|
250
250
|
if (operation === 'update') {
|
|
251
251
|
options.partial = 'deep';
|
|
252
252
|
options.allowPatchOperators = true;
|
|
253
|
+
options.keepKeyFields = true;
|
|
253
254
|
}
|
|
254
255
|
const dataType = this.dataType;
|
|
255
256
|
validator = dataType.generateCodec('decode', options);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/mongodb",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Opra MongoDB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"valgen": "^5.12.0"
|
|
11
11
|
},
|
|
12
12
|
"peerDependencies": {
|
|
13
|
-
"@opra/common": "^1.
|
|
14
|
-
"@opra/core": "^1.
|
|
15
|
-
"@opra/http": "^1.
|
|
13
|
+
"@opra/common": "^1.7.0",
|
|
14
|
+
"@opra/core": "^1.7.0",
|
|
15
|
+
"@opra/http": "^1.7.0",
|
|
16
16
|
"mongodb": ">= 6.0.0"
|
|
17
17
|
},
|
|
18
18
|
"type": "module",
|
|
@@ -7,11 +7,13 @@ interface Context {
|
|
|
7
7
|
$push?: Record<string, any>;
|
|
8
8
|
$pull?: Record<string, any>;
|
|
9
9
|
arrayFilters?: Record<string, any>[];
|
|
10
|
+
initArrayFields?: string[];
|
|
10
11
|
}
|
|
11
12
|
export declare class MongoPatchGenerator {
|
|
12
13
|
generatePatch<T extends object>(dataType: ComplexType, doc: PatchDTO<T>, options?: MongoPatchGenerator.Options): {
|
|
13
14
|
update: UpdateFilter<T>;
|
|
14
15
|
arrayFilters?: Record<string, any>[];
|
|
16
|
+
initArrayFields?: string[];
|
|
15
17
|
};
|
|
16
18
|
protected _processComplexType(ctx: Context, dataType: ComplexType, path: string, input: any, scope?: string): boolean;
|
|
17
19
|
protected _processPush(ctx: Context, dataType: ComplexType, path: string, input: any, scope?: string): boolean;
|
|
@@ -32,12 +32,15 @@ export declare namespace MongoEntityService {
|
|
|
32
32
|
interface FindOneOptions<T> extends MongoService.FindOneOptions<T> {
|
|
33
33
|
}
|
|
34
34
|
interface FindManyOptions<T> extends MongoService.FindManyOptions<T> {
|
|
35
|
+
noDecode?: boolean;
|
|
35
36
|
}
|
|
36
37
|
interface ReplaceOptions<T> extends MongoService.ReplaceOptions<T> {
|
|
37
38
|
}
|
|
38
39
|
interface UpdateOneOptions<T> extends MongoService.UpdateOneOptions<T> {
|
|
40
|
+
initArrayFields?: string[];
|
|
39
41
|
}
|
|
40
42
|
interface UpdateManyOptions<T> extends MongoService.UpdateManyOptions<T> {
|
|
43
|
+
initArrayFields?: string[];
|
|
41
44
|
}
|
|
42
45
|
interface CreateCommand<T> extends StrictOmit<CommandInfo, 'documentId' | 'nestedId' | 'input'> {
|
|
43
46
|
crud: 'create';
|