prisma-mock 0.1.6 → 0.1.8

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 +30 -4
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -78,21 +78,47 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
78
78
  });
79
79
  const Delegate = (prop, model) => {
80
80
  const sortFunc = (orderBy) => (a, b) => {
81
+ if (Array.isArray(orderBy)) {
82
+ for (const order of orderBy) {
83
+ const res = sortFunc(order)(a, b);
84
+ if (res !== 0) {
85
+ return res;
86
+ }
87
+ }
88
+ return 0;
89
+ }
81
90
  const keys = Object.keys(orderBy);
91
+ if (keys.length > 1) {
92
+ throw new runtime_1.PrismaClientValidationError(`Argument orderBy of needs exactly one argument, but you provided ${keys.join(' and ')}. Please choose one.`);
93
+ }
82
94
  const incl = includes({ include: keys.reduce((acc, key) => (Object.assign(Object.assign({}, acc), { [key]: true })), {}) });
83
95
  for (const key of keys) {
84
96
  const dir = orderBy[key];
85
97
  if (typeof dir === "object") {
98
+ const schema = model.fields.find(field => {
99
+ return field.name === key;
100
+ });
101
+ if (!schema) {
102
+ return 0;
103
+ }
86
104
  const submodel = datamodel.models.find(model => {
87
- return getCamelCase(model.name) === key;
105
+ return model.name === schema.type;
88
106
  });
89
- const delegate = Delegate(key, submodel);
90
- const res = delegate._sortFunc(dir)(incl(a)[key], incl(b)[key]);
107
+ const delegate = Delegate(getCamelCase(schema.type), submodel);
108
+ const valA = incl(a);
109
+ const valB = incl(b);
110
+ if (!valB || !valB[key]) {
111
+ return 0;
112
+ }
113
+ if (!valA || !valA[key]) {
114
+ return 0;
115
+ }
116
+ const res = delegate._sortFunc(dir)(valA[key], valB[key]);
91
117
  if (res !== 0) {
92
118
  return res;
93
119
  }
94
120
  }
95
- else {
121
+ else if (!!a && !!b) {
96
122
  if (a[key] > b[key]) {
97
123
  return dir === "asc" ? 1 : -1;
98
124
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-mock",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Mock prisma for unit testing database",
5
5
  "main": "lib/index.js",
6
6
  "repository": "https://github.com/demonsters/prisma-mock",