prisma-mock 0.12.0 → 0.12.2
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/delegate.js +30 -28
- package/lib/utils/queryMatching.js +6 -4
- package/package.json +1 -1
package/lib/delegate.js
CHANGED
|
@@ -44,8 +44,15 @@ indexes) => {
|
|
|
44
44
|
* @param model - The Prisma model definition
|
|
45
45
|
*/
|
|
46
46
|
const Delegate = (prop, model) => {
|
|
47
|
+
const getDelegateForFieldName = (field) => {
|
|
48
|
+
const otherModel = datamodel.models.find((model) => {
|
|
49
|
+
return (0, fieldHelpers_1.getCamelCase)(model.name) === (0, fieldHelpers_1.getCamelCase)(field);
|
|
50
|
+
});
|
|
51
|
+
const name = (0, fieldHelpers_1.getCamelCase)(field);
|
|
52
|
+
return Delegate(name, otherModel);
|
|
53
|
+
};
|
|
47
54
|
// Create matching function for WHERE clauses
|
|
48
|
-
const matchFnc = (0, queryMatching_1.default)({ getFieldRelationshipWhere,
|
|
55
|
+
const matchFnc = (0, queryMatching_1.default)({ getFieldRelationshipWhere, getDelegateForFieldName, model, datamodel, caseInsensitive });
|
|
49
56
|
/**
|
|
50
57
|
* Sorting function that handles both simple and nested orderBy clauses
|
|
51
58
|
* Supports multiple sort criteria and nested relation sorting
|
|
@@ -80,10 +87,7 @@ indexes) => {
|
|
|
80
87
|
if (!schema) {
|
|
81
88
|
return 0;
|
|
82
89
|
}
|
|
83
|
-
const
|
|
84
|
-
return model.name === schema.type;
|
|
85
|
-
});
|
|
86
|
-
const delegate = Delegate((0, fieldHelpers_1.getCamelCase)(schema.type), submodel);
|
|
90
|
+
const delegate = getDelegateForFieldName(schema.type);
|
|
87
91
|
const valA = incl(a);
|
|
88
92
|
const valB = incl(b);
|
|
89
93
|
if (!valB || !valB[key]) {
|
|
@@ -146,7 +150,7 @@ indexes) => {
|
|
|
146
150
|
return model.name === field.type;
|
|
147
151
|
});
|
|
148
152
|
const otherField = otherModel.fields.find((otherField) => field.relationName === otherField.relationName);
|
|
149
|
-
const delegate =
|
|
153
|
+
const delegate = getDelegateForFieldName(field.type);
|
|
150
154
|
const items = inputFieldData.set.map(where => delegate.findUnique({
|
|
151
155
|
where
|
|
152
156
|
})).filter(Boolean);
|
|
@@ -214,7 +218,7 @@ indexes) => {
|
|
|
214
218
|
return model.name === field.type;
|
|
215
219
|
});
|
|
216
220
|
const otherField = otherModel.fields.find((otherField) => field.relationName === otherField.relationName);
|
|
217
|
-
const delegate =
|
|
221
|
+
const delegate = getDelegateForFieldName(field.type);
|
|
218
222
|
const otherTargetKey = otherField.relationToFields[0];
|
|
219
223
|
if ((!targetKey && !keyToGet) && otherTargetKey) {
|
|
220
224
|
delegate.update({
|
|
@@ -261,14 +265,9 @@ indexes) => {
|
|
|
261
265
|
}
|
|
262
266
|
// Handle create operations for relations
|
|
263
267
|
if (inputFieldData.create || inputFieldData.createMany) {
|
|
264
|
-
const otherModel = datamodel.models.find((model) => {
|
|
265
|
-
return model.name === field.type;
|
|
266
|
-
});
|
|
267
268
|
const { [field.name]: _create, ...rest } = inputData;
|
|
268
269
|
inputData = rest;
|
|
269
|
-
|
|
270
|
-
const name = (0, fieldHelpers_1.getCamelCase)(field.type);
|
|
271
|
-
const delegate = Delegate(name, otherModel);
|
|
270
|
+
const delegate = getDelegateForFieldName(field.type);
|
|
272
271
|
const joinfield = getJoinField(field);
|
|
273
272
|
if (field.relationFromFields.length > 0) {
|
|
274
273
|
// One-to-many relation: create the related item and set foreign key
|
|
@@ -357,9 +356,24 @@ indexes) => {
|
|
|
357
356
|
}
|
|
358
357
|
else {
|
|
359
358
|
const item = findOne(args);
|
|
360
|
-
if (field.
|
|
359
|
+
if (field.isList) {
|
|
360
|
+
const otherModel = datamodel.models.find((model) => {
|
|
361
|
+
return model.name === field.type;
|
|
362
|
+
});
|
|
363
|
+
const where = getFieldRelationshipWhere(item, field, otherModel);
|
|
364
|
+
const delegate = Delegate(name, otherModel);
|
|
365
|
+
delegate.update({
|
|
366
|
+
data: inputFieldData.update.data,
|
|
367
|
+
where: where ? {
|
|
368
|
+
AND: [
|
|
369
|
+
inputFieldData.update.where,
|
|
370
|
+
where
|
|
371
|
+
]
|
|
372
|
+
} : inputFieldData.update.where,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
361
376
|
const where = getFieldRelationshipWhere(item, field, model);
|
|
362
|
-
const data = inputFieldData.update;
|
|
363
377
|
if (where) {
|
|
364
378
|
delegate.update({
|
|
365
379
|
data: inputFieldData.update,
|
|
@@ -367,15 +381,6 @@ indexes) => {
|
|
|
367
381
|
});
|
|
368
382
|
}
|
|
369
383
|
}
|
|
370
|
-
else {
|
|
371
|
-
const where = getFieldRelationshipWhere(item, field, model); //?
|
|
372
|
-
// TODO:
|
|
373
|
-
inputFieldData.update.where; //?
|
|
374
|
-
delegate.update({
|
|
375
|
-
data: inputFieldData.update.data,
|
|
376
|
-
where,
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
384
|
}
|
|
380
385
|
}
|
|
381
386
|
// Handle delete operations for relations
|
|
@@ -751,11 +756,8 @@ indexes) => {
|
|
|
751
756
|
if (!schema?.relationName) {
|
|
752
757
|
return;
|
|
753
758
|
}
|
|
754
|
-
const submodel = datamodel.models.find((model) => {
|
|
755
|
-
return model.name === schema.type;
|
|
756
|
-
});
|
|
757
759
|
// Get delegate for relation
|
|
758
|
-
const delegate =
|
|
760
|
+
const delegate = getDelegateForFieldName(schema.type);
|
|
759
761
|
const joinWhere = getFieldRelationshipWhere(item, schema, model);
|
|
760
762
|
_count = {
|
|
761
763
|
..._count,
|
|
@@ -8,7 +8,7 @@ const deepEqual_1 = require("./deepEqual");
|
|
|
8
8
|
const shallowCompare_1 = require("./shallowCompare");
|
|
9
9
|
const getNestedValue_1 = __importDefault(require("./getNestedValue"));
|
|
10
10
|
const fieldHelpers_1 = require("./fieldHelpers");
|
|
11
|
-
function createMatch({ getFieldRelationshipWhere,
|
|
11
|
+
function createMatch({ getFieldRelationshipWhere, getDelegateForFieldName, model, datamodel, caseInsensitive }) {
|
|
12
12
|
const matchItem = (child, item, where) => {
|
|
13
13
|
let val = item[child];
|
|
14
14
|
const filter = where[child];
|
|
@@ -56,7 +56,7 @@ function createMatch({ getFieldRelationshipWhere, Delegate, model, datamodel, ca
|
|
|
56
56
|
const submodel = datamodel.models.find((model) => {
|
|
57
57
|
return (0, fieldHelpers_1.getCamelCase)(model.name) === childName;
|
|
58
58
|
});
|
|
59
|
-
const delegate =
|
|
59
|
+
const delegate = getDelegateForFieldName(childName);
|
|
60
60
|
const joinWhere = getFieldRelationshipWhere(item, info, submodel);
|
|
61
61
|
if (!joinWhere) {
|
|
62
62
|
return false;
|
|
@@ -71,8 +71,6 @@ function createMatch({ getFieldRelationshipWhere, Delegate, model, datamodel, ca
|
|
|
71
71
|
}
|
|
72
72
|
});
|
|
73
73
|
if (filter.every) {
|
|
74
|
-
if (res.length === 0)
|
|
75
|
-
return true;
|
|
76
74
|
// const all = data[childName].filter(
|
|
77
75
|
// matchFnc(getFieldRelationshipWhere(item, info)),
|
|
78
76
|
// )
|
|
@@ -82,6 +80,10 @@ function createMatch({ getFieldRelationshipWhere, Delegate, model, datamodel, ca
|
|
|
82
80
|
const all = delegate.findMany({
|
|
83
81
|
where,
|
|
84
82
|
});
|
|
83
|
+
// For "every": all related records must match the condition
|
|
84
|
+
// If no related records exist, "every" is vacuously true
|
|
85
|
+
if (all.length === 0)
|
|
86
|
+
return true;
|
|
85
87
|
return res.length === all.length;
|
|
86
88
|
}
|
|
87
89
|
else if (filter.some) {
|