@nathapp/nestjs-prisma 4.1.0 → 4.2.0
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
CHANGED
|
@@ -16,6 +16,7 @@ the driver adapter for your database in your consuming app:
|
|
|
16
16
|
npm install @prisma/client@^7.10.0 @prisma/adapter-pg@^7.10.0 # PostgreSQL
|
|
17
17
|
npm install @prisma/client@^7.10.0 @prisma/adapter-mariadb@^7.10.0 # MySQL / MariaDB
|
|
18
18
|
npm install -D prisma@^7.10.0
|
|
19
|
+
npm install dotenv # imported by the prisma.config.ts snippet below
|
|
19
20
|
```
|
|
20
21
|
|
|
21
22
|
Always pin `prisma@^7`: the `latest` tag of the `prisma` package may point at a
|
|
@@ -67,12 +68,15 @@ datasource db {
|
|
|
67
68
|
```ts
|
|
68
69
|
// prisma.config.ts
|
|
69
70
|
import 'dotenv/config';
|
|
70
|
-
import { defineConfig
|
|
71
|
+
import { defineConfig } from 'prisma/config';
|
|
71
72
|
|
|
72
73
|
export default defineConfig({
|
|
73
74
|
schema: 'prisma/schema.prisma',
|
|
74
75
|
migrations: { path: 'prisma/migrations' },
|
|
75
|
-
|
|
76
|
+
// Use process.env with a fallback instead of prisma/config's env():
|
|
77
|
+
// generate/validate never open a connection, but env() throws when the
|
|
78
|
+
// variable is unset, breaking env-less CI and build steps.
|
|
79
|
+
datasource: { url: process.env.DATABASE_URL ?? 'postgresql://localhost:5432/app' },
|
|
76
80
|
});
|
|
77
81
|
```
|
|
78
82
|
|
|
@@ -123,6 +127,23 @@ advisories when you bump.
|
|
|
123
127
|
|
|
124
128
|
## Usage
|
|
125
129
|
|
|
130
|
+
### Test mocks
|
|
131
|
+
|
|
132
|
+
CommonJS Jest tests can call `createMockPrismaClient()` and
|
|
133
|
+
`createMockPrismaService()` directly, without assigning `globalThis.jest`.
|
|
134
|
+
For ESM tests, or another compatible runner, supply the mock factory:
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { jest } from '@jest/globals';
|
|
138
|
+
import { createMockPrismaClient, createMockPrismaService } from '@nathapp/nestjs-prisma';
|
|
139
|
+
|
|
140
|
+
const client = createMockPrismaClient({ fn: jest.fn });
|
|
141
|
+
const service = createMockPrismaService({ fn: jest.fn });
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Each method gets its own mock. Importing the package outside a runner remains
|
|
145
|
+
safe; requesting mocks without a factory or Jest raises a descriptive error.
|
|
146
|
+
|
|
126
147
|
Single-DB setup and a repository implementation:
|
|
127
148
|
|
|
128
149
|
```typescript
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { PrismaClientLike } from '../prisma.interfaces';
|
|
2
|
-
export
|
|
3
|
-
|
|
2
|
+
export interface PrismaMockOptions {
|
|
3
|
+
fn?: () => any;
|
|
4
|
+
}
|
|
5
|
+
export declare function createMockPrismaClient(options?: PrismaMockOptions): PrismaClientLike & Record<string, any>;
|
|
6
|
+
export declare function createMockPrismaService(options?: PrismaMockOptions): {
|
|
4
7
|
client: PrismaClientLike & Record<string, any>;
|
|
5
8
|
isConnected: boolean;
|
|
6
9
|
onModuleInit: any;
|
|
@@ -2,30 +2,34 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createMockPrismaClient = createMockPrismaClient;
|
|
4
4
|
exports.createMockPrismaService = createMockPrismaService;
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
function mockFactory(options) {
|
|
6
|
+
if (typeof options.fn === 'function')
|
|
7
|
+
return options.fn;
|
|
8
|
+
const runner = typeof jest !== 'undefined' ? jest : globalThis.jest;
|
|
9
|
+
if (typeof (runner === null || runner === void 0 ? void 0 : runner.fn) !== 'function') {
|
|
10
|
+
throw new Error('Prisma mock helpers require jest.fn; pass { fn: jest.fn } when using an imported test runner.');
|
|
9
11
|
}
|
|
10
|
-
return
|
|
12
|
+
return () => runner.fn();
|
|
11
13
|
}
|
|
12
|
-
function createMockPrismaClient() {
|
|
14
|
+
function createMockPrismaClient(options = {}) {
|
|
15
|
+
const fn = mockFactory(options);
|
|
13
16
|
return {
|
|
14
|
-
$connect:
|
|
15
|
-
$disconnect:
|
|
16
|
-
$on:
|
|
17
|
-
$queryRaw:
|
|
18
|
-
$executeRaw:
|
|
19
|
-
$transaction:
|
|
17
|
+
$connect: fn().mockResolvedValue(undefined),
|
|
18
|
+
$disconnect: fn().mockResolvedValue(undefined),
|
|
19
|
+
$on: fn(),
|
|
20
|
+
$queryRaw: fn(),
|
|
21
|
+
$executeRaw: fn(),
|
|
22
|
+
$transaction: fn(),
|
|
20
23
|
};
|
|
21
24
|
}
|
|
22
|
-
function createMockPrismaService() {
|
|
23
|
-
const
|
|
25
|
+
function createMockPrismaService(options = {}) {
|
|
26
|
+
const fn = mockFactory(options);
|
|
27
|
+
const mockClient = createMockPrismaClient({ fn });
|
|
24
28
|
return {
|
|
25
29
|
client: mockClient,
|
|
26
30
|
isConnected: true,
|
|
27
|
-
onModuleInit:
|
|
28
|
-
onModuleDestroy:
|
|
31
|
+
onModuleInit: fn().mockResolvedValue(undefined),
|
|
32
|
+
onModuleDestroy: fn().mockResolvedValue(undefined),
|
|
29
33
|
};
|
|
30
34
|
}
|
|
31
35
|
//# sourceMappingURL=prisma-testing.utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prisma-testing.utils.js","sourceRoot":"","sources":["../../lib/testing/prisma-testing.utils.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"prisma-testing.utils.js","sourceRoot":"","sources":["../../lib/testing/prisma-testing.utils.ts"],"names":[],"mappings":";;AAuBA,wDAUC;AAKD,0DASC;AAvCD,SAAS,WAAW,CAAC,OAA0B;IAC7C,IAAI,OAAO,OAAO,CAAC,EAAE,KAAK,UAAU;QAAE,OAAO,OAAO,CAAC,EAAE,CAAC;IAGxD,MAAM,MAAM,GAAG,OAAO,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAE,UAA4C,CAAC,IAAI,CAAC;IACvG,IAAI,OAAO,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,EAAE,CAAA,KAAK,UAAU,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,+FAA+F,CAAC,CAAC;IACnH,CAAC;IACD,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC,EAAG,EAAE,CAAC;AAC5B,CAAC;AAMD,SAAgB,sBAAsB,CAAC,UAA6B,EAAE;IACpE,MAAM,EAAE,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IAChC,OAAO;QACL,QAAQ,EAAE,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC;QAC3C,WAAW,EAAE,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC;QAC9C,GAAG,EAAE,EAAE,EAAE;QACT,SAAS,EAAE,EAAE,EAAE;QACf,WAAW,EAAE,EAAE,EAAE;QACjB,YAAY,EAAE,EAAE,EAAE;KACnB,CAAC;AACJ,CAAC;AAKD,SAAgB,uBAAuB,CAAC,UAA6B,EAAE;IACrE,MAAM,EAAE,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IAChC,MAAM,UAAU,GAAG,sBAAsB,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAClD,OAAO;QACL,MAAM,EAAE,UAAU;QAClB,WAAW,EAAE,IAAI;QACjB,YAAY,EAAE,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC;QAC/C,eAAe,EAAE,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC;KACnD,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nathapp/nestjs-prisma",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "NestJS Prisma integration with multi-database support",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"clean": "rimraf dist/ .turbo/",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"types": "dist/index.d.ts",
|
|
21
21
|
"main": "dist/index.js",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@nathapp/nestjs-common": "^4.
|
|
24
|
-
"@nathapp/nestjs-data": "^4.
|
|
23
|
+
"@nathapp/nestjs-common": "^4.2.0",
|
|
24
|
+
"@nathapp/nestjs-data": "^4.2.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@nathapp/eslint-config": "1.0.1",
|