prisma-mock 1.0.0-alpha.7 → 1.0.0-alpha.9
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 +4 -14
- package/lib/client.js +4 -3
- package/lib/delegate.js +16 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -150,7 +150,7 @@ createPrismaMock<PClient extends PrismaClient, P extends typeof Prisma = typeof
|
|
|
150
150
|
- **`datamodel`** (optional): The Prisma datamodel, typically `Prisma.dmmf.datamodel`. Defaults to the Prisma client's datamodel.
|
|
151
151
|
- **`mockClient`** (optional): A `jest-mock-extended` or `vitest-mock-extended` instance. If not provided, a plain object is used instead.
|
|
152
152
|
- **`caseInsensitive`** (boolean, default: `false`): If true, all string comparisons are case insensitive
|
|
153
|
-
- **`enableIndexes`** (boolean, default: `
|
|
153
|
+
- **`enableIndexes`** (boolean, default: `true`) If true, enables indexing for better query performance on primary keys, unique fields, and foreign keys
|
|
154
154
|
|
|
155
155
|
### Return Value
|
|
156
156
|
|
|
@@ -180,7 +180,7 @@ generator client {
|
|
|
180
180
|
provider = "prisma-client-js"
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
-
generator
|
|
183
|
+
generator dmmf {
|
|
184
184
|
provider = "prisma-mock"
|
|
185
185
|
output = "./generated/dmmf"
|
|
186
186
|
}
|
|
@@ -353,19 +353,9 @@ The following features are planned but not yet implemented:
|
|
|
353
353
|
|
|
354
354
|
## Performance Features
|
|
355
355
|
|
|
356
|
-
### Indexing
|
|
356
|
+
### Indexing
|
|
357
357
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
```js
|
|
361
|
-
import createPrismaMock from "prisma-mock"
|
|
362
|
-
|
|
363
|
-
const client = createPrismaMock({
|
|
364
|
-
enableIndexes: true,
|
|
365
|
-
})
|
|
366
|
-
```
|
|
367
|
-
|
|
368
|
-
When enabled, indexes are automatically created for:
|
|
358
|
+
Indexing is enabled by default for better query performance. Indexes are automatically created for:
|
|
369
359
|
|
|
370
360
|
- Primary key fields
|
|
371
361
|
- Unique fields
|
package/lib/client.js
CHANGED
|
@@ -11,14 +11,15 @@ const fieldHelpers_1 = require("./utils/fieldHelpers");
|
|
|
11
11
|
// @param prisma - The Prisma namespace or client constructor.
|
|
12
12
|
// @param options - Options for configuring the mock client:
|
|
13
13
|
// - data: Initial mock data for your models (default: {}).
|
|
14
|
+
// - datamodel: The Prisma datamodel, typically `Prisma.dmmf.datamodel`. Defaults to the Prisma client's datamodel.
|
|
14
15
|
// - caseInsensitive: If true, string matching is case-insensitive (default: false).
|
|
15
|
-
// - enableIndexes: If true, enables index lookups for performance (default:
|
|
16
|
+
// - enableIndexes: If true, enables index lookups for performance (default: true).
|
|
16
17
|
// - mockClient: Optionally provide your own mock client (jest-mock-extended or vitest-mock-extended) instance to use.
|
|
17
18
|
// @returns A mock Prisma client with all model methods and access to internal state.
|
|
18
19
|
function createPrismaMock(prisma, options = {
|
|
19
20
|
datamodel: prisma.dmmf?.datamodel,
|
|
20
21
|
caseInsensitive: false,
|
|
21
|
-
enableIndexes:
|
|
22
|
+
enableIndexes: true,
|
|
22
23
|
data: {}
|
|
23
24
|
}) {
|
|
24
25
|
// Reference object to hold the mock data state
|
|
@@ -77,7 +78,7 @@ function createPrismaMock(prisma, options = {
|
|
|
77
78
|
// Create delegate functions for model operations
|
|
78
79
|
const Delegate = (0, delegate_1.createDelegate)({ ref, prisma, datamodel: options.datamodel, caseInsensitive, indexes });
|
|
79
80
|
// Initialize each model in the datamodel
|
|
80
|
-
|
|
81
|
+
options.datamodel.models.forEach((model) => {
|
|
81
82
|
if (!model)
|
|
82
83
|
return;
|
|
83
84
|
// Convert model name to camelCase for consistency
|
package/lib/delegate.js
CHANGED
|
@@ -7,9 +7,8 @@ exports.createDelegate = void 0;
|
|
|
7
7
|
const defaults_1 = __importDefault(require("./defaults"));
|
|
8
8
|
const errors_1 = require("./errors");
|
|
9
9
|
const fieldHelpers_1 = require("./utils/fieldHelpers");
|
|
10
|
-
const queryMatching_1 = __importDefault(require("./utils/queryMatching"));
|
|
11
|
-
const shallowCompare_1 = require("./utils/shallowCompare");
|
|
12
10
|
const getWhereOnIds_1 = __importDefault(require("./utils/getWhereOnIds"));
|
|
11
|
+
const queryMatching_1 = __importDefault(require("./utils/queryMatching"));
|
|
13
12
|
/**
|
|
14
13
|
* Creates a delegate function that handles Prisma-like operations for a specific model
|
|
15
14
|
* This is the main factory function that generates model-specific CRUD operations
|
|
@@ -41,10 +40,10 @@ const createDelegate = ({ ref, prisma, datamodel = prisma.dmmf.datamodel, caseIn
|
|
|
41
40
|
*/
|
|
42
41
|
const Delegate = (prop, model) => {
|
|
43
42
|
const getDelegateForFieldName = (field) => {
|
|
43
|
+
const name = (0, fieldHelpers_1.getCamelCase)(field);
|
|
44
44
|
const otherModel = datamodel.models.find((model) => {
|
|
45
|
-
return
|
|
45
|
+
return name === (0, fieldHelpers_1.getCamelCase)(model.name);
|
|
46
46
|
});
|
|
47
|
-
const name = (0, fieldHelpers_1.getCamelCase)(field);
|
|
48
47
|
return Delegate(name, otherModel);
|
|
49
48
|
};
|
|
50
49
|
// Create matching function for WHERE clauses
|
|
@@ -174,30 +173,16 @@ const createDelegate = ({ ref, prisma, datamodel = prisma.dmmf.datamodel, caseIn
|
|
|
174
173
|
const keyToMatch = Object.keys(connect)[0];
|
|
175
174
|
const keyToGet = field.relationToFields[0];
|
|
176
175
|
const targetKey = field.relationFromFields[0];
|
|
176
|
+
const delegate = getDelegateForFieldName(field.type);
|
|
177
177
|
if (keyToGet && targetKey) {
|
|
178
178
|
let connectionValue = connect[keyToGet];
|
|
179
179
|
if (keyToMatch !== keyToGet) {
|
|
180
|
-
|
|
181
|
-
let matchingRow =
|
|
182
|
-
|
|
180
|
+
// Try to find by unique index if direct match fails
|
|
181
|
+
let matchingRow = delegate.findOne({
|
|
182
|
+
where: connect
|
|
183
183
|
});
|
|
184
184
|
if (!matchingRow) {
|
|
185
|
-
|
|
186
|
-
const refModel = datamodel.models.find((model) => (0, fieldHelpers_1.getCamelCase)(field.type) === (0, fieldHelpers_1.getCamelCase)(model.name));
|
|
187
|
-
const uniqueIndexes = refModel.uniqueIndexes.map((index) => {
|
|
188
|
-
return {
|
|
189
|
-
...index,
|
|
190
|
-
key: index.name ?? index.fields.join("_"),
|
|
191
|
-
};
|
|
192
|
-
});
|
|
193
|
-
const indexKey = uniqueIndexes.find((index) => index.key === keyToMatch);
|
|
194
|
-
matchingRow = ref.data[(0, fieldHelpers_1.getCamelCase)(field.type)].find((row) => {
|
|
195
|
-
const target = Object.fromEntries(Object.entries(row).filter((row) => indexKey?.fields.includes(row[0]) ?? false));
|
|
196
|
-
return (0, shallowCompare_1.shallowCompare)(target, valueToMatch);
|
|
197
|
-
});
|
|
198
|
-
if (!matchingRow) {
|
|
199
|
-
(0, errors_1.throwKnownError)(prisma, "An operation failed because it depends on one or more records that were required but not found. {cause}");
|
|
200
|
-
}
|
|
185
|
+
(0, errors_1.throwKnownError)(prisma, "An operation failed because it depends on one or more records that were required but not found. {cause}");
|
|
201
186
|
}
|
|
202
187
|
connectionValue = matchingRow[keyToGet];
|
|
203
188
|
}
|
|
@@ -210,11 +195,14 @@ const createDelegate = ({ ref, prisma, datamodel = prisma.dmmf.datamodel, caseIn
|
|
|
210
195
|
}
|
|
211
196
|
else {
|
|
212
197
|
inputData = rest;
|
|
198
|
+
const newData = {
|
|
199
|
+
...item,
|
|
200
|
+
...inputData,
|
|
201
|
+
};
|
|
213
202
|
const otherModel = datamodel.models.find((model) => {
|
|
214
203
|
return model.name === field.type;
|
|
215
204
|
});
|
|
216
205
|
const otherField = otherModel.fields.find((otherField) => field.relationName === otherField.relationName);
|
|
217
|
-
const delegate = getDelegateForFieldName(field.type);
|
|
218
206
|
const otherTargetKey = otherField.relationToFields[0];
|
|
219
207
|
if ((!targetKey && !keyToGet) && otherTargetKey) {
|
|
220
208
|
delegate.update({
|
|
@@ -222,9 +210,9 @@ const createDelegate = ({ ref, prisma, datamodel = prisma.dmmf.datamodel, caseIn
|
|
|
222
210
|
data: {
|
|
223
211
|
[(0, fieldHelpers_1.getCamelCase)(otherField.name)]: {
|
|
224
212
|
connect: {
|
|
225
|
-
[otherTargetKey]:
|
|
226
|
-
}
|
|
227
|
-
}
|
|
213
|
+
[otherTargetKey]: newData[otherTargetKey],
|
|
214
|
+
}
|
|
215
|
+
}
|
|
228
216
|
}
|
|
229
217
|
});
|
|
230
218
|
}
|
|
@@ -234,7 +222,7 @@ const createDelegate = ({ ref, prisma, datamodel = prisma.dmmf.datamodel, caseIn
|
|
|
234
222
|
[field.type]: delegate.findOne({
|
|
235
223
|
where: connect
|
|
236
224
|
}),
|
|
237
|
-
[otherField.type]: item ||
|
|
225
|
+
[otherField.type]: item || newData
|
|
238
226
|
});
|
|
239
227
|
}
|
|
240
228
|
}
|