-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add @nestjs-cls/transactional-adapter-typeorm (#141)
* feat(transactional-adapter-typeorm): ✨ add new transactional-adapter-typeorm
- Loading branch information
1 parent
f7841dd
commit a2a8e5a
Showing
13 changed files
with
729 additions
and
6 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
docs/docs/06_plugins/01_available-plugins/01-transactional/05-typeorm-adapter.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,50 @@ | ||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
# TypeOrm adapter | ||
|
||
## Installation | ||
|
||
<Tabs> | ||
<TabItem value="npm" label="npm" default> | ||
|
||
```bash | ||
npm install @nestjs-cls/transactional-adapter-typeorm | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="yarn" label="yarn"> | ||
|
||
```bash | ||
yarn add @nestjs-cls/transactional-adapter-typeorm | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="pnpm" label="pnpm"> | ||
|
||
```bash | ||
pnpm add @nestjs-cls/transactional-adapter-typeorm | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Registration | ||
|
||
```ts | ||
|
||
ClsModule.forRoot({ | ||
plugins: [ | ||
new ClsPluginTransactional({ | ||
imports: [ | ||
// module in which the database instance is provided | ||
TypeOrmModule | ||
], | ||
adapter: new TransactionalAdapterTypeOrm({ | ||
// the injection token of the database instance | ||
dataSourceToken: DataSource, | ||
}), | ||
}), | ||
], | ||
}), | ||
``` |
1 change: 1 addition & 0 deletions
1
packages/transactional-adapters/transactional-adapter-typeorm/CHANGES.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 @@ | ||
# Changelog |
5 changes: 5 additions & 0 deletions
5
packages/transactional-adapters/transactional-adapter-typeorm/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-typeorm | ||
|
||
`typeorm` adapter for the `@nestjs-cls/transactional` plugin. | ||
|
||
### ➡️ [Go to the documentation website](https://papooch.github.io/nestjs-cls/plugins/available-plugins/transactional) 📖 |
17 changes: 17 additions & 0 deletions
17
packages/transactional-adapters/transactional-adapter-typeorm/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', | ||
}; |
67 changes: 67 additions & 0 deletions
67
packages/transactional-adapters/transactional-adapter-typeorm/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,67 @@ | ||
{ | ||
"name": "@nestjs-cls/transactional-adapter-typeorm", | ||
"version": "1.2.0", | ||
"description": "A typeorm adapter for @nestjs-cls/transactional", | ||
"author": "Giosuè Delgado S.Z. - Relybytes Srl <info@relybytes.com>", | ||
"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", | ||
"typeorm" | ||
], | ||
"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.2.2", | ||
"nestjs-cls": "workspace:^4.3.0", | ||
"typeorm": "0.3.20" | ||
}, | ||
"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", | ||
"jest": "^29.7.0", | ||
"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", | ||
"typeorm": "0.3.20", | ||
"typescript": "5.0" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/transactional-adapters/transactional-adapter-typeorm/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-typeorm'; |
35 changes: 35 additions & 0 deletions
35
...sactional-adapters/transactional-adapter-typeorm/src/lib/transactional-adapter-typeorm.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,35 @@ | ||
import { DataSource, EntityManager } from 'typeorm'; | ||
import { | ||
TypeOrmTransactionOptions, | ||
TypeOrmTransactionalAdapterOptions, | ||
} from './transactional-options'; | ||
import { TransactionalAdapter } from '@nestjs-cls/transactional'; | ||
|
||
export class TransactionalAdapterTypeOrm | ||
implements | ||
TransactionalAdapter< | ||
DataSource, | ||
EntityManager, | ||
TypeOrmTransactionOptions | ||
> | ||
{ | ||
connectionToken: any; | ||
|
||
constructor(options: TypeOrmTransactionalAdapterOptions) { | ||
this.connectionToken = options.dataSourceToken; | ||
} | ||
|
||
optionsFactory = (dataSource: DataSource) => ({ | ||
wrapWithTransaction: async ( | ||
options: TypeOrmTransactionOptions, | ||
fn: (...args: any[]) => Promise<any>, | ||
setClient: (client?: EntityManager) => void, | ||
) => { | ||
return dataSource.transaction(options?.isolationLevel, (trx) => { | ||
setClient(trx); | ||
return fn(); | ||
}); | ||
}, | ||
getFallbackInstance: () => dataSource.manager, | ||
}); | ||
} |
12 changes: 12 additions & 0 deletions
12
...ges/transactional-adapters/transactional-adapter-typeorm/src/lib/transactional-options.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,12 @@ | ||
import { IsolationLevel } from 'typeorm/driver/types/IsolationLevel'; | ||
|
||
export interface TypeOrmTransactionalAdapterOptions { | ||
/** | ||
* The injection token for the typeOrm instance. | ||
*/ | ||
dataSourceToken: any; | ||
} | ||
|
||
export interface TypeOrmTransactionOptions { | ||
isolationLevel: IsolationLevel; | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/transactional-adapters/transactional-adapter-typeorm/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: | ||
db: | ||
image: postgres:15 | ||
ports: | ||
- 5444: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.