@dnax/core 0.73.5 → 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.
@@ -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)
@@ -1644,6 +1647,7 @@ class useRest {
1644
1647
  $setOnInsert = await hashPasswordAuto($setOnInsert, col);
1645
1648
  $setOnInsert = transformAllDate($setOnInsert);
1646
1649
  $setOnInsert = deepSetId(col, $setOnInsert);
1650
+ $setOnInsert = setDefaultValue($setOnInsert, col, "insertOne");
1647
1651
 
1648
1652
  let up_ = await this.#tenant.database.db
1649
1653
  ?.collection(collection)
@@ -1818,6 +1822,7 @@ class useRest {
1818
1822
  $setOnInsert = await hashPasswordAuto($setOnInsert, col);
1819
1823
  $setOnInsert = transformAllDate($setOnInsert);
1820
1824
  $setOnInsert = deepSetId(col, $setOnInsert);
1825
+ $setOnInsert = setDefaultValue($setOnInsert, col, "insertOne");
1821
1826
 
1822
1827
  result.doc = await this.#tenant.database.db
1823
1828
  ?.collection(collection)
@@ -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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.73.5",
3
+ "version": "0.73.7",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {},