prisma-mock 0.0.16 → 0.0.19
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.
- package/lib/index.js +51 -19
- package/package.json +7 -5
package/lib/index.js
CHANGED
|
@@ -40,16 +40,25 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
40
40
|
if ((idFields === null || idFields === void 0 ? void 0 : idFields.length) > 1) {
|
|
41
41
|
const id = idFields.join('_');
|
|
42
42
|
data = Object.assign(Object.assign({}, data), { [c]: data[c].map(item => {
|
|
43
|
-
|
|
43
|
+
const _a = item, _b = id, idVal = _a[_b], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
|
|
44
|
+
return Object.assign(Object.assign(Object.assign({}, idVal), { [id]: idFields.reduce((prev, field) => {
|
|
44
45
|
return Object.assign(Object.assign({}, prev), { [field]: item[field] });
|
|
45
|
-
}, {}) });
|
|
46
|
+
}, {}) }), item);
|
|
46
47
|
}) });
|
|
47
48
|
}
|
|
48
49
|
return data;
|
|
49
50
|
};
|
|
50
|
-
const getFieldRelationshipWhere = (item, field) =>
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
const getFieldRelationshipWhere = (item, field) => {
|
|
52
|
+
if (field.relationToFields.length === 0) {
|
|
53
|
+
field = getJoinField(field);
|
|
54
|
+
return {
|
|
55
|
+
[field.relationFromFields[0]]: item[field.relationToFields[0]],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return ({
|
|
59
|
+
[field.relationToFields[0]]: item[field.relationFromFields[0]],
|
|
60
|
+
});
|
|
61
|
+
};
|
|
53
62
|
const getJoinField = (field) => {
|
|
54
63
|
const joinmodel = cachedSchema.datamodel.models.find(model => {
|
|
55
64
|
return model.name === field.type;
|
|
@@ -197,7 +206,7 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
197
206
|
}
|
|
198
207
|
if (!filter) {
|
|
199
208
|
if (filter === null) {
|
|
200
|
-
return
|
|
209
|
+
return val === null || val === undefined;
|
|
201
210
|
}
|
|
202
211
|
return true;
|
|
203
212
|
}
|
|
@@ -214,7 +223,32 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
214
223
|
const info = model.fields.find(field => field.name === child);
|
|
215
224
|
if (info === null || info === void 0 ? void 0 : info.relationName) {
|
|
216
225
|
const childName = getCamelCase(info.type);
|
|
217
|
-
|
|
226
|
+
let childWhere = {};
|
|
227
|
+
if (filter.every) {
|
|
228
|
+
childWhere = filter.every;
|
|
229
|
+
}
|
|
230
|
+
else if (filter.some) {
|
|
231
|
+
childWhere = filter.some;
|
|
232
|
+
}
|
|
233
|
+
else if (filter.none) {
|
|
234
|
+
childWhere = filter.none;
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
childWhere = filter;
|
|
238
|
+
}
|
|
239
|
+
const res = data[childName].filter(matchFnc(Object.assign(Object.assign({}, childWhere), getFieldRelationshipWhere(item, info))));
|
|
240
|
+
if (filter.every) {
|
|
241
|
+
if (res.length === 0)
|
|
242
|
+
return false;
|
|
243
|
+
const all = data[childName].filter(matchFnc(getFieldRelationshipWhere(item, info)));
|
|
244
|
+
return res.length === all.length;
|
|
245
|
+
}
|
|
246
|
+
else if (filter.some) {
|
|
247
|
+
return res.length > 0;
|
|
248
|
+
}
|
|
249
|
+
else if (filter.none) {
|
|
250
|
+
return res.length === 0;
|
|
251
|
+
}
|
|
218
252
|
return res.length > 0;
|
|
219
253
|
}
|
|
220
254
|
const idFields = model.idFields || ((_a = model.primaryKey) === null || _a === void 0 ? void 0 : _a.fields);
|
|
@@ -293,7 +327,7 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
293
327
|
};
|
|
294
328
|
const findOne = args => {
|
|
295
329
|
if (!data[prop])
|
|
296
|
-
|
|
330
|
+
return null;
|
|
297
331
|
const items = findMany(args);
|
|
298
332
|
if (items.length === 0) {
|
|
299
333
|
return null;
|
|
@@ -328,11 +362,6 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
328
362
|
}
|
|
329
363
|
return res;
|
|
330
364
|
};
|
|
331
|
-
// const findFirst = args => {
|
|
332
|
-
// const item = data[prop].find(matchFnc(args?.where));
|
|
333
|
-
// if (item) return includes(args)(item);
|
|
334
|
-
// return null;
|
|
335
|
-
// };
|
|
336
365
|
const updateMany = args => {
|
|
337
366
|
// if (!Array.isArray(data[prop])) {
|
|
338
367
|
// throw new Error(`${prop} not found in data`)
|
|
@@ -351,6 +380,7 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
351
380
|
const d = nestedUpdate(args, true, null);
|
|
352
381
|
data = Object.assign(Object.assign({}, data), { [prop]: [...data[prop], d] });
|
|
353
382
|
data = checkIds(model, data);
|
|
383
|
+
// TODO: Grouped-ids
|
|
354
384
|
return findOne(Object.assign({ where: { id: d.id } }, args));
|
|
355
385
|
};
|
|
356
386
|
const deleteMany = args => {
|
|
@@ -388,6 +418,10 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
388
418
|
});
|
|
389
419
|
return newItem;
|
|
390
420
|
};
|
|
421
|
+
const update = (args) => {
|
|
422
|
+
updateMany(args);
|
|
423
|
+
return findOne(args);
|
|
424
|
+
};
|
|
391
425
|
return {
|
|
392
426
|
findOne,
|
|
393
427
|
findUnique: findOne,
|
|
@@ -401,10 +435,7 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
401
435
|
return findMany(args);
|
|
402
436
|
},
|
|
403
437
|
delete: deleteMany,
|
|
404
|
-
update
|
|
405
|
-
updateMany(args);
|
|
406
|
-
return findOne(args);
|
|
407
|
-
},
|
|
438
|
+
update,
|
|
408
439
|
deleteMany,
|
|
409
440
|
updateMany: (args) => {
|
|
410
441
|
updateMany(args);
|
|
@@ -413,10 +444,11 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
413
444
|
upsert(args) {
|
|
414
445
|
const res = findOne(args);
|
|
415
446
|
if (res) {
|
|
416
|
-
|
|
447
|
+
return update(Object.assign(Object.assign({}, args), { data: args.update }));
|
|
417
448
|
}
|
|
418
449
|
else {
|
|
419
|
-
create(Object.assign(Object.assign({}, args), { data: args.create }));
|
|
450
|
+
create(Object.assign(Object.assign({}, args), { data: Object.assign(Object.assign({}, args.where), args.create) }));
|
|
451
|
+
return findOne(args);
|
|
420
452
|
}
|
|
421
453
|
},
|
|
422
454
|
count(args) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prisma-mock",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
4
4
|
"description": "Mock prisma for unit testing database",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"license": "MIT",
|
|
@@ -9,21 +9,23 @@
|
|
|
9
9
|
"lib/"
|
|
10
10
|
],
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@prisma/
|
|
13
|
-
"@prisma/
|
|
14
|
-
"prisma": "^3.
|
|
12
|
+
"@prisma/client": "3.10.0",
|
|
13
|
+
"@prisma/generator-helper": "^3.10.0",
|
|
14
|
+
"@prisma/sdk": "^3.10.0"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"@types/jest": "^27.0.2",
|
|
18
18
|
"jest": "^27.3.1",
|
|
19
19
|
"jest-mock-extended": "^2.0.4",
|
|
20
|
+
"prisma": "3.5.0",
|
|
20
21
|
"ts-jest": "^27.0.7",
|
|
21
22
|
"typescript": "^4.4.4"
|
|
22
23
|
},
|
|
23
24
|
"scripts": {
|
|
24
25
|
"preversion": "tsc",
|
|
25
26
|
"build": "tsc",
|
|
26
|
-
"watch": "tsc --watch"
|
|
27
|
+
"watch": "tsc --watch",
|
|
28
|
+
"postinstall": "prisma generate"
|
|
27
29
|
},
|
|
28
30
|
"peerDependencies": {
|
|
29
31
|
"@prisma/client": "^3.5.0"
|