prisma-mock 1.1.0-alpha.2 → 1.1.0-alpha.4
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 +1 -4
- package/lib/defaults/index.js +2 -0
- package/lib/defaults/objectId.d.ts +6 -0
- package/lib/defaults/objectId.js +19 -0
- package/lib/delegate.js +19 -4
- package/lib/utils/queryMatching.js +38 -6
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -315,6 +315,7 @@ const client = createPrismaMock(Prisma, {
|
|
|
315
315
|
### Attribute Functions ✅
|
|
316
316
|
|
|
317
317
|
- `autoincrement()`
|
|
318
|
+
- `auto()` (MongoDB ObjectId)
|
|
318
319
|
- `cuid()`
|
|
319
320
|
- `uuid()`
|
|
320
321
|
- `now()`
|
|
@@ -362,10 +363,6 @@ The following features are planned but not yet implemented:
|
|
|
362
363
|
- `isEmpty`
|
|
363
364
|
- `equals`
|
|
364
365
|
|
|
365
|
-
### Attributes
|
|
366
|
-
|
|
367
|
-
- `auto()`
|
|
368
|
-
|
|
369
366
|
### Referential Actions
|
|
370
367
|
|
|
371
368
|
- `onDelete: Restrict`
|
package/lib/defaults/index.js
CHANGED
|
@@ -6,11 +6,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const autoincrement_1 = __importDefault(require("./autoincrement"));
|
|
7
7
|
const cuid_1 = __importDefault(require("./cuid"));
|
|
8
8
|
const now_1 = __importDefault(require("./now"));
|
|
9
|
+
const objectId_1 = __importDefault(require("./objectId"));
|
|
9
10
|
const uuid_1 = __importDefault(require("./uuid"));
|
|
10
11
|
function createHandleDefault() {
|
|
11
12
|
// const registry = new Map<string, (string, Prisma.DMMF.Field, PrismaMockData) => any>();
|
|
12
13
|
const registry = new Map();
|
|
13
14
|
registry.set("autoincrement", (0, autoincrement_1.default)());
|
|
15
|
+
registry.set("auto", (0, objectId_1.default)());
|
|
14
16
|
registry.set("cuid", (0, cuid_1.default)());
|
|
15
17
|
registry.set("uuid", (0, uuid_1.default)());
|
|
16
18
|
registry.set("now", now_1.default);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Generates MongoDB ObjectId-like strings (24 hex chars).
|
|
5
|
+
* Used for @default(auto()) with @db.ObjectId
|
|
6
|
+
*/
|
|
7
|
+
const createObjectId = () => {
|
|
8
|
+
let counter = 0;
|
|
9
|
+
return () => {
|
|
10
|
+
const hex = "0123456789abcdef";
|
|
11
|
+
let result = "";
|
|
12
|
+
for (let i = 0; i < 22; i++) {
|
|
13
|
+
result += hex[Math.floor(Math.random() * 16)];
|
|
14
|
+
}
|
|
15
|
+
result += (++counter).toString(16).padStart(2, "0");
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
exports.default = createObjectId;
|
package/lib/delegate.js
CHANGED
|
@@ -133,7 +133,7 @@ const createDelegate = ({ ref, prisma, datamodel = prisma.dmmf.datamodel, caseIn
|
|
|
133
133
|
if (isCreating && (field.isUnique || field.isId)) {
|
|
134
134
|
const existing = findOne({ where: { [field.name]: inputFieldData } });
|
|
135
135
|
if (existing) {
|
|
136
|
-
(0, errors_1.throwKnownError)(prisma, `Unique constraint failed on the fields: (\`${field.name}\`)`, { code: 'P2002', meta: { target: [field.name] } });
|
|
136
|
+
(0, errors_1.throwKnownError)(prisma, `Unique constraint failed on the fields: (\`${field.name}\`)`, { code: 'P2002', meta: { modelName: model.name, target: [field.name] } });
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
// Handle relation fields (object type)
|
|
@@ -509,7 +509,9 @@ const createDelegate = ({ ref, prisma, datamodel = prisma.dmmf.datamodel, caseIn
|
|
|
509
509
|
const findOrThrow = (args) => {
|
|
510
510
|
const found = findOne(args);
|
|
511
511
|
if (!found) {
|
|
512
|
-
(0, errors_1.throwKnownError)(prisma, `No ${prop.slice(0, 1).toUpperCase()}${prop.slice(1)} found
|
|
512
|
+
(0, errors_1.throwKnownError)(prisma, `No ${prop.slice(0, 1).toUpperCase()}${prop.slice(1)} found`, {
|
|
513
|
+
meta: { cause: "No record was found for a query.", modelName: model.name },
|
|
514
|
+
});
|
|
513
515
|
}
|
|
514
516
|
return found;
|
|
515
517
|
};
|
|
@@ -627,6 +629,19 @@ const createDelegate = ({ ref, prisma, datamodel = prisma.dmmf.datamodel, caseIn
|
|
|
627
629
|
return (0, fieldHelpers_1.getCamelCase)(model.name) === prop;
|
|
628
630
|
});
|
|
629
631
|
const d = nestedUpdate(args, true, null);
|
|
632
|
+
// Check compound @@unique constraint violations during creation
|
|
633
|
+
const compoundUniques = model.uniqueFields?.filter((uf) => uf.length > 1) || [];
|
|
634
|
+
for (const fields of compoundUniques) {
|
|
635
|
+
const hasAllValues = fields.every((f) => d[f] !== undefined && d[f] !== null);
|
|
636
|
+
if (hasAllValues) {
|
|
637
|
+
const whereKey = fields.join("_");
|
|
638
|
+
const whereClause = fields.reduce((acc, f) => ({ ...acc, [f]: d[f] }), {});
|
|
639
|
+
const existing = findOne({ where: { [whereKey]: whereClause } });
|
|
640
|
+
if (existing) {
|
|
641
|
+
(0, errors_1.throwKnownError)(prisma, `Unique constraint failed on the fields: (\`${fields.join("`, `")}\`)`, { code: "P2002", meta: { modelName: model.name, target: fields } });
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
630
645
|
ref.data = {
|
|
631
646
|
...ref.data,
|
|
632
647
|
[prop]: [...ref.data[prop] || [], d],
|
|
@@ -824,7 +839,7 @@ const createDelegate = ({ ref, prisma, datamodel = prisma.dmmf.datamodel, caseIn
|
|
|
824
839
|
if (!hasMatch) {
|
|
825
840
|
if (args.skipForeignKeysChecks)
|
|
826
841
|
return;
|
|
827
|
-
(0, errors_1.throwKnownError)(prisma, "An operation failed because it depends on one or more records that were required but not found. Record to update not found.", { meta: { cause: "
|
|
842
|
+
(0, errors_1.throwKnownError)(prisma, "An operation failed because it depends on one or more records that were required but not found. Record to update not found.", { meta: { cause: "No record was found for an update.", modelName: model.name } });
|
|
828
843
|
}
|
|
829
844
|
ref.data = {
|
|
830
845
|
...ref.data,
|
|
@@ -1063,7 +1078,7 @@ const createDelegate = ({ ref, prisma, datamodel = prisma.dmmf.datamodel, caseIn
|
|
|
1063
1078
|
delete: (args) => {
|
|
1064
1079
|
const item = findOne(args);
|
|
1065
1080
|
if (!item) {
|
|
1066
|
-
(0, errors_1.throwKnownError)(prisma, "An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.", { meta: { cause: "
|
|
1081
|
+
(0, errors_1.throwKnownError)(prisma, "An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.", { meta: { cause: "No record was found for a delete.", modelName: model.name } });
|
|
1067
1082
|
}
|
|
1068
1083
|
const deleted = deleteMany(args);
|
|
1069
1084
|
if (deleted.length) {
|
|
@@ -40,6 +40,8 @@ function createMatch({ prisma, getFieldRelationshipWhere, getDelegateForFieldNam
|
|
|
40
40
|
if (info?.relationName) {
|
|
41
41
|
const childName = (0, fieldHelpers_1.getCamelCase)(info.type);
|
|
42
42
|
let childWhere = {};
|
|
43
|
+
let useIsFilter = false;
|
|
44
|
+
let useIsNotFilter = false;
|
|
43
45
|
if (filter.every) {
|
|
44
46
|
childWhere = filter.every;
|
|
45
47
|
}
|
|
@@ -49,6 +51,14 @@ function createMatch({ prisma, getFieldRelationshipWhere, getDelegateForFieldNam
|
|
|
49
51
|
else if (filter.none) {
|
|
50
52
|
childWhere = filter.none;
|
|
51
53
|
}
|
|
54
|
+
else if ("is" in filter) {
|
|
55
|
+
useIsFilter = true;
|
|
56
|
+
childWhere = filter.is === null ? {} : filter.is;
|
|
57
|
+
}
|
|
58
|
+
else if ("isNot" in filter) {
|
|
59
|
+
useIsNotFilter = true;
|
|
60
|
+
childWhere = filter.isNot === null ? {} : filter.isNot;
|
|
61
|
+
}
|
|
52
62
|
else {
|
|
53
63
|
childWhere = filter;
|
|
54
64
|
}
|
|
@@ -57,10 +67,37 @@ function createMatch({ prisma, getFieldRelationshipWhere, getDelegateForFieldNam
|
|
|
57
67
|
});
|
|
58
68
|
const delegate = getDelegateForFieldName(childName);
|
|
59
69
|
const joinWhere = getFieldRelationshipWhere(item, info, submodel);
|
|
70
|
+
if (useIsFilter) {
|
|
71
|
+
if (filter.is === null) {
|
|
72
|
+
if (!joinWhere)
|
|
73
|
+
return true;
|
|
74
|
+
const res = delegate.findMany({ where: joinWhere });
|
|
75
|
+
return res.length === 0;
|
|
76
|
+
}
|
|
77
|
+
if (!joinWhere)
|
|
78
|
+
return false;
|
|
79
|
+
const res = delegate.findMany({
|
|
80
|
+
where: { AND: [childWhere, joinWhere] },
|
|
81
|
+
});
|
|
82
|
+
return res.length > 0;
|
|
83
|
+
}
|
|
84
|
+
if (useIsNotFilter) {
|
|
85
|
+
if (filter.isNot === null) {
|
|
86
|
+
if (!joinWhere)
|
|
87
|
+
return false;
|
|
88
|
+
const res = delegate.findMany({ where: joinWhere });
|
|
89
|
+
return res.length > 0;
|
|
90
|
+
}
|
|
91
|
+
if (!joinWhere)
|
|
92
|
+
return true;
|
|
93
|
+
const res = delegate.findMany({
|
|
94
|
+
where: { AND: [childWhere, joinWhere] },
|
|
95
|
+
});
|
|
96
|
+
return res.length === 0;
|
|
97
|
+
}
|
|
60
98
|
if (!joinWhere) {
|
|
61
99
|
return false;
|
|
62
100
|
}
|
|
63
|
-
// return true
|
|
64
101
|
const res = delegate.findMany({
|
|
65
102
|
where: {
|
|
66
103
|
AND: [
|
|
@@ -70,17 +107,12 @@ function createMatch({ prisma, getFieldRelationshipWhere, getDelegateForFieldNam
|
|
|
70
107
|
}
|
|
71
108
|
});
|
|
72
109
|
if (filter.every) {
|
|
73
|
-
// const all = data[childName].filter(
|
|
74
|
-
// matchFnc(getFieldRelationshipWhere(item, info)),
|
|
75
|
-
// )
|
|
76
110
|
const where = getFieldRelationshipWhere(item, info, model);
|
|
77
111
|
if (!where)
|
|
78
112
|
return false;
|
|
79
113
|
const all = delegate.findMany({
|
|
80
114
|
where,
|
|
81
115
|
});
|
|
82
|
-
// For "every": all related records must match the condition
|
|
83
|
-
// If no related records exist, "every" is vacuously true
|
|
84
116
|
if (all.length === 0)
|
|
85
117
|
return true;
|
|
86
118
|
return res.length === all.length;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prisma-mock",
|
|
3
|
-
"version": "1.1.0-alpha.
|
|
3
|
+
"version": "1.1.0-alpha.4",
|
|
4
4
|
"description": "Mock prisma for unit testing database",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -45,11 +45,13 @@
|
|
|
45
45
|
"preversion": "jest && tsc",
|
|
46
46
|
"lint": "tsc --noEmit",
|
|
47
47
|
"build": "tsc",
|
|
48
|
-
"test": "jest",
|
|
48
|
+
"test": "prisma generate && npm run generate:mongodb && jest",
|
|
49
49
|
"generate": "prisma generate",
|
|
50
|
+
"generate:mongodb": "DATABASE_URL_MONGODB=mongodb://localhost:27017/test prisma generate --schema=prisma/schema-mongodb.prisma",
|
|
50
51
|
"release": "yarn build && changeset publish && git push --follow-tags",
|
|
51
52
|
"watch": "tsc --watch",
|
|
52
53
|
"test:postgres": "env-cmd -e postgres jest --maxWorkers=1",
|
|
54
|
+
"test:mongodb": "npm run generate:mongodb && jest __tests__/mongodb.test.ts",
|
|
53
55
|
"changeset": "changeset",
|
|
54
56
|
"changeset:add": "changeset add",
|
|
55
57
|
"changeset:validate:ci": "changeset status --since=origin/main --verbose"
|