prisma-mock 0.3.1 → 0.4.1
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/README.md +3 -3
- package/lib/index.js +38 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -94,6 +94,7 @@ Most common cases are covered, but not everything. Here is a rough list of the s
|
|
|
94
94
|
## Model queries
|
|
95
95
|
|
|
96
96
|
- findUnique,
|
|
97
|
+
- findUniqueOrThrow,
|
|
97
98
|
- findMany,
|
|
98
99
|
- findFirst,
|
|
99
100
|
- findFirstOrThrow,
|
|
@@ -205,8 +206,7 @@ TODO (path, string_contains, string_starts_with, string_ends_with, array_contain
|
|
|
205
206
|
|
|
206
207
|
- $transaction
|
|
207
208
|
- TODO: $transaction (interactive)
|
|
208
|
-
- TODO: $transaction (isolation)
|
|
209
|
-
|
|
209
|
+
- TODO: $transaction (isolation)
|
|
210
210
|
|
|
211
211
|
# Contribution
|
|
212
212
|
|
|
@@ -226,7 +226,7 @@ Create a `.env-cmdrc` file in the root of your project with the following conten
|
|
|
226
226
|
## Writing Tests
|
|
227
227
|
Create your tests in the `__tests__` directory. You can use snapshot testing with either `expect(res).toMatchSnapshot()` or `expect(res).toMatchInlineSnapshot()`. This captures the result of your tests in a snapshot, which you can use to compare agains prisma-mock results.
|
|
228
228
|
|
|
229
|
-
Note: If you choose to use snapshot testing, make shore to first run your tests against the real database to create a snapshot of the expected result.
|
|
229
|
+
Note: If you choose to use snapshot testing, make shore to first run your tests against the real database to create a snapshot of the expected result.
|
|
230
230
|
|
|
231
231
|
## Running Tests
|
|
232
232
|
To run tests against a postgres database, run the following command:
|
package/lib/index.js
CHANGED
|
@@ -53,7 +53,7 @@ const throwUnkownError = (message, cause) => {
|
|
|
53
53
|
clientVersion);
|
|
54
54
|
}
|
|
55
55
|
error.meta = {
|
|
56
|
-
cause
|
|
56
|
+
cause,
|
|
57
57
|
};
|
|
58
58
|
throw error;
|
|
59
59
|
};
|
|
@@ -96,9 +96,9 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
96
96
|
};
|
|
97
97
|
const getFieldRelationshipWhere = (item, field) => {
|
|
98
98
|
if (field.relationToFields.length === 0) {
|
|
99
|
-
|
|
99
|
+
const otherfield = getJoinField(field);
|
|
100
100
|
return {
|
|
101
|
-
[
|
|
101
|
+
[otherfield.relationFromFields[0]]: item[otherfield.relationToFields[0]],
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
104
|
return {
|
|
@@ -121,6 +121,11 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
121
121
|
}
|
|
122
122
|
return res;
|
|
123
123
|
});
|
|
124
|
+
client["$connect"].mockImplementation(async () => { });
|
|
125
|
+
client["$disconnect"].mockImplementation(async () => { });
|
|
126
|
+
client["$use"].mockImplementation(async () => {
|
|
127
|
+
throw new Error("$use is not yet implemented in prisma-mock");
|
|
128
|
+
});
|
|
124
129
|
// client["$connect"] = async () => { }
|
|
125
130
|
// client["$disconnect"] = async () => { }
|
|
126
131
|
// client["$use"] = async () => { }
|
|
@@ -482,10 +487,12 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
482
487
|
return getCamelCase(model.name) === childName;
|
|
483
488
|
});
|
|
484
489
|
const delegate = Delegate(getCamelCase(childName), submodel);
|
|
485
|
-
const res = delegate.
|
|
490
|
+
const res = delegate._findMany({
|
|
486
491
|
where: {
|
|
487
|
-
|
|
488
|
-
|
|
492
|
+
AND: [
|
|
493
|
+
childWhere,
|
|
494
|
+
getFieldRelationshipWhere(item, info)
|
|
495
|
+
]
|
|
489
496
|
},
|
|
490
497
|
});
|
|
491
498
|
if (filter.every) {
|
|
@@ -525,7 +532,8 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
525
532
|
}
|
|
526
533
|
let match = true;
|
|
527
534
|
const matchFilter = { ...filter };
|
|
528
|
-
if (caseInsensitive ||
|
|
535
|
+
if (caseInsensitive ||
|
|
536
|
+
("mode" in matchFilter && matchFilter.mode === "insensitive")) {
|
|
529
537
|
val = val.toLowerCase ? val.toLowerCase() : val;
|
|
530
538
|
Object.keys(matchFilter).forEach((key) => {
|
|
531
539
|
const value = matchFilter[key];
|
|
@@ -590,7 +598,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
590
598
|
return true;
|
|
591
599
|
};
|
|
592
600
|
const matchAnd = (item, where) => {
|
|
593
|
-
return where.filter((child) => matchItems(item, child)).length
|
|
601
|
+
return where.filter((child) => matchItems(item, child)).length === where.length;
|
|
594
602
|
};
|
|
595
603
|
const matchOr = (item, where) => {
|
|
596
604
|
return where.some((child) => matchItems(item, child));
|
|
@@ -610,8 +618,19 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
610
618
|
}
|
|
611
619
|
return items[0];
|
|
612
620
|
};
|
|
621
|
+
const findOrThrow = (args) => {
|
|
622
|
+
const found = findOne(args);
|
|
623
|
+
if (!found) {
|
|
624
|
+
throw new runtime_1.PrismaClientKnownRequestError(`No ${prop.slice(0, 1).toUpperCase()}${prop.slice(1)} found`, "P2025",
|
|
625
|
+
// @ts-ignore
|
|
626
|
+
"1.2.3");
|
|
627
|
+
}
|
|
628
|
+
return found;
|
|
629
|
+
};
|
|
613
630
|
const findMany = (args) => {
|
|
614
|
-
let res = data[prop]
|
|
631
|
+
let res = data[prop]
|
|
632
|
+
.filter(matchFnc(args?.where))
|
|
633
|
+
.map(includes(args));
|
|
615
634
|
if (args?.distinct) {
|
|
616
635
|
let values = {};
|
|
617
636
|
res = res.filter((item) => {
|
|
@@ -763,13 +782,13 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
763
782
|
// Add relation
|
|
764
783
|
newItem = {
|
|
765
784
|
...newItem,
|
|
766
|
-
[key]: delegate.
|
|
785
|
+
[key]: delegate._findMany(subArgs),
|
|
767
786
|
};
|
|
768
787
|
}
|
|
769
788
|
else {
|
|
770
789
|
newItem = {
|
|
771
790
|
...newItem,
|
|
772
|
-
[key]: delegate.
|
|
791
|
+
[key]: delegate._findMany(subArgs)?.[0] || null,
|
|
773
792
|
};
|
|
774
793
|
}
|
|
775
794
|
});
|
|
@@ -795,20 +814,18 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
795
814
|
data = removeMultiFieldIds(model, data);
|
|
796
815
|
return findOne({ ...args, where: updatedItem });
|
|
797
816
|
};
|
|
817
|
+
const notImplemented = (name) => () => {
|
|
818
|
+
throw new Error(`${name} is not yet implemented in prisma-mock`);
|
|
819
|
+
};
|
|
798
820
|
return {
|
|
821
|
+
aggregate: notImplemented("aggregate"),
|
|
822
|
+
groupBy: notImplemented("groupBy"),
|
|
799
823
|
findOne,
|
|
800
824
|
findUnique: findOne,
|
|
825
|
+
findUniqueOrThrow: findOrThrow,
|
|
801
826
|
findMany,
|
|
802
827
|
findFirst: findOne,
|
|
803
|
-
findFirstOrThrow:
|
|
804
|
-
const found = findOne(args);
|
|
805
|
-
if (!found) {
|
|
806
|
-
throw new runtime_1.PrismaClientKnownRequestError(`No ${prop.slice(0, 1).toUpperCase()}${prop.slice(1)} found`, "P2025",
|
|
807
|
-
// @ts-ignore
|
|
808
|
-
"1.2.3");
|
|
809
|
-
}
|
|
810
|
-
return found;
|
|
811
|
-
},
|
|
828
|
+
findFirstOrThrow: findOrThrow,
|
|
812
829
|
create,
|
|
813
830
|
createMany: (args) => {
|
|
814
831
|
if (!Array.isArray(args.data)) {
|
|
@@ -868,6 +885,7 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
868
885
|
return res.length;
|
|
869
886
|
},
|
|
870
887
|
_sortFunc: sortFunc,
|
|
888
|
+
_findMany: findMany
|
|
871
889
|
};
|
|
872
890
|
};
|
|
873
891
|
datamodel.models.forEach((model) => {
|