@nathapp/nestjs-prisma 3.0.1 → 3.1.1
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +28 -0
- package/CLAUDE.md +35 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/prisma.interfaces.d.ts +1 -0
- package/dist/prisma.module.js +60 -4
- package/dist/prisma.module.js.map +1 -1
- package/dist/repository/abstract-prisma.repository.d.ts +51 -0
- package/dist/repository/abstract-prisma.repository.js +69 -0
- package/dist/repository/abstract-prisma.repository.js.map +1 -0
- package/dist/repository/index.d.ts +1 -0
- package/dist/repository/index.js +18 -0
- package/dist/repository/index.js.map +1 -0
- package/dist/transaction/index.d.ts +1 -0
- package/dist/transaction/index.js +1 -0
- package/dist/transaction/index.js.map +1 -1
- package/dist/transaction/prisma-transaction-manager.d.ts +2 -1
- package/dist/transaction/prisma-transaction-manager.js +6 -3
- package/dist/transaction/prisma-transaction-manager.js.map +1 -1
- package/dist/transaction/prisma-transaction.constants.d.ts +2 -0
- package/dist/transaction/prisma-transaction.constants.js +9 -0
- package/dist/transaction/prisma-transaction.constants.js.map +1 -0
- package/lib/index.ts +1 -0
- package/lib/prisma.interfaces.ts +9 -0
- package/lib/prisma.module.ts +76 -6
- package/lib/repository/abstract-prisma.repository.ts +120 -0
- package/lib/repository/index.ts +1 -0
- package/lib/transaction/index.ts +1 -0
- package/lib/transaction/prisma-transaction-manager.ts +10 -3
- package/lib/transaction/prisma-transaction.constants.ts +13 -0
- package/package.json +2 -2
- package/test/jest-test.json +4 -0
- package/test/repository/abstract-prisma.repository.spec.ts +157 -0
- package/test/transparent-transaction.spec.ts +332 -0
- package/.turbo/turbo-test.log +0 -43
package/.turbo/turbo-build.log
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# @nathapp/nestjs-prisma
|
|
2
2
|
|
|
3
|
+
## 3.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- a448f5b: add irepository and abstract repository
|
|
8
|
+
- Updated dependencies [a448f5b]
|
|
9
|
+
- @nathapp/nestjs-data@3.0.2
|
|
10
|
+
|
|
11
|
+
## 3.1.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- feat: add transparent transaction support via `transaction: true` option
|
|
16
|
+
|
|
17
|
+
**nestjs-prisma:**
|
|
18
|
+
|
|
19
|
+
- `PrismaModule.forRoot({ transaction: true })` wraps the injected client in a transparent proxy
|
|
20
|
+
- Repos automatically use the transaction client inside `txManager.run()` — no code changes needed
|
|
21
|
+
- Provides `TRANSACTION_MANAGER` token from `@nathapp/nestjs-data`
|
|
22
|
+
- `PrismaTransactionManager` accepts optional shared AsyncLocalStorage for proxy integration
|
|
23
|
+
|
|
24
|
+
**nestjs-typeorm:**
|
|
25
|
+
|
|
26
|
+
- `TypeOrmExModule.forRoot({ transaction: true })` provides `TRANSACTION_MANAGER` token
|
|
27
|
+
- TypeORM repos use explicit injection pattern (`@Optional() @Inject(TRANSACTION_MANAGER)`)
|
|
28
|
+
- Transparent proxy not feasible for TypeORM (repos hold manager references at creation time)
|
|
29
|
+
- `TypeOrmTransactionManager` accepts optional shared AsyncLocalStorage
|
|
30
|
+
|
|
3
31
|
## 3.0.1
|
|
4
32
|
|
|
5
33
|
### Patch Changes
|
package/CLAUDE.md
CHANGED
|
@@ -12,6 +12,41 @@ Prisma 6 wrapper with multi-database support.
|
|
|
12
12
|
- `PrismaService` wraps `PrismaClient` (composition, not inheritance) for multi-db flexibility.
|
|
13
13
|
- Pagination uses `Page<T>` from `@nathapp/nestjs-common`.
|
|
14
14
|
|
|
15
|
+
## Portable Repository
|
|
16
|
+
|
|
17
|
+
`AbstractPrismaRepository<TDomain, TModel, TId>` implements
|
|
18
|
+
`IRepository<TDomain, TId>` from `@nathapp/nestjs-data`. Use it when services
|
|
19
|
+
should depend on the portable interface (decoupled from Prisma).
|
|
20
|
+
|
|
21
|
+
Note: PrismaModule's transparent transaction proxy already auto-routes
|
|
22
|
+
`prismaService.client.<model>.x()` to the active transaction client when
|
|
23
|
+
`transaction: true`. The abstract repo's value is the `IRepository` contract
|
|
24
|
+
(decoupling), not transaction wiring.
|
|
25
|
+
|
|
26
|
+
**Limitations:**
|
|
27
|
+
- Assumes a single Prisma database (the one bound to the `TRANSACTION_MANAGER`
|
|
28
|
+
token). Multi-DB Prisma setups expose only one tx manager today — per-DB tx
|
|
29
|
+
managers are a future enhancement.
|
|
30
|
+
- Structurally assumes the primary key property is named `"id"`. Schemas using a
|
|
31
|
+
different PK name must bypass this base class.
|
|
32
|
+
|
|
33
|
+
Wiring example:
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
@Injectable()
|
|
37
|
+
export class PrismaUserRepository extends AbstractPrismaRepository<User, UserModel, string> {
|
|
38
|
+
constructor(@Inject(TRANSACTION_MANAGER) tx: ITransactionManager) { super(tx); }
|
|
39
|
+
protected modelDelegate(client: any) { return client.user; }
|
|
40
|
+
// ...mapper implementations
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Module:
|
|
44
|
+
providers: [
|
|
45
|
+
PrismaUserRepository,
|
|
46
|
+
{ provide: USER_REPOSITORY, useExisting: PrismaUserRepository },
|
|
47
|
+
]
|
|
48
|
+
```
|
|
49
|
+
|
|
15
50
|
## Gotchas
|
|
16
51
|
- Error-case tests must use `await expect(module.compile()).rejects.toThrow()` — NestJS `compile()` eagerly instantiates providers.
|
|
17
52
|
- `prisma` is a peer dependency — consumers must install it.
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -25,4 +25,5 @@ __exportStar(require("./testing"), exports);
|
|
|
25
25
|
__exportStar(require("./errors/prisma-error.mapper"), exports);
|
|
26
26
|
__exportStar(require("./filter/prisma-exception.filter"), exports);
|
|
27
27
|
__exportStar(require("./transaction"), exports);
|
|
28
|
+
__exportStar(require("./repository"), exports);
|
|
28
29
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,sDAAoC;AACpC,kDAAgC;AAChC,mDAAiC;AACjC,qDAAmC;AACnC,kDAAgC;AAChC,+CAA6B;AAC7B,4CAA0B;AAC1B,+DAA6C;AAC7C,mEAAiD;AACjD,gDAA8B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,sDAAoC;AACpC,kDAAgC;AAChC,mDAAiC;AACjC,qDAAmC;AACnC,kDAAgC;AAChC,+CAA6B;AAC7B,4CAA0B;AAC1B,+DAA6C;AAC7C,mEAAiD;AACjD,gDAA8B;AAC9B,+CAA6B"}
|
package/dist/prisma.module.js
CHANGED
|
@@ -9,23 +9,71 @@ var PrismaModule_1;
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.PrismaModule = void 0;
|
|
11
11
|
const common_1 = require("@nestjs/common");
|
|
12
|
+
const async_hooks_1 = require("async_hooks");
|
|
13
|
+
const nestjs_data_1 = require("@nathapp/nestjs-data");
|
|
12
14
|
const prisma_service_1 = require("./prisma.service");
|
|
15
|
+
const prisma_transaction_manager_1 = require("./transaction/prisma-transaction-manager");
|
|
13
16
|
const prisma_constants_1 = require("./prisma.constants");
|
|
17
|
+
const prisma_transaction_constants_1 = require("./transaction/prisma-transaction.constants");
|
|
18
|
+
function patchWithTransactionProxy(prismaService, als) {
|
|
19
|
+
const originalClient = prismaService.client;
|
|
20
|
+
const proxy = new Proxy(originalClient, {
|
|
21
|
+
get(target, prop, receiver) {
|
|
22
|
+
const txClient = als.getStore();
|
|
23
|
+
if (txClient)
|
|
24
|
+
return Reflect.get(txClient, prop, receiver);
|
|
25
|
+
return Reflect.get(target, prop, receiver);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(prismaService, 'client', {
|
|
29
|
+
get: () => proxy,
|
|
30
|
+
configurable: true,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
14
33
|
let PrismaModule = PrismaModule_1 = class PrismaModule {
|
|
15
34
|
static forRoot(options) {
|
|
16
|
-
const { isGlobal = true, name } = options;
|
|
35
|
+
const { isGlobal = true, name, transaction } = options;
|
|
17
36
|
const connectionToken = (0, prisma_constants_1.getPrismaToken)(name);
|
|
37
|
+
const providers = [];
|
|
38
|
+
let als;
|
|
39
|
+
if (transaction) {
|
|
40
|
+
als = new async_hooks_1.AsyncLocalStorage();
|
|
41
|
+
providers.push({
|
|
42
|
+
provide: prisma_transaction_constants_1.PRISMA_TRANSACTION_ALS,
|
|
43
|
+
useValue: als,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
const capturedAls = als;
|
|
47
|
+
let originalClient;
|
|
18
48
|
const prismaServiceProvider = {
|
|
19
49
|
provide: connectionToken,
|
|
20
|
-
useFactory: () =>
|
|
50
|
+
useFactory: () => {
|
|
51
|
+
const service = new prisma_service_1.PrismaService(options);
|
|
52
|
+
if (capturedAls) {
|
|
53
|
+
originalClient = service.client;
|
|
54
|
+
patchWithTransactionProxy(service, capturedAls);
|
|
55
|
+
}
|
|
56
|
+
return service;
|
|
57
|
+
},
|
|
21
58
|
};
|
|
22
|
-
|
|
59
|
+
providers.push(prismaServiceProvider);
|
|
23
60
|
if (!name || name === 'default') {
|
|
24
61
|
providers.push({
|
|
25
62
|
provide: prisma_service_1.PrismaService,
|
|
26
63
|
useExisting: connectionToken,
|
|
27
64
|
});
|
|
28
65
|
}
|
|
66
|
+
if (transaction && capturedAls) {
|
|
67
|
+
providers.push({
|
|
68
|
+
provide: prisma_transaction_manager_1.PrismaTransactionManager,
|
|
69
|
+
useFactory: () => new prisma_transaction_manager_1.PrismaTransactionManager(originalClient, capturedAls),
|
|
70
|
+
inject: [connectionToken],
|
|
71
|
+
});
|
|
72
|
+
providers.push({
|
|
73
|
+
provide: nestjs_data_1.TRANSACTION_MANAGER,
|
|
74
|
+
useExisting: prisma_transaction_manager_1.PrismaTransactionManager,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
29
77
|
return {
|
|
30
78
|
module: PrismaModule_1,
|
|
31
79
|
global: isGlobal,
|
|
@@ -62,7 +110,15 @@ let PrismaModule = PrismaModule_1 = class PrismaModule {
|
|
|
62
110
|
},
|
|
63
111
|
{
|
|
64
112
|
provide: connectionToken,
|
|
65
|
-
useFactory: (moduleOptions) =>
|
|
113
|
+
useFactory: (moduleOptions) => {
|
|
114
|
+
const service = new prisma_service_1.PrismaService(moduleOptions);
|
|
115
|
+
if (moduleOptions.transaction) {
|
|
116
|
+
const als = new async_hooks_1.AsyncLocalStorage();
|
|
117
|
+
patchWithTransactionProxy(service, als);
|
|
118
|
+
service.__transactionAls = als;
|
|
119
|
+
}
|
|
120
|
+
return service;
|
|
121
|
+
},
|
|
66
122
|
inject: [prisma_constants_1.PRISMA_MODULE_OPTIONS],
|
|
67
123
|
},
|
|
68
124
|
];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prisma.module.js","sourceRoot":"","sources":["../lib/prisma.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAiE;
|
|
1
|
+
{"version":3,"file":"prisma.module.js","sourceRoot":"","sources":["../lib/prisma.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAiE;AACjE,6CAAgD;AAChD,sDAA2D;AAE3D,qDAAiD;AACjD,yFAAoF;AACpF,yDAA2E;AAC3E,6FAAoF;AAMpF,SAAS,yBAAyB,CAChC,aAA+B,EAC/B,GAAwC;IAExC,MAAM,cAAc,GAAG,aAAa,CAAC,MAAM,CAAC;IAC5C,MAAM,KAAK,GAAG,IAAI,KAA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,CAAC;iBAC3B;gBACD;oBACE,OAAO,EAAE,eAAe;oBACxB,UAAU,EAAE,CAAC,aAAkC,EAAE,EAAE,CAAC,IAAI,8BAAa,CAAC,aAAa,CAAC;oBACpF,MAAM,EAAE,CAAC,wCAAqB,CAAC;iBAChC;aACF,CAAC;QACJ,CAAC;QAED,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;YACxB,OAAO;gBACL;oBACE,OAAO,EAAE,wCAAqB;oBAC9B,UAAU,EAAE,KAAK,EAAE,cAAoC,EAAE,EAAE,CACzD,MAAM,cAAc,CAAC,mBAAmB,EAAE;oBAC5C,MAAM,EAAE,CAAC,OAAO,CAAC,WAAW,CAAC;iBAC9B;gBACD;oBACE,OAAO,EAAE,eAAe;oBACxB,UAAU,EAAE,CAAC,aAAkC,EAAE,EAAE,CAAC,IAAI,8BAAa,CAAC,aAAa,CAAC;oBACpF,MAAM,EAAE,CAAC,wCAAqB,CAAC;iBAChC;aACF,CAAC;QACJ,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,qFAAqF,CAAC,CAAC;IACzG,CAAC;CACF,CAAA;AAxJY,oCAAY;uBAAZ,YAAY;IADxB,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,YAAY,CAwJxB"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { IPageResult, IRepository, ITransactionManager } from '@nathapp/nestjs-data';
|
|
2
|
+
import type { IPageOption } from '@nathapp/nestjs-common';
|
|
3
|
+
import { PrismaClientLike } from '../prisma.interfaces';
|
|
4
|
+
export interface PrismaModelDelegate<TModel, TId> {
|
|
5
|
+
findUnique(args: {
|
|
6
|
+
where: {
|
|
7
|
+
id: TId;
|
|
8
|
+
};
|
|
9
|
+
}): Promise<TModel | null>;
|
|
10
|
+
findFirst(args: {
|
|
11
|
+
where: any;
|
|
12
|
+
}): Promise<TModel | null>;
|
|
13
|
+
findMany(args: {
|
|
14
|
+
skip: number;
|
|
15
|
+
take: number;
|
|
16
|
+
where?: any;
|
|
17
|
+
}): Promise<TModel[]>;
|
|
18
|
+
count(args: {
|
|
19
|
+
where?: any;
|
|
20
|
+
}): Promise<number>;
|
|
21
|
+
create(args: {
|
|
22
|
+
data: any;
|
|
23
|
+
}): Promise<TModel>;
|
|
24
|
+
update(args: {
|
|
25
|
+
where: {
|
|
26
|
+
id: TId;
|
|
27
|
+
};
|
|
28
|
+
data: any;
|
|
29
|
+
}): Promise<TModel>;
|
|
30
|
+
delete(args: {
|
|
31
|
+
where: {
|
|
32
|
+
id: TId;
|
|
33
|
+
};
|
|
34
|
+
}): Promise<TModel>;
|
|
35
|
+
}
|
|
36
|
+
export declare abstract class AbstractPrismaRepository<TDomain, TModel, TId = string> implements IRepository<TDomain, TId> {
|
|
37
|
+
protected readonly tx: ITransactionManager;
|
|
38
|
+
constructor(tx: ITransactionManager);
|
|
39
|
+
protected abstract modelDelegate(client: PrismaClientLike): PrismaModelDelegate<TModel, TId>;
|
|
40
|
+
protected abstract toDomain(model: TModel): TDomain;
|
|
41
|
+
protected abstract toPersistenceCreate(domain: TDomain): any;
|
|
42
|
+
protected abstract toPersistenceUpdate(patch: Partial<TDomain>): any;
|
|
43
|
+
protected get model(): PrismaModelDelegate<TModel, TId>;
|
|
44
|
+
findById(id: TId): Promise<TDomain | null>;
|
|
45
|
+
findOne(criteria: Partial<TDomain>): Promise<TDomain | null>;
|
|
46
|
+
findAll(page: IPageOption): Promise<IPageResult<TDomain>>;
|
|
47
|
+
count(criteria?: Partial<TDomain>): Promise<number>;
|
|
48
|
+
create(input: TDomain): Promise<TDomain>;
|
|
49
|
+
update(id: TId, patch: Partial<TDomain>): Promise<TDomain>;
|
|
50
|
+
delete(id: TId): Promise<void>;
|
|
51
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.AbstractPrismaRepository = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const nestjs_data_1 = require("@nathapp/nestjs-data");
|
|
18
|
+
const nestjs_common_1 = require("@nathapp/nestjs-common");
|
|
19
|
+
let AbstractPrismaRepository = class AbstractPrismaRepository {
|
|
20
|
+
constructor(tx) {
|
|
21
|
+
this.tx = tx;
|
|
22
|
+
}
|
|
23
|
+
get model() {
|
|
24
|
+
const client = this.tx.getClient();
|
|
25
|
+
return this.modelDelegate(client);
|
|
26
|
+
}
|
|
27
|
+
async findById(id) {
|
|
28
|
+
const m = await this.model.findUnique({ where: { id } });
|
|
29
|
+
return m ? this.toDomain(m) : null;
|
|
30
|
+
}
|
|
31
|
+
async findOne(criteria) {
|
|
32
|
+
const where = this.toPersistenceUpdate(criteria);
|
|
33
|
+
const m = await this.model.findFirst({ where });
|
|
34
|
+
return m ? this.toDomain(m) : null;
|
|
35
|
+
}
|
|
36
|
+
async findAll(page) {
|
|
37
|
+
const { skip, take } = (0, nestjs_common_1.resolvePageOption)(page);
|
|
38
|
+
const [rows, total] = await Promise.all([
|
|
39
|
+
this.model.findMany({ skip, take }),
|
|
40
|
+
this.model.count({ where: undefined }),
|
|
41
|
+
]);
|
|
42
|
+
return new nestjs_common_1.Page(page, total, rows.map((r) => this.toDomain(r)));
|
|
43
|
+
}
|
|
44
|
+
async count(criteria) {
|
|
45
|
+
if (!criteria)
|
|
46
|
+
return this.model.count({});
|
|
47
|
+
const where = this.toPersistenceUpdate(criteria);
|
|
48
|
+
return this.model.count({ where });
|
|
49
|
+
}
|
|
50
|
+
async create(input) {
|
|
51
|
+
const data = this.toPersistenceCreate(input);
|
|
52
|
+
const created = await this.model.create({ data });
|
|
53
|
+
return this.toDomain(created);
|
|
54
|
+
}
|
|
55
|
+
async update(id, patch) {
|
|
56
|
+
const data = this.toPersistenceUpdate(patch);
|
|
57
|
+
const updated = await this.model.update({ where: { id }, data });
|
|
58
|
+
return this.toDomain(updated);
|
|
59
|
+
}
|
|
60
|
+
async delete(id) {
|
|
61
|
+
await this.model.delete({ where: { id } });
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
exports.AbstractPrismaRepository = AbstractPrismaRepository;
|
|
65
|
+
exports.AbstractPrismaRepository = AbstractPrismaRepository = __decorate([
|
|
66
|
+
__param(0, (0, common_1.Inject)(nestjs_data_1.TRANSACTION_MANAGER)),
|
|
67
|
+
__metadata("design:paramtypes", [Object])
|
|
68
|
+
], AbstractPrismaRepository);
|
|
69
|
+
//# sourceMappingURL=abstract-prisma.repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abstract-prisma.repository.js","sourceRoot":"","sources":["../../lib/repository/abstract-prisma.repository.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAwC;AACxC,sDAK8B;AAC9B,0DAAiE;AAqDjE,IAAsB,wBAAwB,GAA9C,MAAsB,wBAAwB;IAE5C,YAA4D,EAAuB;QAAvB,OAAE,GAAF,EAAE,CAAqB;IAAG,CAAC;IAWvF,IAAc,KAAK;QACjB,MAAM,MAAM,GAAG,IAAI,CAAC,EAA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 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./abstract-prisma.repository"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/repository/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+DAA6C"}
|
|
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./prisma-transaction-manager"), exports);
|
|
18
|
+
__exportStar(require("./prisma-transaction.constants"), exports);
|
|
18
19
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/transaction/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+DAA6C"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/transaction/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+DAA6C;AAC7C,iEAA+C"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
1
2
|
import { ITransactionManager } from '@nathapp/nestjs-data';
|
|
2
3
|
import { PrismaClientLike } from '../prisma.interfaces';
|
|
3
4
|
export declare class PrismaTransactionManager implements ITransactionManager {
|
|
@@ -5,7 +6,7 @@ export declare class PrismaTransactionManager implements ITransactionManager {
|
|
|
5
6
|
private readonly als;
|
|
6
7
|
constructor(prisma: PrismaClientLike & {
|
|
7
8
|
$transaction: Function;
|
|
8
|
-
});
|
|
9
|
+
}, externalAls?: AsyncLocalStorage<PrismaClientLike>);
|
|
9
10
|
run<T>(fn: () => Promise<T>): Promise<T>;
|
|
10
11
|
getClient<C = unknown>(): C;
|
|
11
12
|
isInTransaction(): boolean;
|
|
@@ -16,10 +16,11 @@ exports.PrismaTransactionManager = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const async_hooks_1 = require("async_hooks");
|
|
18
18
|
const prisma_constants_1 = require("../prisma.constants");
|
|
19
|
+
const prisma_transaction_constants_1 = require("./prisma-transaction.constants");
|
|
19
20
|
let PrismaTransactionManager = class PrismaTransactionManager {
|
|
20
|
-
constructor(prisma) {
|
|
21
|
+
constructor(prisma, externalAls) {
|
|
21
22
|
this.prisma = prisma;
|
|
22
|
-
this.als = new async_hooks_1.AsyncLocalStorage();
|
|
23
|
+
this.als = externalAls !== null && externalAls !== void 0 ? externalAls : new async_hooks_1.AsyncLocalStorage();
|
|
23
24
|
}
|
|
24
25
|
async run(fn) {
|
|
25
26
|
if (this.als.getStore()) {
|
|
@@ -41,6 +42,8 @@ exports.PrismaTransactionManager = PrismaTransactionManager;
|
|
|
41
42
|
exports.PrismaTransactionManager = PrismaTransactionManager = __decorate([
|
|
42
43
|
(0, common_1.Injectable)(),
|
|
43
44
|
__param(0, (0, common_1.Inject)((0, prisma_constants_1.getPrismaToken)())),
|
|
44
|
-
|
|
45
|
+
__param(1, (0, common_1.Optional)()),
|
|
46
|
+
__param(1, (0, common_1.Inject)(prisma_transaction_constants_1.PRISMA_TRANSACTION_ALS)),
|
|
47
|
+
__metadata("design:paramtypes", [Object, async_hooks_1.AsyncLocalStorage])
|
|
45
48
|
], PrismaTransactionManager);
|
|
46
49
|
//# sourceMappingURL=prisma-transaction-manager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prisma-transaction-manager.js","sourceRoot":"","sources":["../../lib/transaction/prisma-transaction-manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"prisma-transaction-manager.js","sourceRoot":"","sources":["../../lib/transaction/prisma-transaction-manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA8D;AAC9D,6CAAgD;AAGhD,0DAAqD;AACrD,iFAAwE;AA+BjE,IAAM,wBAAwB,GAA9B,MAAM,wBAAwB;IAGnC,YAC6C,MAAqD,EACpD,WAAiD;QADlD,WAAM,GAAN,MAAM,CAA+C;QAGhG,IAAI,CAAC,GAAG,GAAG,WAAW,aAAX,WAAW,cAAX,WAAW,GAAI,IAAI,+BAAiB,EAAoB,CAAC;IACtE,CAAC;IAOD,KAAK,CAAC,GAAG,CAAI,EAAoB;QAE/B,IAAI,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC;YACxB,OAAO,EAAE,EAAE,CAAC;QACd,CAAC;QAED,OAAQ,IAAI,CAAC,MAAc,CAAC,YAAY,CAAC,KAAK,EAAE,EAAoB,EAAE,EAAE;YACtE,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QAC9B,CAAC,CAAC,CAAC;IACL,CAAC;IAMD,SAAS;;QACP,OAAO,CAAC,MAAA,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,mCAAI,IAAI,CAAC,MAAM,CAAM,CAAC;IACnD,CAAC;IAKD,eAAe;QACb,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,SAAS,CAAC;IAC3C,CAAC;CACF,CAAA;AAxCY,4DAAwB;mCAAxB,wBAAwB;IADpC,IAAA,mBAAU,GAAE;IAKR,WAAA,IAAA,eAAM,EAAC,IAAA,iCAAc,GAAE,CAAC,CAAA;IACxB,WAAA,IAAA,iBAAQ,GAAE,CAAA;IAAE,WAAA,IAAA,eAAM,EAAC,qDAAsB,CAAC,CAAA;6CAAe,+BAAiB;GALlE,wBAAwB,CAwCpC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getPrismaTransactionAlsToken = exports.PRISMA_TRANSACTION_ALS = void 0;
|
|
4
|
+
exports.PRISMA_TRANSACTION_ALS = Symbol('PRISMA_TRANSACTION_ALS');
|
|
5
|
+
const getPrismaTransactionAlsToken = (name) => name && name !== 'default'
|
|
6
|
+
? `PRISMA_TRANSACTION_ALS_${name}`
|
|
7
|
+
: exports.PRISMA_TRANSACTION_ALS;
|
|
8
|
+
exports.getPrismaTransactionAlsToken = getPrismaTransactionAlsToken;
|
|
9
|
+
//# sourceMappingURL=prisma-transaction.constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prisma-transaction.constants.js","sourceRoot":"","sources":["../../lib/transaction/prisma-transaction.constants.ts"],"names":[],"mappings":";;;AAIa,QAAA,sBAAsB,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC;AAKhE,MAAM,4BAA4B,GAAG,CAAC,IAAa,EAAmB,EAAE,CAC7E,IAAI,IAAI,IAAI,KAAK,SAAS;IACxB,CAAC,CAAC,0BAA0B,IAAI,EAAE;IAClC,CAAC,CAAC,8BAA6B,CAAC;AAHvB,QAAA,4BAA4B,gCAGL"}
|
package/lib/index.ts
CHANGED
package/lib/prisma.interfaces.ts
CHANGED
|
@@ -25,6 +25,15 @@ export interface PrismaModuleOptions {
|
|
|
25
25
|
|
|
26
26
|
/** Enable logging of Prisma queries. Default: false */
|
|
27
27
|
logging?: boolean;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Enable transparent transaction support.
|
|
31
|
+
* When true, the injected Prisma client is wrapped in a proxy that
|
|
32
|
+
* automatically resolves to the transaction client inside `txManager.run()`.
|
|
33
|
+
* Also provides TRANSACTION_MANAGER token.
|
|
34
|
+
* Default: false
|
|
35
|
+
*/
|
|
36
|
+
transaction?: boolean;
|
|
28
37
|
}
|
|
29
38
|
|
|
30
39
|
export interface PrismaModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
package/lib/prisma.module.ts
CHANGED
|
@@ -1,20 +1,67 @@
|
|
|
1
1
|
import { DynamicModule, Module, Provider } from '@nestjs/common';
|
|
2
|
-
import
|
|
2
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
3
|
+
import { TRANSACTION_MANAGER } from '@nathapp/nestjs-data';
|
|
4
|
+
import type { PrismaModuleOptions, PrismaModuleAsyncOptions, PrismaOptionsFactory, PrismaClientLike } from './prisma.interfaces';
|
|
3
5
|
import { PrismaService } from './prisma.service';
|
|
6
|
+
import { PrismaTransactionManager } from './transaction/prisma-transaction-manager';
|
|
4
7
|
import { getPrismaToken, PRISMA_MODULE_OPTIONS } from './prisma.constants';
|
|
8
|
+
import { PRISMA_TRANSACTION_ALS } from './transaction/prisma-transaction.constants';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Patch a PrismaService's client getter to return a transparent proxy
|
|
12
|
+
* that delegates to the transaction client when inside a transaction.
|
|
13
|
+
*/
|
|
14
|
+
function patchWithTransactionProxy<T extends PrismaClientLike>(
|
|
15
|
+
prismaService: PrismaService<T>,
|
|
16
|
+
als: AsyncLocalStorage<PrismaClientLike>,
|
|
17
|
+
): void {
|
|
18
|
+
const originalClient = prismaService.client;
|
|
19
|
+
const proxy = new Proxy(originalClient, {
|
|
20
|
+
get(target, prop, receiver) {
|
|
21
|
+
const txClient = als.getStore();
|
|
22
|
+
if (txClient) return Reflect.get(txClient, prop, receiver);
|
|
23
|
+
return Reflect.get(target, prop, receiver);
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(prismaService, 'client', {
|
|
27
|
+
get: () => proxy,
|
|
28
|
+
configurable: true,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
5
31
|
|
|
6
32
|
@Module({})
|
|
7
33
|
export class PrismaModule {
|
|
8
34
|
static forRoot(options: PrismaModuleOptions): DynamicModule {
|
|
9
|
-
const { isGlobal = true, name } = options;
|
|
35
|
+
const { isGlobal = true, name, transaction } = options;
|
|
10
36
|
const connectionToken = getPrismaToken(name);
|
|
11
37
|
|
|
38
|
+
const providers: Provider[] = [];
|
|
39
|
+
let als: AsyncLocalStorage<PrismaClientLike> | undefined;
|
|
40
|
+
|
|
41
|
+
if (transaction) {
|
|
42
|
+
als = new AsyncLocalStorage<PrismaClientLike>();
|
|
43
|
+
|
|
44
|
+
providers.push({
|
|
45
|
+
provide: PRISMA_TRANSACTION_ALS,
|
|
46
|
+
useValue: als,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const capturedAls = als;
|
|
51
|
+
// Store reference to original (unproxied) client for transaction manager
|
|
52
|
+
let originalClient: PrismaClientLike | undefined;
|
|
12
53
|
const prismaServiceProvider: Provider = {
|
|
13
54
|
provide: connectionToken,
|
|
14
|
-
useFactory: () =>
|
|
55
|
+
useFactory: () => {
|
|
56
|
+
const service = new PrismaService(options);
|
|
57
|
+
if (capturedAls) {
|
|
58
|
+
originalClient = service.client;
|
|
59
|
+
patchWithTransactionProxy(service, capturedAls);
|
|
60
|
+
}
|
|
61
|
+
return service;
|
|
62
|
+
},
|
|
15
63
|
};
|
|
16
|
-
|
|
17
|
-
const providers: Provider[] = [prismaServiceProvider];
|
|
64
|
+
providers.push(prismaServiceProvider);
|
|
18
65
|
|
|
19
66
|
if (!name || name === 'default') {
|
|
20
67
|
providers.push({
|
|
@@ -23,6 +70,20 @@ export class PrismaModule {
|
|
|
23
70
|
});
|
|
24
71
|
}
|
|
25
72
|
|
|
73
|
+
if (transaction && capturedAls) {
|
|
74
|
+
providers.push({
|
|
75
|
+
provide: PrismaTransactionManager,
|
|
76
|
+
useFactory: () =>
|
|
77
|
+
new PrismaTransactionManager(originalClient as any, capturedAls),
|
|
78
|
+
inject: [connectionToken], // ensure PrismaService is created first
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
providers.push({
|
|
82
|
+
provide: TRANSACTION_MANAGER,
|
|
83
|
+
useExisting: PrismaTransactionManager,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
26
87
|
return {
|
|
27
88
|
module: PrismaModule,
|
|
28
89
|
global: isGlobal,
|
|
@@ -68,7 +129,16 @@ export class PrismaModule {
|
|
|
68
129
|
},
|
|
69
130
|
{
|
|
70
131
|
provide: connectionToken,
|
|
71
|
-
useFactory: (moduleOptions: PrismaModuleOptions) =>
|
|
132
|
+
useFactory: (moduleOptions: PrismaModuleOptions) => {
|
|
133
|
+
const service = new PrismaService(moduleOptions);
|
|
134
|
+
if (moduleOptions.transaction) {
|
|
135
|
+
const als = new AsyncLocalStorage<PrismaClientLike>();
|
|
136
|
+
patchWithTransactionProxy(service, als);
|
|
137
|
+
// Store for potential transaction manager setup
|
|
138
|
+
(service as any).__transactionAls = als;
|
|
139
|
+
}
|
|
140
|
+
return service;
|
|
141
|
+
},
|
|
72
142
|
inject: [PRISMA_MODULE_OPTIONS],
|
|
73
143
|
},
|
|
74
144
|
];
|