prisma-mock 0.0.10 → 0.0.13

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 (3) hide show
  1. package/README.md +1 -0
  2. package/lib/index.js +118 -82
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -0,0 +1 @@
1
+
package/lib/index.js CHANGED
@@ -79,7 +79,6 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
79
79
  return getCamelCase(model.name) === prop;
80
80
  });
81
81
  model.fields.forEach(field => {
82
- d; //?
83
82
  if (d[field.name] && field.kind === 'object') {
84
83
  const c = d[field.name];
85
84
  if (c.connect) {
@@ -166,87 +165,114 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
166
165
  });
167
166
  return d;
168
167
  };
169
- const matchFnc = args => item => {
168
+ const matchItem = (child, item, where) => {
170
169
  var _a;
171
- if (args === null || args === void 0 ? void 0 : args.where) {
172
- for (let child in args === null || args === void 0 ? void 0 : args.where) {
173
- const val = item[child];
174
- const filter = args.where[child];
175
- if (filter instanceof Date) {
176
- if (val === undefined) {
177
- return false;
178
- }
179
- if (!(val instanceof Date) || val.getTime() !== filter.getTime()) {
180
- return false;
181
- }
170
+ const val = item[child];
171
+ const filter = where[child];
172
+ if (child === "OR") {
173
+ return matchOr(item, filter);
174
+ }
175
+ if (child === "AND") {
176
+ return matchAnd(item, filter);
177
+ }
178
+ if (child === "NOT") {
179
+ return !matchOr(item, filter);
180
+ }
181
+ if (filter instanceof Date) {
182
+ if (val === undefined) {
183
+ return false;
184
+ }
185
+ if (!(val instanceof Date) || val.getTime() !== filter.getTime()) {
186
+ return false;
187
+ }
188
+ }
189
+ else {
190
+ if (typeof filter === 'object') {
191
+ const info = model.fields.find(field => field.name === child);
192
+ if (info === null || info === void 0 ? void 0 : info.relationName) {
193
+ const childName = getCamelCase(info.type);
194
+ const res = data[childName].filter(matchFnc(Object.assign(Object.assign({}, filter), getFieldRelationshipWhere(item, info))));
195
+ return res.length > 0;
182
196
  }
183
- else {
184
- if (typeof filter === 'object') {
185
- const info = model.fields.find(field => field.name === child);
186
- if (info === null || info === void 0 ? void 0 : info.relationName) {
187
- const childName = getCamelCase(info.type);
188
- const res = data[childName].filter(matchFnc({
189
- where: Object.assign(Object.assign({}, filter), getFieldRelationshipWhere(item, info)),
190
- }));
191
- return res.length > 0;
192
- }
193
- const idFields = model.idFields || ((_a = model.primaryKey) === null || _a === void 0 ? void 0 : _a.fields);
194
- if ((idFields === null || idFields === void 0 ? void 0 : idFields.length) > 1) {
195
- if (child === idFields.join('_')) {
196
- return shallowCompare(val, filter);
197
- }
198
- }
199
- if (val === undefined) {
200
- return false;
201
- }
202
- let match = true;
203
- if ('equals' in filter && match) {
204
- match = filter.equals === val;
205
- }
206
- if ('startsWith' in filter && match) {
207
- match = val.indexOf(filter.startsWith) === 0;
208
- }
209
- if ('endsWith' in filter && match) {
210
- match =
211
- val.indexOf(filter.endsWith) ===
212
- val.length - filter.endsWith.length;
213
- }
214
- if ('contains' in filter && match) {
215
- match = val.indexOf(filter.contains) > -1;
216
- }
217
- if ('gt' in filter && match) {
218
- match = val > filter.gt;
219
- }
220
- if ('gte' in filter && match) {
221
- match = val >= filter.gte;
222
- }
223
- if ('lt' in filter && match) {
224
- match = val < filter.lt;
225
- }
226
- if ('lte' in filter && match) {
227
- match = val <= filter.lte;
228
- }
229
- if ('in' in filter && match) {
230
- match = filter.in.includes(val);
231
- }
232
- if (!match)
233
- return false;
234
- }
235
- else if (val !== filter) {
236
- return false;
197
+ const idFields = model.idFields || ((_a = model.primaryKey) === null || _a === void 0 ? void 0 : _a.fields);
198
+ if ((idFields === null || idFields === void 0 ? void 0 : idFields.length) > 1) {
199
+ if (child === idFields.join('_')) {
200
+ return shallowCompare(val, filter);
237
201
  }
238
202
  }
203
+ if (val === undefined) {
204
+ return false;
205
+ }
206
+ let match = true;
207
+ if ('equals' in filter && match) {
208
+ match = filter.equals === val;
209
+ }
210
+ if ('startsWith' in filter && match) {
211
+ match = val.indexOf(filter.startsWith) === 0;
212
+ }
213
+ if ('endsWith' in filter && match) {
214
+ match =
215
+ val.indexOf(filter.endsWith) ===
216
+ val.length - filter.endsWith.length;
217
+ }
218
+ if ('contains' in filter && match) {
219
+ match = val.indexOf(filter.contains) > -1;
220
+ }
221
+ if ('gt' in filter && match) {
222
+ match = val > filter.gt;
223
+ }
224
+ if ('gte' in filter && match) {
225
+ match = val >= filter.gte;
226
+ }
227
+ if ('lt' in filter && match) {
228
+ match = val < filter.lt;
229
+ }
230
+ if ('lte' in filter && match) {
231
+ match = val <= filter.lte;
232
+ }
233
+ if ('in' in filter && match) {
234
+ match = filter.in.includes(val);
235
+ }
236
+ if (!match)
237
+ return false;
238
+ }
239
+ else if (val !== filter) {
240
+ return false;
241
+ }
242
+ }
243
+ return true;
244
+ };
245
+ const matchItems = (item, where) => {
246
+ for (let child in where) {
247
+ if (!matchItem(child, item, where)) {
248
+ return false;
239
249
  }
240
250
  }
241
251
  return true;
242
252
  };
253
+ const matchAnd = (item, where) => {
254
+ return where.filter((child) => matchItems(item, child)).length > 0;
255
+ };
256
+ const matchOr = (item, where) => {
257
+ return where.some((child) => matchItems(item, child));
258
+ };
259
+ const matchFnc = where => item => {
260
+ if (where) {
261
+ return matchItems(item, where);
262
+ }
263
+ return true;
264
+ };
243
265
  const findOne = args => {
244
266
  if (!data[prop])
245
267
  throw new Error(`${prop} not found in data`);
246
- return includes(args)(data[prop].find(matchFnc(args)));
268
+ const items = findMany(args);
269
+ if (items.length === 0) {
270
+ return null;
271
+ }
272
+ return items[0];
247
273
  };
248
274
  const findMany = args => {
249
- const res = data[prop].filter(matchFnc(args)).map(includes(args));
275
+ const res = data[prop].filter(matchFnc(args === null || args === void 0 ? void 0 : args.where)).map(includes(args));
250
276
  if (args === null || args === void 0 ? void 0 : args.distinct) {
251
277
  let values = {};
252
278
  return res.filter(item => {
@@ -264,20 +290,26 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
264
290
  return shouldInclude;
265
291
  });
266
292
  }
293
+ if (args === null || args === void 0 ? void 0 : args.select) {
294
+ return res.map(item => {
295
+ const newItem = {};
296
+ Object.keys(args.select).forEach(key => newItem[key] = item[key]);
297
+ return newItem;
298
+ });
299
+ }
267
300
  return res;
268
301
  };
269
- const findFirst = args => {
270
- const item = data[prop].find(matchFnc(args));
271
- if (item)
272
- return includes(args)(item);
273
- return null;
274
- };
302
+ // const findFirst = args => {
303
+ // const item = data[prop].find(matchFnc(args?.where));
304
+ // if (item) return includes(args)(item);
305
+ // return null;
306
+ // };
275
307
  const updateMany = args => {
276
308
  // if (!Array.isArray(data[prop])) {
277
309
  // throw new Error(`${prop} not found in data`)
278
310
  // }
279
311
  const newItems = data[prop].map(e => {
280
- if (matchFnc(args)(e)) {
312
+ if (matchFnc(args.where)(e)) {
281
313
  let data = nestedUpdate(args, false);
282
314
  return Object.assign(Object.assign({}, e), data);
283
315
  }
@@ -289,18 +321,18 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
289
321
  const create = args => {
290
322
  const d = nestedUpdate(args, true);
291
323
  data = Object.assign(Object.assign({}, data), { [prop]: [...data[prop], d] });
292
- data; //?
293
324
  data = checkIds(model, data);
294
325
  return findOne(Object.assign({ where: { id: d.id } }, args));
295
326
  };
296
327
  const deleteMany = args => {
297
- data = Object.assign(Object.assign({}, data), { [prop]: data[prop].filter(e => !matchFnc(args)(e)) });
328
+ data = Object.assign(Object.assign({}, data), { [prop]: data[prop].filter(e => !matchFnc(args === null || args === void 0 ? void 0 : args.where)(e)) });
298
329
  };
299
330
  const includes = args => item => {
300
- if (!(args === null || args === void 0 ? void 0 : args.include) || !item)
331
+ if ((!(args === null || args === void 0 ? void 0 : args.include) && !(args === null || args === void 0 ? void 0 : args.select)) || !item)
301
332
  return item;
302
333
  let newItem = item;
303
- const keys = Object.keys(args.include);
334
+ const obj = (args === null || args === void 0 ? void 0 : args.select) || (args === null || args === void 0 ? void 0 : args.include);
335
+ const keys = Object.keys(obj);
304
336
  keys.forEach(key => {
305
337
  // Get field schema for relation info
306
338
  const model = cachedSchema.datamodel.models.find(model => {
@@ -309,10 +341,14 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
309
341
  const schema = model.fields.find(field => {
310
342
  return field.name === key;
311
343
  });
344
+ switch (schema.type) {
345
+ case "Int":
346
+ return;
347
+ }
312
348
  // Get delegate for relation
313
349
  const delegate = Delegate(getCamelCase(schema.type), model);
314
350
  // Construct arg for relation query
315
- let subArgs = args.include[key] === true ? {} : args.include[key];
351
+ let subArgs = obj[key] === true ? {} : obj[key];
316
352
  subArgs = Object.assign(Object.assign({}, subArgs), { where: Object.assign(Object.assign({}, subArgs.where), getFieldRelationshipWhere(item, schema)) });
317
353
  if (schema.isList) {
318
354
  // Add relation
@@ -328,7 +364,7 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
328
364
  findOne,
329
365
  findUnique: findOne,
330
366
  findMany,
331
- findFirst,
367
+ findFirst: findOne,
332
368
  create,
333
369
  createMany: (args) => {
334
370
  args.data.forEach((data) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-mock",
3
- "version": "0.0.10",
3
+ "version": "0.0.13",
4
4
  "description": "Mock prisma for unit testing database",
5
5
  "main": "lib/index.js",
6
6
  "license": "MIT",
@@ -21,7 +21,7 @@
21
21
  "typescript": "^4.4.4"
22
22
  },
23
23
  "scripts": {
24
- "prepublish": "tsc",
24
+ "preversion": "tsc",
25
25
  "build": "tsc",
26
26
  "watch": "tsc --watch"
27
27
  },