Skip to content

Commit 17e6f77

Browse files
committed
feat(transactional-adapter-drizzle-orm): add drizzle orm adapter
1 parent bc07f4f commit 17e6f77

File tree

9 files changed

+563
-0
lines changed

9 files changed

+563
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# @nestjs-cls/transactional-adapter-knex
2+
3+
Drizzle ORM adapter for the `@nestjs-cls/transactional` plugin.
4+
5+
### ➡️ [Go to the documentation website](https://papooch.github.io/nestjs-cls/plugins/available-plugins/transactional/knex-adapter) 📖
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
moduleFileExtensions: ['js', 'json', 'ts'],
3+
rootDir: '.',
4+
testRegex: '.*\\.spec\\.ts$',
5+
transform: {
6+
'^.+\\.ts$': [
7+
'ts-jest',
8+
{
9+
isolatedModules: true,
10+
maxWorkers: 1,
11+
},
12+
],
13+
},
14+
collectCoverageFrom: ['src/**/*.ts'],
15+
coverageDirectory: '../coverage',
16+
testEnvironment: 'node',
17+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"name": "@nestjs-cls/transactional-adapter-drizzle-orm",
3+
"version": "1.0.0",
4+
"description": "A Drizzle ORM adapter for @nestjs-cls/transactional",
5+
"author": "papooch",
6+
"license": "MIT",
7+
"engines": {
8+
"node": ">=18"
9+
},
10+
"publishConfig": {
11+
"access": "public"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/Papooch/nestjs-cls.git"
16+
},
17+
"homepage": "https://papooch.github.io/nestjs-cls/",
18+
"keywords": [
19+
"nest",
20+
"nestjs",
21+
"cls",
22+
"continuation-local-storage",
23+
"als",
24+
"AsyncLocalStorage",
25+
"async_hooks",
26+
"request context",
27+
"async context",
28+
"transaction",
29+
"transactional",
30+
"transactional decorator",
31+
"aop",
32+
"drizzle",
33+
"drizzle-orm"
34+
],
35+
"main": "dist/src/index.js",
36+
"types": "dist/src/index.d.ts",
37+
"files": [
38+
"dist/src/**/!(*.spec).d.ts",
39+
"dist/src/**/!(*.spec).js"
40+
],
41+
"scripts": {
42+
"prepack": "cp ../../../LICENSE ./LICENSE",
43+
"prebuild": "rimraf dist",
44+
"build": "tsc",
45+
"test": "jest",
46+
"test:watch": "jest --watch",
47+
"test:cov": "jest --coverage"
48+
},
49+
"peerDependencies": {
50+
"@nestjs-cls/transactional": "workspace:^2.4.2",
51+
"drizzle-orm": "^0",
52+
"nestjs-cls": "workspace:^4.4.1"
53+
},
54+
"devDependencies": {
55+
"@nestjs/cli": "^10.0.2",
56+
"@nestjs/common": "^10.3.7",
57+
"@nestjs/core": "^10.3.7",
58+
"@nestjs/testing": "^10.3.7",
59+
"@types/jest": "^28.1.2",
60+
"@types/node": "^18.0.0",
61+
"@types/pg": "^8",
62+
"drizzle-orm": "^0.34.1",
63+
"jest": "^29.7.0",
64+
"pg": "^8.11.3",
65+
"reflect-metadata": "^0.1.13",
66+
"rimraf": "^3.0.2",
67+
"rxjs": "^7.5.5",
68+
"ts-jest": "^29.1.2",
69+
"ts-loader": "^9.3.0",
70+
"ts-node": "^10.8.1",
71+
"tsconfig-paths": "^4.0.0",
72+
"typescript": "5.0"
73+
}
74+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lib/transactional-adapter-drizzle-orm';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { TransactionalAdapter } from '@nestjs-cls/transactional';
2+
3+
type AnyDrizzleClient = {
4+
transaction: (
5+
fn: (tx: AnyDrizzleClient) => Promise<any>,
6+
options?: any,
7+
) => Promise<any>;
8+
};
9+
10+
type DrizzleTransactionOptions<T> = T extends AnyDrizzleClient
11+
? Parameters<T['transaction']>[1]
12+
: never;
13+
14+
export interface DrizzleOrmTransactionalAdapterOptions<
15+
TClient extends AnyDrizzleClient,
16+
> {
17+
/**
18+
* The injection token for the Drizzle instance.
19+
*/
20+
drizzleInstanceToken: any;
21+
22+
/**
23+
* Default options for the transaction. These will be merged with any transaction-specific options
24+
* passed to the `@Transactional` decorator or the `TransactionHost#withTransaction` method.
25+
*/
26+
defaultTxOptions?: Partial<DrizzleTransactionOptions<TClient>>;
27+
}
28+
29+
export class TransactionalAdapterDrizzleOrm<TClient extends AnyDrizzleClient>
30+
implements
31+
TransactionalAdapter<
32+
TClient,
33+
TClient,
34+
DrizzleTransactionOptions<TClient>
35+
>
36+
{
37+
connectionToken: any;
38+
39+
defaultTxOptions?: Partial<DrizzleTransactionOptions<TClient>>;
40+
41+
constructor(options: DrizzleOrmTransactionalAdapterOptions<TClient>) {
42+
this.connectionToken = options.drizzleInstanceToken;
43+
this.defaultTxOptions = options.defaultTxOptions;
44+
}
45+
46+
optionsFactory = (drizzleInstance: TClient) => ({
47+
wrapWithTransaction: async (
48+
options: DrizzleTransactionOptions<TClient>,
49+
fn: (...args: any[]) => Promise<any>,
50+
setClient: (client?: TClient) => void,
51+
) => {
52+
return drizzleInstance.transaction(async (tx) => {
53+
setClient(tx as TClient);
54+
return fn();
55+
}, options);
56+
},
57+
getFallbackInstance: () => drizzleInstance,
58+
});
59+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
drizzle-orm-test-db:
3+
image: postgres:15
4+
ports:
5+
- 5447:5432
6+
environment:
7+
POSTGRES_USER: postgres
8+
POSTGRES_PASSWORD: postgres
9+
POSTGRES_DB: postgres
10+
healthcheck:
11+
test: ['CMD-SHELL', 'pg_isready -U postgres']
12+
interval: 1s
13+
timeout: 1s
14+
retries: 5

0 commit comments

Comments
 (0)