Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transactional-adapter-mikro-orm): add mikro orm adapter #185

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog

<!-- MONODEPLOY:BELOW -->

## [1.0.0](https://github.com/Papooch/nestjs-cls/compare/@nestjs-cls/[email protected]...@nestjs-cls/[email protected]) "@nestjs-cls/transactional-adapter-mikro-orm" (2024-11-15)<a name="1.0.0"></a>

### Features

* **transactional-adapter-mikro-orm**: initial version
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @nestjs-cls/transactional-adapter-mikro-orm

`mikro-orm` 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,69 @@
{
"name": "@nestjs-cls/transactional-adapter-mikro-orm",
"version": "1.0.0",
"description": "A mikro-orm adapter for @nestjs-cls/transactional",
"author": "whileloop99 <[email protected]>",
"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",
"mikro-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": {
"@mikro-orm/core": "^6.4.0",
"@mikro-orm/postgresql": "^6.4.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the postgres driver needs to be in peer dependencies, it is only used dot tests.

"@nestjs-cls/transactional": "workspace:^2.4.2",
"nestjs-cls": "workspace:^4.4.1"
},
"devDependencies": {
"@mikro-orm/core": "^6.4.0",
"@mikro-orm/postgresql": "^6.4.0",
"@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",
"typescript": "5.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/transactional-adapter-mikro-orm';
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { TransactionalAdapter } from '@nestjs-cls/transactional';
import { MikroORM, EntityManager, IsolationLevel, IDatabaseDriver, Connection } from '@mikro-orm/core';

export interface MikroOrmTransactionalAdapterOptions {
/**
* The injection token for the MikroORM instance.
*/
dataSourceToken: any;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option should be called something like mikroOrmInstance


/**
* 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<MikroOrmTransactionOptions>;
}

export interface MikroOrmTransactionOptions {
isolationLevel?: IsolationLevel;
}

export class TransactionalAdapterMikroOrm
implements
TransactionalAdapter<
MikroORM<IDatabaseDriver<Connection>>,
EntityManager<IDatabaseDriver<Connection>>,
MikroOrmTransactionOptions
> {
connectionToken: any;
defaultTxOptions?: Partial<MikroOrmTransactionOptions>;

constructor(options: MikroOrmTransactionalAdapterOptions) {
this.connectionToken = options.dataSourceToken;
this.defaultTxOptions = options.defaultTxOptions;
}

optionsFactory = (orm: MikroORM<IDatabaseDriver<Connection>>) => ({
wrapWithTransaction: async (
options: MikroOrmTransactionOptions,
fn: (...args: any[]) => Promise<any>,
setClient: (client?: EntityManager<IDatabaseDriver<Connection>>) => void,
) => {
const em = orm.em.fork(); // Create a forked EntityManager for the transaction
await em.begin({
isolationLevel: options?.isolationLevel || this.defaultTxOptions?.isolationLevel,
});
try {
setClient(em);
const result = await fn();
await em.commit();
return result;
} catch (error) {
await em.rollback();
throw error;
}
},
getFallbackInstance: () => orm.em,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
db:
image: postgres:15
ports:
- 5446: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
Loading