Skip to content

Commit 4b9b8ab

Browse files
committed
Add docs on kysely adapter
1 parent 94da0fd commit 4b9b8ab

File tree

2 files changed

+144
-2
lines changed

2 files changed

+144
-2
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import Tabs from '@theme/Tabs';
2+
import TabItem from '@theme/TabItem';
3+
4+
# Kysely adapter
5+
6+
## Installation
7+
8+
<Tabs>
9+
<TabItem value="npm" label="npm" default>
10+
11+
```bash
12+
npm install @nestjs-cls/transactional-adapter-kysely
13+
```
14+
15+
</TabItem>
16+
<TabItem value="yarn" label="yarn">
17+
18+
```bash
19+
yarn add @nestjs-cls/transactional-adapter-kysely
20+
```
21+
22+
</TabItem>
23+
<TabItem value="pnpm" label="pnpm">
24+
25+
```bash
26+
pnpm add @nestjs-cls/transactional-adapter-kysely
27+
```
28+
29+
</TabItem>
30+
</Tabs>
31+
32+
## Registration
33+
34+
```ts
35+
ClsModule.forRoot({
36+
plugins: [
37+
new ClsPluginTransactional({
38+
imports: [
39+
// module in which the Kysely is provided
40+
KyselyModule
41+
],
42+
adapter: new TransactionalAdapterKysely({
43+
// the injection token of the Kysely client
44+
kyselyInstanceToken: KYSELY,
45+
}),
46+
}),
47+
],
48+
}),
49+
```
50+
51+
## Typing & usage
52+
53+
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:
54+
55+
```ts
56+
constructor(
57+
private readonly txHost: TransactionHost<
58+
TransactionalAdapterKysely<Database>
59+
>,
60+
) {}
61+
```
62+
63+
:::tip
64+
65+
This may get a bit too verbose, so you you might want to create a type alias for it:
66+
67+
```ts
68+
type MyKyselyAdapter = TransactionalAdapterKysely<Database>;
69+
```
70+
71+
and then inject it with
72+
73+
```ts
74+
constructor(
75+
private readonly txHost: TransactionHost<MyKyselyAdapter>,
76+
) {}
77+
```
78+
79+
:::
80+
81+
## Example
82+
83+
```ts title="database.type.ts"
84+
interface Database {
85+
user: User;
86+
}
87+
88+
interface User {
89+
id: Generated<number>;
90+
name: string;
91+
email: string;
92+
}
93+
```
94+
95+
```ts title="user.service.ts"
96+
@Injectable()
97+
class UserService {
98+
constructor(private readonly userRepository: UserRepository) {}
99+
100+
@Transactional()
101+
async runTransaction() {
102+
// highlight-start
103+
// both methods are executed in the same transaction
104+
const user = await this.userRepository.createUser('John');
105+
const foundUser = await this.userRepository.getUserById(r1.id);
106+
// highlight-end
107+
assert(foundUser.id === user.id);
108+
}
109+
}
110+
```
111+
112+
```ts title="user.repository.ts"
113+
@Injectable()
114+
class UserRepository {
115+
constructor(
116+
private readonly txHost: TransactionHost<
117+
TransactionalAdapterKysely<Database>
118+
>,
119+
) {}
120+
121+
async getUserById(id: number) {
122+
// highlight-start
123+
// txHost.tx is typed as Kysely<Database>
124+
return this.txHost.tx
125+
.selectFrom('user')
126+
.where('id', '=', id)
127+
.selectAll()
128+
.executeTakeFirst();
129+
// highlight-end
130+
}
131+
132+
async createUser(name: string) {
133+
return this.txHost.tx
134+
.insertInto('user')
135+
.values({
136+
name: name,
137+
email: `${name}@email.com`,
138+
})
139+
.returningAll()
140+
.executeTakeFirstOrThrow();
141+
}
142+
}
143+
```

packages/transactional-adapters/transactional-adapter-kysely/test/transactional-adapter-kysely.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ class UserRepository {
4040
}
4141

4242
async createUser(name: string) {
43-
const created = await this.txHost.tx
43+
return this.txHost.tx
4444
.insertInto('user')
4545
.values({
4646
name: name,
4747
email: `${name}@email.com`,
4848
})
4949
.returningAll()
5050
.executeTakeFirstOrThrow();
51-
return created;
5251
}
5352
}
5453

0 commit comments

Comments
 (0)