@goatlab/fluent 0.7.30 → 0.7.31
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/dist/TypeOrmConnector/queryBuilder/mongodb/getMongoBaseAggregations.js +1 -0
- package/dist/TypeOrmConnector/queryBuilder/mongodb/getMongoFindAggregatedQuery.js +1 -0
- package/dist/TypeOrmConnector/test/relations/relationsTestsSuite.js +528 -2
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getMongoBaseAggregation = void 0;
|
|
4
4
|
const getMongoWhere_1 = require("./getMongoWhere");
|
|
5
5
|
const getMongoBaseAggregation = ({ include, self }) => {
|
|
6
|
+
self.initDB();
|
|
6
7
|
if (!include) {
|
|
7
8
|
return [];
|
|
8
9
|
}
|
|
@@ -6,6 +6,7 @@ const getMongoOrderBy_1 = require("./getMongoOrderBy");
|
|
|
6
6
|
const getMongoSelect_1 = require("./getMongoSelect");
|
|
7
7
|
const getMongoWhere_1 = require("./getMongoWhere");
|
|
8
8
|
const getMongoFindAggregatedQuery = ({ query, self }) => {
|
|
9
|
+
self.initDB();
|
|
9
10
|
const selected = (0, getMongoSelect_1.getMongoSelect)(query?.select);
|
|
10
11
|
const orderBy = (0, getMongoOrderBy_1.getMongoOrderBy)(query?.orderBy);
|
|
11
12
|
const where = (0, getMongoWhere_1.getMongoWhere)({
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.relationsTestSuite = void 0;
|
|
4
|
+
const js_utils_1 = require("@goatlab/js-utils");
|
|
4
5
|
let User;
|
|
5
6
|
let Car;
|
|
6
7
|
let Role;
|
|
@@ -30,6 +31,38 @@ const relationsTestSuite = (UserRepo, BelongsToModelF, ManyToManyModelF) => {
|
|
|
30
31
|
const associatedCar4 = await user.cars().associate({ name: 'My new car 4' });
|
|
31
32
|
return { insertedUser, adminRole };
|
|
32
33
|
};
|
|
34
|
+
test('requireById - Should return valid Object', async () => {
|
|
35
|
+
const insertedUser = await User.insert({
|
|
36
|
+
name: 'testUser',
|
|
37
|
+
age: 20
|
|
38
|
+
});
|
|
39
|
+
expect(typeof insertedUser.id).toBe('string');
|
|
40
|
+
const findById = await User.requireById(insertedUser.id);
|
|
41
|
+
expect(findById.id).toBe(insertedUser.id);
|
|
42
|
+
});
|
|
43
|
+
test('requireById - Should fail if not found', async () => {
|
|
44
|
+
const insertedUser = await User.insert({
|
|
45
|
+
name: 'testUser',
|
|
46
|
+
age: 20
|
|
47
|
+
});
|
|
48
|
+
const [error, found] = await js_utils_1.Promises.try(User.requireById('62ed01e4219a6ab760ae5c50'));
|
|
49
|
+
expect(error?.message).toBe('Object 62ed01e4219a6ab760ae5c50 not found');
|
|
50
|
+
});
|
|
51
|
+
test('loadFirst - Should return a cloned class', async () => {
|
|
52
|
+
const insertedUser = await User.insert({
|
|
53
|
+
name: 'testUser',
|
|
54
|
+
age: 20
|
|
55
|
+
});
|
|
56
|
+
expect(typeof insertedUser.id).toBe('string');
|
|
57
|
+
const user = User.loadFirst({
|
|
58
|
+
where: {
|
|
59
|
+
id: insertedUser.id
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
expect(Array.isArray(user)).toBe(false);
|
|
63
|
+
expect(typeof user).toBe('object');
|
|
64
|
+
expect(user).toHaveProperty('associate');
|
|
65
|
+
});
|
|
33
66
|
test('Attach - Many to Many - Should relate Data', async () => {
|
|
34
67
|
const insertedUser = await User.insert({
|
|
35
68
|
name: 'testUser1',
|
|
@@ -40,10 +73,503 @@ const relationsTestSuite = (UserRepo, BelongsToModelF, ManyToManyModelF) => {
|
|
|
40
73
|
});
|
|
41
74
|
const user = await User.loadById(insertedUser.id);
|
|
42
75
|
const attachedRoles = await user.roles().attach(adminRole.id);
|
|
43
|
-
console.log(insertedUser);
|
|
44
|
-
console.log(attachedRoles);
|
|
45
76
|
expect(attachedRoles[0].userId).toBe(insertedUser.id);
|
|
46
77
|
expect(attachedRoles[0].roleId).toBe(adminRole.id);
|
|
47
78
|
});
|
|
79
|
+
test('Associate - OneToMany - Should insert data', async () => {
|
|
80
|
+
const insertedUser = await User.insert({
|
|
81
|
+
name: 'testUser',
|
|
82
|
+
age: 20
|
|
83
|
+
});
|
|
84
|
+
expect(typeof insertedUser.id).toBe('string');
|
|
85
|
+
const user = await User.loadById(insertedUser.id);
|
|
86
|
+
const cars = await user.cars().associate({ name: 'Another new car' });
|
|
87
|
+
expect(Array.isArray(cars)).toBe(true);
|
|
88
|
+
expect(cars[0].name).toBe('Another new car');
|
|
89
|
+
expect(cars[0].userId).toBe(insertedUser.id);
|
|
90
|
+
});
|
|
91
|
+
test('Associate - OneToMany - Should relate existing model', async () => {
|
|
92
|
+
const insertedUser = await User.insert({
|
|
93
|
+
name: 'testUser',
|
|
94
|
+
age: 20
|
|
95
|
+
});
|
|
96
|
+
expect(typeof insertedUser.id).toBe('string');
|
|
97
|
+
const user = await User.loadById(insertedUser.id);
|
|
98
|
+
const createdCar = await Car.insert({ name: 'Another new car' });
|
|
99
|
+
const cars = await user.cars().associate(createdCar);
|
|
100
|
+
expect(Array.isArray(cars)).toBe(true);
|
|
101
|
+
expect(cars[0].name).toBe('Another new car');
|
|
102
|
+
expect(cars[0].userId).toBe(insertedUser.id);
|
|
103
|
+
expect(createdCar.id).toBe(cars[0].id);
|
|
104
|
+
});
|
|
105
|
+
test('Query related model - OneToMany (belongsToMany)', async () => {
|
|
106
|
+
const insertedUser = await User.insert({
|
|
107
|
+
name: 'testUser',
|
|
108
|
+
age: 20
|
|
109
|
+
});
|
|
110
|
+
expect(typeof insertedUser.id).toBe('string');
|
|
111
|
+
const user1 = await User.loadById(insertedUser.id);
|
|
112
|
+
const cars = await user1.cars().associate({ name: 'My new car' });
|
|
113
|
+
expect(Array.isArray(cars)).toBe(true);
|
|
114
|
+
const searchUserWithRelation = await User.findMany({
|
|
115
|
+
where: { id: insertedUser.id },
|
|
116
|
+
include: {
|
|
117
|
+
cars: true
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
const firstResult = searchUserWithRelation[0];
|
|
121
|
+
expect(Array.isArray(searchUserWithRelation)).toBe(true);
|
|
122
|
+
expect(Array.isArray(firstResult.cars)).toBe(true);
|
|
123
|
+
expect(firstResult.cars.length > 0).toBe(true);
|
|
124
|
+
expect(firstResult.cars[0].userId).toBe(insertedUser.id);
|
|
125
|
+
const searchCar = await user1.cars().findMany({
|
|
126
|
+
where: { name: 'My new car' },
|
|
127
|
+
include: { user: true }
|
|
128
|
+
});
|
|
129
|
+
expect(Array.isArray(searchCar)).toBe(true);
|
|
130
|
+
expect(searchCar.length > 0).toBe(true);
|
|
131
|
+
expect(searchCar[0].user?.id).toBe(insertedUser.id);
|
|
132
|
+
const searchCar2 = await user1
|
|
133
|
+
.cars()
|
|
134
|
+
.findMany({ where: { name: 'My.......' } });
|
|
135
|
+
expect(Array.isArray(searchCar2)).toBe(true);
|
|
136
|
+
expect(searchCar2.length === 0).toBe(true);
|
|
137
|
+
});
|
|
138
|
+
test('Query related model - ManyToOne (BelongsTo)', async () => {
|
|
139
|
+
const insertedUser = await User.insert({
|
|
140
|
+
name: 'testUser',
|
|
141
|
+
age: 20
|
|
142
|
+
});
|
|
143
|
+
expect(typeof insertedUser.id).toBe('string');
|
|
144
|
+
const user1 = await User.loadById(insertedUser.id);
|
|
145
|
+
await user1.cars().associate({ name: 'My new car' });
|
|
146
|
+
const results = await Car.findMany({
|
|
147
|
+
where: {
|
|
148
|
+
userId: insertedUser.id
|
|
149
|
+
},
|
|
150
|
+
include: {
|
|
151
|
+
user: true
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
expect(Array.isArray(results)).toBe(true);
|
|
155
|
+
expect(results.length > 0).toBe(true);
|
|
156
|
+
expect(typeof results[0].user?.name).toBe('string');
|
|
157
|
+
expect(results[0].user['id']).toBe(insertedUser.id);
|
|
158
|
+
});
|
|
159
|
+
test('Include - can load cyclical relations', async () => {
|
|
160
|
+
const { insertedUser } = await insertRelatedData();
|
|
161
|
+
const searchUserWithRelations = await User.findMany({
|
|
162
|
+
where: {
|
|
163
|
+
id: insertedUser.id
|
|
164
|
+
},
|
|
165
|
+
include: {
|
|
166
|
+
cars: {
|
|
167
|
+
include: {
|
|
168
|
+
user: {
|
|
169
|
+
include: {
|
|
170
|
+
cars: {
|
|
171
|
+
include: {
|
|
172
|
+
user: true
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
expect(searchUserWithRelations.length).toBe(1);
|
|
182
|
+
expect(searchUserWithRelations[0].cars?.length).toBe(4);
|
|
183
|
+
expect(searchUserWithRelations[0].cars[0].user?.id).toBe(insertedUser.id);
|
|
184
|
+
expect(searchUserWithRelations[0].cars[0].user?.name).toBe(insertedUser.name);
|
|
185
|
+
expect(searchUserWithRelations[0].cars[0].user?.id).toBe(searchUserWithRelations[0].id);
|
|
186
|
+
expect(searchUserWithRelations[0].cars[0].user?.name).toBe(searchUserWithRelations[0].name);
|
|
187
|
+
expect(searchUserWithRelations[0].cars[0].user?.cars?.length).toBe(4);
|
|
188
|
+
expect(searchUserWithRelations[0].cars[0].user?.cars[0].user?.id).toBe(searchUserWithRelations[0].id);
|
|
189
|
+
});
|
|
190
|
+
test('Include - can filter related models', async () => {
|
|
191
|
+
const { insertedUser } = await insertRelatedData();
|
|
192
|
+
const searchUserWithRelations = await User.findMany({
|
|
193
|
+
where: {
|
|
194
|
+
id: insertedUser.id
|
|
195
|
+
},
|
|
196
|
+
include: {
|
|
197
|
+
cars: {
|
|
198
|
+
select: {
|
|
199
|
+
name: true,
|
|
200
|
+
id: true
|
|
201
|
+
},
|
|
202
|
+
where: {
|
|
203
|
+
name: 'My new car 4'
|
|
204
|
+
},
|
|
205
|
+
include: {
|
|
206
|
+
user: {
|
|
207
|
+
include: {
|
|
208
|
+
cars: {
|
|
209
|
+
where: {
|
|
210
|
+
name: 'My new car XXXxX'
|
|
211
|
+
},
|
|
212
|
+
include: {
|
|
213
|
+
user: true
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
expect(searchUserWithRelations[0].cars?.length).toBe(2);
|
|
223
|
+
expect(searchUserWithRelations[0].cars[0].name).toBe('My new car 4');
|
|
224
|
+
expect(searchUserWithRelations[0].cars[0].user?.cars?.length).toBe(0);
|
|
225
|
+
});
|
|
226
|
+
test('Include - (One to Many) Should display [] if not found', async () => {
|
|
227
|
+
const { insertedUser } = await insertRelatedData();
|
|
228
|
+
const searchUserWithRelations = await User.findMany({
|
|
229
|
+
where: {
|
|
230
|
+
id: insertedUser.id
|
|
231
|
+
},
|
|
232
|
+
include: {
|
|
233
|
+
cars: {
|
|
234
|
+
where: {
|
|
235
|
+
name: 'My new car XX'
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
expect(Array.isArray(searchUserWithRelations[0].cars)).toBe(true);
|
|
241
|
+
expect(searchUserWithRelations[0].cars?.length).toBe(0);
|
|
242
|
+
});
|
|
243
|
+
test('Include - (Many to One) Should display null if not found', async () => {
|
|
244
|
+
const { insertedUser } = await insertRelatedData();
|
|
245
|
+
const insertedCars = await Car.findMany({
|
|
246
|
+
where: {
|
|
247
|
+
userId: insertedUser.id
|
|
248
|
+
},
|
|
249
|
+
select: {
|
|
250
|
+
id: true,
|
|
251
|
+
name: true,
|
|
252
|
+
userId: true,
|
|
253
|
+
user: true
|
|
254
|
+
},
|
|
255
|
+
include: {
|
|
256
|
+
user: {
|
|
257
|
+
where: {
|
|
258
|
+
name: 'JOHN'
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
expect(insertedCars[0].user === null || insertedCars[0].user === undefined).toBe(true);
|
|
264
|
+
expect(insertedCars.length).toBe(4);
|
|
265
|
+
});
|
|
266
|
+
test('Include - Should pull info from the main search object if no related data', async () => {
|
|
267
|
+
const { insertedUser } = await insertRelatedData();
|
|
268
|
+
const searchUserWithRelations = await User.findMany({
|
|
269
|
+
select: {
|
|
270
|
+
id: true,
|
|
271
|
+
name: true,
|
|
272
|
+
cars: {
|
|
273
|
+
name: true,
|
|
274
|
+
user: {
|
|
275
|
+
id: true,
|
|
276
|
+
name: true,
|
|
277
|
+
cars: {
|
|
278
|
+
id: true,
|
|
279
|
+
user: true,
|
|
280
|
+
name: true
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
where: {
|
|
286
|
+
id: insertedUser.id
|
|
287
|
+
},
|
|
288
|
+
include: {
|
|
289
|
+
cars: {
|
|
290
|
+
select: {
|
|
291
|
+
name: true,
|
|
292
|
+
id: true
|
|
293
|
+
},
|
|
294
|
+
where: {
|
|
295
|
+
name: 'My new car XXX'
|
|
296
|
+
},
|
|
297
|
+
include: {
|
|
298
|
+
user: {
|
|
299
|
+
include: {
|
|
300
|
+
cars: {
|
|
301
|
+
where: {
|
|
302
|
+
name: 'My new car XXXxX'
|
|
303
|
+
},
|
|
304
|
+
include: {
|
|
305
|
+
user: true
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
expect(searchUserWithRelations[0].id).toBe(insertedUser.id);
|
|
315
|
+
expect(searchUserWithRelations[0].name).toBe(insertedUser.name);
|
|
316
|
+
expect(searchUserWithRelations[0].cars?.length).toBe(0);
|
|
317
|
+
});
|
|
318
|
+
test('Include - Can filter and select specific keys', async () => {
|
|
319
|
+
const { insertedUser } = await insertRelatedData();
|
|
320
|
+
const searchUserWithRelations = await User.findMany({
|
|
321
|
+
select: {
|
|
322
|
+
id: true,
|
|
323
|
+
name: true,
|
|
324
|
+
age: true,
|
|
325
|
+
cars: {
|
|
326
|
+
name: true,
|
|
327
|
+
user: {
|
|
328
|
+
id: true,
|
|
329
|
+
name: true,
|
|
330
|
+
cars: {
|
|
331
|
+
id: true,
|
|
332
|
+
user: true,
|
|
333
|
+
name: true
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
},
|
|
338
|
+
where: {
|
|
339
|
+
id: insertedUser.id
|
|
340
|
+
},
|
|
341
|
+
include: {
|
|
342
|
+
cars: {
|
|
343
|
+
select: {
|
|
344
|
+
name: true,
|
|
345
|
+
id: true
|
|
346
|
+
},
|
|
347
|
+
where: {
|
|
348
|
+
name: 'My new car 4'
|
|
349
|
+
},
|
|
350
|
+
include: {
|
|
351
|
+
user: {
|
|
352
|
+
include: {
|
|
353
|
+
cars: {
|
|
354
|
+
where: {
|
|
355
|
+
name: 'My new car XXXxX'
|
|
356
|
+
},
|
|
357
|
+
include: {
|
|
358
|
+
user: true
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
expect(searchUserWithRelations[0]).toHaveProperty('age');
|
|
368
|
+
expect(searchUserWithRelations[0].age).toBe(insertedUser.age);
|
|
369
|
+
expect(searchUserWithRelations[0]).not.toHaveProperty('breed');
|
|
370
|
+
});
|
|
371
|
+
test('Include - Should filter keys from nested objects', async () => {
|
|
372
|
+
const { insertedUser } = await insertRelatedData();
|
|
373
|
+
const searchUserWithRelations = await User.findMany({
|
|
374
|
+
select: {
|
|
375
|
+
id: true,
|
|
376
|
+
name: true,
|
|
377
|
+
cars: {
|
|
378
|
+
name: true,
|
|
379
|
+
user: {
|
|
380
|
+
id: true,
|
|
381
|
+
name: true,
|
|
382
|
+
cars: {
|
|
383
|
+
id: true,
|
|
384
|
+
user: true,
|
|
385
|
+
name: true
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
},
|
|
390
|
+
where: {
|
|
391
|
+
id: insertedUser.id
|
|
392
|
+
},
|
|
393
|
+
include: {
|
|
394
|
+
cars: {
|
|
395
|
+
select: {
|
|
396
|
+
name: true
|
|
397
|
+
},
|
|
398
|
+
where: {
|
|
399
|
+
name: 'My new car 4'
|
|
400
|
+
},
|
|
401
|
+
include: {
|
|
402
|
+
user: {
|
|
403
|
+
select: {
|
|
404
|
+
id: true,
|
|
405
|
+
name: true
|
|
406
|
+
},
|
|
407
|
+
include: {
|
|
408
|
+
cars: {
|
|
409
|
+
where: {
|
|
410
|
+
name: 'My new car XXXxX'
|
|
411
|
+
},
|
|
412
|
+
include: {
|
|
413
|
+
user: true
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
expect(searchUserWithRelations[0].cars[0]).not.toHaveProperty('id');
|
|
423
|
+
expect(searchUserWithRelations[0].cars[0]).not.toHaveProperty('userId');
|
|
424
|
+
expect(searchUserWithRelations[0].cars[0]).toHaveProperty('user');
|
|
425
|
+
expect(searchUserWithRelations[0].cars[0].name).toBe('My new car 4');
|
|
426
|
+
expect(searchUserWithRelations[0].cars[0].user).not.toHaveProperty('age');
|
|
427
|
+
expect(searchUserWithRelations[0].cars[0].user).not.toHaveProperty('breed');
|
|
428
|
+
expect(searchUserWithRelations[0].cars[0].user?.id).toBe(insertedUser.id);
|
|
429
|
+
});
|
|
430
|
+
test('Include - Can select keys using main select or nested selects', async () => {
|
|
431
|
+
const { insertedUser } = await insertRelatedData();
|
|
432
|
+
await User.findMany({
|
|
433
|
+
select: {
|
|
434
|
+
id: true,
|
|
435
|
+
name: true,
|
|
436
|
+
cars: {
|
|
437
|
+
name: true,
|
|
438
|
+
user: {
|
|
439
|
+
id: true,
|
|
440
|
+
name: true,
|
|
441
|
+
cars: {
|
|
442
|
+
id: true,
|
|
443
|
+
user: true,
|
|
444
|
+
name: true
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
},
|
|
449
|
+
where: {
|
|
450
|
+
id: insertedUser.id
|
|
451
|
+
},
|
|
452
|
+
include: {
|
|
453
|
+
cars: {
|
|
454
|
+
select: {
|
|
455
|
+
name: true,
|
|
456
|
+
id: true
|
|
457
|
+
},
|
|
458
|
+
where: {
|
|
459
|
+
name: 'My new car 4'
|
|
460
|
+
},
|
|
461
|
+
include: {
|
|
462
|
+
user: {
|
|
463
|
+
include: {
|
|
464
|
+
cars: {
|
|
465
|
+
where: {
|
|
466
|
+
name: 'My new car XXXxX'
|
|
467
|
+
},
|
|
468
|
+
include: {
|
|
469
|
+
user: true
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
});
|
|
479
|
+
test('Include - (Many to Many) should display [] if not found', async () => {
|
|
480
|
+
const { insertedUser } = await insertRelatedData();
|
|
481
|
+
await User.findMany({
|
|
482
|
+
select: {
|
|
483
|
+
id: true,
|
|
484
|
+
name: true,
|
|
485
|
+
cars: {
|
|
486
|
+
name: true,
|
|
487
|
+
user: {
|
|
488
|
+
id: true,
|
|
489
|
+
name: true,
|
|
490
|
+
cars: {
|
|
491
|
+
id: true,
|
|
492
|
+
user: true,
|
|
493
|
+
name: true
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
},
|
|
498
|
+
where: {
|
|
499
|
+
id: insertedUser.id
|
|
500
|
+
},
|
|
501
|
+
include: {
|
|
502
|
+
cars: {
|
|
503
|
+
select: {
|
|
504
|
+
name: true,
|
|
505
|
+
id: true
|
|
506
|
+
},
|
|
507
|
+
where: {
|
|
508
|
+
name: 'My new car 4'
|
|
509
|
+
},
|
|
510
|
+
include: {
|
|
511
|
+
user: {
|
|
512
|
+
include: {
|
|
513
|
+
cars: {
|
|
514
|
+
where: {
|
|
515
|
+
name: 'My new car XXXxX'
|
|
516
|
+
},
|
|
517
|
+
include: {
|
|
518
|
+
user: true
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
});
|
|
528
|
+
test('Query related model - ManyToMany', async () => {
|
|
529
|
+
const insertedUser = await User.insert({
|
|
530
|
+
name: 'testUser',
|
|
531
|
+
age: 20
|
|
532
|
+
});
|
|
533
|
+
expect(typeof insertedUser.id).toBe('string');
|
|
534
|
+
const adminRole = await Role.insert({
|
|
535
|
+
name: 'Administrator'
|
|
536
|
+
});
|
|
537
|
+
const user = await User.loadById(insertedUser.id);
|
|
538
|
+
const attached = await user.roles().attach(adminRole.id);
|
|
539
|
+
expect(attached[0].userId).toBe(insertedUser.id);
|
|
540
|
+
expect(attached[0].roleId).toBe(adminRole.id);
|
|
541
|
+
const searchUserWithRelation = await User.findMany({
|
|
542
|
+
where: {
|
|
543
|
+
id: insertedUser.id
|
|
544
|
+
},
|
|
545
|
+
include: {
|
|
546
|
+
roles: {
|
|
547
|
+
withPivot: true
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
expect(Array.isArray(searchUserWithRelation[0].roles)).toBe(true);
|
|
552
|
+
expect(searchUserWithRelation[0].roles.length > 0).toBe(true);
|
|
553
|
+
expect(typeof searchUserWithRelation[0].roles[0].name).toBe('string');
|
|
554
|
+
expect(searchUserWithRelation[0].roles[0].id).toBe(adminRole.id);
|
|
555
|
+
expect(searchUserWithRelation[0].roles[0].pivot.id).toBe(attached[0].id);
|
|
556
|
+
const roles = await Role.findMany({
|
|
557
|
+
where: {
|
|
558
|
+
name: 'Administrator'
|
|
559
|
+
},
|
|
560
|
+
include: {
|
|
561
|
+
users: { withPivot: true }
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
expect(Array.isArray(roles)).toBe(true);
|
|
565
|
+
expect(roles.length > 0).toBe(true);
|
|
566
|
+
expect(typeof roles[0].users[0].name).toBe('string');
|
|
567
|
+
expect(typeof roles[0].users[0].age).toBe('number');
|
|
568
|
+
expect(roles[0].users[0].pivot).toBeDefined();
|
|
569
|
+
expect(roles[0].users[0].id).toBe(roles[0].users[0].pivot.userId);
|
|
570
|
+
expect(roles.some(r => {
|
|
571
|
+
return r.users.some(u => u.id === insertedUser.id);
|
|
572
|
+
})).toBe(true);
|
|
573
|
+
});
|
|
48
574
|
};
|
|
49
575
|
exports.relationsTestSuite = relationsTestSuite;
|