@ruiapp/rapid-core 0.1.46 → 0.1.48

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
@@ -2409,28 +2409,42 @@ async function createEntity(server, dataAccessor, options, plugin) {
2409
2409
  }
2410
2410
  });
2411
2411
  const { row, baseRow } = mapEntityToDbRow(server, model, entity);
2412
+ const newEntityOneRelationProps = {};
2412
2413
  // save one-relation properties
2413
2414
  for (const property of oneRelationPropertiesToCreate) {
2414
2415
  const targetRow = property.isBaseProperty ? baseRow : row;
2415
2416
  const fieldValue = entity[property.code];
2417
+ const targetDataAccessor = server.getDataAccessor({
2418
+ singularCode: property.targetSingularCode,
2419
+ });
2416
2420
  if (lodash.isObject(fieldValue)) {
2417
- if (!fieldValue["id"]) {
2418
- const targetDataAccessor = server.getDataAccessor({
2419
- singularCode: property.targetSingularCode,
2420
- });
2421
+ const targetEntityId = fieldValue["id"];
2422
+ if (!targetEntityId) {
2421
2423
  const targetEntity = fieldValue;
2422
2424
  const newTargetEntity = await createEntity(server, targetDataAccessor, {
2423
2425
  entity: targetEntity,
2424
2426
  });
2427
+ newEntityOneRelationProps[property.code] = newTargetEntity;
2425
2428
  targetRow[property.targetIdColumnName] = newTargetEntity["id"];
2426
2429
  }
2427
2430
  else {
2428
- targetRow[property.targetIdColumnName] = fieldValue["id"];
2431
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
2432
+ if (!targetEntity) {
2433
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
2434
+ }
2435
+ newEntityOneRelationProps[property.code] = targetEntity;
2436
+ targetRow[property.targetIdColumnName] = targetEntityId;
2429
2437
  }
2430
2438
  }
2431
2439
  else {
2432
2440
  // fieldValue is id;
2433
- targetRow[property.targetIdColumnName] = fieldValue;
2441
+ const targetEntityId = fieldValue;
2442
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
2443
+ if (!targetEntity) {
2444
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
2445
+ }
2446
+ newEntityOneRelationProps[property.code] = targetEntity;
2447
+ targetRow[property.targetIdColumnName] = targetEntityId;
2434
2448
  }
2435
2449
  }
2436
2450
  let newBaseRow;
@@ -2442,7 +2456,7 @@ async function createEntity(server, dataAccessor, options, plugin) {
2442
2456
  row.id = newBaseRow.id;
2443
2457
  }
2444
2458
  const newRow = await dataAccessor.create(row);
2445
- const newEntity = mapDbRowToEntity(server, model, Object.assign(newBaseRow, newRow), true);
2459
+ const newEntity = mapDbRowToEntity(server, model, Object.assign({}, newBaseRow, newRow, newEntityOneRelationProps), true);
2446
2460
  // save many-relation properties
2447
2461
  for (const property of manyRelationPropertiesToCreate) {
2448
2462
  newEntity[property.code] = [];
@@ -2560,16 +2574,43 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
2560
2574
  }
2561
2575
  });
2562
2576
  const { row, baseRow } = mapEntityToDbRow(server, model, changes);
2563
- oneRelationPropertiesToUpdate.forEach((property) => {
2577
+ const updatedEntityOneRelationProps = {};
2578
+ for (const property of oneRelationPropertiesToUpdate) {
2564
2579
  const targetRow = property.isBaseProperty ? baseRow : row;
2565
2580
  const fieldValue = changes[property.code];
2581
+ const targetDataAccessor = server.getDataAccessor({
2582
+ singularCode: property.targetSingularCode,
2583
+ });
2566
2584
  if (lodash.isObject(fieldValue)) {
2567
- targetRow[property.targetIdColumnName] = fieldValue["id"];
2585
+ const targetEntityId = fieldValue["id"];
2586
+ if (!targetEntityId) {
2587
+ const targetEntity = fieldValue;
2588
+ const newTargetEntity = await createEntity(server, targetDataAccessor, {
2589
+ entity: targetEntity,
2590
+ });
2591
+ updatedEntityOneRelationProps[property.code] = newTargetEntity;
2592
+ targetRow[property.targetIdColumnName] = newTargetEntity["id"];
2593
+ }
2594
+ else {
2595
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
2596
+ if (!targetEntity) {
2597
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
2598
+ }
2599
+ updatedEntityOneRelationProps[property.code] = targetEntity;
2600
+ targetRow[property.targetIdColumnName] = targetEntityId;
2601
+ }
2568
2602
  }
2569
2603
  else {
2570
- targetRow[property.targetIdColumnName] = fieldValue;
2604
+ // fieldValue is id;
2605
+ const targetEntityId = fieldValue;
2606
+ const targetEntity = await findById(server, targetDataAccessor, targetEntityId);
2607
+ if (!targetEntity) {
2608
+ throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
2609
+ }
2610
+ updatedEntityOneRelationProps[property.code] = targetEntity;
2611
+ targetRow[property.targetIdColumnName] = targetEntityId;
2571
2612
  }
2572
- });
2613
+ }
2573
2614
  let updatedRow = row;
2574
2615
  if (Object.keys(row).length) {
2575
2616
  updatedRow = await dataAccessor.updateById(id, row);
@@ -2581,7 +2622,7 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
2581
2622
  });
2582
2623
  updatedBaseRow = await baseDataAccessor.updateById(id, updatedBaseRow);
2583
2624
  }
2584
- let updatedEntity = mapDbRowToEntity(server, model, { ...updatedRow, ...updatedBaseRow }, true);
2625
+ let updatedEntity = mapDbRowToEntity(server, model, { ...updatedRow, ...updatedBaseRow, ...updatedEntityOneRelationProps }, true);
2585
2626
  updatedEntity = Object.assign({}, entity, updatedEntity);
2586
2627
  // save many-relation properties
2587
2628
  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.46",
3
+ "version": "0.1.48",
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, Object.assign(newBaseRow, 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