@e22m4u/js-repository 0.8.0 → 0.8.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.
Files changed (96) hide show
  1. package/.mocharc.json +2 -1
  2. package/README.md +175 -135
  3. package/dist/cjs/index.cjs +86 -1
  4. package/eslint.config.js +2 -1
  5. package/mocha.setup.js +6 -0
  6. package/package.json +21 -15
  7. package/src/adapter/adapter-loader.d.ts +16 -0
  8. package/src/adapter/adapter-registry.d.ts +14 -0
  9. package/src/adapter/adapter.d.ts +153 -0
  10. package/src/adapter/adapter.spec.js +2 -2
  11. package/src/adapter/builtin/memory-adapter.d.ts +148 -0
  12. package/src/adapter/builtin/memory-adapter.js +1 -1
  13. package/src/adapter/decorator/data-sanitizing-decorator.d.ts +14 -0
  14. package/src/adapter/decorator/data-sanitizing-decorator.spec.js +7 -7
  15. package/src/adapter/decorator/default-values-decorator.d.ts +14 -0
  16. package/src/adapter/decorator/default-values-decorator.spec.js +2 -2
  17. package/src/adapter/decorator/fields-filtering-decorator.d.ts +14 -0
  18. package/src/adapter/decorator/fields-filtering-decorator.spec.js +7 -27
  19. package/src/adapter/decorator/inclusion-decorator.d.ts +14 -0
  20. package/src/adapter/decorator/inclusion-decorator.spec.js +2 -2
  21. package/src/adapter/decorator/index.d.ts +5 -0
  22. package/src/adapter/decorator/property-uniqueness-decorator.d.ts +14 -0
  23. package/src/adapter/decorator/property-uniqueness-decorator.spec.js +2 -2
  24. package/src/adapter/index.d.ts +3 -0
  25. package/src/database-schema.d.ts +37 -0
  26. package/src/definition/datasource/datasource-definition-validator.d.ts +14 -0
  27. package/src/definition/datasource/datasource-definition.d.ts +8 -0
  28. package/src/definition/datasource/index.d.ts +2 -0
  29. package/src/definition/definition-registry.d.ts +50 -0
  30. package/src/definition/definition-registry.spec.js +4 -4
  31. package/src/definition/index.d.ts +3 -0
  32. package/src/definition/model/index.d.ts +6 -0
  33. package/src/definition/model/index.js +1 -0
  34. package/src/definition/model/model-data-sanitizer.d.ts +15 -0
  35. package/src/definition/model/model-definition-utils.d.ts +180 -0
  36. package/src/definition/model/model-definition-utils.spec.js +2 -2
  37. package/src/definition/model/model-definition-validator.d.ts +14 -0
  38. package/src/definition/model/model-definition-validator.spec.js +4 -4
  39. package/src/definition/model/model-definition.d.ts +28 -0
  40. package/src/definition/model/model-definition.js +1 -0
  41. package/src/definition/model/properties/data-type.d.ts +16 -0
  42. package/src/definition/model/properties/index.d.ts +6 -0
  43. package/src/definition/model/properties/index.js +1 -0
  44. package/src/definition/model/properties/primary-keys-definition-validator.d.ts +15 -0
  45. package/src/definition/model/properties/properties-definition-validator.d.ts +15 -0
  46. package/src/definition/model/properties/properties-definition-validator.spec.js +3 -3
  47. package/src/definition/model/properties/property-definition.d.ts +23 -0
  48. package/src/definition/model/properties/property-definition.js +1 -0
  49. package/src/definition/model/properties/property-uniqueness-validator.d.ts +31 -0
  50. package/src/definition/model/properties/property-uniqueness.d.ts +14 -0
  51. package/src/definition/model/relations/index.d.ts +3 -0
  52. package/src/definition/model/relations/index.js +1 -0
  53. package/src/definition/model/relations/relation-definition.d.ts +236 -0
  54. package/src/definition/model/relations/relation-definition.js +1 -0
  55. package/src/definition/model/relations/relation-type.d.ts +14 -0
  56. package/src/definition/model/relations/relations-definition-validator.d.ts +15 -0
  57. package/src/errors/index.d.ts +3 -0
  58. package/src/errors/invalid-argument-error.d.ts +6 -0
  59. package/src/errors/invalid-operator-value-error.d.ts +13 -0
  60. package/src/errors/not-implemented-error.d.ts +6 -0
  61. package/src/filter/fields-clause-tool.d.ts +38 -0
  62. package/src/filter/filter-clause.d.ts +348 -0
  63. package/src/filter/include-clause-tool.d.ts +55 -0
  64. package/src/filter/index.d.ts +7 -0
  65. package/src/filter/operator-clause-tool.d.ts +224 -0
  66. package/src/filter/order-clause-tool.d.ts +32 -0
  67. package/src/filter/slice-clause-tool.d.ts +30 -0
  68. package/src/filter/where-clause-tool.d.ts +23 -0
  69. package/src/index.d.ts +9 -0
  70. package/src/index.js +1 -0
  71. package/src/relations/belongs-to-resolver.d.ts +46 -0
  72. package/src/relations/has-many-resolver.d.ts +67 -0
  73. package/src/relations/has-one-resolver.d.ts +67 -0
  74. package/src/relations/index.d.ts +4 -0
  75. package/src/relations/references-many-resolver.d.ts +27 -0
  76. package/src/repository/index.d.ts +2 -0
  77. package/src/repository/repository-registry.d.ts +29 -0
  78. package/src/repository/repository.d.ts +183 -0
  79. package/src/types.d.ts +43 -0
  80. package/src/types.js +1 -0
  81. package/src/utils/capitalize.d.ts +6 -0
  82. package/src/utils/clone-deep.d.ts +6 -0
  83. package/src/utils/exclude-object-keys.d.ts +10 -0
  84. package/src/utils/get-value-by-path.d.ts +12 -0
  85. package/src/utils/index.d.ts +12 -0
  86. package/src/utils/is-deep-equal.d.ts +10 -0
  87. package/src/utils/is-plain-object.d.ts +6 -0
  88. package/src/utils/is-promise.d.ts +10 -0
  89. package/src/utils/like-to-regexp.d.ts +14 -0
  90. package/src/utils/model-name-to-model-key.d.ts +6 -0
  91. package/src/utils/select-object-keys.d.ts +10 -0
  92. package/src/utils/singularize.d.ts +6 -0
  93. package/src/utils/string-to-regexp.d.ts +10 -0
  94. package/tsconfig.json +14 -0
  95. package/jsconfig.json +0 -7
  96. package/src/chai.js +0 -9
@@ -0,0 +1,183 @@
1
+ import {ModelId} from '../types.js';
2
+ import {Flatten} from '../types.js';
3
+ import {ModelData} from '../types.js';
4
+ import {PartialBy} from '../types.js';
5
+ import {Service} from '@e22m4u/js-service';
6
+ import {Adapter} from '../adapter/index.js';
7
+ import {WhereClause} from '../filter/index.js';
8
+ import {FilterClause} from '../filter/index.js';
9
+ import {ItemFilterClause} from '../filter/index.js';
10
+ import {ServiceContainer} from '@e22m4u/js-service';
11
+ import {DEFAULT_PRIMARY_KEY_PROPERTY_NAME} from '../definition/index.js';
12
+
13
+ /**
14
+ * Repository.
15
+ */
16
+ export declare class Repository<
17
+ Data extends object = ModelData,
18
+ IdType extends ModelId = ModelId,
19
+ IdName extends string = typeof DEFAULT_PRIMARY_KEY_PROPERTY_NAME,
20
+ FlatData extends object = Flatten<Data>,
21
+ > extends Service {
22
+ // it fixes unused generic bug
23
+ private _Data?: Data;
24
+ private _IdType?: IdType;
25
+ private _IdName?: IdName;
26
+ private _FlatData?: FlatData;
27
+
28
+ /**
29
+ * Model name.
30
+ */
31
+ get modelName(): string;
32
+
33
+ /**
34
+ * Datasource name.
35
+ */
36
+ get datasourceName(): string;
37
+
38
+ /**
39
+ * Constructor.
40
+ *
41
+ * @param container
42
+ * @param modelName
43
+ */
44
+ constructor(container: ServiceContainer, modelName: string);
45
+
46
+ /**
47
+ * Get adapter.
48
+ */
49
+ getAdapter(): Promise<Adapter>;
50
+
51
+ /**
52
+ * Create.
53
+ *
54
+ * @param data
55
+ * @param filter
56
+ */
57
+ create(
58
+ data: WithOptionalId<FlatData, IdName>,
59
+ filter?: ItemFilterClause<FlatData>,
60
+ ): Promise<FlatData>;
61
+
62
+ /**
63
+ * Replace by id.
64
+ *
65
+ * @param id
66
+ * @param data
67
+ * @param filter
68
+ */
69
+ replaceById(
70
+ id: IdType,
71
+ data: WithoutId<FlatData, IdName>,
72
+ filter?: ItemFilterClause<FlatData>,
73
+ ): Promise<FlatData>;
74
+
75
+ /**
76
+ * Replace or create.
77
+ *
78
+ * @param data
79
+ * @param filter
80
+ */
81
+ replaceOrCreate(
82
+ data: WithOptionalId<FlatData, IdName>,
83
+ filter?: ItemFilterClause<FlatData>,
84
+ ): Promise<FlatData>;
85
+
86
+ /**
87
+ * Patch.
88
+ *
89
+ * @param data
90
+ * @param where
91
+ */
92
+ patch(
93
+ data: PartialWithoutId<FlatData, IdName>,
94
+ where?: WhereClause<FlatData>,
95
+ ): Promise<number>;
96
+
97
+ /**
98
+ * Patch by id.
99
+ *
100
+ * @param id
101
+ * @param data
102
+ * @param filter
103
+ */
104
+ patchById(
105
+ id: IdType,
106
+ data: PartialWithoutId<FlatData, IdName>,
107
+ filter?: ItemFilterClause<FlatData>,
108
+ ): Promise<FlatData>;
109
+
110
+ /**
111
+ * Find.
112
+ *
113
+ * @param filter
114
+ */
115
+ find(filter?: FilterClause<FlatData>): Promise<FlatData[]>;
116
+
117
+ /**
118
+ * Find one.
119
+ *
120
+ * @param filter
121
+ */
122
+ findOne(filter?: FilterClause<FlatData>): Promise<FlatData | undefined>;
123
+
124
+ /**
125
+ * Find by id.
126
+ *
127
+ * @param id
128
+ * @param filter
129
+ */
130
+ findById(id: IdType, filter?: ItemFilterClause<FlatData>): Promise<FlatData>;
131
+
132
+ /**
133
+ * Delete.
134
+ *
135
+ * @param where
136
+ */
137
+ delete(where?: WhereClause<FlatData>): Promise<number>;
138
+
139
+ /**
140
+ * Delete by id.
141
+ *
142
+ * @param id
143
+ */
144
+ deleteById(id: IdType): Promise<boolean>;
145
+
146
+ /**
147
+ * Exists.
148
+ *
149
+ * @param id
150
+ */
151
+ exists(id: IdType): Promise<boolean>;
152
+
153
+ /**
154
+ * Count.
155
+ *
156
+ * @param where
157
+ */
158
+ count(where?: WhereClause<FlatData>): Promise<number>;
159
+ }
160
+
161
+ /**
162
+ * Removes id field.
163
+ */
164
+ export declare type WithoutId<
165
+ Data extends object,
166
+ IdName extends string = 'id',
167
+ > = Flatten<Omit<Data, IdName>>;
168
+
169
+ /**
170
+ * Makes fields as optional and remove id field.
171
+ */
172
+ export declare type PartialWithoutId<
173
+ Data extends object,
174
+ IdName extends string = 'id',
175
+ > = Flatten<Partial<Omit<Data, IdName>>>;
176
+
177
+ /**
178
+ * Makes the required id field as optional.
179
+ */
180
+ export declare type WithOptionalId<
181
+ Data extends object,
182
+ IdName extends string = 'id',
183
+ > = Flatten<Data extends {[K in IdName]: any} ? PartialBy<Data, IdName> : Data>;
package/src/types.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Free-form object with open properties.
3
+ */
4
+ export declare type AnyObject = {
5
+ [property: string]: unknown;
6
+ };
7
+
8
+ /**
9
+ * Makes specific field as optional.
10
+ */
11
+ export declare type PartialBy<T, K extends keyof T> = Omit<T, K> &
12
+ Partial<Pick<T, K>>;
13
+
14
+ /**
15
+ * Model data.
16
+ */
17
+ export declare type ModelData = Record<string, unknown>;
18
+
19
+ /**
20
+ * Model id.
21
+ */
22
+ export declare type ModelId = unknown;
23
+
24
+ /**
25
+ * Flatten.
26
+ */
27
+ type Identity<T> = T;
28
+ export declare type Flatten<T> = Identity<{[k in keyof T]: T[k]}>;
29
+
30
+ /**
31
+ * A callable type with the "new" operator
32
+ * allows class and constructor.
33
+ */
34
+ export interface Constructor<T = unknown> {
35
+ new (...args: any[]): T;
36
+ }
37
+
38
+ /**
39
+ * Representing a value or promise. This type is used
40
+ * to represent results of synchronous/asynchronous
41
+ * resolution of values.
42
+ */
43
+ export type ValueOrPromise<T> = T | PromiseLike<T>;
package/src/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Capitalize.
3
+ *
4
+ * @param string
5
+ */
6
+ export declare function capitalize(string: string): string;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Clone deep.
3
+ *
4
+ * @param value
5
+ */
6
+ export declare function cloneDeep<T>(value: T): T;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Exclude object keys.
3
+ *
4
+ * @param obj
5
+ * @param keys
6
+ */
7
+ export declare function excludeObjectKeys<T extends object>(
8
+ obj: T,
9
+ keys: string | string[],
10
+ ): Partial<T>;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Get value by path.
3
+ *
4
+ * @param obj
5
+ * @param path
6
+ * @param orElse
7
+ */
8
+ export declare function getValueByPath(
9
+ obj: object,
10
+ path: string,
11
+ orElse?: unknown,
12
+ ): unknown;
@@ -0,0 +1,12 @@
1
+ export * from './is-promise.js';
2
+ export * from './capitalize.js';
3
+ export * from './clone-deep.js';
4
+ export * from './singularize.js';
5
+ export * from './is-deep-equal.js';
6
+ export * from './like-to-regexp.js';
7
+ export * from './is-plain-object.js';
8
+ export * from './string-to-regexp.js';
9
+ export * from './get-value-by-path.js';
10
+ export * from './select-object-keys.js';
11
+ export * from './exclude-object-keys.js';
12
+ export * from './model-name-to-model-key.js';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Is deep equal.
3
+ *
4
+ * @param firstValue
5
+ * @param secondValue
6
+ */
7
+ export declare function isDeepEqual(
8
+ firstValue: unknown,
9
+ secondValue: unknown,
10
+ ): boolean;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Is plain object.
3
+ *
4
+ * @param value
5
+ */
6
+ export declare function isPlainObject(value: unknown): boolean;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Check whether a value is a Promise-like
3
+ * instance. Recognizes both native promises
4
+ * and third-party promise libraries.
5
+ *
6
+ * @param value
7
+ */
8
+ export declare function isPromise<T>(
9
+ value: T | PromiseLike<T> | undefined
10
+ ): value is PromiseLike<T>;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Преобразует SQL LIKE-шаблон в объект RegExp.
3
+ *
4
+ * Экранирует специальные символы регулярных выражений,
5
+ * чтобы они обрабатывались как обычные символы, и преобразует
6
+ * SQL wildcards (% и _) в их эквиваленты в регулярных выражениях.
7
+ *
8
+ * @param pattern
9
+ * @param isCaseInsensitive
10
+ */
11
+ export function likeToRegexp(
12
+ pattern: string,
13
+ isCaseInsensitive?: boolean,
14
+ ): RegExp;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Model name to model key.
3
+ *
4
+ * @param modelName
5
+ */
6
+ export function modelNameToModelKey(modelName: string): string;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Select object keys.
3
+ *
4
+ * @param obj
5
+ * @param keys
6
+ */
7
+ export declare function selectObjectKeys<T extends object>(
8
+ obj: T,
9
+ keys: string[],
10
+ ): Partial<T>;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Singularize.
3
+ *
4
+ * @param noun
5
+ */
6
+ export declare function singularize(noun: string): string;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * String to regexp.
3
+ *
4
+ * @param pattern
5
+ * @param flags
6
+ */
7
+ export declare function stringToRegexp(
8
+ pattern: string | RegExp,
9
+ flags?: string,
10
+ ): RegExp;
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "strict": true,
4
+ "target": "es2022",
5
+ "module": "NodeNext",
6
+ "moduleResolution": "NodeNext",
7
+ "noEmit": true,
8
+ "allowJs": true
9
+ },
10
+ "include": [
11
+ "./src/**/*.ts",
12
+ "./src/**/*.js"
13
+ ]
14
+ }
package/jsconfig.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es2022",
4
- "module": "NodeNext",
5
- "moduleResolution": "NodeNext"
6
- }
7
- }
package/src/chai.js DELETED
@@ -1,9 +0,0 @@
1
- import * as chaiModule from 'chai';
2
- import chaiSpies from 'chai-spies';
3
- import chaiAsPromised from 'chai-as-promised';
4
- const chai = {...chaiModule};
5
-
6
- chaiSpies(chai, chai.util);
7
- chaiAsPromised(chai, chai.util);
8
-
9
- export {chai};