@ruiapp/rapid-core 0.2.6 → 0.2.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/dist/index.js CHANGED
@@ -2972,7 +2972,7 @@ async function createEntity(server, dataAccessor, options, plugin) {
2972
2972
  const newEntityOneRelationProps = {};
2973
2973
  // save one-relation properties
2974
2974
  for (const property of oneRelationPropertiesToCreate) {
2975
- const targetRow = property.isBaseProperty ? baseRow : row;
2975
+ const rowToBeSaved = property.isBaseProperty ? baseRow : row;
2976
2976
  const fieldValue = entity[property.code];
2977
2977
  const targetDataAccessor = server.getDataAccessor({
2978
2978
  singularCode: property.targetSingularCode,
@@ -2980,13 +2980,15 @@ async function createEntity(server, dataAccessor, options, plugin) {
2980
2980
  if (lodash.isObject(fieldValue)) {
2981
2981
  const targetEntityId = fieldValue["id"];
2982
2982
  if (!targetEntityId) {
2983
- const targetEntity = fieldValue;
2984
- const newTargetEntity = await createEntity(server, targetDataAccessor, {
2985
- routeContext,
2986
- entity: targetEntity,
2987
- });
2988
- newEntityOneRelationProps[property.code] = newTargetEntity;
2989
- targetRow[property.targetIdColumnName] = newTargetEntity["id"];
2983
+ if (!property.selfIdColumnName) {
2984
+ const targetEntity = fieldValue;
2985
+ const newTargetEntity = await createEntity(server, targetDataAccessor, {
2986
+ routeContext,
2987
+ entity: targetEntity,
2988
+ });
2989
+ newEntityOneRelationProps[property.code] = newTargetEntity;
2990
+ rowToBeSaved[property.targetIdColumnName] = newTargetEntity["id"];
2991
+ }
2990
2992
  }
2991
2993
  else {
2992
2994
  const targetEntity = await findById(server, targetDataAccessor, {
@@ -2997,7 +2999,7 @@ async function createEntity(server, dataAccessor, options, plugin) {
2997
2999
  throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
2998
3000
  }
2999
3001
  newEntityOneRelationProps[property.code] = targetEntity;
3000
- targetRow[property.targetIdColumnName] = targetEntityId;
3002
+ rowToBeSaved[property.targetIdColumnName] = targetEntityId;
3001
3003
  }
3002
3004
  }
3003
3005
  else if (lodash.isNumber(fieldValue) || lodash.isString(fieldValue)) {
@@ -3011,16 +3013,17 @@ async function createEntity(server, dataAccessor, options, plugin) {
3011
3013
  throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
3012
3014
  }
3013
3015
  newEntityOneRelationProps[property.code] = targetEntity;
3014
- targetRow[property.targetIdColumnName] = targetEntityId;
3016
+ rowToBeSaved[property.targetIdColumnName] = targetEntityId;
3015
3017
  }
3016
3018
  else {
3017
3019
  newEntityOneRelationProps[property.code] = null;
3018
- targetRow[property.targetIdColumnName] = null;
3020
+ rowToBeSaved[property.targetIdColumnName] = null;
3019
3021
  }
3020
3022
  }
3021
3023
  let newBaseRow;
3024
+ let baseDataAccessor;
3022
3025
  if (model.base) {
3023
- const baseDataAccessor = server.getDataAccessor({
3026
+ baseDataAccessor = server.getDataAccessor({
3024
3027
  singularCode: model.base,
3025
3028
  });
3026
3029
  newBaseRow = await baseDataAccessor.create(baseRow);
@@ -3028,6 +3031,35 @@ async function createEntity(server, dataAccessor, options, plugin) {
3028
3031
  }
3029
3032
  const newRow = await dataAccessor.create(row);
3030
3033
  const newEntity = mapDbRowToEntity(server, model, Object.assign({}, newBaseRow, newRow, newEntityOneRelationProps), true);
3034
+ // save one-relation properties that has selfIdColumnName
3035
+ for (const property of oneRelationPropertiesToCreate) {
3036
+ const fieldValue = entity[property.code];
3037
+ const targetDataAccessor = server.getDataAccessor({
3038
+ singularCode: property.targetSingularCode,
3039
+ });
3040
+ if (lodash.isObject(fieldValue)) {
3041
+ const targetEntityId = fieldValue["id"];
3042
+ if (!targetEntityId) {
3043
+ if (property.selfIdColumnName) {
3044
+ const targetEntity = fieldValue;
3045
+ targetEntity[property.selfIdColumnName] = newEntity.id;
3046
+ const newTargetEntity = await createEntity(server, targetDataAccessor, {
3047
+ routeContext,
3048
+ entity: targetEntity,
3049
+ });
3050
+ let dataAccessorOfMainEntity = dataAccessor;
3051
+ if (property.isBaseProperty) {
3052
+ dataAccessorOfMainEntity = baseDataAccessor;
3053
+ }
3054
+ const relationFieldChanges = {
3055
+ [property.targetIdColumnName]: newTargetEntity.id,
3056
+ };
3057
+ await dataAccessorOfMainEntity.updateById(newEntity.id, relationFieldChanges);
3058
+ newEntity[property.code] = newTargetEntity;
3059
+ }
3060
+ }
3061
+ }
3062
+ }
3031
3063
  // save many-relation properties
3032
3064
  for (const property of manyRelationPropertiesToCreate) {
3033
3065
  newEntity[property.code] = [];
@@ -3221,7 +3253,7 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
3221
3253
  const { row, baseRow } = mapEntityToDbRow(server, model, changes);
3222
3254
  const updatedEntityOneRelationProps = {};
3223
3255
  for (const property of oneRelationPropertiesToUpdate) {
3224
- const targetRow = property.isBaseProperty ? baseRow : row;
3256
+ const rowToBeSaved = property.isBaseProperty ? baseRow : row;
3225
3257
  const fieldValue = changes[property.code];
3226
3258
  const targetDataAccessor = server.getDataAccessor({
3227
3259
  singularCode: property.targetSingularCode,
@@ -3229,13 +3261,15 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
3229
3261
  if (lodash.isObject(fieldValue)) {
3230
3262
  const targetEntityId = fieldValue["id"];
3231
3263
  if (!targetEntityId) {
3232
- const targetEntity = fieldValue;
3233
- const newTargetEntity = await createEntity(server, targetDataAccessor, {
3234
- routeContext,
3235
- entity: targetEntity,
3236
- });
3237
- updatedEntityOneRelationProps[property.code] = newTargetEntity;
3238
- targetRow[property.targetIdColumnName] = newTargetEntity["id"];
3264
+ if (!property.selfIdColumnName) {
3265
+ const targetEntity = fieldValue;
3266
+ const newTargetEntity = await createEntity(server, targetDataAccessor, {
3267
+ routeContext,
3268
+ entity: targetEntity,
3269
+ });
3270
+ updatedEntityOneRelationProps[property.code] = newTargetEntity;
3271
+ rowToBeSaved[property.targetIdColumnName] = newTargetEntity["id"];
3272
+ }
3239
3273
  }
3240
3274
  else {
3241
3275
  const targetEntity = await findById(server, targetDataAccessor, {
@@ -3246,7 +3280,7 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
3246
3280
  throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
3247
3281
  }
3248
3282
  updatedEntityOneRelationProps[property.code] = targetEntity;
3249
- targetRow[property.targetIdColumnName] = targetEntityId;
3283
+ rowToBeSaved[property.targetIdColumnName] = targetEntityId;
3250
3284
  }
3251
3285
  }
3252
3286
  else if (lodash.isNumber(fieldValue) || lodash.isString(fieldValue)) {
@@ -3260,11 +3294,11 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
3260
3294
  throw newEntityOperationError(`Create ${model.singularCode} entity failed. Property '${property.code}' was invalid. Related ${property.targetSingularCode} entity with id '${targetEntityId}' was not found.`);
3261
3295
  }
3262
3296
  updatedEntityOneRelationProps[property.code] = targetEntity;
3263
- targetRow[property.targetIdColumnName] = targetEntityId;
3297
+ rowToBeSaved[property.targetIdColumnName] = targetEntityId;
3264
3298
  }
3265
3299
  else {
3266
3300
  updatedEntityOneRelationProps[property.code] = null;
3267
- targetRow[property.targetIdColumnName] = null;
3301
+ rowToBeSaved[property.targetIdColumnName] = null;
3268
3302
  }
3269
3303
  }
3270
3304
  let updatedRow = row;
@@ -3272,14 +3306,47 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
3272
3306
  updatedRow = await dataAccessor.updateById(id, row);
3273
3307
  }
3274
3308
  let updatedBaseRow = baseRow;
3275
- if (model.base && Object.keys(baseRow).length) {
3276
- const baseDataAccessor = server.getDataAccessor({
3309
+ let baseDataAccessor;
3310
+ if (model.base) {
3311
+ baseDataAccessor = server.getDataAccessor({
3277
3312
  singularCode: model.base,
3278
3313
  });
3279
- updatedBaseRow = await baseDataAccessor.updateById(id, updatedBaseRow);
3314
+ if (Object.keys(baseRow).length) {
3315
+ updatedBaseRow = await baseDataAccessor.updateById(id, updatedBaseRow);
3316
+ }
3280
3317
  }
3281
3318
  let updatedEntity = mapDbRowToEntity(server, model, { ...updatedRow, ...updatedBaseRow, ...updatedEntityOneRelationProps }, true);
3282
3319
  updatedEntity = Object.assign({}, entity, updatedEntity);
3320
+ // save one-relation properties that has selfIdColumnName
3321
+ for (const property of oneRelationPropertiesToUpdate) {
3322
+ const fieldValue = changes[property.code];
3323
+ const targetDataAccessor = server.getDataAccessor({
3324
+ singularCode: property.targetSingularCode,
3325
+ });
3326
+ if (lodash.isObject(fieldValue)) {
3327
+ const targetEntityId = fieldValue["id"];
3328
+ if (!targetEntityId) {
3329
+ if (property.selfIdColumnName) {
3330
+ const targetEntity = fieldValue;
3331
+ targetEntity[property.selfIdColumnName] = updatedEntity.id;
3332
+ const newTargetEntity = await createEntity(server, targetDataAccessor, {
3333
+ routeContext,
3334
+ entity: targetEntity,
3335
+ });
3336
+ let dataAccessorOfMainEntity = dataAccessor;
3337
+ if (property.isBaseProperty) {
3338
+ dataAccessorOfMainEntity = baseDataAccessor;
3339
+ }
3340
+ const relationFieldChanges = {
3341
+ [property.targetIdColumnName]: newTargetEntity.id,
3342
+ };
3343
+ await dataAccessorOfMainEntity.updateById(updatedEntity.id, relationFieldChanges);
3344
+ updatedEntity[property.code] = newTargetEntity;
3345
+ changes[property.code] = newTargetEntity;
3346
+ }
3347
+ }
3348
+ }
3349
+ }
3283
3350
  // save many-relation properties
3284
3351
  for (const property of manyRelationPropertiesToUpdate) {
3285
3352
  const relatedEntities = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -796,7 +796,7 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
796
796
  const newEntityOneRelationProps = {};
797
797
  // save one-relation properties
798
798
  for (const property of oneRelationPropertiesToCreate) {
799
- const targetRow = property.isBaseProperty ? baseRow : row;
799
+ const rowToBeSaved = property.isBaseProperty ? baseRow : row;
800
800
  const fieldValue = entity[property.code];
801
801
  const targetDataAccessor = server.getDataAccessor({
802
802
  singularCode: property.targetSingularCode!,
@@ -804,13 +804,15 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
804
804
  if (isObject(fieldValue)) {
805
805
  const targetEntityId = fieldValue["id"];
806
806
  if (!targetEntityId) {
807
- const targetEntity = fieldValue;
808
- const newTargetEntity = await createEntity(server, targetDataAccessor, {
809
- routeContext,
810
- entity: targetEntity,
811
- });
812
- newEntityOneRelationProps[property.code] = newTargetEntity;
813
- targetRow[property.targetIdColumnName!] = newTargetEntity["id"];
807
+ if (!property.selfIdColumnName) {
808
+ const targetEntity = fieldValue;
809
+ const newTargetEntity = await createEntity(server, targetDataAccessor, {
810
+ routeContext,
811
+ entity: targetEntity,
812
+ });
813
+ newEntityOneRelationProps[property.code] = newTargetEntity;
814
+ rowToBeSaved[property.targetIdColumnName!] = newTargetEntity["id"];
815
+ }
814
816
  } else {
815
817
  const targetEntity = await findById(server, targetDataAccessor, {
816
818
  id: targetEntityId,
@@ -822,7 +824,7 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
822
824
  );
823
825
  }
824
826
  newEntityOneRelationProps[property.code] = targetEntity;
825
- targetRow[property.targetIdColumnName!] = targetEntityId;
827
+ rowToBeSaved[property.targetIdColumnName!] = targetEntityId;
826
828
  }
827
829
  } else if (isNumber(fieldValue) || isString(fieldValue)) {
828
830
  // fieldValue is id;
@@ -837,16 +839,17 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
837
839
  );
838
840
  }
839
841
  newEntityOneRelationProps[property.code] = targetEntity;
840
- targetRow[property.targetIdColumnName!] = targetEntityId;
842
+ rowToBeSaved[property.targetIdColumnName!] = targetEntityId;
841
843
  } else {
842
844
  newEntityOneRelationProps[property.code] = null;
843
- targetRow[property.targetIdColumnName!] = null;
845
+ rowToBeSaved[property.targetIdColumnName!] = null;
844
846
  }
845
847
  }
846
848
 
847
849
  let newBaseRow: any;
850
+ let baseDataAccessor: any;
848
851
  if (model.base) {
849
- const baseDataAccessor = server.getDataAccessor({
852
+ baseDataAccessor = server.getDataAccessor({
850
853
  singularCode: model.base,
851
854
  });
852
855
  newBaseRow = await baseDataAccessor.create(baseRow);
@@ -856,6 +859,38 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
856
859
  const newRow = await dataAccessor.create(row);
857
860
  const newEntity = mapDbRowToEntity(server, model, Object.assign({}, newBaseRow, newRow, newEntityOneRelationProps), true);
858
861
 
862
+ // save one-relation properties that has selfIdColumnName
863
+ for (const property of oneRelationPropertiesToCreate) {
864
+ const fieldValue = entity[property.code];
865
+ const targetDataAccessor = server.getDataAccessor({
866
+ singularCode: property.targetSingularCode!,
867
+ });
868
+ if (isObject(fieldValue)) {
869
+ const targetEntityId = fieldValue["id"];
870
+ if (!targetEntityId) {
871
+ if (property.selfIdColumnName) {
872
+ const targetEntity = fieldValue;
873
+ targetEntity[property.selfIdColumnName] = newEntity.id;
874
+ const newTargetEntity = await createEntity(server, targetDataAccessor, {
875
+ routeContext,
876
+ entity: targetEntity,
877
+ });
878
+
879
+ let dataAccessorOfMainEntity = dataAccessor;
880
+ if (property.isBaseProperty) {
881
+ dataAccessorOfMainEntity = baseDataAccessor;
882
+ }
883
+
884
+ const relationFieldChanges = {
885
+ [property.targetIdColumnName]: newTargetEntity.id,
886
+ };
887
+ await dataAccessorOfMainEntity.updateById(newEntity.id, relationFieldChanges);
888
+ newEntity[property.code] = newTargetEntity;
889
+ }
890
+ }
891
+ }
892
+ }
893
+
859
894
  // save many-relation properties
860
895
  for (const property of manyRelationPropertiesToCreate) {
861
896
  newEntity[property.code] = [];
@@ -1068,7 +1103,7 @@ async function updateEntityById(server: IRpdServer, dataAccessor: IRpdDataAccess
1068
1103
 
1069
1104
  const updatedEntityOneRelationProps = {};
1070
1105
  for (const property of oneRelationPropertiesToUpdate) {
1071
- const targetRow = property.isBaseProperty ? baseRow : row;
1106
+ const rowToBeSaved = property.isBaseProperty ? baseRow : row;
1072
1107
  const fieldValue = changes[property.code];
1073
1108
  const targetDataAccessor = server.getDataAccessor({
1074
1109
  singularCode: property.targetSingularCode!,
@@ -1077,13 +1112,15 @@ async function updateEntityById(server: IRpdServer, dataAccessor: IRpdDataAccess
1077
1112
  if (isObject(fieldValue)) {
1078
1113
  const targetEntityId = fieldValue["id"];
1079
1114
  if (!targetEntityId) {
1080
- const targetEntity = fieldValue;
1081
- const newTargetEntity = await createEntity(server, targetDataAccessor, {
1082
- routeContext,
1083
- entity: targetEntity,
1084
- });
1085
- updatedEntityOneRelationProps[property.code] = newTargetEntity;
1086
- targetRow[property.targetIdColumnName!] = newTargetEntity["id"];
1115
+ if (!property.selfIdColumnName) {
1116
+ const targetEntity = fieldValue;
1117
+ const newTargetEntity = await createEntity(server, targetDataAccessor, {
1118
+ routeContext,
1119
+ entity: targetEntity,
1120
+ });
1121
+ updatedEntityOneRelationProps[property.code] = newTargetEntity;
1122
+ rowToBeSaved[property.targetIdColumnName!] = newTargetEntity["id"];
1123
+ }
1087
1124
  } else {
1088
1125
  const targetEntity = await findById(server, targetDataAccessor, {
1089
1126
  id: targetEntityId,
@@ -1095,7 +1132,7 @@ async function updateEntityById(server: IRpdServer, dataAccessor: IRpdDataAccess
1095
1132
  );
1096
1133
  }
1097
1134
  updatedEntityOneRelationProps[property.code] = targetEntity;
1098
- targetRow[property.targetIdColumnName!] = targetEntityId;
1135
+ rowToBeSaved[property.targetIdColumnName!] = targetEntityId;
1099
1136
  }
1100
1137
  } else if (isNumber(fieldValue) || isString(fieldValue)) {
1101
1138
  // fieldValue is id;
@@ -1110,10 +1147,10 @@ async function updateEntityById(server: IRpdServer, dataAccessor: IRpdDataAccess
1110
1147
  );
1111
1148
  }
1112
1149
  updatedEntityOneRelationProps[property.code] = targetEntity;
1113
- targetRow[property.targetIdColumnName!] = targetEntityId;
1150
+ rowToBeSaved[property.targetIdColumnName!] = targetEntityId;
1114
1151
  } else {
1115
1152
  updatedEntityOneRelationProps[property.code] = null;
1116
- targetRow[property.targetIdColumnName!] = null;
1153
+ rowToBeSaved[property.targetIdColumnName!] = null;
1117
1154
  }
1118
1155
  }
1119
1156
 
@@ -1122,16 +1159,52 @@ async function updateEntityById(server: IRpdServer, dataAccessor: IRpdDataAccess
1122
1159
  updatedRow = await dataAccessor.updateById(id, row);
1123
1160
  }
1124
1161
  let updatedBaseRow = baseRow;
1125
- if (model.base && Object.keys(baseRow).length) {
1126
- const baseDataAccessor = server.getDataAccessor({
1162
+ let baseDataAccessor: any;
1163
+ if (model.base) {
1164
+ baseDataAccessor = server.getDataAccessor({
1127
1165
  singularCode: model.base,
1128
1166
  });
1129
- updatedBaseRow = await baseDataAccessor.updateById(id, updatedBaseRow);
1167
+ if (Object.keys(baseRow).length) {
1168
+ updatedBaseRow = await baseDataAccessor.updateById(id, updatedBaseRow);
1169
+ }
1130
1170
  }
1131
1171
 
1132
1172
  let updatedEntity = mapDbRowToEntity(server, model, { ...updatedRow, ...updatedBaseRow, ...updatedEntityOneRelationProps }, true);
1133
1173
  updatedEntity = Object.assign({}, entity, updatedEntity);
1134
1174
 
1175
+ // save one-relation properties that has selfIdColumnName
1176
+ for (const property of oneRelationPropertiesToUpdate) {
1177
+ const fieldValue = changes[property.code];
1178
+ const targetDataAccessor = server.getDataAccessor({
1179
+ singularCode: property.targetSingularCode!,
1180
+ });
1181
+ if (isObject(fieldValue)) {
1182
+ const targetEntityId = fieldValue["id"];
1183
+ if (!targetEntityId) {
1184
+ if (property.selfIdColumnName) {
1185
+ const targetEntity = fieldValue;
1186
+ targetEntity[property.selfIdColumnName] = updatedEntity.id;
1187
+ const newTargetEntity = await createEntity(server, targetDataAccessor, {
1188
+ routeContext,
1189
+ entity: targetEntity,
1190
+ });
1191
+
1192
+ let dataAccessorOfMainEntity = dataAccessor;
1193
+ if (property.isBaseProperty) {
1194
+ dataAccessorOfMainEntity = baseDataAccessor;
1195
+ }
1196
+
1197
+ const relationFieldChanges = {
1198
+ [property.targetIdColumnName]: newTargetEntity.id,
1199
+ };
1200
+ await dataAccessorOfMainEntity.updateById(updatedEntity.id, relationFieldChanges);
1201
+ updatedEntity[property.code] = newTargetEntity;
1202
+ changes[property.code] = newTargetEntity;
1203
+ }
1204
+ }
1205
+ }
1206
+ }
1207
+
1135
1208
  // save many-relation properties
1136
1209
  for (const property of manyRelationPropertiesToUpdate) {
1137
1210
  const relatedEntities: any[] = [];