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-drizzle-orm): add drizzle orm adapter #180

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,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) 📖
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,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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/transactional-adapter-drizzle-orm';
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,
});
}
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
Loading
Loading