@e22m4u/js-repository 0.2.1 → 0.2.3
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/cjs/index.cjs
CHANGED
|
@@ -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
|
-
|
|
2352
|
-
const
|
|
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)
|
|
2372
|
-
|
|
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
|
});
|
|
@@ -2555,6 +2578,34 @@ var init_model_definition_utils = __esm({
|
|
|
2555
2578
|
const relNames = Object.keys(relDefs);
|
|
2556
2579
|
return excludeObjectKeys(modelData, relNames);
|
|
2557
2580
|
}
|
|
2581
|
+
/**
|
|
2582
|
+
* Get model name of property value if defined.
|
|
2583
|
+
*
|
|
2584
|
+
* @param {string} modelName
|
|
2585
|
+
* @param {string} propertyName
|
|
2586
|
+
* @returns {undefined|string}
|
|
2587
|
+
*/
|
|
2588
|
+
getModelNameOfPropertyValueIfDefined(modelName, propertyName) {
|
|
2589
|
+
if (!modelName || typeof modelName !== "string")
|
|
2590
|
+
throw new InvalidArgumentError(
|
|
2591
|
+
'Parameter "modelName" of ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined requires a non-empty String, but %v given.',
|
|
2592
|
+
modelName
|
|
2593
|
+
);
|
|
2594
|
+
if (!propertyName || typeof propertyName !== "string")
|
|
2595
|
+
throw new InvalidArgumentError(
|
|
2596
|
+
'Parameter "propertyName" of ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined requires a non-empty String, but %v given.',
|
|
2597
|
+
propertyName
|
|
2598
|
+
);
|
|
2599
|
+
const propDefs = this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
|
|
2600
|
+
const propDef = propDefs[propertyName];
|
|
2601
|
+
if (!propDef) return void 0;
|
|
2602
|
+
if (propDef && typeof propDef === "object") {
|
|
2603
|
+
if (propDef.type === DataType.OBJECT) return propDef.model || void 0;
|
|
2604
|
+
if (propDef.type === DataType.ARRAY)
|
|
2605
|
+
return propDef.itemModel || void 0;
|
|
2606
|
+
}
|
|
2607
|
+
return void 0;
|
|
2608
|
+
}
|
|
2558
2609
|
};
|
|
2559
2610
|
__name(_ModelDefinitionUtils, "ModelDefinitionUtils");
|
|
2560
2611
|
ModelDefinitionUtils = _ModelDefinitionUtils;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e22m4u/js-repository",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Модуль для работы с базами данных для Node.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./src/index.d.ts",
|
|
@@ -51,21 +51,21 @@
|
|
|
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.
|
|
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",
|
|
58
58
|
"eslint": "~9.15.0",
|
|
59
59
|
"eslint-config-prettier": "~9.1.0",
|
|
60
60
|
"eslint-plugin-chai-expect": "~3.1.0",
|
|
61
|
-
"eslint-plugin-jsdoc": "~50.
|
|
61
|
+
"eslint-plugin-jsdoc": "~50.6.0",
|
|
62
62
|
"eslint-plugin-mocha": "~10.5.0",
|
|
63
63
|
"husky": "~9.1.7",
|
|
64
64
|
"mocha": "~10.8.2",
|
|
65
|
-
"prettier": "~3.
|
|
65
|
+
"prettier": "~3.4.1",
|
|
66
66
|
"rimraf": "~6.0.1",
|
|
67
67
|
"tsx": "~4.19.2",
|
|
68
68
|
"typescript": "~5.6.3",
|
|
69
|
-
"typescript-eslint": "~8.
|
|
69
|
+
"typescript-eslint": "~8.16.0"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -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
|
-
|
|
183
|
-
|
|
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)
|
|
205
|
-
|
|
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
|
});
|
|
@@ -404,4 +475,51 @@ export class ModelDefinitionUtils extends Service {
|
|
|
404
475
|
const relNames = Object.keys(relDefs);
|
|
405
476
|
return excludeObjectKeys(modelData, relNames);
|
|
406
477
|
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Get model name of property value if defined.
|
|
481
|
+
*
|
|
482
|
+
* @param {string} modelName
|
|
483
|
+
* @param {string} propertyName
|
|
484
|
+
* @returns {undefined|string}
|
|
485
|
+
*/
|
|
486
|
+
getModelNameOfPropertyValueIfDefined(modelName, propertyName) {
|
|
487
|
+
if (!modelName || typeof modelName !== 'string')
|
|
488
|
+
throw new InvalidArgumentError(
|
|
489
|
+
'Parameter "modelName" of ' +
|
|
490
|
+
'ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined ' +
|
|
491
|
+
'requires a non-empty String, but %v given.',
|
|
492
|
+
modelName,
|
|
493
|
+
);
|
|
494
|
+
if (!propertyName || typeof propertyName !== 'string')
|
|
495
|
+
throw new InvalidArgumentError(
|
|
496
|
+
'Parameter "propertyName" of ' +
|
|
497
|
+
'ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined ' +
|
|
498
|
+
'requires a non-empty String, but %v given.',
|
|
499
|
+
propertyName,
|
|
500
|
+
);
|
|
501
|
+
// если определение свойства не найдено,
|
|
502
|
+
// то возвращаем undefined
|
|
503
|
+
const propDefs =
|
|
504
|
+
this.getPropertiesDefinitionInBaseModelHierarchy(modelName);
|
|
505
|
+
const propDef = propDefs[propertyName];
|
|
506
|
+
if (!propDef) return undefined;
|
|
507
|
+
// если определение свойства является
|
|
508
|
+
// объектом, то проверяем тип и возвращаем
|
|
509
|
+
// название модели
|
|
510
|
+
if (propDef && typeof propDef === 'object') {
|
|
511
|
+
// если тип свойства является объектом,
|
|
512
|
+
// то возвращаем значение опции "model",
|
|
513
|
+
// или undefined
|
|
514
|
+
if (propDef.type === DataType.OBJECT) return propDef.model || undefined;
|
|
515
|
+
// если тип свойства является массивом,
|
|
516
|
+
// то возвращаем значение опции "itemModel",
|
|
517
|
+
// или undefined
|
|
518
|
+
if (propDef.type === DataType.ARRAY)
|
|
519
|
+
return propDef.itemModel || undefined;
|
|
520
|
+
}
|
|
521
|
+
// если определение свойства не является
|
|
522
|
+
// объектом, то возвращаем undefined
|
|
523
|
+
return undefined;
|
|
524
|
+
}
|
|
407
525
|
}
|
|
@@ -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 () {
|
|
@@ -1553,4 +1921,236 @@ describe('ModelDefinitionUtils', function () {
|
|
|
1553
1921
|
throwable({foo: 'bar'})();
|
|
1554
1922
|
});
|
|
1555
1923
|
});
|
|
1924
|
+
|
|
1925
|
+
describe('getModelNameOfPropertyValueIfDefined', function () {
|
|
1926
|
+
it('returns undefined if a given property does not exist in the model', function () {
|
|
1927
|
+
const schema = new Schema();
|
|
1928
|
+
schema.defineModel({name: 'model'});
|
|
1929
|
+
const S = schema.getService(ModelDefinitionUtils);
|
|
1930
|
+
const res = S.getModelNameOfPropertyValueIfDefined('model', 'foo');
|
|
1931
|
+
expect(res).to.be.undefined;
|
|
1932
|
+
});
|
|
1933
|
+
|
|
1934
|
+
describe('short property definition', function () {
|
|
1935
|
+
it('requires parameter "modelName" to be a non-empty String', function () {
|
|
1936
|
+
const schema = new Schema();
|
|
1937
|
+
schema.defineModel({
|
|
1938
|
+
name: 'model',
|
|
1939
|
+
properties: {
|
|
1940
|
+
foo: DataType.OBJECT,
|
|
1941
|
+
},
|
|
1942
|
+
});
|
|
1943
|
+
const S = schema.getService(ModelDefinitionUtils);
|
|
1944
|
+
const throwable = v => () =>
|
|
1945
|
+
S.getModelNameOfPropertyValueIfDefined(v, 'foo');
|
|
1946
|
+
const error = v =>
|
|
1947
|
+
format(
|
|
1948
|
+
'Parameter "modelName" of ' +
|
|
1949
|
+
'ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined ' +
|
|
1950
|
+
'requires a non-empty String, but %s given.',
|
|
1951
|
+
v,
|
|
1952
|
+
);
|
|
1953
|
+
expect(throwable('')).to.throw(error('""'));
|
|
1954
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
1955
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
1956
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
1957
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
1958
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
1959
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
1960
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
1961
|
+
throwable('model')();
|
|
1962
|
+
});
|
|
1963
|
+
|
|
1964
|
+
it('requires parameter "propertyName" to be a non-empty String', function () {
|
|
1965
|
+
const schema = new Schema();
|
|
1966
|
+
schema.defineModel({
|
|
1967
|
+
name: 'model',
|
|
1968
|
+
properties: {
|
|
1969
|
+
foo: DataType.OBJECT,
|
|
1970
|
+
},
|
|
1971
|
+
});
|
|
1972
|
+
const S = schema.getService(ModelDefinitionUtils);
|
|
1973
|
+
const throwable = v => () =>
|
|
1974
|
+
S.getModelNameOfPropertyValueIfDefined('model', v);
|
|
1975
|
+
const error = v =>
|
|
1976
|
+
format(
|
|
1977
|
+
'Parameter "propertyName" of ' +
|
|
1978
|
+
'ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined ' +
|
|
1979
|
+
'requires a non-empty String, but %s given.',
|
|
1980
|
+
v,
|
|
1981
|
+
);
|
|
1982
|
+
expect(throwable('')).to.throw(error('""'));
|
|
1983
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
1984
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
1985
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
1986
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
1987
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
1988
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
1989
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
1990
|
+
throwable('foo')();
|
|
1991
|
+
});
|
|
1992
|
+
|
|
1993
|
+
it('returns undefined if the property definition is DataType', function () {
|
|
1994
|
+
const fn = v => {
|
|
1995
|
+
const schema = new Schema();
|
|
1996
|
+
schema.defineModel({
|
|
1997
|
+
name: 'model',
|
|
1998
|
+
properties: {
|
|
1999
|
+
foo: v,
|
|
2000
|
+
},
|
|
2001
|
+
});
|
|
2002
|
+
const S = schema.getService(ModelDefinitionUtils);
|
|
2003
|
+
return S.getModelNameOfPropertyValueIfDefined('model', 'foo');
|
|
2004
|
+
};
|
|
2005
|
+
expect(fn(DataType.ANY)).to.be.undefined;
|
|
2006
|
+
expect(fn(DataType.STRING)).to.be.undefined;
|
|
2007
|
+
expect(fn(DataType.NUMBER)).to.be.undefined;
|
|
2008
|
+
expect(fn(DataType.BOOLEAN)).to.be.undefined;
|
|
2009
|
+
expect(fn(DataType.ARRAY)).to.be.undefined;
|
|
2010
|
+
expect(fn(DataType.OBJECT)).to.be.undefined;
|
|
2011
|
+
});
|
|
2012
|
+
});
|
|
2013
|
+
|
|
2014
|
+
describe('full property definition', function () {
|
|
2015
|
+
it('requires parameter "modelName" to be a non-empty String', function () {
|
|
2016
|
+
const schema = new Schema();
|
|
2017
|
+
schema.defineModel({
|
|
2018
|
+
name: 'model',
|
|
2019
|
+
properties: {
|
|
2020
|
+
foo: {
|
|
2021
|
+
type: DataType.OBJECT,
|
|
2022
|
+
},
|
|
2023
|
+
},
|
|
2024
|
+
});
|
|
2025
|
+
const S = schema.getService(ModelDefinitionUtils);
|
|
2026
|
+
const throwable = v => () =>
|
|
2027
|
+
S.getModelNameOfPropertyValueIfDefined(v, 'foo');
|
|
2028
|
+
const error = v =>
|
|
2029
|
+
format(
|
|
2030
|
+
'Parameter "modelName" of ' +
|
|
2031
|
+
'ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined ' +
|
|
2032
|
+
'requires a non-empty String, but %s given.',
|
|
2033
|
+
v,
|
|
2034
|
+
);
|
|
2035
|
+
expect(throwable('')).to.throw(error('""'));
|
|
2036
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
2037
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
2038
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
2039
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
2040
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
2041
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
2042
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
2043
|
+
throwable('model')();
|
|
2044
|
+
});
|
|
2045
|
+
|
|
2046
|
+
it('requires parameter "propertyName" to be a non-empty String', function () {
|
|
2047
|
+
const schema = new Schema();
|
|
2048
|
+
schema.defineModel({
|
|
2049
|
+
name: 'model',
|
|
2050
|
+
properties: {
|
|
2051
|
+
foo: {
|
|
2052
|
+
type: DataType.OBJECT,
|
|
2053
|
+
},
|
|
2054
|
+
},
|
|
2055
|
+
});
|
|
2056
|
+
const S = schema.getService(ModelDefinitionUtils);
|
|
2057
|
+
const throwable = v => () =>
|
|
2058
|
+
S.getModelNameOfPropertyValueIfDefined('model', v);
|
|
2059
|
+
const error = v =>
|
|
2060
|
+
format(
|
|
2061
|
+
'Parameter "propertyName" of ' +
|
|
2062
|
+
'ModelDefinitionUtils.getModelNameOfPropertyValueIfDefined ' +
|
|
2063
|
+
'requires a non-empty String, but %s given.',
|
|
2064
|
+
v,
|
|
2065
|
+
);
|
|
2066
|
+
expect(throwable('')).to.throw(error('""'));
|
|
2067
|
+
expect(throwable(10)).to.throw(error('10'));
|
|
2068
|
+
expect(throwable(true)).to.throw(error('true'));
|
|
2069
|
+
expect(throwable(false)).to.throw(error('false'));
|
|
2070
|
+
expect(throwable([])).to.throw(error('Array'));
|
|
2071
|
+
expect(throwable({})).to.throw(error('Object'));
|
|
2072
|
+
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
2073
|
+
expect(throwable(null)).to.throw(error('null'));
|
|
2074
|
+
throwable('foo')();
|
|
2075
|
+
});
|
|
2076
|
+
|
|
2077
|
+
it('return undefined if no model name specified', function () {
|
|
2078
|
+
const fn = v => {
|
|
2079
|
+
const schema = new Schema();
|
|
2080
|
+
schema.defineModel({
|
|
2081
|
+
name: 'model',
|
|
2082
|
+
properties: {
|
|
2083
|
+
foo: {
|
|
2084
|
+
type: v,
|
|
2085
|
+
},
|
|
2086
|
+
},
|
|
2087
|
+
});
|
|
2088
|
+
const S = schema.getService(ModelDefinitionUtils);
|
|
2089
|
+
return S.getModelNameOfPropertyValueIfDefined('model', 'foo');
|
|
2090
|
+
};
|
|
2091
|
+
expect(fn(DataType.ANY)).to.be.undefined;
|
|
2092
|
+
expect(fn(DataType.STRING)).to.be.undefined;
|
|
2093
|
+
expect(fn(DataType.NUMBER)).to.be.undefined;
|
|
2094
|
+
expect(fn(DataType.BOOLEAN)).to.be.undefined;
|
|
2095
|
+
expect(fn(DataType.ARRAY)).to.be.undefined;
|
|
2096
|
+
expect(fn(DataType.OBJECT)).to.be.undefined;
|
|
2097
|
+
});
|
|
2098
|
+
|
|
2099
|
+
it('return undefined if no model name specified in case of Array property', function () {
|
|
2100
|
+
const fn = v => {
|
|
2101
|
+
const schema = new Schema();
|
|
2102
|
+
schema.defineModel({
|
|
2103
|
+
name: 'model',
|
|
2104
|
+
properties: {
|
|
2105
|
+
foo: {
|
|
2106
|
+
type: DataType.ARRAY,
|
|
2107
|
+
itemType: v,
|
|
2108
|
+
},
|
|
2109
|
+
},
|
|
2110
|
+
});
|
|
2111
|
+
const S = schema.getService(ModelDefinitionUtils);
|
|
2112
|
+
return S.getModelNameOfPropertyValueIfDefined('model', 'foo');
|
|
2113
|
+
};
|
|
2114
|
+
expect(fn(DataType.ANY)).to.be.undefined;
|
|
2115
|
+
expect(fn(DataType.STRING)).to.be.undefined;
|
|
2116
|
+
expect(fn(DataType.NUMBER)).to.be.undefined;
|
|
2117
|
+
expect(fn(DataType.BOOLEAN)).to.be.undefined;
|
|
2118
|
+
expect(fn(DataType.ARRAY)).to.be.undefined;
|
|
2119
|
+
expect(fn(DataType.OBJECT)).to.be.undefined;
|
|
2120
|
+
});
|
|
2121
|
+
|
|
2122
|
+
it('returns a model name from the option "model" in case of Object property', function () {
|
|
2123
|
+
const schema = new Schema();
|
|
2124
|
+
schema.defineModel({
|
|
2125
|
+
name: 'model',
|
|
2126
|
+
properties: {
|
|
2127
|
+
foo: {
|
|
2128
|
+
type: DataType.OBJECT,
|
|
2129
|
+
model: 'myModel',
|
|
2130
|
+
},
|
|
2131
|
+
},
|
|
2132
|
+
});
|
|
2133
|
+
const S = schema.getService(ModelDefinitionUtils);
|
|
2134
|
+
const res = S.getModelNameOfPropertyValueIfDefined('model', 'foo');
|
|
2135
|
+
expect(res).to.be.eq('myModel');
|
|
2136
|
+
});
|
|
2137
|
+
|
|
2138
|
+
it('returns a model name from the option "itemModel" in case of Array property', function () {
|
|
2139
|
+
const schema = new Schema();
|
|
2140
|
+
schema.defineModel({
|
|
2141
|
+
name: 'model',
|
|
2142
|
+
properties: {
|
|
2143
|
+
foo: {
|
|
2144
|
+
type: DataType.ARRAY,
|
|
2145
|
+
itemType: DataType.OBJECT,
|
|
2146
|
+
itemModel: 'myModel',
|
|
2147
|
+
},
|
|
2148
|
+
},
|
|
2149
|
+
});
|
|
2150
|
+
const S = schema.getService(ModelDefinitionUtils);
|
|
2151
|
+
const res = S.getModelNameOfPropertyValueIfDefined('model', 'foo');
|
|
2152
|
+
expect(res).to.be.eq('myModel');
|
|
2153
|
+
});
|
|
2154
|
+
});
|
|
2155
|
+
});
|
|
1556
2156
|
});
|