prisma-mock 0.10.2 → 0.10.3

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 +45 -24
  2. package/package.json +3 -3
package/lib/index.js CHANGED
@@ -36,28 +36,39 @@ const getNestedValue_1 = __importDefault(require("./utils/getNestedValue"));
36
36
  function IsFieldDefault(f) {
37
37
  return f.name !== undefined;
38
38
  }
39
- const throwKnownError = (message, { code = "P2025", meta } = {}) => {
39
+ function isDefinedWithValue(v, key) {
40
+ return v[key] !== undefined;
41
+ }
42
+ const throwPrismaError = (message, { code = "P2025", meta } = {}, errorClass = client_1.Prisma.PrismaClientKnownRequestError) => {
40
43
  const clientVersion = client_1.Prisma.prismaVersion.client;
41
44
  // PrismaClientKnownRequestError prototype changed in version 4.7.0
42
45
  // from: constructor(message: string, code: string, clientVersion: string, meta?: any)
43
46
  // to: constructor(message: string, { code, clientVersion, meta, batchRequestIdx }: KnownErrorParams)
44
47
  let error;
45
- if (client_1.Prisma.PrismaClientKnownRequestError.length === 2) {
48
+ if (errorClass.length === 2) {
46
49
  // @ts-ignore
47
- error = new client_1.Prisma.PrismaClientKnownRequestError(message, {
50
+ error = new errorClass(message, {
48
51
  code,
49
52
  clientVersion,
50
53
  });
51
54
  }
52
55
  else {
53
56
  // @ts-ignore
54
- error = new client_1.Prisma.PrismaClientKnownRequestError(message, code,
57
+ error = new errorClass(message,
58
+ // @ts-ignore
59
+ code,
55
60
  // @ts-ignore
56
61
  clientVersion);
57
62
  }
58
63
  error.meta = meta;
59
64
  throw error;
60
65
  };
66
+ const throwKnownError = (message, { code = "P2025", meta } = {}) => {
67
+ throwPrismaError(message, { code, meta }, client_1.Prisma.PrismaClientKnownRequestError);
68
+ };
69
+ const throwValidationError = (message, { code = "P2025", meta } = {}) => {
70
+ throwPrismaError(message, { code, meta }, client_1.Prisma.PrismaClientValidationError);
71
+ };
61
72
  const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel, client = (0, jest_mock_extended_1.mockDeep)(), options = {
62
73
  caseInsensitive: false,
63
74
  }) => {
@@ -72,6 +83,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
72
83
  };
73
84
  const removeMultiFieldIds = (model, data) => {
74
85
  const c = getCamelCase(model.name);
86
+ // @ts-ignore
75
87
  const idFields = model.idFields || model.primaryKey?.fields;
76
88
  const removeId = (ids) => {
77
89
  const id = ids.join("_");
@@ -174,7 +186,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
174
186
  }
175
187
  const keys = Object.keys(orderBy);
176
188
  if (keys.length > 1) {
177
- throw new client_1.Prisma.PrismaClientValidationError(`Argument orderBy of needs exactly one argument, but you provided ${keys.join(" and ")}. Please choose one.`);
189
+ throwValidationError(`Argument orderBy of needs exactly one argument, but you provided ${keys.join(" and ")}. Please choose one.`);
178
190
  }
179
191
  const incl = includes({
180
192
  include: keys.reduce((acc, key) => ({ ...acc, [key]: true }), {}),
@@ -351,21 +363,26 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
351
363
  };
352
364
  }
353
365
  else {
354
- const map = (val) => ({
355
- ...val,
356
- [joinfield.name]: {
357
- connect: joinfield.relationToFields.reduce((prev, cur, index) => {
358
- let val = d[cur];
359
- if (!isCreating && !val) {
360
- val = findOne(args)[cur];
361
- }
362
- return {
363
- ...prev,
364
- [cur]: val,
365
- };
366
- }, {}),
367
- },
368
- });
366
+ const map = (val) => {
367
+ if (joinfield.relationToFields.length === 0) {
368
+ return val;
369
+ }
370
+ return ({
371
+ ...val,
372
+ [joinfield.name]: {
373
+ connect: joinfield.relationToFields.reduce((prev, cur, index) => {
374
+ let val = d[cur];
375
+ if (!isCreating && !val) {
376
+ val = findOne(args)[cur];
377
+ }
378
+ return {
379
+ ...prev,
380
+ [cur]: val,
381
+ };
382
+ }, {}),
383
+ },
384
+ });
385
+ };
369
386
  let createdItems = [];
370
387
  if (c.createMany) {
371
388
  createdItems = delegate._createMany({
@@ -624,6 +641,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
624
641
  }
625
642
  return res.length > 0;
626
643
  }
644
+ // @ts-ignore Backwards compatibility
627
645
  const idFields = model.idFields || model.primaryKey?.fields;
628
646
  if (idFields?.length > 1) {
629
647
  if (child === idFields.join("_")) {
@@ -718,16 +736,16 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
718
736
  if ("contains" in matchFilter && match) {
719
737
  match = val.indexOf(matchFilter.contains) > -1;
720
738
  }
721
- if ("gt" in matchFilter && match) {
739
+ if (isDefinedWithValue(matchFilter, "gt") && match) {
722
740
  match = val > matchFilter.gt;
723
741
  }
724
- if ("gte" in matchFilter && match) {
742
+ if (isDefinedWithValue(matchFilter, "gte") && match) {
725
743
  match = val >= matchFilter.gte;
726
744
  }
727
- if ("lt" in matchFilter && match) {
745
+ if (isDefinedWithValue(matchFilter, "lt") && match) {
728
746
  match = val < matchFilter.lt;
729
747
  }
730
- if ("lte" in matchFilter && match) {
748
+ if (isDefinedWithValue(matchFilter, "lte") && match) {
731
749
  match = val <= matchFilter.lte;
732
750
  }
733
751
  if ("in" in matchFilter && match) {
@@ -989,6 +1007,9 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
989
1007
  const model = datamodel.models.find((model) => {
990
1008
  return getCamelCase(model.name) === prop;
991
1009
  });
1010
+ if (!obj[key]) {
1011
+ return;
1012
+ }
992
1013
  if (key === "_count") {
993
1014
  const select = obj[key]?.select;
994
1015
  const subkeys = Object.keys(select);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-mock",
3
- "version": "0.10.2",
3
+ "version": "0.10.3",
4
4
  "description": "Mock prisma for unit testing database",
5
5
  "main": "lib/index.js",
6
6
  "repository": {
@@ -13,12 +13,12 @@
13
13
  "lib/"
14
14
  ],
15
15
  "devDependencies": {
16
- "@prisma/client": "4.7.1",
16
+ "@prisma/client": "5.13.0",
17
17
  "@types/jest": "^27.0.2",
18
18
  "cross-spawn": "^7.0.3",
19
19
  "env-cmd": "^10.1.0",
20
20
  "jest": "^27.3.1",
21
- "prisma": "4.7.1",
21
+ "prisma": "5.13.0",
22
22
  "ts-jest": "^27.0.7",
23
23
  "typescript": "^4.4.4",
24
24
  "uuid": "^9.0.0"