@e22m4u/js-repository 0.2.7 → 0.3.0
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 +79 -80
- package/dist/cjs/index.cjs +2472 -2472
- package/eslint.config.js +1 -1
- package/package.json +12 -12
- package/src/adapter/adapter-registry.spec.js +7 -7
- package/src/adapter/adapter.spec.js +11 -11
- package/src/adapter/builtin/memory-adapter.spec.js +537 -537
- package/src/adapter/decorator/data-sanitizing-decorator.spec.js +5 -5
- package/src/adapter/decorator/data-transformation-decorator.spec.js +5 -5
- package/src/adapter/decorator/data-validation-decorator.spec.js +5 -5
- package/src/adapter/decorator/default-values-decorator.spec.js +5 -5
- package/src/adapter/decorator/fields-filtering-decorator.spec.js +5 -5
- package/src/adapter/decorator/inclusion-decorator.spec.js +5 -5
- package/src/adapter/decorator/property-uniqueness-decorator.spec.js +5 -5
- package/src/{schema.d.ts → database-schema.d.ts} +2 -2
- package/src/{schema.js → database-schema.js} +2 -2
- package/src/database-schema.spec.ts +86 -0
- package/src/definition/model/model-data-transformer.spec.js +93 -93
- package/src/definition/model/model-data-validator.spec.js +514 -528
- package/src/definition/model/model-definition-utils.spec.js +343 -341
- package/src/definition/model/properties/property-transformer/property-transformer-registry.spec.js +36 -36
- package/src/definition/model/properties/property-uniqueness-validator.spec.js +410 -377
- package/src/definition/model/properties/property-validator/property-validator-registry.spec.js +36 -36
- package/src/filter/fields-clause-tool.spec.js +4 -4
- package/src/index.d.ts +1 -1
- package/src/index.js +1 -1
- package/src/relations/belongs-to-resolver.spec.js +166 -166
- package/src/relations/has-many-resolver.spec.js +281 -281
- package/src/relations/has-one-resolver.spec.js +281 -281
- package/src/relations/references-many-resolver.spec.js +92 -92
- package/src/repository/repository-registry.spec.js +10 -10
- package/src/repository/repository.spec.js +73 -73
- package/src/utils/is-promise.spec.js +1 -2
- package/src/utils/transform-promise.spec.js +0 -1
- package/src/schema.spec.ts +0 -86
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import {expect} from 'chai';
|
|
2
|
-
import {Schema} from '../schema.js';
|
|
3
2
|
import {format} from '@e22m4u/js-format';
|
|
4
3
|
import {DataType} from '../definition/index.js';
|
|
5
4
|
import {RelationType} from '../definition/index.js';
|
|
5
|
+
import {DatabaseSchema} from '../database-schema.js';
|
|
6
6
|
import {BelongsToResolver} from './belongs-to-resolver.js';
|
|
7
7
|
import {DEFAULT_PRIMARY_KEY_PROPERTY_NAME as DEF_PK} from '../definition/index.js';
|
|
8
8
|
|
|
9
9
|
describe('BelongsToResolver', function () {
|
|
10
10
|
describe('includeTo', function () {
|
|
11
11
|
it('requires the "entities" parameter to be an array', async function () {
|
|
12
|
-
const
|
|
13
|
-
const R =
|
|
12
|
+
const dbs = new DatabaseSchema();
|
|
13
|
+
const R = dbs.getService(BelongsToResolver);
|
|
14
14
|
const error = v =>
|
|
15
15
|
format(
|
|
16
16
|
'The parameter "entities" of BelongsToResolver.includeTo requires ' +
|
|
@@ -30,8 +30,8 @@ describe('BelongsToResolver', function () {
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
it('requires elements of the "entities" parameter to be an Object', async function () {
|
|
33
|
-
const
|
|
34
|
-
const R =
|
|
33
|
+
const dbs = new DatabaseSchema();
|
|
34
|
+
const R = dbs.getService(BelongsToResolver);
|
|
35
35
|
const error = v =>
|
|
36
36
|
format(
|
|
37
37
|
'The parameter "entities" of BelongsToResolver.includeTo requires ' +
|
|
@@ -51,8 +51,8 @@ describe('BelongsToResolver', function () {
|
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
it('requires the "sourceName" parameter to be a non-empty string', async function () {
|
|
54
|
-
const
|
|
55
|
-
const R =
|
|
54
|
+
const dbs = new DatabaseSchema();
|
|
55
|
+
const R = dbs.getService(BelongsToResolver);
|
|
56
56
|
const error = v =>
|
|
57
57
|
format(
|
|
58
58
|
'The parameter "sourceName" of BelongsToResolver.includeTo requires ' +
|
|
@@ -71,8 +71,8 @@ describe('BelongsToResolver', function () {
|
|
|
71
71
|
});
|
|
72
72
|
|
|
73
73
|
it('requires the "targetName" parameter to be a non-empty string', async function () {
|
|
74
|
-
const
|
|
75
|
-
const R =
|
|
74
|
+
const dbs = new DatabaseSchema();
|
|
75
|
+
const R = dbs.getService(BelongsToResolver);
|
|
76
76
|
const error = v =>
|
|
77
77
|
format(
|
|
78
78
|
'The parameter "targetName" of BelongsToResolver.includeTo requires ' +
|
|
@@ -91,8 +91,8 @@ describe('BelongsToResolver', function () {
|
|
|
91
91
|
});
|
|
92
92
|
|
|
93
93
|
it('requires the "relationName" parameter to be a non-empty string', async function () {
|
|
94
|
-
const
|
|
95
|
-
const R =
|
|
94
|
+
const dbs = new DatabaseSchema();
|
|
95
|
+
const R = dbs.getService(BelongsToResolver);
|
|
96
96
|
const error = v =>
|
|
97
97
|
format(
|
|
98
98
|
'The parameter "relationName" of BelongsToResolver.includeTo requires ' +
|
|
@@ -111,8 +111,8 @@ describe('BelongsToResolver', function () {
|
|
|
111
111
|
});
|
|
112
112
|
|
|
113
113
|
it('requires the provided parameter "foreignKey" to be a string', async function () {
|
|
114
|
-
const
|
|
115
|
-
const R =
|
|
114
|
+
const dbs = new DatabaseSchema();
|
|
115
|
+
const R = dbs.getService(BelongsToResolver);
|
|
116
116
|
const error = v =>
|
|
117
117
|
format(
|
|
118
118
|
'The provided parameter "foreignKey" of BelongsToResolver.includeTo ' +
|
|
@@ -128,8 +128,8 @@ describe('BelongsToResolver', function () {
|
|
|
128
128
|
});
|
|
129
129
|
|
|
130
130
|
it('requires the provided parameter "scope" to be an object', async function () {
|
|
131
|
-
const
|
|
132
|
-
const R =
|
|
131
|
+
const dbs = new DatabaseSchema();
|
|
132
|
+
const R = dbs.getService(BelongsToResolver);
|
|
133
133
|
const error = v =>
|
|
134
134
|
format(
|
|
135
135
|
'The provided parameter "scope" of BelongsToResolver.includeTo ' +
|
|
@@ -152,9 +152,9 @@ describe('BelongsToResolver', function () {
|
|
|
152
152
|
});
|
|
153
153
|
|
|
154
154
|
it('throws an error if the given target model is not found', async function () {
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
const R =
|
|
155
|
+
const dbs = new DatabaseSchema();
|
|
156
|
+
dbs.defineModel({name: 'source'});
|
|
157
|
+
const R = dbs.getService(BelongsToResolver);
|
|
158
158
|
const promise = R.includeTo([], 'source', 'target', 'relation');
|
|
159
159
|
await expect(promise).to.be.rejectedWith(
|
|
160
160
|
'The model "target" is not defined',
|
|
@@ -162,9 +162,9 @@ describe('BelongsToResolver', function () {
|
|
|
162
162
|
});
|
|
163
163
|
|
|
164
164
|
it('throws an error if the given target model does not have a datasource', async function () {
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
const R =
|
|
165
|
+
const dbs = new DatabaseSchema();
|
|
166
|
+
dbs.defineModel({name: 'target'});
|
|
167
|
+
const R = dbs.getService(BelongsToResolver);
|
|
168
168
|
const promise = R.includeTo([], 'source', 'target', 'relation');
|
|
169
169
|
await expect(promise).to.be.rejectedWith(
|
|
170
170
|
'The model "target" does not have a specified datasource.',
|
|
@@ -172,13 +172,13 @@ describe('BelongsToResolver', function () {
|
|
|
172
172
|
});
|
|
173
173
|
|
|
174
174
|
it('does not throw an error if a relation target is not found', async function () {
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const sourceRel =
|
|
175
|
+
const dbs = new DatabaseSchema();
|
|
176
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
177
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
178
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
179
|
+
const sourceRel = dbs.getRepository('source');
|
|
180
180
|
const source = await sourceRel.create({parentId: 10});
|
|
181
|
-
const R =
|
|
181
|
+
const R = dbs.getService(BelongsToResolver);
|
|
182
182
|
await R.includeTo([source], 'source', 'target', 'parent');
|
|
183
183
|
expect(source).to.be.eql({
|
|
184
184
|
[DEF_PK]: source[DEF_PK],
|
|
@@ -187,12 +187,12 @@ describe('BelongsToResolver', function () {
|
|
|
187
187
|
});
|
|
188
188
|
|
|
189
189
|
it('includes if a primary key is not defined in the target model', async function () {
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const sourceRep =
|
|
195
|
-
const targetRep =
|
|
190
|
+
const dbs = new DatabaseSchema();
|
|
191
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
192
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
193
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
194
|
+
const sourceRep = dbs.getRepository('source');
|
|
195
|
+
const targetRep = dbs.getRepository('target');
|
|
196
196
|
const target = await targetRep.create({});
|
|
197
197
|
expect(target).to.be.eql({[DEF_PK]: target[DEF_PK]});
|
|
198
198
|
const source = await sourceRep.create({parentId: target[DEF_PK]});
|
|
@@ -200,7 +200,7 @@ describe('BelongsToResolver', function () {
|
|
|
200
200
|
[DEF_PK]: source[DEF_PK],
|
|
201
201
|
parentId: target[DEF_PK],
|
|
202
202
|
});
|
|
203
|
-
const R =
|
|
203
|
+
const R = dbs.getService(BelongsToResolver);
|
|
204
204
|
await R.includeTo([source], 'source', 'target', 'parent');
|
|
205
205
|
expect(source).to.be.eql({
|
|
206
206
|
[DEF_PK]: source[DEF_PK],
|
|
@@ -210,10 +210,10 @@ describe('BelongsToResolver', function () {
|
|
|
210
210
|
});
|
|
211
211
|
|
|
212
212
|
it('includes if the target model has a custom primary key', async function () {
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
213
|
+
const dbs = new DatabaseSchema();
|
|
214
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
215
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
216
|
+
dbs.defineModel({
|
|
217
217
|
name: 'target',
|
|
218
218
|
datasource: 'datasource',
|
|
219
219
|
properties: {
|
|
@@ -223,8 +223,8 @@ describe('BelongsToResolver', function () {
|
|
|
223
223
|
},
|
|
224
224
|
},
|
|
225
225
|
});
|
|
226
|
-
const sourceRep =
|
|
227
|
-
const targetRep =
|
|
226
|
+
const sourceRep = dbs.getRepository('source');
|
|
227
|
+
const targetRep = dbs.getRepository('target');
|
|
228
228
|
const target = await targetRep.create({});
|
|
229
229
|
expect(target).to.be.eql({myId: target.myId});
|
|
230
230
|
const source = await sourceRep.create({parentId: target.myId});
|
|
@@ -232,7 +232,7 @@ describe('BelongsToResolver', function () {
|
|
|
232
232
|
[DEF_PK]: source[DEF_PK],
|
|
233
233
|
parentId: target.myId,
|
|
234
234
|
});
|
|
235
|
-
const R =
|
|
235
|
+
const R = dbs.getService(BelongsToResolver);
|
|
236
236
|
await R.includeTo([source], 'source', 'target', 'parent');
|
|
237
237
|
expect(source).to.be.eql({
|
|
238
238
|
[DEF_PK]: source[DEF_PK],
|
|
@@ -242,9 +242,9 @@ describe('BelongsToResolver', function () {
|
|
|
242
242
|
});
|
|
243
243
|
|
|
244
244
|
it('includes if the source model has a custom primary key', async function () {
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
245
|
+
const dbs = new DatabaseSchema();
|
|
246
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
247
|
+
dbs.defineModel({
|
|
248
248
|
name: 'source',
|
|
249
249
|
datasource: 'datasource',
|
|
250
250
|
properties: {
|
|
@@ -254,9 +254,9 @@ describe('BelongsToResolver', function () {
|
|
|
254
254
|
},
|
|
255
255
|
},
|
|
256
256
|
});
|
|
257
|
-
|
|
258
|
-
const sourceRep =
|
|
259
|
-
const targetRep =
|
|
257
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
258
|
+
const sourceRep = dbs.getRepository('source');
|
|
259
|
+
const targetRep = dbs.getRepository('target');
|
|
260
260
|
const target = await targetRep.create({});
|
|
261
261
|
expect(target).to.be.eql({[DEF_PK]: target[DEF_PK]});
|
|
262
262
|
const source = await sourceRep.create({parentId: target[DEF_PK]});
|
|
@@ -264,7 +264,7 @@ describe('BelongsToResolver', function () {
|
|
|
264
264
|
myId: source.myId,
|
|
265
265
|
parentId: target[DEF_PK],
|
|
266
266
|
});
|
|
267
|
-
const R =
|
|
267
|
+
const R = dbs.getService(BelongsToResolver);
|
|
268
268
|
await R.includeTo([source], 'source', 'target', 'parent');
|
|
269
269
|
expect(source).to.be.eql({
|
|
270
270
|
myId: source.myId,
|
|
@@ -274,12 +274,12 @@ describe('BelongsToResolver', function () {
|
|
|
274
274
|
});
|
|
275
275
|
|
|
276
276
|
it('includes if the property "foreignKey" is specified', async function () {
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
const sourceRep =
|
|
282
|
-
const targetRep =
|
|
277
|
+
const dbs = new DatabaseSchema();
|
|
278
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
279
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
280
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
281
|
+
const sourceRep = dbs.getRepository('source');
|
|
282
|
+
const targetRep = dbs.getRepository('target');
|
|
283
283
|
const target = await targetRep.create({});
|
|
284
284
|
expect(target).to.be.eql({[DEF_PK]: target[DEF_PK]});
|
|
285
285
|
const source = await sourceRep.create({parentId: target[DEF_PK]});
|
|
@@ -287,7 +287,7 @@ describe('BelongsToResolver', function () {
|
|
|
287
287
|
[DEF_PK]: source[DEF_PK],
|
|
288
288
|
parentId: target[DEF_PK],
|
|
289
289
|
});
|
|
290
|
-
const R =
|
|
290
|
+
const R = dbs.getService(BelongsToResolver);
|
|
291
291
|
await R.includeTo([source], 'source', 'target', 'relation', 'parentId');
|
|
292
292
|
expect(source).to.be.eql({
|
|
293
293
|
[DEF_PK]: source[DEF_PK],
|
|
@@ -297,12 +297,12 @@ describe('BelongsToResolver', function () {
|
|
|
297
297
|
});
|
|
298
298
|
|
|
299
299
|
it('uses a where clause of the given scope to filter the relation target', async function () {
|
|
300
|
-
const
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
const sourceRep =
|
|
305
|
-
const targetRep =
|
|
300
|
+
const dbs = new DatabaseSchema();
|
|
301
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
302
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
303
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
304
|
+
const sourceRep = dbs.getRepository('source');
|
|
305
|
+
const targetRep = dbs.getRepository('target');
|
|
306
306
|
const target = await targetRep.create({
|
|
307
307
|
foo: 'fooVal',
|
|
308
308
|
bar: 'barVal',
|
|
@@ -319,7 +319,7 @@ describe('BelongsToResolver', function () {
|
|
|
319
319
|
[DEF_PK]: source[DEF_PK],
|
|
320
320
|
parentId: target[DEF_PK],
|
|
321
321
|
});
|
|
322
|
-
const R =
|
|
322
|
+
const R = dbs.getService(BelongsToResolver);
|
|
323
323
|
await R.includeTo([source], 'source', 'target', 'parent', undefined, {
|
|
324
324
|
where: {foo: 'barVal'},
|
|
325
325
|
});
|
|
@@ -338,12 +338,12 @@ describe('BelongsToResolver', function () {
|
|
|
338
338
|
});
|
|
339
339
|
|
|
340
340
|
it('uses a fields clause of the given scope to filter the relation target', async function () {
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
const sourceRep =
|
|
346
|
-
const targetRep =
|
|
341
|
+
const dbs = new DatabaseSchema();
|
|
342
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
343
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
344
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
345
|
+
const sourceRep = dbs.getRepository('source');
|
|
346
|
+
const targetRep = dbs.getRepository('target');
|
|
347
347
|
const target = await targetRep.create({
|
|
348
348
|
foo: 'fooVal',
|
|
349
349
|
bar: 'barVal',
|
|
@@ -360,7 +360,7 @@ describe('BelongsToResolver', function () {
|
|
|
360
360
|
[DEF_PK]: source[DEF_PK],
|
|
361
361
|
parentId: target[DEF_PK],
|
|
362
362
|
});
|
|
363
|
-
const R =
|
|
363
|
+
const R = dbs.getService(BelongsToResolver);
|
|
364
364
|
await R.includeTo([source], 'source', 'target', 'parent', undefined, {
|
|
365
365
|
fields: [DEF_PK, 'bar'],
|
|
366
366
|
});
|
|
@@ -375,12 +375,12 @@ describe('BelongsToResolver', function () {
|
|
|
375
375
|
});
|
|
376
376
|
|
|
377
377
|
it('uses an include clause of the given scope to resolve target relations', async function () {
|
|
378
|
-
const
|
|
379
|
-
|
|
378
|
+
const dbs = new DatabaseSchema();
|
|
379
|
+
dbs.defineDatasource({
|
|
380
380
|
name: 'datasource',
|
|
381
381
|
adapter: 'memory',
|
|
382
382
|
});
|
|
383
|
-
|
|
383
|
+
dbs.defineModel({
|
|
384
384
|
name: 'modelA',
|
|
385
385
|
datasource: 'datasource',
|
|
386
386
|
properties: {
|
|
@@ -394,7 +394,7 @@ describe('BelongsToResolver', function () {
|
|
|
394
394
|
},
|
|
395
395
|
},
|
|
396
396
|
});
|
|
397
|
-
|
|
397
|
+
dbs.defineModel({
|
|
398
398
|
name: 'modelB',
|
|
399
399
|
datasource: 'datasource',
|
|
400
400
|
properties: {
|
|
@@ -414,7 +414,7 @@ describe('BelongsToResolver', function () {
|
|
|
414
414
|
},
|
|
415
415
|
},
|
|
416
416
|
});
|
|
417
|
-
|
|
417
|
+
dbs.defineModel({
|
|
418
418
|
name: 'modelC',
|
|
419
419
|
datasource: 'datasource',
|
|
420
420
|
properties: {
|
|
@@ -434,9 +434,9 @@ describe('BelongsToResolver', function () {
|
|
|
434
434
|
},
|
|
435
435
|
},
|
|
436
436
|
});
|
|
437
|
-
const aRep =
|
|
438
|
-
const bRep =
|
|
439
|
-
const cRep =
|
|
437
|
+
const aRep = dbs.getRepository('modelA');
|
|
438
|
+
const bRep = dbs.getRepository('modelB');
|
|
439
|
+
const cRep = dbs.getRepository('modelC');
|
|
440
440
|
const a = await aRep.create({});
|
|
441
441
|
const b = await bRep.create({parentId: a.id});
|
|
442
442
|
const c = await cRep.create({parentId: b.id});
|
|
@@ -454,7 +454,7 @@ describe('BelongsToResolver', function () {
|
|
|
454
454
|
source: 'modelC',
|
|
455
455
|
parentId: b.id,
|
|
456
456
|
});
|
|
457
|
-
const R =
|
|
457
|
+
const R = dbs.getService(BelongsToResolver);
|
|
458
458
|
await R.includeTo([c], 'modelC', 'modelB', 'parent', undefined, {
|
|
459
459
|
include: 'parent',
|
|
460
460
|
});
|
|
@@ -475,12 +475,12 @@ describe('BelongsToResolver', function () {
|
|
|
475
475
|
});
|
|
476
476
|
|
|
477
477
|
it('does not break the "and" operator of the given "where" clause', async function () {
|
|
478
|
-
const
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
const sourceRep =
|
|
483
|
-
const targetRep =
|
|
478
|
+
const dbs = new DatabaseSchema();
|
|
479
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
480
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
481
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
482
|
+
const sourceRep = dbs.getRepository('source');
|
|
483
|
+
const targetRep = dbs.getRepository('target');
|
|
484
484
|
const target = await targetRep.create({
|
|
485
485
|
foo: 'fooVal',
|
|
486
486
|
bar: 'barVal',
|
|
@@ -497,7 +497,7 @@ describe('BelongsToResolver', function () {
|
|
|
497
497
|
[DEF_PK]: source[DEF_PK],
|
|
498
498
|
parentId: target[DEF_PK],
|
|
499
499
|
});
|
|
500
|
-
const R =
|
|
500
|
+
const R = dbs.getService(BelongsToResolver);
|
|
501
501
|
await R.includeTo([source], 'source', 'target', 'parent', undefined, {
|
|
502
502
|
where: {and: [{foo: 'barVal'}]},
|
|
503
503
|
fields: [DEF_PK, 'bar'],
|
|
@@ -523,8 +523,8 @@ describe('BelongsToResolver', function () {
|
|
|
523
523
|
|
|
524
524
|
describe('includePolymorphicTo', function () {
|
|
525
525
|
it('requires the "entities" parameter to be an array', async function () {
|
|
526
|
-
const
|
|
527
|
-
const R =
|
|
526
|
+
const dbs = new DatabaseSchema();
|
|
527
|
+
const R = dbs.getService(BelongsToResolver);
|
|
528
528
|
const error = v =>
|
|
529
529
|
format(
|
|
530
530
|
'The parameter "entities" of BelongsToResolver.includePolymorphicTo requires ' +
|
|
@@ -544,8 +544,8 @@ describe('BelongsToResolver', function () {
|
|
|
544
544
|
});
|
|
545
545
|
|
|
546
546
|
it('requires elements of the "entities" parameter to be an Object', async function () {
|
|
547
|
-
const
|
|
548
|
-
const R =
|
|
547
|
+
const dbs = new DatabaseSchema();
|
|
548
|
+
const R = dbs.getService(BelongsToResolver);
|
|
549
549
|
const error = v =>
|
|
550
550
|
format(
|
|
551
551
|
'The parameter "entities" of BelongsToResolver.includePolymorphicTo requires ' +
|
|
@@ -565,8 +565,8 @@ describe('BelongsToResolver', function () {
|
|
|
565
565
|
});
|
|
566
566
|
|
|
567
567
|
it('requires the "sourceName" parameter to be a non-empty string', async function () {
|
|
568
|
-
const
|
|
569
|
-
const R =
|
|
568
|
+
const dbs = new DatabaseSchema();
|
|
569
|
+
const R = dbs.getService(BelongsToResolver);
|
|
570
570
|
const error = v =>
|
|
571
571
|
format(
|
|
572
572
|
'The parameter "sourceName" of BelongsToResolver.includePolymorphicTo requires ' +
|
|
@@ -586,8 +586,8 @@ describe('BelongsToResolver', function () {
|
|
|
586
586
|
});
|
|
587
587
|
|
|
588
588
|
it('requires the "relationName" parameter to be a non-empty string', async function () {
|
|
589
|
-
const
|
|
590
|
-
const R =
|
|
589
|
+
const dbs = new DatabaseSchema();
|
|
590
|
+
const R = dbs.getService(BelongsToResolver);
|
|
591
591
|
const error = v =>
|
|
592
592
|
format(
|
|
593
593
|
'The parameter "relationName" of BelongsToResolver.includePolymorphicTo requires ' +
|
|
@@ -606,8 +606,8 @@ describe('BelongsToResolver', function () {
|
|
|
606
606
|
});
|
|
607
607
|
|
|
608
608
|
it('requires the provided parameter "foreignKey" to be a string', async function () {
|
|
609
|
-
const
|
|
610
|
-
const R =
|
|
609
|
+
const dbs = new DatabaseSchema();
|
|
610
|
+
const R = dbs.getService(BelongsToResolver);
|
|
611
611
|
const error = v =>
|
|
612
612
|
format(
|
|
613
613
|
'The provided parameter "foreignKey" of BelongsToResolver.includePolymorphicTo ' +
|
|
@@ -623,8 +623,8 @@ describe('BelongsToResolver', function () {
|
|
|
623
623
|
});
|
|
624
624
|
|
|
625
625
|
it('requires the provided parameter "discriminator" to be a string', async function () {
|
|
626
|
-
const
|
|
627
|
-
const R =
|
|
626
|
+
const dbs = new DatabaseSchema();
|
|
627
|
+
const R = dbs.getService(BelongsToResolver);
|
|
628
628
|
const error = v =>
|
|
629
629
|
format(
|
|
630
630
|
'The provided parameter "discriminator" of BelongsToResolver.includePolymorphicTo ' +
|
|
@@ -640,8 +640,8 @@ describe('BelongsToResolver', function () {
|
|
|
640
640
|
});
|
|
641
641
|
|
|
642
642
|
it('requires the provided parameter "scope" to be an object', async function () {
|
|
643
|
-
const
|
|
644
|
-
const R =
|
|
643
|
+
const dbs = new DatabaseSchema();
|
|
644
|
+
const R = dbs.getService(BelongsToResolver);
|
|
645
645
|
const error = v =>
|
|
646
646
|
format(
|
|
647
647
|
'The provided parameter "scope" of BelongsToResolver.includePolymorphicTo ' +
|
|
@@ -664,35 +664,35 @@ describe('BelongsToResolver', function () {
|
|
|
664
664
|
});
|
|
665
665
|
|
|
666
666
|
it('does not throw an error if a target model is not found', async function () {
|
|
667
|
-
const
|
|
668
|
-
|
|
669
|
-
const R =
|
|
667
|
+
const dbs = new DatabaseSchema();
|
|
668
|
+
dbs.defineModel({name: 'source'});
|
|
669
|
+
const R = dbs.getService(BelongsToResolver);
|
|
670
670
|
const entity = {[DEF_PK]: 1, parentId: 1, parentType: 'target'};
|
|
671
671
|
await R.includePolymorphicTo([entity], 'source', 'parent');
|
|
672
672
|
expect(entity).to.be.eql(entity);
|
|
673
673
|
});
|
|
674
674
|
|
|
675
675
|
it('does not throws an error if a target model does not have datasource', async function () {
|
|
676
|
-
const
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
const R =
|
|
676
|
+
const dbs = new DatabaseSchema();
|
|
677
|
+
dbs.defineModel({name: 'source'});
|
|
678
|
+
dbs.defineModel({name: 'target'});
|
|
679
|
+
const R = dbs.getService(BelongsToResolver);
|
|
680
680
|
const entity = {[DEF_PK]: 1, parentId: 1, parentType: 'target'};
|
|
681
681
|
await R.includePolymorphicTo([entity], 'source', 'parent');
|
|
682
682
|
expect(entity).to.be.eql(entity);
|
|
683
683
|
});
|
|
684
684
|
|
|
685
685
|
it('does not throw an error if a relation target is not found', async function () {
|
|
686
|
-
const
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
const sourceRel =
|
|
686
|
+
const dbs = new DatabaseSchema();
|
|
687
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
688
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
689
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
690
|
+
const sourceRel = dbs.getRepository('source');
|
|
691
691
|
const source = await sourceRel.create({
|
|
692
692
|
parentId: 10,
|
|
693
693
|
parentType: 'target',
|
|
694
694
|
});
|
|
695
|
-
const R =
|
|
695
|
+
const R = dbs.getService(BelongsToResolver);
|
|
696
696
|
await R.includePolymorphicTo([source], 'source', 'parent');
|
|
697
697
|
expect(source).to.be.eql({
|
|
698
698
|
[DEF_PK]: source[DEF_PK],
|
|
@@ -702,21 +702,21 @@ describe('BelongsToResolver', function () {
|
|
|
702
702
|
});
|
|
703
703
|
|
|
704
704
|
it('does not throw an error if no discriminator value', async function () {
|
|
705
|
-
const
|
|
706
|
-
|
|
707
|
-
const R =
|
|
705
|
+
const dbs = new DatabaseSchema();
|
|
706
|
+
dbs.defineModel({name: 'source'});
|
|
707
|
+
const R = dbs.getService(BelongsToResolver);
|
|
708
708
|
const entity = {[DEF_PK]: 1, parentId: 1};
|
|
709
709
|
await R.includePolymorphicTo([entity], 'source', 'parent');
|
|
710
710
|
expect(entity).to.be.eql(entity);
|
|
711
711
|
});
|
|
712
712
|
|
|
713
713
|
it('includes if a primary key is not defined in the target model', async function () {
|
|
714
|
-
const
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
const sourceRep =
|
|
719
|
-
const targetRep =
|
|
714
|
+
const dbs = new DatabaseSchema();
|
|
715
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
716
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
717
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
718
|
+
const sourceRep = dbs.getRepository('source');
|
|
719
|
+
const targetRep = dbs.getRepository('target');
|
|
720
720
|
const target = await targetRep.create({});
|
|
721
721
|
expect(target).to.be.eql({[DEF_PK]: target[DEF_PK]});
|
|
722
722
|
const source = await sourceRep.create({
|
|
@@ -728,7 +728,7 @@ describe('BelongsToResolver', function () {
|
|
|
728
728
|
parentId: target[DEF_PK],
|
|
729
729
|
parentType: 'target',
|
|
730
730
|
});
|
|
731
|
-
const R =
|
|
731
|
+
const R = dbs.getService(BelongsToResolver);
|
|
732
732
|
await R.includePolymorphicTo([source], 'source', 'parent');
|
|
733
733
|
expect(source).to.be.eql({
|
|
734
734
|
[DEF_PK]: source[DEF_PK],
|
|
@@ -739,9 +739,9 @@ describe('BelongsToResolver', function () {
|
|
|
739
739
|
});
|
|
740
740
|
|
|
741
741
|
it('includes if the source model has a custom primary key', async function () {
|
|
742
|
-
const
|
|
743
|
-
|
|
744
|
-
|
|
742
|
+
const dbs = new DatabaseSchema();
|
|
743
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
744
|
+
dbs.defineModel({
|
|
745
745
|
name: 'source',
|
|
746
746
|
datasource: 'datasource',
|
|
747
747
|
properties: {
|
|
@@ -751,9 +751,9 @@ describe('BelongsToResolver', function () {
|
|
|
751
751
|
},
|
|
752
752
|
},
|
|
753
753
|
});
|
|
754
|
-
|
|
755
|
-
const sourceRep =
|
|
756
|
-
const targetRep =
|
|
754
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
755
|
+
const sourceRep = dbs.getRepository('source');
|
|
756
|
+
const targetRep = dbs.getRepository('target');
|
|
757
757
|
const target = await targetRep.create({});
|
|
758
758
|
expect(target).to.be.eql({[DEF_PK]: target[DEF_PK]});
|
|
759
759
|
const source = await sourceRep.create({
|
|
@@ -765,7 +765,7 @@ describe('BelongsToResolver', function () {
|
|
|
765
765
|
parentId: target[DEF_PK],
|
|
766
766
|
parentType: 'target',
|
|
767
767
|
});
|
|
768
|
-
const R =
|
|
768
|
+
const R = dbs.getService(BelongsToResolver);
|
|
769
769
|
await R.includePolymorphicTo([source], 'source', 'parent');
|
|
770
770
|
expect(source).to.be.eql({
|
|
771
771
|
myId: source.myId,
|
|
@@ -776,12 +776,12 @@ describe('BelongsToResolver', function () {
|
|
|
776
776
|
});
|
|
777
777
|
|
|
778
778
|
it('includes if the property "foreignKey" is specified', async function () {
|
|
779
|
-
const
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
const sourceRep =
|
|
784
|
-
const targetRep =
|
|
779
|
+
const dbs = new DatabaseSchema();
|
|
780
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
781
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
782
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
783
|
+
const sourceRep = dbs.getRepository('source');
|
|
784
|
+
const targetRep = dbs.getRepository('target');
|
|
785
785
|
const target = await targetRep.create({});
|
|
786
786
|
expect(target).to.be.eql({[DEF_PK]: target[DEF_PK]});
|
|
787
787
|
const source = await sourceRep.create({
|
|
@@ -793,7 +793,7 @@ describe('BelongsToResolver', function () {
|
|
|
793
793
|
parentId: target[DEF_PK],
|
|
794
794
|
relationType: 'target',
|
|
795
795
|
});
|
|
796
|
-
const R =
|
|
796
|
+
const R = dbs.getService(BelongsToResolver);
|
|
797
797
|
await R.includePolymorphicTo([source], 'source', 'relation', 'parentId');
|
|
798
798
|
expect(source).to.be.eql({
|
|
799
799
|
[DEF_PK]: source[DEF_PK],
|
|
@@ -804,12 +804,12 @@ describe('BelongsToResolver', function () {
|
|
|
804
804
|
});
|
|
805
805
|
|
|
806
806
|
it('includes if the property "discriminator" is specified', async function () {
|
|
807
|
-
const
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
const sourceRep =
|
|
812
|
-
const targetRep =
|
|
807
|
+
const dbs = new DatabaseSchema();
|
|
808
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
809
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
810
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
811
|
+
const sourceRep = dbs.getRepository('source');
|
|
812
|
+
const targetRep = dbs.getRepository('target');
|
|
813
813
|
const target = await targetRep.create({});
|
|
814
814
|
expect(target).to.be.eql({[DEF_PK]: target[DEF_PK]});
|
|
815
815
|
const source = await sourceRep.create({
|
|
@@ -821,7 +821,7 @@ describe('BelongsToResolver', function () {
|
|
|
821
821
|
relationId: target[DEF_PK],
|
|
822
822
|
parentType: 'target',
|
|
823
823
|
});
|
|
824
|
-
const R =
|
|
824
|
+
const R = dbs.getService(BelongsToResolver);
|
|
825
825
|
await R.includePolymorphicTo(
|
|
826
826
|
[source],
|
|
827
827
|
'source',
|
|
@@ -838,12 +838,12 @@ describe('BelongsToResolver', function () {
|
|
|
838
838
|
});
|
|
839
839
|
|
|
840
840
|
it('uses a where clause of the given scope to filter the relation target', async function () {
|
|
841
|
-
const
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
const sourceRep =
|
|
846
|
-
const targetRep =
|
|
841
|
+
const dbs = new DatabaseSchema();
|
|
842
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
843
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
844
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
845
|
+
const sourceRep = dbs.getRepository('source');
|
|
846
|
+
const targetRep = dbs.getRepository('target');
|
|
847
847
|
const target = await targetRep.create({
|
|
848
848
|
foo: 'fooVal',
|
|
849
849
|
bar: 'barVal',
|
|
@@ -862,7 +862,7 @@ describe('BelongsToResolver', function () {
|
|
|
862
862
|
parentId: target[DEF_PK],
|
|
863
863
|
parentType: 'target',
|
|
864
864
|
});
|
|
865
|
-
const R =
|
|
865
|
+
const R = dbs.getService(BelongsToResolver);
|
|
866
866
|
await R.includePolymorphicTo(
|
|
867
867
|
[source],
|
|
868
868
|
'source',
|
|
@@ -893,12 +893,12 @@ describe('BelongsToResolver', function () {
|
|
|
893
893
|
});
|
|
894
894
|
|
|
895
895
|
it('uses a fields clause of the given scope to filter the relation target', async function () {
|
|
896
|
-
const
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
const sourceRep =
|
|
901
|
-
const targetRep =
|
|
896
|
+
const dbs = new DatabaseSchema();
|
|
897
|
+
dbs.defineDatasource({name: 'datasource', adapter: 'memory'});
|
|
898
|
+
dbs.defineModel({name: 'source', datasource: 'datasource'});
|
|
899
|
+
dbs.defineModel({name: 'target', datasource: 'datasource'});
|
|
900
|
+
const sourceRep = dbs.getRepository('source');
|
|
901
|
+
const targetRep = dbs.getRepository('target');
|
|
902
902
|
const target = await targetRep.create({
|
|
903
903
|
foo: 'fooVal',
|
|
904
904
|
bar: 'barVal',
|
|
@@ -917,7 +917,7 @@ describe('BelongsToResolver', function () {
|
|
|
917
917
|
parentId: target[DEF_PK],
|
|
918
918
|
parentType: 'target',
|
|
919
919
|
});
|
|
920
|
-
const R =
|
|
920
|
+
const R = dbs.getService(BelongsToResolver);
|
|
921
921
|
await R.includePolymorphicTo(
|
|
922
922
|
[source],
|
|
923
923
|
'source',
|
|
@@ -938,12 +938,12 @@ describe('BelongsToResolver', function () {
|
|
|
938
938
|
});
|
|
939
939
|
|
|
940
940
|
it('uses an include clause of the given scope to resolve target relations', async function () {
|
|
941
|
-
const
|
|
942
|
-
|
|
941
|
+
const dbs = new DatabaseSchema();
|
|
942
|
+
dbs.defineDatasource({
|
|
943
943
|
name: 'datasource',
|
|
944
944
|
adapter: 'memory',
|
|
945
945
|
});
|
|
946
|
-
|
|
946
|
+
dbs.defineModel({
|
|
947
947
|
name: 'modelA',
|
|
948
948
|
datasource: 'datasource',
|
|
949
949
|
properties: {
|
|
@@ -957,7 +957,7 @@ describe('BelongsToResolver', function () {
|
|
|
957
957
|
},
|
|
958
958
|
},
|
|
959
959
|
});
|
|
960
|
-
|
|
960
|
+
dbs.defineModel({
|
|
961
961
|
name: 'modelB',
|
|
962
962
|
datasource: 'datasource',
|
|
963
963
|
properties: {
|
|
@@ -977,7 +977,7 @@ describe('BelongsToResolver', function () {
|
|
|
977
977
|
},
|
|
978
978
|
},
|
|
979
979
|
});
|
|
980
|
-
|
|
980
|
+
dbs.defineModel({
|
|
981
981
|
name: 'modelC',
|
|
982
982
|
datasource: 'datasource',
|
|
983
983
|
properties: {
|
|
@@ -997,9 +997,9 @@ describe('BelongsToResolver', function () {
|
|
|
997
997
|
},
|
|
998
998
|
},
|
|
999
999
|
});
|
|
1000
|
-
const aRep =
|
|
1001
|
-
const bRep =
|
|
1002
|
-
const cRep =
|
|
1000
|
+
const aRep = dbs.getRepository('modelA');
|
|
1001
|
+
const bRep = dbs.getRepository('modelB');
|
|
1002
|
+
const cRep = dbs.getRepository('modelC');
|
|
1003
1003
|
const a = await aRep.create({});
|
|
1004
1004
|
const b = await bRep.create({parentId: a.id});
|
|
1005
1005
|
const c = await cRep.create({parentId: b.id, parentType: 'modelB'});
|
|
@@ -1018,7 +1018,7 @@ describe('BelongsToResolver', function () {
|
|
|
1018
1018
|
parentId: b.id,
|
|
1019
1019
|
parentType: 'modelB',
|
|
1020
1020
|
});
|
|
1021
|
-
const R =
|
|
1021
|
+
const R = dbs.getService(BelongsToResolver);
|
|
1022
1022
|
await R.includePolymorphicTo(
|
|
1023
1023
|
[c],
|
|
1024
1024
|
'modelC',
|