Skip to content

Commit

Permalink
feat: add @nestjs-cls/transactional-adapter-typeorm (#141)
Browse files Browse the repository at this point in the history
* feat(transactional-adapter-typeorm): ✨ add new transactional-adapter-typeorm
  • Loading branch information
giosueDelgado authored Apr 22, 2024
1 parent f7841dd commit a2a8e5a
Show file tree
Hide file tree
Showing 13 changed files with 729 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"editor.formatOnSave": true,
"typescript.preferences.importModuleSpecifier": "relative",
"typescript.tsdk": "node_modules/typescript/lib"
}
}
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,
}),
}),
],
}),
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
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) 📖
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',
};
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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/transactional-adapter-typeorm';
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,
});
}
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;
}
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
Loading

0 comments on commit a2a8e5a

Please sign in to comment.