@e22m4u/js-repository 0.2.2 → 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
|
@@ -2578,6 +2578,34 @@ var init_model_definition_utils = __esm({
|
|
|
2578
2578
|
const relNames = Object.keys(relDefs);
|
|
2579
2579
|
return excludeObjectKeys(modelData, relNames);
|
|
2580
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
|
+
}
|
|
2581
2609
|
};
|
|
2582
2610
|
__name(_ModelDefinitionUtils, "ModelDefinitionUtils");
|
|
2583
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",
|
|
@@ -58,14 +58,14 @@
|
|
|
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
|
}
|
|
@@ -475,4 +475,51 @@ export class ModelDefinitionUtils extends Service {
|
|
|
475
475
|
const relNames = Object.keys(relDefs);
|
|
476
476
|
return excludeObjectKeys(modelData, relNames);
|
|
477
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
|
+
}
|
|
478
525
|
}
|
|
@@ -1921,4 +1921,236 @@ describe('ModelDefinitionUtils', function () {
|
|
|
1921
1921
|
throwable({foo: 'bar'})();
|
|
1922
1922
|
});
|
|
1923
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
|
+
});
|
|
1924
2156
|
});
|