-
Notifications
You must be signed in to change notification settings - Fork 33
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-kysely (#112)
- Loading branch information
Showing
13 changed files
with
661 additions
and
5 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
docs/docs/06_plugins/01_available-plugins/01-transactional/03-kysely-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,143 @@ | ||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
# Kysely adapter | ||
|
||
## Installation | ||
|
||
<Tabs> | ||
<TabItem value="npm" label="npm" default> | ||
|
||
```bash | ||
npm install @nestjs-cls/transactional-adapter-kysely | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="yarn" label="yarn"> | ||
|
||
```bash | ||
yarn add @nestjs-cls/transactional-adapter-kysely | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="pnpm" label="pnpm"> | ||
|
||
```bash | ||
pnpm add @nestjs-cls/transactional-adapter-kysely | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Registration | ||
|
||
```ts | ||
ClsModule.forRoot({ | ||
plugins: [ | ||
new ClsPluginTransactional({ | ||
imports: [ | ||
// module in which the Kysely is provided | ||
KyselyModule | ||
], | ||
adapter: new TransactionalAdapterKysely({ | ||
// the injection token of the Kysely client | ||
kyselyInstanceToken: KYSELY, | ||
}), | ||
}), | ||
], | ||
}), | ||
``` | ||
|
||
## Typing & usage | ||
|
||
The `tx` property on the `TransactionHost<TransactionalAdapterKysely>` is typed as `Kysely<any>` by default. To get the full typing, you need to supply your database type as the type parameter for the `TransactionalAdapterKysely` when injecting it: | ||
|
||
```ts | ||
constructor( | ||
private readonly txHost: TransactionHost< | ||
TransactionalAdapterKysely<Database> | ||
>, | ||
) {} | ||
``` | ||
|
||
:::tip | ||
|
||
This may get a bit too verbose, so you you might want to create a type alias for it: | ||
|
||
```ts | ||
type MyKyselyAdapter = TransactionalAdapterKysely<Database>; | ||
``` | ||
|
||
and then inject it with | ||
|
||
```ts | ||
constructor( | ||
private readonly txHost: TransactionHost<MyKyselyAdapter>, | ||
) {} | ||
``` | ||
|
||
::: | ||
|
||
## Example | ||
|
||
```ts title="database.type.ts" | ||
interface Database { | ||
user: User; | ||
} | ||
|
||
interface User { | ||
id: Generated<number>; | ||
name: string; | ||
email: string; | ||
} | ||
``` | ||
|
||
```ts title="user.service.ts" | ||
@Injectable() | ||
class UserService { | ||
constructor(private readonly userRepository: UserRepository) {} | ||
|
||
@Transactional() | ||
async runTransaction() { | ||
// highlight-start | ||
// both methods are executed in the same transaction | ||
const user = await this.userRepository.createUser('John'); | ||
const foundUser = await this.userRepository.getUserById(r1.id); | ||
// highlight-end | ||
assert(foundUser.id === user.id); | ||
} | ||
} | ||
``` | ||
|
||
```ts title="user.repository.ts" | ||
@Injectable() | ||
class UserRepository { | ||
constructor( | ||
private readonly txHost: TransactionHost< | ||
TransactionalAdapterKysely<Database> | ||
>, | ||
) {} | ||
|
||
async getUserById(id: number) { | ||
// highlight-start | ||
// txHost.tx is typed as Kysely<Database> | ||
return this.txHost.tx | ||
.selectFrom('user') | ||
.where('id', '=', id) | ||
.selectAll() | ||
.executeTakeFirst(); | ||
// highlight-end | ||
} | ||
|
||
async createUser(name: string) { | ||
return this.txHost.tx | ||
.insertInto('user') | ||
.values({ | ||
name: name, | ||
email: `${name}@email.com`, | ||
}) | ||
.returningAll() | ||
.executeTakeFirstOrThrow(); | ||
} | ||
} | ||
``` |
2 changes: 1 addition & 1 deletion
2
...01-transactional/03-pg-promise-adapter.md → ...01-transactional/04-pg-promise-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
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
1 change: 1 addition & 0 deletions
1
packages/transactional-adapters/transactional-adapter-kysely/.gitignore
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 @@ | ||
test.db |
5 changes: 5 additions & 0 deletions
5
packages/transactional-adapters/transactional-adapter-kysely/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-kysely | ||
|
||
Kysely adapter for the `@nestjs-cls/transactional` plugin. | ||
|
||
### ➡️ [Go to the documentation website](https://papooch.github.io/nestjs-cls/plugins/available-plugins/transactional/kysely-adapter) 📖 |
17 changes: 17 additions & 0 deletions
17
packages/transactional-adapters/transactional-adapter-kysely/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', | ||
}, | ||
collectCoverageFrom: ['src/**/*.ts'], | ||
coverageDirectory: '../coverage', | ||
testEnvironment: 'node', | ||
globals: { | ||
'ts-jest': { | ||
isolatedModules: true, | ||
maxWorkers: 1, | ||
}, | ||
}, | ||
}; |
74 changes: 74 additions & 0 deletions
74
packages/transactional-adapters/transactional-adapter-kysely/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,74 @@ | ||
{ | ||
"name": "@nestjs-cls/transactional-adapter-kysely", | ||
"version": "1.0.0", | ||
"description": "A Kysely 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", | ||
"kysely" | ||
], | ||
"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.0.0", | ||
"kysely": "^0.27", | ||
"nestjs-cls": "workspace:^4.0.1" | ||
}, | ||
"devDependencies": { | ||
"@nestjs/cli": "^10.0.2", | ||
"@nestjs/common": "^10.0.0", | ||
"@nestjs/core": "^10.0.0", | ||
"@nestjs/testing": "^10.0.0", | ||
"@types/better-sqlite3": "^7.6.9", | ||
"@types/jest": "^28.1.2", | ||
"@types/node": "^18.0.0", | ||
"@types/pg": "^8", | ||
"jest": "^28.1.1", | ||
"kysely": "^0.27.2", | ||
"pg": "^8.11.3", | ||
"reflect-metadata": "^0.1.13", | ||
"rimraf": "^3.0.2", | ||
"rxjs": "^7.5.5", | ||
"ts-jest": "^28.0.5", | ||
"ts-loader": "^9.3.0", | ||
"ts-node": "^10.8.1", | ||
"tsconfig-paths": "^4.0.0", | ||
"typescript": "~4.8.0" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/transactional-adapters/transactional-adapter-kysely/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-kysely'; |
43 changes: 43 additions & 0 deletions
43
...ansactional-adapters/transactional-adapter-kysely/src/lib/transactional-adapter-kysely.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,43 @@ | ||
import { TransactionalAdapter } from '@nestjs-cls/transactional'; | ||
import { Kysely, TransactionBuilder } from 'kysely'; | ||
|
||
export interface KyselyTransactionalAdapterOptions { | ||
/** | ||
* The injection token for the Kysely instance. | ||
*/ | ||
kyselyInstanceToken: any; | ||
} | ||
|
||
export interface KyselyTransactionOptions { | ||
isolationLevel?: Parameters< | ||
TransactionBuilder<any>['setIsolationLevel'] | ||
>[0]; | ||
} | ||
|
||
export class TransactionalAdapterKysely<DB = any> | ||
implements TransactionalAdapter<Kysely<DB>, Kysely<DB>, any> | ||
{ | ||
connectionToken: any; | ||
|
||
constructor(options: KyselyTransactionalAdapterOptions) { | ||
this.connectionToken = options.kyselyInstanceToken; | ||
} | ||
|
||
optionsFactory = (kyselyDb: Kysely<DB>) => ({ | ||
wrapWithTransaction: async ( | ||
options: KyselyTransactionOptions, | ||
fn: (...args: any[]) => Promise<any>, | ||
setClient: (client?: Kysely<DB>) => void, | ||
) => { | ||
const transaction = kyselyDb.transaction(); | ||
if (options?.isolationLevel) { | ||
transaction.setIsolationLevel(options.isolationLevel); | ||
} | ||
return transaction.execute(async (trx) => { | ||
setClient(trx); | ||
return fn(); | ||
}); | ||
}, | ||
getFallbackInstance: () => kyselyDb, | ||
}); | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/transactional-adapters/transactional-adapter-kysely/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: | ||
kysely-test-db: | ||
image: postgres:15 | ||
ports: | ||
- 5445: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.