prisma-mock 0.0.12 → 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 (2) hide show
  1. package/lib/index.js +26 -11
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -265,7 +265,11 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
265
265
  const findOne = args => {
266
266
  if (!data[prop])
267
267
  throw new Error(`${prop} not found in data`);
268
- return includes(args)(data[prop].find(matchFnc(args.where)));
268
+ const items = findMany(args);
269
+ if (items.length === 0) {
270
+ return null;
271
+ }
272
+ return items[0];
269
273
  };
270
274
  const findMany = args => {
271
275
  const res = data[prop].filter(matchFnc(args === null || args === void 0 ? void 0 : args.where)).map(includes(args));
@@ -286,14 +290,20 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
286
290
  return shouldInclude;
287
291
  });
288
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
+ }
289
300
  return res;
290
301
  };
291
- const findFirst = args => {
292
- const item = data[prop].find(matchFnc(args === null || args === void 0 ? void 0 : args.where));
293
- if (item)
294
- return includes(args)(item);
295
- return null;
296
- };
302
+ // const findFirst = args => {
303
+ // const item = data[prop].find(matchFnc(args?.where));
304
+ // if (item) return includes(args)(item);
305
+ // return null;
306
+ // };
297
307
  const updateMany = args => {
298
308
  // if (!Array.isArray(data[prop])) {
299
309
  // throw new Error(`${prop} not found in data`)
@@ -318,10 +328,11 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
318
328
  data = Object.assign(Object.assign({}, data), { [prop]: data[prop].filter(e => !matchFnc(args === null || args === void 0 ? void 0 : args.where)(e)) });
319
329
  };
320
330
  const includes = args => item => {
321
- 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)
322
332
  return item;
323
333
  let newItem = item;
324
- 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);
325
336
  keys.forEach(key => {
326
337
  // Get field schema for relation info
327
338
  const model = cachedSchema.datamodel.models.find(model => {
@@ -330,10 +341,14 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
330
341
  const schema = model.fields.find(field => {
331
342
  return field.name === key;
332
343
  });
344
+ switch (schema.type) {
345
+ case "Int":
346
+ return;
347
+ }
333
348
  // Get delegate for relation
334
349
  const delegate = Delegate(getCamelCase(schema.type), model);
335
350
  // Construct arg for relation query
336
- let subArgs = args.include[key] === true ? {} : args.include[key];
351
+ let subArgs = obj[key] === true ? {} : obj[key];
337
352
  subArgs = Object.assign(Object.assign({}, subArgs), { where: Object.assign(Object.assign({}, subArgs.where), getFieldRelationshipWhere(item, schema)) });
338
353
  if (schema.isList) {
339
354
  // Add relation
@@ -349,7 +364,7 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
349
364
  findOne,
350
365
  findUnique: findOne,
351
366
  findMany,
352
- findFirst,
367
+ findFirst: findOne,
353
368
  create,
354
369
  createMany: (args) => {
355
370
  args.data.forEach((data) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-mock",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "Mock prisma for unit testing database",
5
5
  "main": "lib/index.js",
6
6
  "license": "MIT",