prisma-mock 0.4.1 → 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/lib/index.js +110 -51
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -60,6 +60,7 @@ const throwUnkownError = (message, cause) => {
|
|
|
60
60
|
const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel, client = (0, jest_mock_extended_1.mockDeep)(), options = {
|
|
61
61
|
caseInsensitive: false,
|
|
62
62
|
}) => {
|
|
63
|
+
const manyToManyData = {};
|
|
63
64
|
// let data = options.data || {}
|
|
64
65
|
// const datamodel = options.datamodel || Prisma.dmmf.datamodel
|
|
65
66
|
const caseInsensitive = options.caseInsensitive || false;
|
|
@@ -94,9 +95,27 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
94
95
|
}
|
|
95
96
|
return data;
|
|
96
97
|
};
|
|
97
|
-
const getFieldRelationshipWhere = (item, field) => {
|
|
98
|
-
if (field.
|
|
99
|
-
const
|
|
98
|
+
const getFieldRelationshipWhere = (item, field, model) => {
|
|
99
|
+
if (field.relationFromFields.length === 0) {
|
|
100
|
+
const joinmodel = datamodel.models.find((model) => {
|
|
101
|
+
return model.name === field.type;
|
|
102
|
+
});
|
|
103
|
+
const otherfield = joinmodel?.fields.find((f) => {
|
|
104
|
+
return f.relationName === field.relationName;
|
|
105
|
+
});
|
|
106
|
+
// Many-to-many
|
|
107
|
+
if (otherfield?.relationFromFields.length === 0) {
|
|
108
|
+
const idField = model?.fields.find((f) => f.isId)?.name;
|
|
109
|
+
const otherIdField = joinmodel?.fields.find((f) => f.isId);
|
|
110
|
+
const items = manyToManyData[field.relationName]
|
|
111
|
+
?.filter(subitem => subitem[otherfield.type]?.[idField] === item[idField]);
|
|
112
|
+
if (!items?.length) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
[otherIdField.name]: { in: items.map(subitem => (subitem[field.type][otherIdField.name])) }
|
|
117
|
+
};
|
|
118
|
+
}
|
|
100
119
|
return {
|
|
101
120
|
[otherfield.relationFromFields[0]]: item[otherfield.relationToFields[0]],
|
|
102
121
|
};
|
|
@@ -204,9 +223,9 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
204
223
|
const connections = connect instanceof Array ? connect : [connect];
|
|
205
224
|
connections.forEach((connect, idx) => {
|
|
206
225
|
const keyToMatch = Object.keys(connect)[0];
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
226
|
+
const keyToGet = field.relationToFields[0];
|
|
227
|
+
const targetKey = field.relationFromFields[0];
|
|
228
|
+
if (keyToGet && targetKey) {
|
|
210
229
|
let connectionValue = connect[keyToGet];
|
|
211
230
|
if (keyToMatch !== keyToGet) {
|
|
212
231
|
const valueToMatch = connect[keyToMatch];
|
|
@@ -218,32 +237,42 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
218
237
|
}
|
|
219
238
|
connectionValue = matchingRow[keyToGet];
|
|
220
239
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
240
|
+
if (targetKey) {
|
|
241
|
+
d = {
|
|
242
|
+
...rest,
|
|
243
|
+
[targetKey]: connectionValue,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
225
246
|
}
|
|
226
247
|
else {
|
|
227
248
|
d = rest;
|
|
228
249
|
const otherModel = datamodel.models.find((model) => {
|
|
229
250
|
return model.name === field.type;
|
|
230
251
|
});
|
|
231
|
-
const
|
|
232
|
-
const targetKey = inverse.relationToFields[0];
|
|
233
|
-
const fromKey = inverse.relationFromFields[0];
|
|
252
|
+
const otherField = otherModel.fields.find((otherField) => field.relationName === otherField.relationName);
|
|
234
253
|
const delegate = Delegate(getCamelCase(otherModel.name), otherModel);
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
254
|
+
if (!targetKey && !keyToGet) {
|
|
255
|
+
const targetKey = otherField.relationToFields[0];
|
|
256
|
+
delegate.update({
|
|
257
|
+
where: connect,
|
|
258
|
+
data: {
|
|
259
|
+
[getCamelCase(otherField.name)]: {
|
|
260
|
+
connect: {
|
|
261
|
+
[targetKey]: d[targetKey],
|
|
262
|
+
},
|
|
243
263
|
},
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
const a = manyToManyData[field.relationName] = manyToManyData[field.relationName] || [];
|
|
269
|
+
a.push({
|
|
270
|
+
[field.type]: delegate.findOne({
|
|
271
|
+
where: connect
|
|
272
|
+
}),
|
|
273
|
+
[otherField.type]: d
|
|
274
|
+
});
|
|
275
|
+
}
|
|
247
276
|
}
|
|
248
277
|
});
|
|
249
278
|
}
|
|
@@ -279,26 +308,37 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
279
308
|
}, {}),
|
|
280
309
|
},
|
|
281
310
|
});
|
|
311
|
+
let createdItems = [];
|
|
282
312
|
if (c.createMany) {
|
|
283
|
-
delegate.createMany({
|
|
313
|
+
createdItems = delegate.createMany({
|
|
284
314
|
...c.createMany,
|
|
285
315
|
data: c.createMany.data.map(map),
|
|
286
316
|
});
|
|
287
317
|
}
|
|
288
318
|
else {
|
|
289
319
|
if (Array.isArray(c.create)) {
|
|
290
|
-
delegate.createMany({
|
|
320
|
+
createdItems = delegate.createMany({
|
|
291
321
|
...c.create,
|
|
292
322
|
data: c.create.map(map),
|
|
293
323
|
});
|
|
294
324
|
}
|
|
295
325
|
else {
|
|
296
|
-
delegate.create({
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
326
|
+
createdItems = [delegate.create({
|
|
327
|
+
...create.create,
|
|
328
|
+
data: map(create.create),
|
|
329
|
+
})];
|
|
300
330
|
}
|
|
301
331
|
}
|
|
332
|
+
const targetKey = joinfield.relationFromFields[0];
|
|
333
|
+
if (!targetKey) {
|
|
334
|
+
const a = manyToManyData[field.relationName] = manyToManyData[field.relationName] || [];
|
|
335
|
+
createdItems.forEach((item) => {
|
|
336
|
+
a.push({
|
|
337
|
+
[field.type]: item,
|
|
338
|
+
[joinfield.type]: d
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
}
|
|
302
342
|
}
|
|
303
343
|
}
|
|
304
344
|
const name = getCamelCase(field.type);
|
|
@@ -321,10 +361,13 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
321
361
|
}
|
|
322
362
|
else {
|
|
323
363
|
const item = findOne(args);
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
364
|
+
const where = getFieldRelationshipWhere(item, field, model);
|
|
365
|
+
if (where) {
|
|
366
|
+
delegate.update({
|
|
367
|
+
data: c.update,
|
|
368
|
+
where,
|
|
369
|
+
});
|
|
370
|
+
}
|
|
328
371
|
}
|
|
329
372
|
}
|
|
330
373
|
if (c.deleteMany) {
|
|
@@ -487,13 +530,17 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
487
530
|
return getCamelCase(model.name) === childName;
|
|
488
531
|
});
|
|
489
532
|
const delegate = Delegate(getCamelCase(childName), submodel);
|
|
490
|
-
const
|
|
533
|
+
const joinWhere = getFieldRelationshipWhere(item, info, submodel);
|
|
534
|
+
if (!joinWhere) {
|
|
535
|
+
return false;
|
|
536
|
+
}
|
|
537
|
+
const res = delegate.findMany({
|
|
491
538
|
where: {
|
|
492
539
|
AND: [
|
|
493
540
|
childWhere,
|
|
494
|
-
|
|
541
|
+
joinWhere
|
|
495
542
|
]
|
|
496
|
-
}
|
|
543
|
+
}
|
|
497
544
|
});
|
|
498
545
|
if (filter.every) {
|
|
499
546
|
if (res.length === 0)
|
|
@@ -501,8 +548,11 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
501
548
|
// const all = data[childName].filter(
|
|
502
549
|
// matchFnc(getFieldRelationshipWhere(item, info)),
|
|
503
550
|
// )
|
|
551
|
+
const where = getFieldRelationshipWhere(item, info, model);
|
|
552
|
+
if (!where)
|
|
553
|
+
return false;
|
|
504
554
|
const all = delegate.findMany({
|
|
505
|
-
where
|
|
555
|
+
where,
|
|
506
556
|
});
|
|
507
557
|
return res.length === all.length;
|
|
508
558
|
}
|
|
@@ -771,24 +821,33 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
771
821
|
const delegate = Delegate(getCamelCase(schema.type), submodel);
|
|
772
822
|
// Construct arg for relation query
|
|
773
823
|
let subArgs = obj[key] === true ? {} : obj[key];
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
...subArgs
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
// Add relation
|
|
783
|
-
newItem = {
|
|
784
|
-
...newItem,
|
|
785
|
-
[key]: delegate._findMany(subArgs),
|
|
824
|
+
const joinWhere = getFieldRelationshipWhere(item, schema, model);
|
|
825
|
+
if (joinWhere) {
|
|
826
|
+
subArgs = {
|
|
827
|
+
...subArgs,
|
|
828
|
+
where: {
|
|
829
|
+
...subArgs.where,
|
|
830
|
+
...joinWhere,
|
|
831
|
+
},
|
|
786
832
|
};
|
|
833
|
+
if (schema.isList) {
|
|
834
|
+
// Add relation
|
|
835
|
+
newItem = {
|
|
836
|
+
...newItem,
|
|
837
|
+
[key]: delegate._findMany(subArgs),
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
else {
|
|
841
|
+
newItem = {
|
|
842
|
+
...newItem,
|
|
843
|
+
[key]: delegate._findMany(subArgs)?.[0] || null,
|
|
844
|
+
};
|
|
845
|
+
}
|
|
787
846
|
}
|
|
788
847
|
else {
|
|
789
848
|
newItem = {
|
|
790
849
|
...newItem,
|
|
791
|
-
[key]:
|
|
850
|
+
[key]: [],
|
|
792
851
|
};
|
|
793
852
|
}
|
|
794
853
|
});
|