@ruiapp/rapid-core 0.1.47 → 0.1.49

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/dist/index.js CHANGED
@@ -1858,6 +1858,9 @@ function mapDbRowToEntity(server, model, row, keepNonPropertyFields) {
1858
1858
  propertyName = columnName;
1859
1859
  }
1860
1860
  }
1861
+ if (property?.config?.dataManage?.hidden) {
1862
+ return;
1863
+ }
1861
1864
  if (isRelationProp) {
1862
1865
  if (row[propertyName]) {
1863
1866
  if (!result[propertyName]) {
@@ -2409,28 +2412,42 @@ async function createEntity(server, dataAccessor, options, plugin) {
2409
2412
  }
2410
2413
  });
2411
2414
  const { row, baseRow } = mapEntityToDbRow(server, model, entity);
2415
+ const newEntityOneRelationProps = {};
2412
2416
  // save one-relation properties
2413
2417
  for (const property of oneRelationPropertiesToCreate) {
2414
2418
  const targetRow = property.isBaseProperty ? baseRow : row;
2415
2419
  const fieldValue = entity[property.code];
2420
+ const targetDataAccessor = server.getDataAccessor({
2421
+ singularCode: property.targetSingularCode,
2422
+ });
2416
2423
  if (lodash.isObject(fieldValue)) {
2417
- if (!fieldValue["id"]) {
2418
- const targetDataAccessor = server.getDataAccessor({
2419
- singularCode: property.targetSingularCode,
2420
- });
2424
+ const targetEntityId = fieldValue["id"];
2425
+ if (!targetEntityId) {
2421
2426
  const targetEntity = fieldValue;
2422
2427
  const newTargetEntity = await createEntity(server, targetDataAccessor, {
2423
2428
  entity: targetEntity,
2424
2429
  });
2430
+ newEntityOneRelationProps[property.code] = newTargetEntity;
2425
2431
  targetRow[property.targetIdColumnName] = newTargetEntity["id"];
2426
2432
  }
2427
2433
  else {
2428
- targetRow[property.targetIdColumnName] = fieldValue["id"];
2434
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
2435
+ if (!targetEntity) {
2436
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
2437
+ }
2438
+ newEntityOneRelationProps[property.code] = targetEntity;
2439
+ targetRow[property.targetIdColumnName] = targetEntityId;
2429
2440
  }
2430
2441
  }
2431
2442
  else {
2432
2443
  // fieldValue is id;
2433
- targetRow[property.targetIdColumnName] = fieldValue;
2444
+ const targetEntityId = fieldValue;
2445
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
2446
+ if (!targetEntity) {
2447
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
2448
+ }
2449
+ newEntityOneRelationProps[property.code] = targetEntity;
2450
+ targetRow[property.targetIdColumnName] = targetEntityId;
2434
2451
  }
2435
2452
  }
2436
2453
  let newBaseRow;
@@ -2442,7 +2459,7 @@ async function createEntity(server, dataAccessor, options, plugin) {
2442
2459
  row.id = newBaseRow.id;
2443
2460
  }
2444
2461
  const newRow = await dataAccessor.create(row);
2445
- const newEntity = mapDbRowToEntity(server, model, newBaseRow ? Object.assign(newBaseRow, newRow) : newRow, true);
2462
+ const newEntity = mapDbRowToEntity(server, model, Object.assign({}, newBaseRow, newRow, newEntityOneRelationProps), true);
2446
2463
  // save many-relation properties
2447
2464
  for (const property of manyRelationPropertiesToCreate) {
2448
2465
  newEntity[property.code] = [];
@@ -2560,16 +2577,43 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
2560
2577
  }
2561
2578
  });
2562
2579
  const { row, baseRow } = mapEntityToDbRow(server, model, changes);
2563
- oneRelationPropertiesToUpdate.forEach((property) => {
2580
+ const updatedEntityOneRelationProps = {};
2581
+ for (const property of oneRelationPropertiesToUpdate) {
2564
2582
  const targetRow = property.isBaseProperty ? baseRow : row;
2565
2583
  const fieldValue = changes[property.code];
2584
+ const targetDataAccessor = server.getDataAccessor({
2585
+ singularCode: property.targetSingularCode,
2586
+ });
2566
2587
  if (lodash.isObject(fieldValue)) {
2567
- targetRow[property.targetIdColumnName] = fieldValue["id"];
2588
+ const targetEntityId = fieldValue["id"];
2589
+ if (!targetEntityId) {
2590
+ const targetEntity = fieldValue;
2591
+ const newTargetEntity = await createEntity(server, targetDataAccessor, {
2592
+ entity: targetEntity,
2593
+ });
2594
+ updatedEntityOneRelationProps[property.code] = newTargetEntity;
2595
+ targetRow[property.targetIdColumnName] = newTargetEntity["id"];
2596
+ }
2597
+ else {
2598
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
2599
+ if (!targetEntity) {
2600
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
2601
+ }
2602
+ updatedEntityOneRelationProps[property.code] = targetEntity;
2603
+ targetRow[property.targetIdColumnName] = targetEntityId;
2604
+ }
2568
2605
  }
2569
2606
  else {
2570
- targetRow[property.targetIdColumnName] = fieldValue;
2607
+ // fieldValue is id;
2608
+ const targetEntityId = fieldValue;
2609
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
2610
+ if (!targetEntity) {
2611
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
2612
+ }
2613
+ updatedEntityOneRelationProps[property.code] = targetEntity;
2614
+ targetRow[property.targetIdColumnName] = targetEntityId;
2571
2615
  }
2572
- });
2616
+ }
2573
2617
  let updatedRow = row;
2574
2618
  if (Object.keys(row).length) {
2575
2619
  updatedRow = await dataAccessor.updateById(id, row);
@@ -2581,7 +2625,7 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
2581
2625
  });
2582
2626
  updatedBaseRow = await baseDataAccessor.updateById(id, updatedBaseRow);
2583
2627
  }
2584
- let updatedEntity = mapDbRowToEntity(server, model, { ...updatedRow, ...updatedBaseRow }, true);
2628
+ let updatedEntity = mapDbRowToEntity(server, model, { ...updatedRow, ...updatedBaseRow, ...updatedEntityOneRelationProps }, true);
2585
2629
  updatedEntity = Object.assign({}, entity, updatedEntity);
2586
2630
  // save many-relation properties
2587
2631
  for (const property of manyRelationPropertiesToUpdate) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.1.47",
3
+ "version": "0.1.49",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -527,26 +527,40 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
527
527
 
528
528
  const { row, baseRow } = mapEntityToDbRow(server, model, entity);
529
529
 
530
+ const newEntityOneRelationProps = {};
530
531
  // save one-relation properties
531
532
  for (const property of oneRelationPropertiesToCreate) {
532
533
  const targetRow = property.isBaseProperty ? baseRow : row;
533
534
  const fieldValue = entity[property.code];
535
+ const targetDataAccessor = server.getDataAccessor({
536
+ singularCode: property.targetSingularCode!,
537
+ });
534
538
  if (isObject(fieldValue)) {
535
- if (!fieldValue["id"]) {
536
- const targetDataAccessor = server.getDataAccessor({
537
- singularCode: property.targetSingularCode!,
538
- });
539
+ const targetEntityId = fieldValue["id"];
540
+ if (!targetEntityId) {
539
541
  const targetEntity = fieldValue;
540
542
  const newTargetEntity = await createEntity(server, targetDataAccessor, {
541
543
  entity: targetEntity,
542
544
  });
545
+ newEntityOneRelationProps[property.code] = newTargetEntity;
543
546
  targetRow[property.targetIdColumnName!] = newTargetEntity["id"];
544
547
  } else {
545
- targetRow[property.targetIdColumnName!] = fieldValue["id"];
548
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
549
+ if (!targetEntity) {
550
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
551
+ }
552
+ newEntityOneRelationProps[property.code] = targetEntity;
553
+ targetRow[property.targetIdColumnName!] = targetEntityId;
546
554
  }
547
555
  } else {
548
556
  // fieldValue is id;
549
- targetRow[property.targetIdColumnName!] = fieldValue;
557
+ const targetEntityId = fieldValue;
558
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
559
+ if (!targetEntity) {
560
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
561
+ }
562
+ newEntityOneRelationProps[property.code] = targetEntity;
563
+ targetRow[property.targetIdColumnName!] = targetEntityId;
550
564
  }
551
565
  }
552
566
 
@@ -560,7 +574,7 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
560
574
  row.id = newBaseRow.id;
561
575
  }
562
576
  const newRow = await dataAccessor.create(row);
563
- const newEntity = mapDbRowToEntity(server, model, newBaseRow ? Object.assign(newBaseRow, newRow) : newRow, true);
577
+ const newEntity = mapDbRowToEntity(server, model, Object.assign({}, newBaseRow, newRow, newEntityOneRelationProps), true);
564
578
 
565
579
  // save many-relation properties
566
580
  for (const property of manyRelationPropertiesToCreate) {
@@ -701,15 +715,44 @@ async function updateEntityById(server: IRpdServer, dataAccessor: IRpdDataAccess
701
715
  });
702
716
 
703
717
  const { row, baseRow } = mapEntityToDbRow(server, model, changes);
704
- oneRelationPropertiesToUpdate.forEach((property) => {
718
+
719
+ const updatedEntityOneRelationProps = {};
720
+ for (const property of oneRelationPropertiesToUpdate) {
705
721
  const targetRow = property.isBaseProperty ? baseRow : row;
706
722
  const fieldValue = changes[property.code];
723
+ const targetDataAccessor = server.getDataAccessor({
724
+ singularCode: property.targetSingularCode!,
725
+ });
726
+
707
727
  if (isObject(fieldValue)) {
708
- targetRow[property.targetIdColumnName!] = fieldValue["id"];
728
+ const targetEntityId = fieldValue["id"];
729
+ if (!targetEntityId) {
730
+ const targetEntity = fieldValue;
731
+ const newTargetEntity = await createEntity(server, targetDataAccessor, {
732
+ entity: targetEntity,
733
+ });
734
+ updatedEntityOneRelationProps[property.code] = newTargetEntity;
735
+ targetRow[property.targetIdColumnName!] = newTargetEntity["id"];
736
+ } else {
737
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
738
+ if (!targetEntity) {
739
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
740
+ }
741
+ updatedEntityOneRelationProps[property.code] = targetEntity;
742
+ targetRow[property.targetIdColumnName!] = targetEntityId;
743
+ }
709
744
  } else {
710
- targetRow[property.targetIdColumnName!] = fieldValue;
745
+ // fieldValue is id;
746
+ const targetEntityId = fieldValue;
747
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
748
+ if (!targetEntity) {
749
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
750
+ }
751
+ updatedEntityOneRelationProps[property.code] = targetEntity;
752
+ targetRow[property.targetIdColumnName!] = targetEntityId;
711
753
  }
712
- });
754
+ };
755
+
713
756
  let updatedRow = row;
714
757
  if (Object.keys(row).length) {
715
758
  updatedRow = await dataAccessor.updateById(id, row);
@@ -722,7 +765,7 @@ async function updateEntityById(server: IRpdServer, dataAccessor: IRpdDataAccess
722
765
  updatedBaseRow = await baseDataAccessor.updateById(id, updatedBaseRow);
723
766
  }
724
767
 
725
- let updatedEntity = mapDbRowToEntity(server, model, {...updatedRow, ...updatedBaseRow}, true);
768
+ let updatedEntity = mapDbRowToEntity(server, model, {...updatedRow, ...updatedBaseRow, ...updatedEntityOneRelationProps}, true);
726
769
  updatedEntity = Object.assign({}, entity, updatedEntity);
727
770
 
728
771
  // save many-relation properties
@@ -33,6 +33,10 @@ export function mapDbRowToEntity(server: IRpdServer, model: RpdDataModel, row: a
33
33
  }
34
34
  }
35
35
 
36
+ if (property?.config?.dataManage?.hidden) {
37
+ return;
38
+ }
39
+
36
40
  if (isRelationProp) {
37
41
  if (row[propertyName]) {
38
42
  if (!result[propertyName]) {