Skip to content

Commit

Permalink
feat(transactional-adapter-drizzle-orm): add drizzle orm adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Papooch committed Oct 11, 2024
1 parent bc07f4f commit 17e6f77
Show file tree
Hide file tree
Showing 9 changed files with 563 additions and 0 deletions.
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

0 comments on commit 17e6f77

Please sign in to comment.