@e22m4u/js-repository 0.1.1 → 0.1.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.
Files changed (141) hide show
  1. package/.eslintrc.cjs +41 -17
  2. package/.husky/commit-msg +1 -4
  3. package/.husky/pre-commit +0 -3
  4. package/.mocharc.cjs +2 -2
  5. package/README.md +2 -2
  6. package/docs/assets/main.js +2 -2
  7. package/docs/assets/navigation.js +1 -1
  8. package/docs/assets/search.js +1 -1
  9. package/docs/assets/style.css +35 -15
  10. package/docs/classes/Adapter.html +19 -19
  11. package/docs/classes/AdapterLoader.html +8 -8
  12. package/docs/classes/AdapterRegistry.html +8 -8
  13. package/docs/classes/BelongsToResolver.html +9 -9
  14. package/docs/classes/DatasourceDefinitionValidator.html +8 -8
  15. package/docs/classes/DefinitionRegistry.html +12 -12
  16. package/docs/classes/FieldsClauseTool.html +10 -10
  17. package/docs/classes/HasManyResolver.html +10 -10
  18. package/docs/classes/HasOneResolver.html +10 -10
  19. package/docs/classes/IncludeClauseTool.html +12 -12
  20. package/docs/classes/InvalidArgumentError.html +7 -5
  21. package/docs/classes/InvalidOperatorValueError.html +7 -5
  22. package/docs/classes/ModelDataSanitizer.html +8 -8
  23. package/docs/classes/ModelDataValidator.html +9 -9
  24. package/docs/classes/ModelDefinitionUtils.html +23 -23
  25. package/docs/classes/ModelDefinitionValidator.html +8 -8
  26. package/docs/classes/NotImplementedError.html +7 -5
  27. package/docs/classes/OperatorClauseTool.html +32 -32
  28. package/docs/classes/OrderClauseTool.html +10 -10
  29. package/docs/classes/PrimaryKeysDefinitionValidator.html +8 -8
  30. package/docs/classes/PropertiesDefinitionValidator.html +8 -8
  31. package/docs/classes/ReferencesManyResolver.html +8 -8
  32. package/docs/classes/RelationsDefinitionValidator.html +8 -8
  33. package/docs/classes/Repository.html +28 -24
  34. package/docs/classes/RepositoryRegistry.html +9 -9
  35. package/docs/classes/Schema.html +10 -10
  36. package/docs/classes/SliceClauseTool.html +10 -10
  37. package/docs/classes/WhereClauseTool.html +9 -9
  38. package/docs/enums/DataType.html +2 -2
  39. package/docs/enums/DecoratorTargetType.html +11 -0
  40. package/docs/enums/RelationType.html +2 -2
  41. package/docs/functions/capitalize.html +1 -1
  42. package/docs/functions/cloneDeep.html +2 -2
  43. package/docs/functions/excludeObjectKeys.html +2 -2
  44. package/docs/functions/getCtorName.html +1 -1
  45. package/docs/functions/getDecoratorTargetType.html +2 -0
  46. package/docs/functions/getValueByPath.html +1 -1
  47. package/docs/functions/isCtor.html +3 -2
  48. package/docs/functions/isPureObject.html +1 -1
  49. package/docs/functions/selectObjectKeys.html +2 -2
  50. package/docs/functions/singularize.html +1 -1
  51. package/docs/functions/stringToRegexp.html +2 -2
  52. package/docs/index.html +3 -3
  53. package/docs/interfaces/AndClause.html +2 -2
  54. package/docs/interfaces/Constructor.html +4 -0
  55. package/docs/interfaces/OrClause.html +2 -2
  56. package/docs/modules.html +4 -1
  57. package/docs/types/AnyObject.html +1 -1
  58. package/docs/types/BelongsToDefinition.html +1 -1
  59. package/docs/types/DEFAULT_PRIMARY_KEY_PROPERTY_NAME.html +1 -1
  60. package/docs/types/DatasourceDefinition.html +1 -1
  61. package/docs/types/FieldsClause.html +1 -1
  62. package/docs/types/FilterClause.html +1 -1
  63. package/docs/types/Flatten.html +1 -1
  64. package/docs/types/FullPropertyDefinition.html +1 -1
  65. package/docs/types/HasManyDefinition.html +1 -1
  66. package/docs/types/HasOneDefinition.html +1 -1
  67. package/docs/types/Identity.html +2 -2
  68. package/docs/types/IncludeClause.html +1 -1
  69. package/docs/types/ItemFilterClause.html +2 -2
  70. package/docs/types/ModelData.html +1 -1
  71. package/docs/types/ModelDefinition.html +1 -1
  72. package/docs/types/ModelId.html +1 -1
  73. package/docs/types/NestedIncludeClause.html +1 -1
  74. package/docs/types/NormalizedFieldsClause.html +1 -1
  75. package/docs/types/NormalizedIncludeClause.html +1 -1
  76. package/docs/types/OperatorClause.html +3 -3
  77. package/docs/types/OptionalUnlessRequiredId.html +2 -2
  78. package/docs/types/OrderClause.html +1 -1
  79. package/docs/types/PartialBy.html +2 -2
  80. package/docs/types/PartialWithoutId.html +2 -2
  81. package/docs/types/PolyBelongsToDefinition.html +1 -1
  82. package/docs/types/PolyHasManyDefinitionWithTargetKeys.html +1 -1
  83. package/docs/types/PolyHasManyDefinitionWithTargetRelationName.html +1 -1
  84. package/docs/types/PolyHasOneDefinitionWithTargetKeys.html +1 -1
  85. package/docs/types/PolyHasOneDefinitionWithTargetRelationName.html +1 -1
  86. package/docs/types/PropertiesClause.html +3 -3
  87. package/docs/types/PropertyDefinition.html +1 -1
  88. package/docs/types/PropertyDefinitionMap.html +1 -1
  89. package/docs/types/ReferencesManyDefinition.html +1 -1
  90. package/docs/types/RelationDefinition.html +1 -1
  91. package/docs/types/RelationDefinitionMap.html +1 -1
  92. package/docs/types/WhereClause.html +2 -2
  93. package/docs/types/WithoutId.html +2 -2
  94. package/mocha.setup.js +8 -3
  95. package/package.json +23 -19
  96. package/src/adapter/decorator/data-sanitizing-decorator.d.ts +2 -1
  97. package/src/adapter/decorator/data-validation-decorator.d.ts +2 -1
  98. package/src/adapter/decorator/data-validation-decorator.js +18 -10
  99. package/src/adapter/decorator/data-validation-decorator.spec.js +40 -0
  100. package/src/adapter/decorator/default-values-decorator.d.ts +2 -1
  101. package/src/adapter/decorator/fields-filtering-decorator.d.ts +2 -1
  102. package/src/adapter/decorator/inclusion-decorator.d.ts +2 -1
  103. package/src/definition/model/index.js +1 -0
  104. package/src/definition/model/model-data-validator.d.ts +4 -16
  105. package/src/definition/model/model-data-validator.js +92 -11
  106. package/src/definition/model/model-data-validator.spec.js +1140 -442
  107. package/src/definition/model/model-definition.js +1 -0
  108. package/src/definition/model/properties/index.d.ts +1 -0
  109. package/src/definition/model/properties/index.js +2 -0
  110. package/src/definition/model/properties/properties-definition-validator.js +47 -0
  111. package/src/definition/model/properties/properties-definition-validator.spec.js +50 -0
  112. package/src/definition/model/properties/property-definition.d.ts +2 -0
  113. package/src/definition/model/properties/property-definition.js +1 -0
  114. package/src/definition/model/properties/property-validator/index.d.ts +2 -0
  115. package/src/definition/model/properties/property-validator/index.js +2 -0
  116. package/src/definition/model/properties/property-validator/property-validator-registry.d.ts +29 -0
  117. package/src/definition/model/properties/property-validator/property-validator-registry.js +69 -0
  118. package/src/definition/model/properties/property-validator/property-validator-registry.spec.js +120 -0
  119. package/src/definition/model/properties/property-validator/property-validator.d.ts +30 -0
  120. package/src/definition/model/properties/property-validator/property-validator.js +1 -0
  121. package/src/definition/model/relations/index.js +1 -0
  122. package/src/definition/model/relations/relation-definition.js +1 -0
  123. package/src/filter/filter-clause.d.ts +4 -4
  124. package/src/index.d.ts +1 -1
  125. package/src/index.js +1 -0
  126. package/src/repository/repository.d.ts +9 -3
  127. package/src/repository/repository.js +0 -1
  128. package/src/schema.d.ts +10 -1
  129. package/src/schema.js +13 -0
  130. package/src/schema.spec.ts +115 -0
  131. package/src/types.d.ts +8 -0
  132. package/src/types.js +1 -0
  133. package/src/utils/get-decorator-target-type.d.ts +27 -0
  134. package/src/utils/get-decorator-target-type.js +63 -0
  135. package/src/utils/get-decorator-target-type.spec.js +77 -0
  136. package/src/utils/index.d.ts +1 -0
  137. package/src/utils/index.js +1 -0
  138. package/src/utils/is-ctor.d.ts +2 -1
  139. package/src/utils/is-ctor.js +2 -1
  140. package/src/utils/is-ctor.spec.js +1 -1
  141. package/tsconfig.json +3 -1
@@ -0,0 +1,115 @@
1
+ import {expect} from 'chai';
2
+ import {Schema} from './schema.js';
3
+ import {Repository} from './repository/index.js';
4
+ import {DefinitionRegistry} from './definition/index.js';
5
+ import {PropertyValidatorRegistry} from './definition/index.js';
6
+
7
+ describe('Schema', function () {
8
+ describe('defineDatasource', function () {
9
+ it('returns this', function () {
10
+ const schema = new Schema();
11
+ const res = schema.defineDatasource({
12
+ name: 'datasource',
13
+ adapter: 'memory',
14
+ });
15
+ expect(res).to.be.eq(schema);
16
+ });
17
+
18
+ it('sets the datasource definition', function () {
19
+ const schema = new Schema();
20
+ schema.defineDatasource({name: 'datasource', adapter: 'memory'});
21
+ const res =
22
+ schema.getService(DefinitionRegistry).getDatasource('datasource');
23
+ expect(res).to.be.eql({name: 'datasource', adapter: 'memory'});
24
+ });
25
+
26
+ it('throws an error if the datasource name already defined', function () {
27
+ const schema = new Schema();
28
+ schema.defineDatasource({name: 'datasource', adapter: 'memory'});
29
+ const throwable =
30
+ () => schema.defineDatasource({name: 'datasource', adapter: 'memory'});
31
+ expect(throwable).to.throw(
32
+ 'The datasource "datasource" is already defined.',
33
+ );
34
+ });
35
+ });
36
+
37
+ describe('defineModel', function () {
38
+ it('returns this', function () {
39
+ const schema = new Schema();
40
+ const res = schema.defineModel({name: 'model'});
41
+ expect(res).to.be.eq(schema);
42
+ });
43
+
44
+ it('sets the model definition', function () {
45
+ const schema = new Schema();
46
+ schema.defineModel({name: 'model'});
47
+ const res = schema.getService(DefinitionRegistry).getModel('model');
48
+ expect(res).to.be.eql({name: 'model'});
49
+ });
50
+
51
+ it('throws an error if the model name already defined', function () {
52
+ const schema = new Schema();
53
+ schema.defineModel({name: 'model'});
54
+ const throwable = () => schema.defineModel({name: 'model'});
55
+ expect(throwable).to.throw('The model "model" is already defined.');
56
+ });
57
+ });
58
+
59
+ describe('getRepository', function () {
60
+ it('returns a repository by the model name', function () {
61
+ const schema = new Schema();
62
+ schema.defineDatasource({name: 'datasource', adapter: 'memory'});
63
+ schema.defineModel({name: 'model', datasource: 'datasource'});
64
+ const res = schema.getRepository('model');
65
+ expect(res).to.be.instanceof(Repository);
66
+ });
67
+
68
+ it('throws an error if the model is not defined', function () {
69
+ const schema = new Schema();
70
+ const throwable = () => schema.getRepository('model');
71
+ expect(throwable).to.throw('The model "model" is not defined.');
72
+ });
73
+
74
+ it('uses generic types to define the repository type', function () {
75
+ const schema = new Schema();
76
+ schema.defineDatasource({name: 'datasource', adapter: 'memory'});
77
+ schema.defineModel({name: 'model', datasource: 'datasource'});
78
+ interface MyModel {
79
+ myId: number;
80
+ }
81
+ const res1: Repository = schema.getRepository('model');
82
+ const res2: Repository<MyModel, number, 'myId'> =
83
+ schema.getRepository<MyModel, number, 'myId'>('model');
84
+ expect(res1).to.be.eq(res2);
85
+ });
86
+ });
87
+
88
+ describe('addPropertyValidator', function () {
89
+ it('returns this', function () {
90
+ const schema = new Schema();
91
+ const res = schema.addPropertyValidator('validator', () => true);
92
+ expect(res).to.be.eq(schema);
93
+ });
94
+
95
+ it('adds a new property validator', function () {
96
+ const schema = new Schema();
97
+ const validator = () => true;
98
+ schema.addPropertyValidator('validator', validator);
99
+ const res = schema.getService(PropertyValidatorRegistry)
100
+ .getValidator('validator');
101
+ expect(res).to.be.eq(validator);
102
+ });
103
+
104
+ it('throws an error for already existing validator name', function () {
105
+ const schema = new Schema();
106
+ const validator = () => true;
107
+ schema.addPropertyValidator('validator', validator);
108
+ const throwable =
109
+ () => schema.addPropertyValidator('validator', validator);
110
+ expect(throwable).to.throw(
111
+ 'The property validator "validator" is already defined.',
112
+ );
113
+ });
114
+ });
115
+ });
package/src/types.d.ts CHANGED
@@ -28,3 +28,11 @@ export declare type ModelId = unknown;
28
28
  */
29
29
  type Identity<T> = T;
30
30
  export declare type Flatten<T> = Identity<{[k in keyof T]: T[k]}>;
31
+
32
+ /**
33
+ * A callable type with the "new" operator
34
+ * allows class and constructor.
35
+ */
36
+ export interface Constructor<T = unknown> {
37
+ new (...args: any[]): T;
38
+ }
package/src/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Decorator target type.
3
+ */
4
+ export declare enum DecoratorTargetType {
5
+ CONSTRUCTOR = 'constructor',
6
+ INSTANCE = 'instance',
7
+ STATIC_METHOD = 'staticMethod',
8
+ INSTANCE_METHOD = 'instanceMethod',
9
+ STATIC_PROPERTY = 'staticProperty',
10
+ INSTANCE_PROPERTY = 'instanceProperty',
11
+ CONSTRUCTOR_PARAMETER = 'constructorParameter',
12
+ STATIC_METHOD_PARAMETER = 'staticMethodParameter',
13
+ INSTANCE_METHOD_PARAMETER = 'instanceMethodParameter',
14
+ }
15
+
16
+ /**
17
+ * Get decorator target type.
18
+ *
19
+ * @param target
20
+ * @param propertyKey
21
+ * @param descriptorOrIndex
22
+ */
23
+ export declare function getDecoratorTargetType(
24
+ target: object,
25
+ propertyKey?: string,
26
+ descriptorOrIndex?: PropertyDescriptor | number,
27
+ ): DecoratorTargetType;
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @typedef {object} PropertyDescriptor
3
+ * @property {boolean|undefined} configurable
4
+ * @property {boolean|undefined} enumerable
5
+ * @property {*} value
6
+ * @property {boolean|undefined} writable
7
+ * @property {Function} get
8
+ * @property {Function} set
9
+ */
10
+
11
+ /**
12
+ * Decorator target type.
13
+ *
14
+ * @enum {string}
15
+ * @property {string} CONSTRUCTOR
16
+ * @property {string} INSTANCE
17
+ * @property {string} STATIC_METHOD
18
+ * @property {string} INSTANCE_METHOD
19
+ * @property {string} STATIC_PROPERTY
20
+ * @property {string} INSTANCE_PROPERTY
21
+ * @property {string} CONSTRUCTOR_PARAMETER
22
+ * @property {string} STATIC_METHOD_PARAMETER
23
+ * @property {string} INSTANCE_METHOD_PARAMETER
24
+ */
25
+ export const DecoratorTargetType = {
26
+ CONSTRUCTOR: 'constructor',
27
+ INSTANCE: 'instance',
28
+ STATIC_METHOD: 'staticMethod',
29
+ INSTANCE_METHOD: 'instanceMethod',
30
+ STATIC_PROPERTY: 'staticProperty',
31
+ INSTANCE_PROPERTY: 'instanceProperty',
32
+ CONSTRUCTOR_PARAMETER: 'constructorParameter',
33
+ STATIC_METHOD_PARAMETER: 'staticMethodParameter',
34
+ INSTANCE_METHOD_PARAMETER: 'instanceMethodParameter',
35
+ };
36
+
37
+ /**
38
+ * Get decorator target type.
39
+ *
40
+ * @param {object} target
41
+ * @param {string|undefined} propertyKey
42
+ * @param {PropertyDescriptor|number|undefined} descriptorOrIndex
43
+ * @returns {DecoratorTargetType}
44
+ */
45
+ export function getDecoratorTargetType(target, propertyKey, descriptorOrIndex) {
46
+ const isCtor = typeof target === 'function';
47
+ const isParameter = typeof descriptorOrIndex === 'number';
48
+ const isProperty = propertyKey != null && descriptorOrIndex == null;
49
+ const isMethod = propertyKey != null && descriptorOrIndex != null;
50
+ const D = DecoratorTargetType;
51
+ if (isCtor) {
52
+ if (isParameter)
53
+ return propertyKey ? D.STATIC_METHOD_PARAMETER : D.CONSTRUCTOR_PARAMETER;
54
+ if (isProperty) return D.STATIC_PROPERTY;
55
+ if (isMethod) return D.STATIC_METHOD;
56
+ return D.CONSTRUCTOR;
57
+ } else {
58
+ if (isParameter) return D.INSTANCE_METHOD_PARAMETER;
59
+ if (isProperty) return D.INSTANCE_PROPERTY;
60
+ if (isMethod) return D.INSTANCE_METHOD;
61
+ return D.INSTANCE;
62
+ }
63
+ }
@@ -0,0 +1,77 @@
1
+ import {expect} from 'chai';
2
+ import {getDecoratorTargetType} from './get-decorator-target-type.js';
3
+ import {DecoratorTargetType as DTT} from './get-decorator-target-type.js';
4
+
5
+ describe('getDecoratorTargetType', function () {
6
+ const validate = function (value) {
7
+ return function (target, propertyKey, descriptorOrIndex) {
8
+ const type = getDecoratorTargetType(
9
+ target,
10
+ propertyKey,
11
+ descriptorOrIndex,
12
+ );
13
+ expect(value).to.be.eq(type);
14
+ };
15
+ };
16
+
17
+ it('returns CONSTRUCTOR', function () {
18
+ class Target {}
19
+ validate(DTT.CONSTRUCTOR)(Target);
20
+ });
21
+
22
+ it('returns INSTANCE', function () {
23
+ class Target {}
24
+ validate(DTT.INSTANCE)(Target.prototype);
25
+ });
26
+
27
+ it('returns STATIC_METHOD', function () {
28
+ class Target {
29
+ static method() {}
30
+ }
31
+ const desc = Object.getOwnPropertyDescriptor(Target, 'method');
32
+ validate(DTT.STATIC_METHOD)(Target, 'method', desc);
33
+ });
34
+
35
+ it('returns INSTANCE_METHOD', function () {
36
+ class Target {
37
+ method() {}
38
+ }
39
+ const desc = Object.getOwnPropertyDescriptor(Target.prototype, 'method');
40
+ validate(DTT.INSTANCE_METHOD)(Target.prototype, 'method', desc);
41
+ });
42
+
43
+ it('returns STATIC_PROPERTY', function () {
44
+ class Target {
45
+ static prop;
46
+ }
47
+ validate(DTT.STATIC_PROPERTY)(Target, 'prop');
48
+ });
49
+
50
+ it('returns INSTANCE_PROPERTY', function () {
51
+ class Target {
52
+ prop;
53
+ }
54
+ validate(DTT.INSTANCE_PROPERTY)(Target.prototype, 'prop');
55
+ });
56
+
57
+ it('returns CONSTRUCTOR_PARAMETER', function () {
58
+ class Target {
59
+ constructor(param) {}
60
+ }
61
+ validate(DTT.CONSTRUCTOR_PARAMETER)(Target, undefined, 0);
62
+ });
63
+
64
+ it('returns STATIC_METHOD_PARAMETER', function () {
65
+ class Target {
66
+ static method(param) {}
67
+ }
68
+ validate(DTT.STATIC_METHOD_PARAMETER)(Target, 'method', 0);
69
+ });
70
+
71
+ it('returns INSTANCE_METHOD_PARAMETER', function () {
72
+ class Target {
73
+ method(param) {}
74
+ }
75
+ validate(DTT.INSTANCE_METHOD_PARAMETER)(Target.prototype, 'method', 0);
76
+ });
77
+ });
@@ -8,3 +8,4 @@ export * from './string-to-regexp.js';
8
8
  export * from './get-value-by-path.js';
9
9
  export * from './select-object-keys.js';
10
10
  export * from './exclude-object-keys.js';
11
+ export * from './get-decorator-target-type.js';
@@ -8,3 +8,4 @@ export * from './string-to-regexp.js';
8
8
  export * from './get-value-by-path.js';
9
9
  export * from './select-object-keys.js';
10
10
  export * from './exclude-object-keys.js';
11
+ export * from './get-decorator-target-type.js';
@@ -1,5 +1,6 @@
1
1
  /**
2
- * Is ctor.
2
+ * Returns true if the given value
3
+ * is a constructor function or a class.
3
4
  *
4
5
  * @param {*} value
5
6
  * @returns {boolean}
@@ -1,5 +1,6 @@
1
1
  /**
2
- * Is ctor.
2
+ * Returns true if the given value
3
+ * is a constructor function or a class.
3
4
  *
4
5
  * @param {*} value
5
6
  * @returns {boolean}
@@ -1,7 +1,7 @@
1
1
  import {expect} from 'chai';
2
2
  import {isCtor} from './is-ctor.js';
3
3
 
4
- describe('isConstructor', function () {
4
+ describe('isCtor', function () {
5
5
  it('returns true if a given value is a constructor', function () {
6
6
  expect(isCtor(Date)).to.be.true;
7
7
  expect(isCtor(Number)).to.be.true;
package/tsconfig.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "compilerOptions": {
3
+ "strict": true,
3
4
  "rootDir": "src",
4
5
  "noEmit": true,
5
6
  "target": "es2022",
6
7
  "module": "NodeNext",
7
- "moduleResolution": "NodeNext"
8
+ "moduleResolution": "NodeNext",
9
+ "strictNullChecks": true
8
10
  }
9
11
  }