-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transactional-adapter-drizzle-orm): add drizzle orm adapter
- Loading branch information
Showing
9 changed files
with
563 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/transactional-adapters/transactional-adapter-drizzle-orm/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# @nestjs-cls/transactional-adapter-knex | ||
|
||
Drizzle ORM adapter for the `@nestjs-cls/transactional` plugin. | ||
|
||
### ➡️ [Go to the documentation website](https://papooch.github.io/nestjs-cls/plugins/available-plugins/transactional/knex-adapter) 📖 |
17 changes: 17 additions & 0 deletions
17
packages/transactional-adapters/transactional-adapter-drizzle-orm/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = { | ||
moduleFileExtensions: ['js', 'json', 'ts'], | ||
rootDir: '.', | ||
testRegex: '.*\\.spec\\.ts$', | ||
transform: { | ||
'^.+\\.ts$': [ | ||
'ts-jest', | ||
{ | ||
isolatedModules: true, | ||
maxWorkers: 1, | ||
}, | ||
], | ||
}, | ||
collectCoverageFrom: ['src/**/*.ts'], | ||
coverageDirectory: '../coverage', | ||
testEnvironment: 'node', | ||
}; |
74 changes: 74 additions & 0 deletions
74
packages/transactional-adapters/transactional-adapter-drizzle-orm/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
{ | ||
"name": "@nestjs-cls/transactional-adapter-drizzle-orm", | ||
"version": "1.0.0", | ||
"description": "A Drizzle ORM adapter for @nestjs-cls/transactional", | ||
"author": "papooch", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Papooch/nestjs-cls.git" | ||
}, | ||
"homepage": "https://papooch.github.io/nestjs-cls/", | ||
"keywords": [ | ||
"nest", | ||
"nestjs", | ||
"cls", | ||
"continuation-local-storage", | ||
"als", | ||
"AsyncLocalStorage", | ||
"async_hooks", | ||
"request context", | ||
"async context", | ||
"transaction", | ||
"transactional", | ||
"transactional decorator", | ||
"aop", | ||
"drizzle", | ||
"drizzle-orm" | ||
], | ||
"main": "dist/src/index.js", | ||
"types": "dist/src/index.d.ts", | ||
"files": [ | ||
"dist/src/**/!(*.spec).d.ts", | ||
"dist/src/**/!(*.spec).js" | ||
], | ||
"scripts": { | ||
"prepack": "cp ../../../LICENSE ./LICENSE", | ||
"prebuild": "rimraf dist", | ||
"build": "tsc", | ||
"test": "jest", | ||
"test:watch": "jest --watch", | ||
"test:cov": "jest --coverage" | ||
}, | ||
"peerDependencies": { | ||
"@nestjs-cls/transactional": "workspace:^2.4.2", | ||
"drizzle-orm": "^0", | ||
"nestjs-cls": "workspace:^4.4.1" | ||
}, | ||
"devDependencies": { | ||
"@nestjs/cli": "^10.0.2", | ||
"@nestjs/common": "^10.3.7", | ||
"@nestjs/core": "^10.3.7", | ||
"@nestjs/testing": "^10.3.7", | ||
"@types/jest": "^28.1.2", | ||
"@types/node": "^18.0.0", | ||
"@types/pg": "^8", | ||
"drizzle-orm": "^0.34.1", | ||
"jest": "^29.7.0", | ||
"pg": "^8.11.3", | ||
"reflect-metadata": "^0.1.13", | ||
"rimraf": "^3.0.2", | ||
"rxjs": "^7.5.5", | ||
"ts-jest": "^29.1.2", | ||
"ts-loader": "^9.3.0", | ||
"ts-node": "^10.8.1", | ||
"tsconfig-paths": "^4.0.0", | ||
"typescript": "5.0" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/transactional-adapters/transactional-adapter-drizzle-orm/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lib/transactional-adapter-drizzle-orm'; |
59 changes: 59 additions & 0 deletions
59
...l-adapters/transactional-adapter-drizzle-orm/src/lib/transactional-adapter-drizzle-orm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { TransactionalAdapter } from '@nestjs-cls/transactional'; | ||
|
||
type AnyDrizzleClient = { | ||
transaction: ( | ||
fn: (tx: AnyDrizzleClient) => Promise<any>, | ||
options?: any, | ||
) => Promise<any>; | ||
}; | ||
|
||
type DrizzleTransactionOptions<T> = T extends AnyDrizzleClient | ||
? Parameters<T['transaction']>[1] | ||
: never; | ||
|
||
export interface DrizzleOrmTransactionalAdapterOptions< | ||
TClient extends AnyDrizzleClient, | ||
> { | ||
/** | ||
* The injection token for the Drizzle instance. | ||
*/ | ||
drizzleInstanceToken: any; | ||
|
||
/** | ||
* Default options for the transaction. These will be merged with any transaction-specific options | ||
* passed to the `@Transactional` decorator or the `TransactionHost#withTransaction` method. | ||
*/ | ||
defaultTxOptions?: Partial<DrizzleTransactionOptions<TClient>>; | ||
} | ||
|
||
export class TransactionalAdapterDrizzleOrm<TClient extends AnyDrizzleClient> | ||
implements | ||
TransactionalAdapter< | ||
TClient, | ||
TClient, | ||
DrizzleTransactionOptions<TClient> | ||
> | ||
{ | ||
connectionToken: any; | ||
|
||
defaultTxOptions?: Partial<DrizzleTransactionOptions<TClient>>; | ||
|
||
constructor(options: DrizzleOrmTransactionalAdapterOptions<TClient>) { | ||
this.connectionToken = options.drizzleInstanceToken; | ||
this.defaultTxOptions = options.defaultTxOptions; | ||
} | ||
|
||
optionsFactory = (drizzleInstance: TClient) => ({ | ||
wrapWithTransaction: async ( | ||
options: DrizzleTransactionOptions<TClient>, | ||
fn: (...args: any[]) => Promise<any>, | ||
setClient: (client?: TClient) => void, | ||
) => { | ||
return drizzleInstance.transaction(async (tx) => { | ||
setClient(tx as TClient); | ||
return fn(); | ||
}, options); | ||
}, | ||
getFallbackInstance: () => drizzleInstance, | ||
}); | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/transactional-adapters/transactional-adapter-drizzle-orm/test/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
services: | ||
drizzle-orm-test-db: | ||
image: postgres:15 | ||
ports: | ||
- 5447:5432 | ||
environment: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_DB: postgres | ||
healthcheck: | ||
test: ['CMD-SHELL', 'pg_isready -U postgres'] | ||
interval: 1s | ||
timeout: 1s | ||
retries: 5 |
Oops, something went wrong.