prisma-mock 0.4.1 → 0.5.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/lib/index.js +49 -22
- 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]?.filter(subitem => subitem[otherfield.type][idField] === item[idField]);
|
|
111
|
+
if (!items?.length) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
// Many-to-many
|
|
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
|
};
|
|
@@ -230,20 +249,28 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
230
249
|
});
|
|
231
250
|
const inverse = otherModel.fields.find((otherField) => field.relationName === otherField.relationName);
|
|
232
251
|
const targetKey = inverse.relationToFields[0];
|
|
233
|
-
const fromKey = inverse.relationFromFields[0];
|
|
234
252
|
const delegate = Delegate(getCamelCase(otherModel.name), otherModel);
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
253
|
+
if (!targetKey) {
|
|
254
|
+
const a = manyToManyData[field.relationName] = manyToManyData[field.relationName] || [];
|
|
255
|
+
a.push({
|
|
256
|
+
[field.type]: delegate.findOne({
|
|
257
|
+
where: connect
|
|
258
|
+
}),
|
|
259
|
+
[inverse.type]: d
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
delegate.update({
|
|
264
|
+
where: connect,
|
|
265
|
+
data: {
|
|
266
|
+
[getCamelCase(inverse.name)]: {
|
|
267
|
+
connect: {
|
|
268
|
+
[targetKey]: d[targetKey],
|
|
269
|
+
},
|
|
243
270
|
},
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
}
|
|
247
274
|
}
|
|
248
275
|
});
|
|
249
276
|
}
|
|
@@ -323,7 +350,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
323
350
|
const item = findOne(args);
|
|
324
351
|
delegate.update({
|
|
325
352
|
data: c.update,
|
|
326
|
-
where: getFieldRelationshipWhere(item, field),
|
|
353
|
+
where: getFieldRelationshipWhere(item, field, model),
|
|
327
354
|
});
|
|
328
355
|
}
|
|
329
356
|
}
|
|
@@ -487,13 +514,13 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
487
514
|
return getCamelCase(model.name) === childName;
|
|
488
515
|
});
|
|
489
516
|
const delegate = Delegate(getCamelCase(childName), submodel);
|
|
490
|
-
const res = delegate.
|
|
517
|
+
const res = delegate.findMany({
|
|
491
518
|
where: {
|
|
492
519
|
AND: [
|
|
493
520
|
childWhere,
|
|
494
|
-
getFieldRelationshipWhere(item, info)
|
|
521
|
+
getFieldRelationshipWhere(item, info, submodel)
|
|
495
522
|
]
|
|
496
|
-
}
|
|
523
|
+
}
|
|
497
524
|
});
|
|
498
525
|
if (filter.every) {
|
|
499
526
|
if (res.length === 0)
|
|
@@ -502,12 +529,12 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
502
529
|
// matchFnc(getFieldRelationshipWhere(item, info)),
|
|
503
530
|
// )
|
|
504
531
|
const all = delegate.findMany({
|
|
505
|
-
where: getFieldRelationshipWhere(item, info),
|
|
532
|
+
where: getFieldRelationshipWhere(item, info, model),
|
|
506
533
|
});
|
|
507
534
|
return res.length === all.length;
|
|
508
535
|
}
|
|
509
536
|
else if (filter.some) {
|
|
510
|
-
return res.length > 0;
|
|
537
|
+
return res.length > 0; //?
|
|
511
538
|
}
|
|
512
539
|
else if (filter.none) {
|
|
513
540
|
return res.length === 0;
|
|
@@ -775,7 +802,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
775
802
|
...subArgs,
|
|
776
803
|
where: {
|
|
777
804
|
...subArgs.where,
|
|
778
|
-
...getFieldRelationshipWhere(item, schema),
|
|
805
|
+
...getFieldRelationshipWhere(item, schema, model),
|
|
779
806
|
},
|
|
780
807
|
};
|
|
781
808
|
if (schema.isList) {
|