@e22m4u/js-repository 0.2.1 → 0.2.2

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.
@@ -2348,8 +2348,19 @@ var init_model_definition_utils = __esm({
2348
2348
  propNames.forEach((propName) => {
2349
2349
  if (!(propName in convertedData)) return;
2350
2350
  const colName = this.getColumnNameByPropertyName(modelName, propName);
2351
- if (propName === colName) return;
2352
- const propValue = convertedData[propName];
2351
+ let propValue = convertedData[propName];
2352
+ const propDef = propDefs[propName];
2353
+ if (propValue !== null && typeof propValue === "object" && !Array.isArray(propValue) && propDef !== null && typeof propDef === "object" && propDef.type === DataType.OBJECT && propDef.model) {
2354
+ propValue = this.convertPropertyNamesToColumnNames(
2355
+ propDef.model,
2356
+ propValue
2357
+ );
2358
+ }
2359
+ if (Array.isArray(propValue) && propDef !== null && typeof propDef === "object" && propDef.type === DataType.ARRAY && propDef.itemModel) {
2360
+ propValue = propValue.map((el) => {
2361
+ return el !== null && typeof el === "object" && !Array.isArray(el) ? this.convertPropertyNamesToColumnNames(propDef.itemModel, el) : el;
2362
+ });
2363
+ }
2353
2364
  delete convertedData[propName];
2354
2365
  convertedData[colName] = propValue;
2355
2366
  });
@@ -2368,8 +2379,20 @@ var init_model_definition_utils = __esm({
2368
2379
  const convertedData = cloneDeep(tableData);
2369
2380
  propNames.forEach((propName) => {
2370
2381
  const colName = this.getColumnNameByPropertyName(modelName, propName);
2371
- if (!(colName in convertedData) || colName === propName) return;
2372
- const colValue = convertedData[colName];
2382
+ if (!(colName in convertedData)) return;
2383
+ let colValue = convertedData[colName];
2384
+ const propDef = propDefs[propName];
2385
+ if (colValue !== null && typeof colValue === "object" && !Array.isArray(colValue) && propDef !== null && typeof propDef === "object" && propDef.type === DataType.OBJECT && propDef.model) {
2386
+ colValue = this.convertColumnNamesToPropertyNames(
2387
+ propDef.model,
2388
+ colValue
2389
+ );
2390
+ }
2391
+ if (Array.isArray(colValue) && propDef !== null && typeof propDef === "object" && propDef.type === DataType.ARRAY && propDef.itemModel) {
2392
+ colValue = colValue.map((el) => {
2393
+ return el !== null && typeof el === "object" && !Array.isArray(el) ? this.convertColumnNamesToPropertyNames(propDef.itemModel, el) : el;
2394
+ });
2395
+ }
2373
2396
  delete convertedData[colName];
2374
2397
  convertedData[propName] = colValue;
2375
2398
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/js-repository",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Модуль для работы с базами данных для Node.js",
5
5
  "type": "module",
6
6
  "types": "./src/index.d.ts",
@@ -51,7 +51,7 @@
51
51
  "@types/mocha": "~10.0.10",
52
52
  "c8": "~10.1.2",
53
53
  "chai": "~5.1.2",
54
- "chai-as-promised": "~8.0.0",
54
+ "chai-as-promised": "~8.0.1",
55
55
  "chai-spies": "~1.1.0",
56
56
  "chai-subset": "~1.6.0",
57
57
  "esbuild": "~0.24.0",
@@ -179,8 +179,43 @@ export class ModelDefinitionUtils extends Service {
179
179
  propNames.forEach(propName => {
180
180
  if (!(propName in convertedData)) return;
181
181
  const colName = this.getColumnNameByPropertyName(modelName, propName);
182
- if (propName === colName) return;
183
- const propValue = convertedData[propName];
182
+ let propValue = convertedData[propName];
183
+ // если значением является объект, то проверяем
184
+ // тип свойства и наличие модели для замены
185
+ // полей данного объекта
186
+ const propDef = propDefs[propName];
187
+ if (
188
+ propValue !== null &&
189
+ typeof propValue === 'object' &&
190
+ !Array.isArray(propValue) &&
191
+ propDef !== null &&
192
+ typeof propDef === 'object' &&
193
+ propDef.type === DataType.OBJECT &&
194
+ propDef.model
195
+ ) {
196
+ propValue = this.convertPropertyNamesToColumnNames(
197
+ propDef.model,
198
+ propValue,
199
+ );
200
+ }
201
+ // если значением является массив, то проверяем
202
+ // тип свойства и наличие модели элементов массива
203
+ // для замены полей каждого объекта
204
+ if (
205
+ Array.isArray(propValue) &&
206
+ propDef !== null &&
207
+ typeof propDef === 'object' &&
208
+ propDef.type === DataType.ARRAY &&
209
+ propDef.itemModel
210
+ ) {
211
+ propValue = propValue.map(el => {
212
+ // если элементом массива является объект,
213
+ // то конвертируем поля согласно модели
214
+ return el !== null && typeof el === 'object' && !Array.isArray(el)
215
+ ? this.convertPropertyNamesToColumnNames(propDef.itemModel, el)
216
+ : el;
217
+ });
218
+ }
184
219
  delete convertedData[propName];
185
220
  convertedData[colName] = propValue;
186
221
  });
@@ -201,8 +236,44 @@ export class ModelDefinitionUtils extends Service {
201
236
  const convertedData = cloneDeep(tableData);
202
237
  propNames.forEach(propName => {
203
238
  const colName = this.getColumnNameByPropertyName(modelName, propName);
204
- if (!(colName in convertedData) || colName === propName) return;
205
- const colValue = convertedData[colName];
239
+ if (!(colName in convertedData)) return;
240
+ let colValue = convertedData[colName];
241
+ // если значением является объект, то проверяем
242
+ // тип свойства и наличие модели для замены
243
+ // полей данного объекта
244
+ const propDef = propDefs[propName];
245
+ if (
246
+ colValue !== null &&
247
+ typeof colValue === 'object' &&
248
+ !Array.isArray(colValue) &&
249
+ propDef !== null &&
250
+ typeof propDef === 'object' &&
251
+ propDef.type === DataType.OBJECT &&
252
+ propDef.model
253
+ ) {
254
+ colValue = this.convertColumnNamesToPropertyNames(
255
+ propDef.model,
256
+ colValue,
257
+ );
258
+ }
259
+ // если значением является массив, то проверяем
260
+ // тип свойства и наличие модели элементов массива
261
+ // для замены полей каждого объекта
262
+ if (
263
+ Array.isArray(colValue) &&
264
+ propDef !== null &&
265
+ typeof propDef === 'object' &&
266
+ propDef.type === DataType.ARRAY &&
267
+ propDef.itemModel
268
+ ) {
269
+ colValue = colValue.map(el => {
270
+ // если элементом массива является объект,
271
+ // то конвертируем поля согласно модели
272
+ return el !== null && typeof el === 'object' && !Array.isArray(el)
273
+ ? this.convertColumnNamesToPropertyNames(propDef.itemModel, el)
274
+ : el;
275
+ });
276
+ }
206
277
  delete convertedData[colName];
207
278
  convertedData[propName] = colValue;
208
279
  });
@@ -690,6 +690,191 @@ describe('ModelDefinitionUtils', function () {
690
690
  .convertPropertyNamesToColumnNames('modelB', {foo: 'string'});
691
691
  expect(result).to.be.eql({fooColumn: 'string'});
692
692
  });
693
+
694
+ describe('embedded object with model', function () {
695
+ it('does nothing if no property definitions', function () {
696
+ const schema = new Schema();
697
+ schema.defineModel({
698
+ name: 'modelA',
699
+ properties: {
700
+ embedded: {
701
+ type: DataType.OBJECT,
702
+ model: 'modelB',
703
+ },
704
+ },
705
+ });
706
+ schema.defineModel({
707
+ name: 'modelB',
708
+ });
709
+ const result = schema
710
+ .getService(ModelDefinitionUtils)
711
+ .convertPropertyNamesToColumnNames('modelA', {
712
+ embedded: {foo: 'string'},
713
+ });
714
+ expect(result).to.be.eql({embedded: {foo: 'string'}});
715
+ });
716
+
717
+ it('does nothing if no column name specified', function () {
718
+ const schema = new Schema();
719
+ schema.defineModel({
720
+ name: 'modelA',
721
+ properties: {
722
+ embedded: {
723
+ type: DataType.OBJECT,
724
+ model: 'modelB',
725
+ },
726
+ },
727
+ });
728
+ schema.defineModel({
729
+ name: 'modelB',
730
+ properties: {
731
+ foo: DataType.STRING,
732
+ bar: DataType.NUMBER,
733
+ },
734
+ });
735
+ const result = schema
736
+ .getService(ModelDefinitionUtils)
737
+ .convertPropertyNamesToColumnNames('modelA', {
738
+ embedded: {foo: 'string', bar: 10},
739
+ });
740
+ expect(result).to.be.eql({embedded: {foo: 'string', bar: 10}});
741
+ });
742
+
743
+ it('replaces property names by column names', function () {
744
+ const schema = new Schema();
745
+ schema.defineModel({
746
+ name: 'modelA',
747
+ properties: {
748
+ embedded: {
749
+ type: DataType.OBJECT,
750
+ model: 'modelB',
751
+ },
752
+ },
753
+ });
754
+ schema.defineModel({
755
+ name: 'modelB',
756
+ properties: {
757
+ foo: {
758
+ type: DataType.STRING,
759
+ columnName: 'fooColumn',
760
+ },
761
+ bar: {
762
+ type: DataType.NUMBER,
763
+ columnName: 'barColumn',
764
+ },
765
+ },
766
+ });
767
+ const result = schema
768
+ .getService(ModelDefinitionUtils)
769
+ .convertPropertyNamesToColumnNames('modelA', {
770
+ embedded: {foo: 'string', bar: 10},
771
+ });
772
+ expect(result).to.be.eql({
773
+ embedded: {fooColumn: 'string', barColumn: 10},
774
+ });
775
+ });
776
+ });
777
+
778
+ describe('embedded array with items model', function () {
779
+ it('does nothing if no property definitions', function () {
780
+ const schema = new Schema();
781
+ schema.defineModel({
782
+ name: 'modelA',
783
+ properties: {
784
+ embedded: {
785
+ type: DataType.ARRAY,
786
+ itemType: DataType.OBJECT,
787
+ itemModel: 'modelB',
788
+ },
789
+ },
790
+ });
791
+ schema.defineModel({
792
+ name: 'modelB',
793
+ });
794
+ const result = schema
795
+ .getService(ModelDefinitionUtils)
796
+ .convertPropertyNamesToColumnNames('modelA', {
797
+ embedded: [{foo: 'val'}, {bar: 10}],
798
+ });
799
+ expect(result).to.be.eql({embedded: [{foo: 'val'}, {bar: 10}]});
800
+ });
801
+
802
+ it('does nothing if no column name specified', function () {
803
+ const schema = new Schema();
804
+ schema.defineModel({
805
+ name: 'modelA',
806
+ properties: {
807
+ embedded: {
808
+ type: DataType.ARRAY,
809
+ itemType: DataType.OBJECT,
810
+ itemModel: 'modelB',
811
+ },
812
+ },
813
+ });
814
+ schema.defineModel({
815
+ name: 'modelB',
816
+ properties: {
817
+ foo: DataType.STRING,
818
+ bar: DataType.NUMBER,
819
+ },
820
+ });
821
+ const result = schema
822
+ .getService(ModelDefinitionUtils)
823
+ .convertPropertyNamesToColumnNames('modelA', {
824
+ embedded: [
825
+ {foo: 'val1', bar: 10},
826
+ {foo: 'val2', bar: 20},
827
+ ],
828
+ });
829
+ expect(result).to.be.eql({
830
+ embedded: [
831
+ {foo: 'val1', bar: 10},
832
+ {foo: 'val2', bar: 20},
833
+ ],
834
+ });
835
+ });
836
+
837
+ it('replaces property names by column names', function () {
838
+ const schema = new Schema();
839
+ schema.defineModel({
840
+ name: 'modelA',
841
+ properties: {
842
+ embedded: {
843
+ type: DataType.ARRAY,
844
+ itemType: DataType.OBJECT,
845
+ itemModel: 'modelB',
846
+ },
847
+ },
848
+ });
849
+ schema.defineModel({
850
+ name: 'modelB',
851
+ properties: {
852
+ foo: {
853
+ type: DataType.STRING,
854
+ columnName: 'fooColumn',
855
+ },
856
+ bar: {
857
+ type: DataType.NUMBER,
858
+ columnName: 'barColumn',
859
+ },
860
+ },
861
+ });
862
+ const result = schema
863
+ .getService(ModelDefinitionUtils)
864
+ .convertPropertyNamesToColumnNames('modelA', {
865
+ embedded: [
866
+ {foo: 'val1', bar: 10},
867
+ {foo: 'val2', bar: 20},
868
+ ],
869
+ });
870
+ expect(result).to.be.eql({
871
+ embedded: [
872
+ {fooColumn: 'val1', barColumn: 10},
873
+ {fooColumn: 'val2', barColumn: 20},
874
+ ],
875
+ });
876
+ });
877
+ });
693
878
  });
694
879
 
695
880
  describe('convertColumnNamesToPropertyNames', function () {
@@ -764,6 +949,189 @@ describe('ModelDefinitionUtils', function () {
764
949
  .convertColumnNamesToPropertyNames('modelA', {fooColumn: 'string'});
765
950
  expect(result).to.be.eql({foo: 'string'});
766
951
  });
952
+
953
+ describe('embedded object with model', function () {
954
+ it('does nothing if no property definitions', function () {
955
+ const schema = new Schema();
956
+ schema.defineModel({
957
+ name: 'modelA',
958
+ properties: {
959
+ embedded: {
960
+ type: DataType.OBJECT,
961
+ model: 'modelB',
962
+ },
963
+ },
964
+ });
965
+ schema.defineModel({
966
+ name: 'modelB',
967
+ });
968
+ const result = schema
969
+ .getService(ModelDefinitionUtils)
970
+ .convertColumnNamesToPropertyNames('modelA', {
971
+ embedded: {foo: 'string'},
972
+ });
973
+ expect(result).to.be.eql({embedded: {foo: 'string'}});
974
+ });
975
+
976
+ it('does nothing if no column name specified', function () {
977
+ const schema = new Schema();
978
+ schema.defineModel({
979
+ name: 'modelA',
980
+ properties: {
981
+ embedded: {
982
+ type: DataType.OBJECT,
983
+ model: 'modelB',
984
+ },
985
+ },
986
+ });
987
+ schema.defineModel({
988
+ name: 'modelB',
989
+ properties: {
990
+ foo: DataType.STRING,
991
+ bar: DataType.NUMBER,
992
+ },
993
+ });
994
+ const result = schema
995
+ .getService(ModelDefinitionUtils)
996
+ .convertColumnNamesToPropertyNames('modelA', {
997
+ embedded: {foo: 'string', bar: 10},
998
+ });
999
+ expect(result).to.be.eql({embedded: {foo: 'string', bar: 10}});
1000
+ });
1001
+
1002
+ it('replaces property names by column names', function () {
1003
+ const schema = new Schema();
1004
+ schema.defineModel({
1005
+ name: 'modelA',
1006
+ properties: {
1007
+ embedded: {
1008
+ type: DataType.OBJECT,
1009
+ model: 'modelB',
1010
+ },
1011
+ },
1012
+ });
1013
+ schema.defineModel({
1014
+ name: 'modelB',
1015
+ properties: {
1016
+ foo: {
1017
+ type: DataType.STRING,
1018
+ columnName: 'fooColumn',
1019
+ },
1020
+ bar: {
1021
+ type: DataType.NUMBER,
1022
+ columnName: 'barColumn',
1023
+ },
1024
+ },
1025
+ });
1026
+ const result = schema
1027
+ .getService(ModelDefinitionUtils)
1028
+ .convertColumnNamesToPropertyNames('modelA', {
1029
+ embedded: {fooColumn: 'string', barColumn: 10},
1030
+ });
1031
+ expect(result).to.be.eql({embedded: {foo: 'string', bar: 10}});
1032
+ });
1033
+ });
1034
+
1035
+ describe('embedded array with items model', function () {
1036
+ it('does nothing if no property definitions', function () {
1037
+ const schema = new Schema();
1038
+ schema.defineModel({
1039
+ name: 'modelA',
1040
+ properties: {
1041
+ embedded: {
1042
+ type: DataType.ARRAY,
1043
+ itemType: DataType.OBJECT,
1044
+ itemModel: 'modelB',
1045
+ },
1046
+ },
1047
+ });
1048
+ schema.defineModel({
1049
+ name: 'modelB',
1050
+ });
1051
+ const result = schema
1052
+ .getService(ModelDefinitionUtils)
1053
+ .convertColumnNamesToPropertyNames('modelA', {
1054
+ embedded: [{foo: 'val'}, {bar: 10}],
1055
+ });
1056
+ expect(result).to.be.eql({embedded: [{foo: 'val'}, {bar: 10}]});
1057
+ });
1058
+
1059
+ it('does nothing if no column name specified', function () {
1060
+ const schema = new Schema();
1061
+ schema.defineModel({
1062
+ name: 'modelA',
1063
+ properties: {
1064
+ embedded: {
1065
+ type: DataType.ARRAY,
1066
+ itemType: DataType.OBJECT,
1067
+ itemModel: 'modelB',
1068
+ },
1069
+ },
1070
+ });
1071
+ schema.defineModel({
1072
+ name: 'modelB',
1073
+ properties: {
1074
+ foo: DataType.STRING,
1075
+ bar: DataType.NUMBER,
1076
+ },
1077
+ });
1078
+ const result = schema
1079
+ .getService(ModelDefinitionUtils)
1080
+ .convertColumnNamesToPropertyNames('modelA', {
1081
+ embedded: [
1082
+ {foo: 'val1', bar: 10},
1083
+ {foo: 'val2', bar: 20},
1084
+ ],
1085
+ });
1086
+ expect(result).to.be.eql({
1087
+ embedded: [
1088
+ {foo: 'val1', bar: 10},
1089
+ {foo: 'val2', bar: 20},
1090
+ ],
1091
+ });
1092
+ });
1093
+
1094
+ it('replaces property names by column names', function () {
1095
+ const schema = new Schema();
1096
+ schema.defineModel({
1097
+ name: 'modelA',
1098
+ properties: {
1099
+ embedded: {
1100
+ type: DataType.ARRAY,
1101
+ itemType: DataType.OBJECT,
1102
+ itemModel: 'modelB',
1103
+ },
1104
+ },
1105
+ });
1106
+ schema.defineModel({
1107
+ name: 'modelB',
1108
+ properties: {
1109
+ foo: {
1110
+ type: DataType.STRING,
1111
+ columnName: 'fooColumn',
1112
+ },
1113
+ bar: {
1114
+ type: DataType.NUMBER,
1115
+ columnName: 'barColumn',
1116
+ },
1117
+ },
1118
+ });
1119
+ const result = schema
1120
+ .getService(ModelDefinitionUtils)
1121
+ .convertColumnNamesToPropertyNames('modelA', {
1122
+ embedded: [
1123
+ {fooColumn: 'val1', barColumn: 10},
1124
+ {fooColumn: 'val2', barColumn: 20},
1125
+ ],
1126
+ });
1127
+ expect(result).to.be.eql({
1128
+ embedded: [
1129
+ {foo: 'val1', bar: 10},
1130
+ {foo: 'val2', bar: 20},
1131
+ ],
1132
+ });
1133
+ });
1134
+ });
767
1135
  });
768
1136
 
769
1137
  describe('getDataTypeByPropertyName', function () {