@dnax/core 0.73.4 → 0.73.7
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/driver/mongo/rest.ts +23 -2
- package/driver/mongo/utils.ts +45 -0
- package/package.json +1 -1
package/driver/mongo/rest.ts
CHANGED
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
transformAllDate,
|
|
38
38
|
deepSetId,
|
|
39
39
|
setUUID,
|
|
40
|
+
setDefaultValue,
|
|
40
41
|
} from "./utils";
|
|
41
42
|
import { Cfg } from "../../config";
|
|
42
43
|
import cleanDeep from "clean-deep";
|
|
@@ -497,6 +498,7 @@ class useRest {
|
|
|
497
498
|
data = await hashPasswordAuto(data, col);
|
|
498
499
|
data = transformAllDate(data);
|
|
499
500
|
data = deepSetId(col, data);
|
|
501
|
+
data = setDefaultValue(data, col, "insertOne");
|
|
500
502
|
data = omit(
|
|
501
503
|
data,
|
|
502
504
|
["_id", "createdAt", "updatedAt"].concat(disableReadOnlyFields)
|
|
@@ -678,6 +680,7 @@ class useRest {
|
|
|
678
680
|
d = await setUUID(d, col);
|
|
679
681
|
d = await randomCode(d, col);
|
|
680
682
|
d = await hashPasswordAuto(d, col);
|
|
683
|
+
d = setDefaultValue(d, col, "insertOne");
|
|
681
684
|
d = omit(
|
|
682
685
|
d,
|
|
683
686
|
["_id", "createdAt", "updatedAt"].concat(disableReadOnlyFields)
|
|
@@ -1637,6 +1640,15 @@ class useRest {
|
|
|
1637
1640
|
if (!valid) fn.error(error, 400);
|
|
1638
1641
|
}
|
|
1639
1642
|
|
|
1643
|
+
let $setOnInsert = update.$setOnInsert || {};
|
|
1644
|
+
// Processing before validation
|
|
1645
|
+
$setOnInsert = await setUUID($setOnInsert, col);
|
|
1646
|
+
$setOnInsert = await randomCode($setOnInsert, col);
|
|
1647
|
+
$setOnInsert = await hashPasswordAuto($setOnInsert, col);
|
|
1648
|
+
$setOnInsert = transformAllDate($setOnInsert);
|
|
1649
|
+
$setOnInsert = deepSetId(col, $setOnInsert);
|
|
1650
|
+
$setOnInsert = setDefaultValue($setOnInsert, col, "insertOne");
|
|
1651
|
+
|
|
1640
1652
|
let up_ = await this.#tenant.database.db
|
|
1641
1653
|
?.collection(collection)
|
|
1642
1654
|
.updateMany(
|
|
@@ -1653,7 +1665,7 @@ class useRest {
|
|
|
1653
1665
|
},
|
|
1654
1666
|
$setOnInsert: {
|
|
1655
1667
|
createdAt: new Date(),
|
|
1656
|
-
...formatData(
|
|
1668
|
+
...formatData($setOnInsert || {}, {
|
|
1657
1669
|
collection: collection,
|
|
1658
1670
|
tenant_id: this.#tenant_id,
|
|
1659
1671
|
action: "updateMany",
|
|
@@ -1803,6 +1815,15 @@ class useRest {
|
|
|
1803
1815
|
if (!valid) fn.error(error, 400);
|
|
1804
1816
|
}
|
|
1805
1817
|
|
|
1818
|
+
let $setOnInsert = update.$setOnInsert || {};
|
|
1819
|
+
// Processing before validation
|
|
1820
|
+
$setOnInsert = await setUUID($setOnInsert, col);
|
|
1821
|
+
$setOnInsert = await randomCode($setOnInsert, col);
|
|
1822
|
+
$setOnInsert = await hashPasswordAuto($setOnInsert, col);
|
|
1823
|
+
$setOnInsert = transformAllDate($setOnInsert);
|
|
1824
|
+
$setOnInsert = deepSetId(col, $setOnInsert);
|
|
1825
|
+
$setOnInsert = setDefaultValue($setOnInsert, col, "insertOne");
|
|
1826
|
+
|
|
1806
1827
|
result.doc = await this.#tenant.database.db
|
|
1807
1828
|
?.collection(collection)
|
|
1808
1829
|
.findOneAndUpdate(
|
|
@@ -1816,7 +1837,7 @@ class useRest {
|
|
|
1816
1837
|
}),
|
|
1817
1838
|
|
|
1818
1839
|
$setOnInsert: {
|
|
1819
|
-
...formatData(
|
|
1840
|
+
...formatData($setOnInsert || {}, {
|
|
1820
1841
|
collection: collection,
|
|
1821
1842
|
tenant_id: this.#tenant_id,
|
|
1822
1843
|
action: "insertOne",
|
package/driver/mongo/utils.ts
CHANGED
|
@@ -409,6 +409,30 @@ async function setUUID(
|
|
|
409
409
|
return data;
|
|
410
410
|
}
|
|
411
411
|
|
|
412
|
+
function setDefaultValue(data: any, col: Collection, action?: Actions) {
|
|
413
|
+
col?.fields?.map((f) => {
|
|
414
|
+
if (
|
|
415
|
+
f?.defaultValue &&
|
|
416
|
+
!data?.hasOwnProperty(f?.name) &&
|
|
417
|
+
action == "insertOne"
|
|
418
|
+
) {
|
|
419
|
+
data[f?.name] = f?.defaultValue;
|
|
420
|
+
}
|
|
421
|
+
if (
|
|
422
|
+
f?.defaultValue &&
|
|
423
|
+
!data?.hasOwnProperty(f?.name) &&
|
|
424
|
+
action == "insertMany"
|
|
425
|
+
) {
|
|
426
|
+
data.map((d) => {
|
|
427
|
+
if (!d?.hasOwnProperty(f?.name)) {
|
|
428
|
+
d[f?.name] = f?.defaultValue;
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
return data;
|
|
434
|
+
}
|
|
435
|
+
|
|
412
436
|
function formatData(
|
|
413
437
|
data: any,
|
|
414
438
|
options: {
|
|
@@ -422,8 +446,28 @@ function formatData(
|
|
|
422
446
|
}
|
|
423
447
|
) {
|
|
424
448
|
let col = getCollection(options.collection, options?.tenant_id);
|
|
449
|
+
|
|
425
450
|
if (col) {
|
|
426
451
|
data = deepSetId(col, data);
|
|
452
|
+
if (options?.action == "insertOne") {
|
|
453
|
+
col?.fields?.map((f) => {
|
|
454
|
+
if (f?.defaultValue && !data?.hasOwnProperty(f?.name)) {
|
|
455
|
+
data[f?.name] = f?.defaultValue;
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
if (options?.action == "insertMany") {
|
|
461
|
+
col?.fields?.map((f) => {
|
|
462
|
+
if (f?.defaultValue) {
|
|
463
|
+
data.map((d) => {
|
|
464
|
+
if (!d?.hasOwnProperty(f?.name)) {
|
|
465
|
+
d[f?.name] = f?.defaultValue;
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
}
|
|
427
471
|
}
|
|
428
472
|
data = toBson(data, {
|
|
429
473
|
upsert: options?.upsert,
|
|
@@ -460,4 +504,5 @@ export {
|
|
|
460
504
|
hashPasswordAuto,
|
|
461
505
|
transformAllDate,
|
|
462
506
|
setUUID,
|
|
507
|
+
setDefaultValue,
|
|
463
508
|
};
|