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