@dnax/core 0.73.5 → 0.73.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.
@@ -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)
@@ -363,7 +363,6 @@ async function randomCode(
363
363
  // if unique
364
364
  if (f?.unique) {
365
365
  let tenant = Cfg.tenants.find((t) => t.id == col.tenant_id);
366
-
367
366
  if (tenant) {
368
367
  let doc = await tenant.database.db
369
368
  ?.collection(col.slug)
@@ -409,6 +408,30 @@ async function setUUID(
409
408
  return data;
410
409
  }
411
410
 
411
+ function setDefaultValue(data: any, col: Collection, action?: Actions) {
412
+ col?.fields?.map((f) => {
413
+ if (
414
+ f?.defaultValue &&
415
+ !data?.hasOwnProperty(f?.name) &&
416
+ action == "insertOne"
417
+ ) {
418
+ data[f?.name] = f?.defaultValue;
419
+ }
420
+ if (
421
+ f?.defaultValue &&
422
+ !data?.hasOwnProperty(f?.name) &&
423
+ action == "insertMany"
424
+ ) {
425
+ data.map((d) => {
426
+ if (!d?.hasOwnProperty(f?.name)) {
427
+ d[f?.name] = f?.defaultValue;
428
+ }
429
+ });
430
+ }
431
+ });
432
+ return data;
433
+ }
434
+
412
435
  function formatData(
413
436
  data: any,
414
437
  options: {
@@ -422,8 +445,28 @@ function formatData(
422
445
  }
423
446
  ) {
424
447
  let col = getCollection(options.collection, options?.tenant_id);
448
+
425
449
  if (col) {
426
450
  data = deepSetId(col, data);
451
+ if (options?.action == "insertOne") {
452
+ col?.fields?.map((f) => {
453
+ if (f?.defaultValue && !data?.hasOwnProperty(f?.name)) {
454
+ data[f?.name] = f?.defaultValue;
455
+ }
456
+ });
457
+ }
458
+
459
+ if (options?.action == "insertMany") {
460
+ col?.fields?.map((f) => {
461
+ if (f?.defaultValue) {
462
+ data.map((d) => {
463
+ if (!d?.hasOwnProperty(f?.name)) {
464
+ d[f?.name] = f?.defaultValue;
465
+ }
466
+ });
467
+ }
468
+ });
469
+ }
427
470
  }
428
471
  data = toBson(data, {
429
472
  upsert: options?.upsert,
@@ -460,4 +503,5 @@ export {
460
503
  hashPasswordAuto,
461
504
  transformAllDate,
462
505
  setUUID,
506
+ setDefaultValue,
463
507
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.73.5",
3
+ "version": "0.73.8",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {},