prisma-mock 0.0.0-20250808181135
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/LICENSE +21 -0
- package/README.md +344 -0
- package/lib/defaults/autoincrement.d.ts +4 -0
- package/lib/defaults/autoincrement.js +31 -0
- package/lib/defaults/cuid.d.ts +3 -0
- package/lib/defaults/cuid.js +15 -0
- package/lib/defaults/index.d.ts +4 -0
- package/lib/defaults/index.js +25 -0
- package/lib/defaults/now.d.ts +2 -0
- package/lib/defaults/now.js +4 -0
- package/lib/defaults/uuid.d.ts +3 -0
- package/lib/defaults/uuid.js +15 -0
- package/lib/delegate.js +1138 -0
- package/lib/errors.js +35 -0
- package/lib/index.d.ts +20 -0
- package/lib/index.js +129 -0
- package/lib/indexes.d.ts +7 -0
- package/lib/indexes.js +224 -0
- package/lib/indexes.test.d.ts +1 -0
- package/lib/indexes.test.js +193 -0
- package/lib/types.js +2 -0
- package/lib/utils/deepCopy.d.ts +1 -0
- package/lib/utils/deepCopy.js +17 -0
- package/lib/utils/deepEqual.d.ts +1 -0
- package/lib/utils/deepEqual.js +42 -0
- package/lib/utils/fieldHelpers.js +76 -0
- package/lib/utils/getNestedValue.d.ts +1 -0
- package/lib/utils/getNestedValue.js +17 -0
- package/lib/utils/getWhereOnIds.js +23 -0
- package/lib/utils/pad.d.ts +1 -0
- package/lib/utils/pad.js +9 -0
- package/lib/utils/queryMatching.js +279 -0
- package/lib/utils/shallowCompare.d.ts +5 -0
- package/lib/utils/shallowCompare.js +11 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 De Monsters
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
# Prisma Mock
|
|
2
|
+
|
|
3
|
+
A comprehensive mock of the Prisma API intended for unit testing. All data is stored in memory, providing fast and reliable test execution without external dependencies.
|
|
4
|
+
|
|
5
|
+
The library uses `jest-mock-extended` or `vitest-mock-extended`, which means that if functionality you need is not implemented yet, you can mock it yourself.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install prisma-mock --save-dev
|
|
11
|
+
# or
|
|
12
|
+
yarn add prisma-mock --dev
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Basic Example
|
|
18
|
+
|
|
19
|
+
Simple example of how to create a prisma mock instance:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import createPrismaMock from "prisma-mock"
|
|
23
|
+
|
|
24
|
+
let client
|
|
25
|
+
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
client = createPrismaMock()
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Mocking Global Prisma Instance
|
|
32
|
+
|
|
33
|
+
Example of how to mock a global prisma instance, as the default export in a "db" directory (like BlitzJS):
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import createPrismaMock from "prisma-mock"
|
|
37
|
+
import { mockDeep, mockReset } from "jest-mock-extended"
|
|
38
|
+
|
|
39
|
+
jest.mock("db", () => ({
|
|
40
|
+
__esModule: true,
|
|
41
|
+
...jest.requireActual("db"),
|
|
42
|
+
default: mockDeep(),
|
|
43
|
+
}))
|
|
44
|
+
|
|
45
|
+
import db, { Prisma } from "db"
|
|
46
|
+
|
|
47
|
+
beforeEach(() => {
|
|
48
|
+
mockReset(db)
|
|
49
|
+
createPrismaMock({}, Prisma.dmmf.datamodel)
|
|
50
|
+
})
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### With Initial Data
|
|
54
|
+
|
|
55
|
+
You can optionally start with pre-filled data:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const client = createPrismaMock({
|
|
59
|
+
user: [
|
|
60
|
+
{
|
|
61
|
+
id: 1,
|
|
62
|
+
name: "John Doe",
|
|
63
|
+
accountId: 1,
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
account: [
|
|
67
|
+
{
|
|
68
|
+
id: 1,
|
|
69
|
+
name: "Company",
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## API
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
createPrismaMock(
|
|
79
|
+
data: PrismaMockData<P> = {},
|
|
80
|
+
datamodel?: Prisma.DMMF.Datamodel,
|
|
81
|
+
client = mockDeep<P>(),
|
|
82
|
+
options: {
|
|
83
|
+
caseInsensitive?: boolean
|
|
84
|
+
enableIndexes?: boolean
|
|
85
|
+
} = {}
|
|
86
|
+
): Promise<P>
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Parameters
|
|
90
|
+
|
|
91
|
+
#### `data` (optional)
|
|
92
|
+
|
|
93
|
+
Initial mock data for the Prisma models. An object containing keys for tables and values as arrays of objects.
|
|
94
|
+
|
|
95
|
+
#### `datamodel` (optional)
|
|
96
|
+
|
|
97
|
+
The Prisma datamodel, typically `Prisma.dmmf.datamodel`. Defaults to the current Prisma client's datamodel.
|
|
98
|
+
|
|
99
|
+
#### `client` (optional)
|
|
100
|
+
|
|
101
|
+
A `jest-mock-extended` instance. If not provided, a new instance is created.
|
|
102
|
+
|
|
103
|
+
#### `options` (optional)
|
|
104
|
+
|
|
105
|
+
Configuration options for the mock client:
|
|
106
|
+
|
|
107
|
+
- **`caseInsensitive`** (boolean, default: `false`): If true, all string comparisons are case insensitive
|
|
108
|
+
- **`enableIndexes`** (boolean, default: `false`) Experimental: If true, enables indexing for better query performance on primary keys, unique fields, and foreign keys
|
|
109
|
+
|
|
110
|
+
### Return Value
|
|
111
|
+
|
|
112
|
+
Returns a mock Prisma client with all standard model methods plus:
|
|
113
|
+
|
|
114
|
+
- `$getInternalState()`: Method to access the internal data state for testing/debugging
|
|
115
|
+
|
|
116
|
+
## Supported Features
|
|
117
|
+
|
|
118
|
+
### Model Queries ✅
|
|
119
|
+
|
|
120
|
+
- `findUnique` / `findUniqueOrThrow`
|
|
121
|
+
- `findMany`
|
|
122
|
+
- `findFirst` / `findFirstOrThrow`
|
|
123
|
+
- `create`
|
|
124
|
+
- `createMany`
|
|
125
|
+
- `delete`
|
|
126
|
+
- `update`
|
|
127
|
+
- `deleteMany`
|
|
128
|
+
- `updateMany`
|
|
129
|
+
- `upsert`
|
|
130
|
+
- `count`
|
|
131
|
+
- `aggregate`
|
|
132
|
+
|
|
133
|
+
### Model Query Options ✅
|
|
134
|
+
|
|
135
|
+
- `distinct`
|
|
136
|
+
- `include`
|
|
137
|
+
- `where`
|
|
138
|
+
- `select`
|
|
139
|
+
- `orderBy`
|
|
140
|
+
- `select: _count`
|
|
141
|
+
|
|
142
|
+
### Nested Queries ✅
|
|
143
|
+
|
|
144
|
+
- `create`
|
|
145
|
+
- `createMany`
|
|
146
|
+
- `update`
|
|
147
|
+
- `updateMany`
|
|
148
|
+
- `delete`
|
|
149
|
+
- `deleteMany`
|
|
150
|
+
- `connect`
|
|
151
|
+
- `disconnect`
|
|
152
|
+
- `set`
|
|
153
|
+
- `upsert`
|
|
154
|
+
|
|
155
|
+
### Filter Conditions and Operators ✅
|
|
156
|
+
|
|
157
|
+
- `equals`
|
|
158
|
+
- `gt`, `gte`, `lt`, `lte`
|
|
159
|
+
- `not`
|
|
160
|
+
- `in`, `notIn`
|
|
161
|
+
- `contains`, `startsWith`, `endsWith`
|
|
162
|
+
- `AND`, `OR`, `NOT`
|
|
163
|
+
- `mode` (for case-insensitive matching)
|
|
164
|
+
|
|
165
|
+
### Relation Filters ✅
|
|
166
|
+
|
|
167
|
+
- `some`
|
|
168
|
+
- `every`
|
|
169
|
+
- `none`
|
|
170
|
+
|
|
171
|
+
### Atomic Number Operations ✅
|
|
172
|
+
|
|
173
|
+
- `increment`
|
|
174
|
+
- `decrement`
|
|
175
|
+
- `multiply`
|
|
176
|
+
- `divide`
|
|
177
|
+
- `set`
|
|
178
|
+
|
|
179
|
+
### JSON Filters ✅
|
|
180
|
+
|
|
181
|
+
- `path`
|
|
182
|
+
- `string_contains`
|
|
183
|
+
- `string_starts_with`
|
|
184
|
+
- `string_ends_with`
|
|
185
|
+
- `array_contains`
|
|
186
|
+
- `array_starts_with`
|
|
187
|
+
- `array_ends_with`
|
|
188
|
+
|
|
189
|
+
### Attributes ✅
|
|
190
|
+
|
|
191
|
+
- `@@id` (Primary keys)
|
|
192
|
+
- `@default` (Default values)
|
|
193
|
+
- `@unique` (Unique constraints)
|
|
194
|
+
- `@@unique` (Compound unique constraints)
|
|
195
|
+
- `@relation` (Relationships)
|
|
196
|
+
- `@updatedAt` (Partially supported - set at creation)
|
|
197
|
+
|
|
198
|
+
### Attribute Functions ✅
|
|
199
|
+
|
|
200
|
+
- `autoincrement()`
|
|
201
|
+
- `cuid()`
|
|
202
|
+
- `uuid()`
|
|
203
|
+
- `now()`
|
|
204
|
+
|
|
205
|
+
### Referential Actions ✅
|
|
206
|
+
|
|
207
|
+
- `onDelete: SetNull`
|
|
208
|
+
- `onDelete: Cascade`
|
|
209
|
+
|
|
210
|
+
### Prisma Client Methods ✅
|
|
211
|
+
|
|
212
|
+
- `$transaction` (Array of promises)
|
|
213
|
+
- `$transaction` (Interactive transactions with rollback)
|
|
214
|
+
- `$connect`
|
|
215
|
+
- `$disconnect`
|
|
216
|
+
|
|
217
|
+
## Not Yet Implemented
|
|
218
|
+
|
|
219
|
+
The following features are planned but not yet implemented:
|
|
220
|
+
|
|
221
|
+
### Model Queries
|
|
222
|
+
|
|
223
|
+
- `groupBy`
|
|
224
|
+
|
|
225
|
+
### Nested Queries
|
|
226
|
+
|
|
227
|
+
- `connectOrCreate`
|
|
228
|
+
|
|
229
|
+
### Filter Conditions
|
|
230
|
+
|
|
231
|
+
- `search` (Full-text search)
|
|
232
|
+
|
|
233
|
+
### Relation Filters
|
|
234
|
+
|
|
235
|
+
- `is`
|
|
236
|
+
|
|
237
|
+
### Scalar List Methods
|
|
238
|
+
|
|
239
|
+
- `set`
|
|
240
|
+
- `push`
|
|
241
|
+
|
|
242
|
+
### Scalar List Filters
|
|
243
|
+
|
|
244
|
+
- `has`
|
|
245
|
+
- `hasEvery`
|
|
246
|
+
- `hasSome`
|
|
247
|
+
- `isEmpty`
|
|
248
|
+
- `equals`
|
|
249
|
+
|
|
250
|
+
### Attributes
|
|
251
|
+
|
|
252
|
+
- `auto()`
|
|
253
|
+
- `dbgenerated()`
|
|
254
|
+
|
|
255
|
+
### Referential Actions
|
|
256
|
+
|
|
257
|
+
- `onDelete: Restrict`
|
|
258
|
+
- `onDelete: NoAction`
|
|
259
|
+
- `onDelete: SetDefault`
|
|
260
|
+
- `onUpdate` actions
|
|
261
|
+
|
|
262
|
+
### Prisma Client Methods
|
|
263
|
+
|
|
264
|
+
- `$transaction` (Isolation levels)
|
|
265
|
+
- `$use` (Middleware)
|
|
266
|
+
|
|
267
|
+
## Performance Features
|
|
268
|
+
|
|
269
|
+
### Indexing (Experimental)
|
|
270
|
+
|
|
271
|
+
Enable indexing for better query performance:
|
|
272
|
+
|
|
273
|
+
```js
|
|
274
|
+
const client = createPrismaMock({}, undefined, undefined, {
|
|
275
|
+
enableIndexes: true,
|
|
276
|
+
})
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
When enabled, indexes are automatically created for:
|
|
280
|
+
|
|
281
|
+
- Primary key fields
|
|
282
|
+
- Unique fields
|
|
283
|
+
- Foreign key fields
|
|
284
|
+
|
|
285
|
+
This can significantly improve query performance for large datasets.
|
|
286
|
+
|
|
287
|
+
## Error Handling
|
|
288
|
+
|
|
289
|
+
The mock client throws appropriate Prisma errors with correct error codes:
|
|
290
|
+
|
|
291
|
+
- `P2025`: Record not found (for `findUniqueOrThrow`, `findFirstOrThrow`)
|
|
292
|
+
- `P2002`: Unique constraint violation
|
|
293
|
+
- `P2003`: Foreign key constraint violation
|
|
294
|
+
|
|
295
|
+
## Testing
|
|
296
|
+
|
|
297
|
+
### Writing Tests
|
|
298
|
+
|
|
299
|
+
Create your tests in the `__tests__` directory. You can use snapshot testing with either `expect(res).toMatchSnapshot()` or `expect(res).toMatchInlineSnapshot()`.
|
|
300
|
+
|
|
301
|
+
**Note**: If you choose to use snapshot testing, make sure to first run your tests against the real database to create a snapshot of the expected result.
|
|
302
|
+
|
|
303
|
+
### Running Tests
|
|
304
|
+
|
|
305
|
+
To run tests against a PostgreSQL database:
|
|
306
|
+
|
|
307
|
+
```bash
|
|
308
|
+
yarn run test:postgres
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
To run tests against prisma-mock (in-memory database):
|
|
312
|
+
|
|
313
|
+
```bash
|
|
314
|
+
yarn test
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
## Development
|
|
318
|
+
|
|
319
|
+
### Requirements
|
|
320
|
+
|
|
321
|
+
Create a `.env-cmdrc` file in the root of your project with the following content:
|
|
322
|
+
|
|
323
|
+
```json
|
|
324
|
+
{
|
|
325
|
+
"postgres": {
|
|
326
|
+
"PROVIDER": "postgresql",
|
|
327
|
+
"DATABASE_URL": "postgresql://postgres:postgres@localhost:5432/postgres?schema=public"
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### Building
|
|
333
|
+
|
|
334
|
+
```bash
|
|
335
|
+
yarn build
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## Contributing
|
|
339
|
+
|
|
340
|
+
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
|
341
|
+
|
|
342
|
+
## License
|
|
343
|
+
|
|
344
|
+
MIT
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function createAutoincrement() {
|
|
4
|
+
let autoincrement_cache = {};
|
|
5
|
+
return (prop, field, data = {}) => {
|
|
6
|
+
const key = `${prop}_${field.name}`;
|
|
7
|
+
let m = autoincrement_cache?.[key];
|
|
8
|
+
if (field.type === 'BigInt') {
|
|
9
|
+
if (m === undefined) {
|
|
10
|
+
m = 0n;
|
|
11
|
+
data[prop].forEach((item) => {
|
|
12
|
+
m = m > item[field.name] ? m : item[field.name];
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
m = m + 1n;
|
|
16
|
+
autoincrement_cache[key] = m;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
if (m === undefined) {
|
|
20
|
+
m = 0;
|
|
21
|
+
data[prop]?.forEach((item) => {
|
|
22
|
+
m = Math.max(m, item[field.name]);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
m = m + 1;
|
|
26
|
+
autoincrement_cache[key] = m;
|
|
27
|
+
}
|
|
28
|
+
return m;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
exports.default = createAutoincrement;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const pad_1 = __importDefault(require("../utils/pad"));
|
|
7
|
+
// Format from: https://cuid.marcoonroad.dev/
|
|
8
|
+
const createCuid = () => {
|
|
9
|
+
let ciud_cache = 0;
|
|
10
|
+
return () => {
|
|
11
|
+
ciud_cache++;
|
|
12
|
+
return `c00p6qup2${(0, pad_1.default)(String(ciud_cache), 4)}ckkzslahp5pn`;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
exports.default = createCuid;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const autoincrement_1 = __importDefault(require("./autoincrement"));
|
|
7
|
+
const cuid_1 = __importDefault(require("./cuid"));
|
|
8
|
+
const now_1 = __importDefault(require("./now"));
|
|
9
|
+
const uuid_1 = __importDefault(require("./uuid"));
|
|
10
|
+
function createHandleDefault() {
|
|
11
|
+
// const registry = new Map<string, (string, Prisma.DMMF.Field, PrismaMockData) => any>();
|
|
12
|
+
const registry = new Map();
|
|
13
|
+
registry.set("autoincrement", (0, autoincrement_1.default)());
|
|
14
|
+
registry.set("cuid", (0, cuid_1.default)());
|
|
15
|
+
registry.set("uuid", (0, uuid_1.default)());
|
|
16
|
+
registry.set("now", now_1.default);
|
|
17
|
+
registry.set("dbgenerated", (0, uuid_1.default)());
|
|
18
|
+
registry.set(`dbgenerated("gen_random_uuid()")`, (0, uuid_1.default)());
|
|
19
|
+
return (prop, field, ref) => {
|
|
20
|
+
const key = field.default.name;
|
|
21
|
+
const val = registry.get(key)?.(prop, field, ref.data);
|
|
22
|
+
return val;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
exports.default = createHandleDefault;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const pad_1 = __importDefault(require("../utils/pad"));
|
|
7
|
+
// https://en.wikipedia.org/wiki/Universally_unique_identifier
|
|
8
|
+
const createUuid = () => {
|
|
9
|
+
let uuid_cache = 0;
|
|
10
|
+
return () => {
|
|
11
|
+
uuid_cache++;
|
|
12
|
+
return `123e4567-e89b-12d3-a456-${(0, pad_1.default)(String(uuid_cache), 12)}`;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
exports.default = createUuid;
|