@e22m4u/js-repository 0.5.0 → 0.5.1
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/README.md
CHANGED
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
npm install @e22m4u/js-repository
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
Опционально
|
|
36
|
+
Опционально устанавливается нужный адаптер.
|
|
37
37
|
|
|
38
38
|
| | описание |
|
|
39
39
|
|-----------|--------------------------------------------------------------------------------------------------------------------------------|
|
|
@@ -651,7 +651,8 @@ dbs.defineModel({
|
|
|
651
651
|
|
|
652
652
|
Трансформеры используются для модификации значения свойства перед проверкой
|
|
653
653
|
типа и передачей данных в базу. Трансформеры позволяют автоматически очищать
|
|
654
|
-
или приводить данные к нужному формату.
|
|
654
|
+
или приводить данные к нужному формату. [Пустые значения](#Пустые-значения)
|
|
655
|
+
не передаются в трансформеры, так как не имеют полезной нагрузки.
|
|
655
656
|
|
|
656
657
|
### Глобальные трансформеры
|
|
657
658
|
|
|
@@ -1136,31 +1137,101 @@ const res = await rep.find({
|
|
|
1136
1137
|
|
|
1137
1138
|
## Связи
|
|
1138
1139
|
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1140
|
+
Связи позволяют описывать отношения между моделями, что дает возможность
|
|
1141
|
+
автоматически встраивать связанные данные с помощью опции `include`
|
|
1142
|
+
в методах репозитория. Ниже приводится пример автоматического разрешения
|
|
1143
|
+
связи при использовании метода `findById`.
|
|
1142
1144
|
|
|
1143
|
-
|
|
1145
|
+
```js
|
|
1146
|
+
// запрос документа коллекции "users",
|
|
1147
|
+
// включая связанные данные (role и city)
|
|
1148
|
+
const user = await userRep.findById(1, {
|
|
1149
|
+
include: ['role', 'city'],
|
|
1150
|
+
});
|
|
1151
|
+
|
|
1152
|
+
console.log(user);
|
|
1153
|
+
// {
|
|
1154
|
+
// id: 1,
|
|
1155
|
+
// name: 'John Doe',
|
|
1156
|
+
// roleId: 3,
|
|
1157
|
+
// role: {
|
|
1158
|
+
// id: 3,
|
|
1159
|
+
// name: 'Manager'
|
|
1160
|
+
// },
|
|
1161
|
+
// cityId: 24,
|
|
1162
|
+
// city: {
|
|
1163
|
+
// id: 24,
|
|
1164
|
+
// name: 'Moscow'
|
|
1165
|
+
// }
|
|
1166
|
+
// }
|
|
1167
|
+
```
|
|
1168
|
+
|
|
1169
|
+
### Определение связи
|
|
1170
|
+
|
|
1171
|
+
Свойство `relations` в определении модели принимает объект, ключи которого
|
|
1172
|
+
являются названиями связей, а значения их параметрами. В дальнейшем название
|
|
1173
|
+
связи можно будет использовать в опции `include` методах репозитория.
|
|
1174
|
+
|
|
1175
|
+
```js
|
|
1176
|
+
import {DataType} from '@e22m4u/js-repository';
|
|
1177
|
+
import {RelationType} from '@e22m4u/js-repository';
|
|
1178
|
+
|
|
1179
|
+
dbs.defineModel({
|
|
1180
|
+
name: 'user',
|
|
1181
|
+
datasource: 'memory',
|
|
1182
|
+
properties: {
|
|
1183
|
+
name: DataType.STRING,
|
|
1184
|
+
},
|
|
1185
|
+
relations: {
|
|
1186
|
+
// связь role -> параметры
|
|
1187
|
+
role: {
|
|
1188
|
+
type: RelationType.BELONGS_TO,
|
|
1189
|
+
model: 'role',
|
|
1190
|
+
},
|
|
1191
|
+
// связь city -> параметры
|
|
1192
|
+
city: {
|
|
1193
|
+
type: RelationType.BELONGS_TO,
|
|
1194
|
+
model: 'city',
|
|
1195
|
+
},
|
|
1196
|
+
},
|
|
1197
|
+
});
|
|
1198
|
+
```
|
|
1199
|
+
|
|
1200
|
+
**Основные параметры**
|
|
1201
|
+
|
|
1202
|
+
- `type: string` тип связи (обязательно);
|
|
1203
|
+
- `model: string` название целевой модели (обязательно для некоторых типов);
|
|
1204
|
+
- `foreignKey: string` свойство текущей модели для идентификатора цели;
|
|
1144
1205
|
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1206
|
+
*i. Для типов Belongs To и References Many значение параметра `foreignKey`
|
|
1207
|
+
можно опустить, так как генерируется автоматически по названию связи.*
|
|
1208
|
+
|
|
1209
|
+
**Полиморфный режим**
|
|
1210
|
+
|
|
1211
|
+
- `polymorphic: boolean|string` объявление полиморфной связи;
|
|
1212
|
+
- `discriminator: string` свойство текущей модели для названия цели;
|
|
1150
1213
|
|
|
1151
1214
|
*i. Полиморфный режим позволяет динамически определять целевую модель
|
|
1152
1215
|
по ее названию, которое хранит документ в свойстве-дискриминаторе.*
|
|
1153
1216
|
|
|
1154
|
-
|
|
1217
|
+
**Типы связи**
|
|
1218
|
+
|
|
1219
|
+
- `belongsTo` - текущая модель ссылается на целевую по идентификатору;
|
|
1220
|
+
- `hasOne` - обратная сторона `belongsTo` по принципу "один к одному";
|
|
1221
|
+
- `hasMany` - обратная сторона `belongsTo` по принципу "один ко многим";
|
|
1222
|
+
- `referencesMany` - модель ссылается через массив идентификаторов;
|
|
1223
|
+
|
|
1224
|
+
Параметр `type` в определении связи принимает строку с названием типа. Чтобы исключить опечатку, рекомендуется использовать константы объекта `RelationType`
|
|
1225
|
+
указанные ниже.
|
|
1155
1226
|
|
|
1156
|
-
- `
|
|
1157
|
-
- `
|
|
1158
|
-
- `
|
|
1159
|
-
- `
|
|
1227
|
+
- `RelationType.BELONGS_TO`
|
|
1228
|
+
- `RelationType.HAS_ONE`
|
|
1229
|
+
- `RelationType.HAS_MANY`
|
|
1230
|
+
- `RelationType.REFERENCES_MANY`
|
|
1160
1231
|
|
|
1161
1232
|
**Примеры**
|
|
1162
1233
|
|
|
1163
|
-
Объявление связи `belongsTo
|
|
1234
|
+
Объявление связи `belongsTo`.
|
|
1164
1235
|
|
|
1165
1236
|
```js
|
|
1166
1237
|
dbs.defineModel({
|
|
@@ -1178,7 +1249,7 @@ dbs.defineModel({
|
|
|
1178
1249
|
});
|
|
1179
1250
|
```
|
|
1180
1251
|
|
|
1181
|
-
Объявление связи `hasMany
|
|
1252
|
+
Объявление связи `hasMany`.
|
|
1182
1253
|
|
|
1183
1254
|
```js
|
|
1184
1255
|
dbs.defineModel({
|
|
@@ -1193,7 +1264,7 @@ dbs.defineModel({
|
|
|
1193
1264
|
});
|
|
1194
1265
|
```
|
|
1195
1266
|
|
|
1196
|
-
Объявление связи `referencesMany
|
|
1267
|
+
Объявление связи `referencesMany`.
|
|
1197
1268
|
|
|
1198
1269
|
```js
|
|
1199
1270
|
dbs.defineModel({
|
|
@@ -1211,7 +1282,7 @@ dbs.defineModel({
|
|
|
1211
1282
|
});
|
|
1212
1283
|
```
|
|
1213
1284
|
|
|
1214
|
-
Полиморфная версия `belongsTo
|
|
1285
|
+
Полиморфная версия `belongsTo`.
|
|
1215
1286
|
|
|
1216
1287
|
```js
|
|
1217
1288
|
dbs.defineModel({
|
package/package.json
CHANGED
|
@@ -7,8 +7,8 @@ import {PropertyTransformOptions} from './property-transformer/index.js';
|
|
|
7
7
|
* Full property definition.
|
|
8
8
|
*/
|
|
9
9
|
export declare type FullPropertyDefinition = {
|
|
10
|
-
type: DataType
|
|
11
|
-
itemType?: DataType
|
|
10
|
+
type: `${DataType}`;
|
|
11
|
+
itemType?: `${DataType}`;
|
|
12
12
|
itemModel?: string;
|
|
13
13
|
model?: string;
|
|
14
14
|
primaryKey?: boolean;
|
|
@@ -18,7 +18,7 @@ export declare type FullPropertyDefinition = {
|
|
|
18
18
|
default?: unknown;
|
|
19
19
|
validate?: PropertyValidateOptions;
|
|
20
20
|
transform?: PropertyTransformOptions;
|
|
21
|
-
unique?: boolean | PropertyUniqueness
|
|
21
|
+
unique?: boolean | `${PropertyUniqueness}`;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
/**
|
|
@@ -35,7 +35,7 @@ export declare type RelationDefinition =
|
|
|
35
35
|
* @example Required options only.
|
|
36
36
|
* ```
|
|
37
37
|
* {
|
|
38
|
-
* type:
|
|
38
|
+
* type: 'belongsTo',
|
|
39
39
|
* model: 'model',
|
|
40
40
|
* }
|
|
41
41
|
* ```
|
|
@@ -43,14 +43,14 @@ export declare type RelationDefinition =
|
|
|
43
43
|
* @example Verbose definition.
|
|
44
44
|
* ```
|
|
45
45
|
* {
|
|
46
|
-
* type:
|
|
46
|
+
* type: 'belongsTo',
|
|
47
47
|
* model: 'model',
|
|
48
48
|
* foreignKey: 'modelId',
|
|
49
49
|
* }
|
|
50
50
|
* ```
|
|
51
51
|
*/
|
|
52
52
|
export declare type BelongsToDefinition = {
|
|
53
|
-
type: RelationType.BELONGS_TO
|
|
53
|
+
type: `${RelationType.BELONGS_TO}`;
|
|
54
54
|
polymorphic?: false;
|
|
55
55
|
model: string;
|
|
56
56
|
foreignKey?: string;
|
|
@@ -62,7 +62,7 @@ export declare type BelongsToDefinition = {
|
|
|
62
62
|
* @example Required fields only.
|
|
63
63
|
* ```
|
|
64
64
|
* {
|
|
65
|
-
* type:
|
|
65
|
+
* type: 'belongsTo',
|
|
66
66
|
* polymorphic: true,
|
|
67
67
|
* }
|
|
68
68
|
* ```
|
|
@@ -70,7 +70,7 @@ export declare type BelongsToDefinition = {
|
|
|
70
70
|
* @example Verbose definition.
|
|
71
71
|
* ```
|
|
72
72
|
* {
|
|
73
|
-
* type:
|
|
73
|
+
* type: 'belongsTo',
|
|
74
74
|
* polymorphic: true,
|
|
75
75
|
* foreignKey: 'referenceId',
|
|
76
76
|
* discriminator: 'referenceType',
|
|
@@ -78,7 +78,7 @@ export declare type BelongsToDefinition = {
|
|
|
78
78
|
* ```
|
|
79
79
|
*/
|
|
80
80
|
export declare type PolyBelongsToDefinition = {
|
|
81
|
-
type: RelationType.BELONGS_TO
|
|
81
|
+
type: `${RelationType.BELONGS_TO}`;
|
|
82
82
|
polymorphic: true;
|
|
83
83
|
foreignKey?: string;
|
|
84
84
|
discriminator?: string;
|
|
@@ -90,14 +90,14 @@ export declare type PolyBelongsToDefinition = {
|
|
|
90
90
|
* @example
|
|
91
91
|
* ```ts
|
|
92
92
|
* {
|
|
93
|
-
* type:
|
|
93
|
+
* type: 'hasOne',
|
|
94
94
|
* model: 'model',
|
|
95
95
|
* foreignKey: 'modelId',
|
|
96
96
|
* }
|
|
97
97
|
* ```
|
|
98
98
|
*/
|
|
99
99
|
export declare type HasOneDefinition = {
|
|
100
|
-
type: RelationType.HAS_ONE
|
|
100
|
+
type: `${RelationType.HAS_ONE}`;
|
|
101
101
|
model: string;
|
|
102
102
|
foreignKey: string;
|
|
103
103
|
polymorphic?: false;
|
|
@@ -110,14 +110,14 @@ export declare type HasOneDefinition = {
|
|
|
110
110
|
* @example
|
|
111
111
|
* ```ts
|
|
112
112
|
* {
|
|
113
|
-
* type:
|
|
113
|
+
* type: 'hasOne',
|
|
114
114
|
* model: 'model',
|
|
115
115
|
* polymorphic: 'reference',
|
|
116
116
|
* }
|
|
117
117
|
* ```
|
|
118
118
|
*/
|
|
119
119
|
export declare type PolyHasOneDefinitionWithTargetRelationName = {
|
|
120
|
-
type: RelationType.HAS_ONE
|
|
120
|
+
type: `${RelationType.HAS_ONE}`;
|
|
121
121
|
model: string;
|
|
122
122
|
polymorphic: string;
|
|
123
123
|
foreignKey?: undefined;
|
|
@@ -130,7 +130,7 @@ export declare type PolyHasOneDefinitionWithTargetRelationName = {
|
|
|
130
130
|
* @example
|
|
131
131
|
* ```
|
|
132
132
|
* {
|
|
133
|
-
* type:
|
|
133
|
+
* type: 'hasOne',
|
|
134
134
|
* model: 'model',
|
|
135
135
|
* polymorphic: true,
|
|
136
136
|
* foreignKey: 'referenceId',
|
|
@@ -139,7 +139,7 @@ export declare type PolyHasOneDefinitionWithTargetRelationName = {
|
|
|
139
139
|
* ```
|
|
140
140
|
*/
|
|
141
141
|
export declare type PolyHasOneDefinitionWithTargetKeys = {
|
|
142
|
-
type: RelationType.HAS_ONE
|
|
142
|
+
type: `${RelationType.HAS_ONE}`;
|
|
143
143
|
model: string;
|
|
144
144
|
polymorphic: true;
|
|
145
145
|
foreignKey: string;
|
|
@@ -152,14 +152,14 @@ export declare type PolyHasOneDefinitionWithTargetKeys = {
|
|
|
152
152
|
* @example
|
|
153
153
|
* ```ts
|
|
154
154
|
* {
|
|
155
|
-
* type:
|
|
155
|
+
* type: 'hasMany',
|
|
156
156
|
* model: 'model',
|
|
157
157
|
* foreignKey: 'modelId',
|
|
158
158
|
* }
|
|
159
159
|
* ```
|
|
160
160
|
*/
|
|
161
161
|
export declare type HasManyDefinition = {
|
|
162
|
-
type: RelationType.HAS_MANY
|
|
162
|
+
type: `${RelationType.HAS_MANY}`;
|
|
163
163
|
model: string;
|
|
164
164
|
foreignKey: string;
|
|
165
165
|
polymorphic?: false;
|
|
@@ -172,14 +172,14 @@ export declare type HasManyDefinition = {
|
|
|
172
172
|
* @example
|
|
173
173
|
* ```ts
|
|
174
174
|
* {
|
|
175
|
-
* type:
|
|
175
|
+
* type: 'hasMany',
|
|
176
176
|
* model: 'model',
|
|
177
177
|
* polymorphic: 'reference',
|
|
178
178
|
* }
|
|
179
179
|
* ```
|
|
180
180
|
*/
|
|
181
181
|
export declare type PolyHasManyDefinitionWithTargetRelationName = {
|
|
182
|
-
type: RelationType.HAS_MANY
|
|
182
|
+
type: `${RelationType.HAS_MANY}`;
|
|
183
183
|
model: string;
|
|
184
184
|
polymorphic: string;
|
|
185
185
|
foreignKey?: undefined;
|
|
@@ -192,7 +192,7 @@ export declare type PolyHasManyDefinitionWithTargetRelationName = {
|
|
|
192
192
|
* @example
|
|
193
193
|
* ```
|
|
194
194
|
* {
|
|
195
|
-
* type:
|
|
195
|
+
* type: 'hasMany',
|
|
196
196
|
* model: 'model',
|
|
197
197
|
* polymorphic: true,
|
|
198
198
|
* foreignKey: 'referenceId',
|
|
@@ -201,7 +201,7 @@ export declare type PolyHasManyDefinitionWithTargetRelationName = {
|
|
|
201
201
|
* ```
|
|
202
202
|
*/
|
|
203
203
|
export declare type PolyHasManyDefinitionWithTargetKeys = {
|
|
204
|
-
type: RelationType.HAS_MANY
|
|
204
|
+
type: `${RelationType.HAS_MANY}`;
|
|
205
205
|
model: string;
|
|
206
206
|
polymorphic: true;
|
|
207
207
|
foreignKey: string;
|
|
@@ -214,7 +214,7 @@ export declare type PolyHasManyDefinitionWithTargetKeys = {
|
|
|
214
214
|
* @example Required options only.
|
|
215
215
|
* ```
|
|
216
216
|
* {
|
|
217
|
-
* type:
|
|
217
|
+
* type: 'referencesMany',
|
|
218
218
|
* model: 'model',
|
|
219
219
|
* }
|
|
220
220
|
* ```
|
|
@@ -222,14 +222,14 @@ export declare type PolyHasManyDefinitionWithTargetKeys = {
|
|
|
222
222
|
* @example Verbose definition.
|
|
223
223
|
* ```
|
|
224
224
|
* {
|
|
225
|
-
* type:
|
|
225
|
+
* type: 'referencesMany',
|
|
226
226
|
* model: 'model',
|
|
227
227
|
* foreignKey: 'modelIds',
|
|
228
228
|
* }
|
|
229
229
|
* ```
|
|
230
230
|
*/
|
|
231
231
|
export declare type ReferencesManyDefinition = {
|
|
232
|
-
type: RelationType.REFERENCES_MANY
|
|
232
|
+
type: `${RelationType.REFERENCES_MANY}`;
|
|
233
233
|
model: string;
|
|
234
234
|
foreignKey?: string;
|
|
235
235
|
discriminator?: undefined;
|