prisma-mock 0.0.20 → 0.0.21

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.
Files changed (2) hide show
  1. package/lib/index.js +40 -2
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -71,7 +71,7 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
71
71
  const joinmodel = cachedSchema.datamodel.models.find(model => {
72
72
  return model.name === field.type;
73
73
  });
74
- const joinfield = joinmodel.fields.find(f => {
74
+ const joinfield = joinmodel === null || joinmodel === void 0 ? void 0 : joinmodel.fields.find(f => {
75
75
  return f.relationName === field.relationName;
76
76
  });
77
77
  return joinfield;
@@ -399,7 +399,45 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
399
399
  return findOne(Object.assign({ where: { id: d.id } }, args));
400
400
  };
401
401
  const deleteMany = args => {
402
- data = Object.assign(Object.assign({}, data), { [prop]: data[prop].filter(e => !matchFnc(args === null || args === void 0 ? void 0 : args.where)(e)) });
402
+ const relations = [];
403
+ const model = cachedSchema.datamodel.models.find(model => {
404
+ return getCamelCase(model.name) === prop;
405
+ });
406
+ const deleted = [];
407
+ data = Object.assign(Object.assign({}, data), { [prop]: data[prop].filter(e => {
408
+ const shouldDelete = matchFnc(args === null || args === void 0 ? void 0 : args.where)(e);
409
+ if (shouldDelete) {
410
+ deleted.push(e);
411
+ }
412
+ return !shouldDelete;
413
+ }) });
414
+ // Referential Actions
415
+ deleted.forEach(item => {
416
+ model.fields.forEach(field => {
417
+ const joinfield = getJoinField(field);
418
+ if (!joinfield)
419
+ return;
420
+ const delegate = Delegate(getCamelCase(field.type), model);
421
+ if (joinfield.relationOnDelete === "SetNull") {
422
+ delegate.update({
423
+ where: {
424
+ [joinfield.relationFromFields[0]]: item[joinfield.relationToFields[0]],
425
+ },
426
+ data: {
427
+ [joinfield.relationFromFields[0]]: null,
428
+ }
429
+ });
430
+ }
431
+ else if (joinfield.relationOnDelete === "Cascade") {
432
+ delegate.delete({
433
+ where: {
434
+ [joinfield.relationFromFields[0]]: item[joinfield.relationToFields[0]],
435
+ }
436
+ });
437
+ }
438
+ });
439
+ });
440
+ return deleted;
403
441
  };
404
442
  const includes = args => item => {
405
443
  if ((!(args === null || args === void 0 ? void 0 : args.include) && !(args === null || args === void 0 ? void 0 : args.select)) || !item)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-mock",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "description": "Mock prisma for unit testing database",
5
5
  "main": "lib/index.js",
6
6
  "license": "MIT",