@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.
- package/.mocharc.json +2 -1
- package/README.md +175 -135
- package/dist/cjs/index.cjs +86 -1
- package/eslint.config.js +2 -1
- package/mocha.setup.js +6 -0
- package/package.json +21 -15
- package/src/adapter/adapter-loader.d.ts +16 -0
- package/src/adapter/adapter-registry.d.ts +14 -0
- package/src/adapter/adapter.d.ts +153 -0
- package/src/adapter/adapter.spec.js +2 -2
- package/src/adapter/builtin/memory-adapter.d.ts +148 -0
- package/src/adapter/builtin/memory-adapter.js +1 -1
- package/src/adapter/decorator/data-sanitizing-decorator.d.ts +14 -0
- package/src/adapter/decorator/data-sanitizing-decorator.spec.js +7 -7
- package/src/adapter/decorator/default-values-decorator.d.ts +14 -0
- package/src/adapter/decorator/default-values-decorator.spec.js +2 -2
- package/src/adapter/decorator/fields-filtering-decorator.d.ts +14 -0
- package/src/adapter/decorator/fields-filtering-decorator.spec.js +7 -27
- package/src/adapter/decorator/inclusion-decorator.d.ts +14 -0
- package/src/adapter/decorator/inclusion-decorator.spec.js +2 -2
- package/src/adapter/decorator/index.d.ts +5 -0
- package/src/adapter/decorator/property-uniqueness-decorator.d.ts +14 -0
- package/src/adapter/decorator/property-uniqueness-decorator.spec.js +2 -2
- package/src/adapter/index.d.ts +3 -0
- package/src/database-schema.d.ts +37 -0
- package/src/definition/datasource/datasource-definition-validator.d.ts +14 -0
- package/src/definition/datasource/datasource-definition.d.ts +8 -0
- package/src/definition/datasource/index.d.ts +2 -0
- package/src/definition/definition-registry.d.ts +50 -0
- package/src/definition/definition-registry.spec.js +4 -4
- package/src/definition/index.d.ts +3 -0
- package/src/definition/model/index.d.ts +6 -0
- package/src/definition/model/index.js +1 -0
- package/src/definition/model/model-data-sanitizer.d.ts +15 -0
- package/src/definition/model/model-definition-utils.d.ts +180 -0
- package/src/definition/model/model-definition-utils.spec.js +2 -2
- package/src/definition/model/model-definition-validator.d.ts +14 -0
- package/src/definition/model/model-definition-validator.spec.js +4 -4
- package/src/definition/model/model-definition.d.ts +28 -0
- package/src/definition/model/model-definition.js +1 -0
- package/src/definition/model/properties/data-type.d.ts +16 -0
- package/src/definition/model/properties/index.d.ts +6 -0
- package/src/definition/model/properties/index.js +1 -0
- package/src/definition/model/properties/primary-keys-definition-validator.d.ts +15 -0
- package/src/definition/model/properties/properties-definition-validator.d.ts +15 -0
- package/src/definition/model/properties/properties-definition-validator.spec.js +3 -3
- package/src/definition/model/properties/property-definition.d.ts +23 -0
- package/src/definition/model/properties/property-definition.js +1 -0
- package/src/definition/model/properties/property-uniqueness-validator.d.ts +31 -0
- package/src/definition/model/properties/property-uniqueness.d.ts +14 -0
- package/src/definition/model/relations/index.d.ts +3 -0
- package/src/definition/model/relations/index.js +1 -0
- package/src/definition/model/relations/relation-definition.d.ts +236 -0
- package/src/definition/model/relations/relation-definition.js +1 -0
- package/src/definition/model/relations/relation-type.d.ts +14 -0
- package/src/definition/model/relations/relations-definition-validator.d.ts +15 -0
- package/src/errors/index.d.ts +3 -0
- package/src/errors/invalid-argument-error.d.ts +6 -0
- package/src/errors/invalid-operator-value-error.d.ts +13 -0
- package/src/errors/not-implemented-error.d.ts +6 -0
- package/src/filter/fields-clause-tool.d.ts +38 -0
- package/src/filter/filter-clause.d.ts +348 -0
- package/src/filter/include-clause-tool.d.ts +55 -0
- package/src/filter/index.d.ts +7 -0
- package/src/filter/operator-clause-tool.d.ts +224 -0
- package/src/filter/order-clause-tool.d.ts +32 -0
- package/src/filter/slice-clause-tool.d.ts +30 -0
- package/src/filter/where-clause-tool.d.ts +23 -0
- package/src/index.d.ts +9 -0
- package/src/index.js +1 -0
- package/src/relations/belongs-to-resolver.d.ts +46 -0
- package/src/relations/has-many-resolver.d.ts +67 -0
- package/src/relations/has-one-resolver.d.ts +67 -0
- package/src/relations/index.d.ts +4 -0
- package/src/relations/references-many-resolver.d.ts +27 -0
- package/src/repository/index.d.ts +2 -0
- package/src/repository/repository-registry.d.ts +29 -0
- package/src/repository/repository.d.ts +183 -0
- package/src/types.d.ts +43 -0
- package/src/types.js +1 -0
- package/src/utils/capitalize.d.ts +6 -0
- package/src/utils/clone-deep.d.ts +6 -0
- package/src/utils/exclude-object-keys.d.ts +10 -0
- package/src/utils/get-value-by-path.d.ts +12 -0
- package/src/utils/index.d.ts +12 -0
- package/src/utils/is-deep-equal.d.ts +10 -0
- package/src/utils/is-plain-object.d.ts +6 -0
- package/src/utils/is-promise.d.ts +10 -0
- package/src/utils/like-to-regexp.d.ts +14 -0
- package/src/utils/model-name-to-model-key.d.ts +6 -0
- package/src/utils/select-object-keys.d.ts +10 -0
- package/src/utils/singularize.d.ts +6 -0
- package/src/utils/string-to-regexp.d.ts +10 -0
- package/tsconfig.json +14 -0
- package/jsconfig.json +0 -7
- 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,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
|
+
* 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;
|
package/tsconfig.json
ADDED
package/jsconfig.json
DELETED
package/src/chai.js
DELETED