-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06bbb18
commit 53847fc
Showing
2 changed files
with
108 additions
and
9 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
.../docs/06_plugins/01_available-plugins/01-transactional/03-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
# pg-promise adapter | ||
|
||
## Installation | ||
|
||
<Tabs> | ||
<TabItem value="npm" label="npm" default> | ||
|
||
```bash | ||
npm install @nestjs-cls/transactional-adapter-pg-promise | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="yarn" label="yarn"> | ||
|
||
```bash | ||
yarn add @nestjs-cls/transactional-adapter-pg-promise | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="pnpm" label="pnpm"> | ||
|
||
```bash | ||
pnpm add @nestjs-cls/transactional-adapter-pg-promise | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Registration | ||
|
||
```ts | ||
ClsModule.forRoot({ | ||
plugins: [ | ||
new ClsPluginTransactional({ | ||
imports: [ | ||
// module in which the database instance is provided | ||
DbModule | ||
], | ||
adapter: new TransactionalAdapterPgPromise({ | ||
// the injection token of the database instance | ||
dbInstanceToken: DB, | ||
}), | ||
}), | ||
], | ||
}), | ||
``` | ||
|
||
## Typing & usage | ||
|
||
The `tx` property on the `TransactionHost<TransactionalAdapterPgPromise>` is typed as [`Task`](https://vitaly-t.github.io/pg-promise/Task.html). | ||
|
||
## Example | ||
|
||
```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', | ||
'[email protected]', | ||
); | ||
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<TransactionalAdapterPgPromise>, | ||
) {} | ||
|
||
async getUserById(id: number) { | ||
// highlight-start | ||
// txHost.tx is typed as Task | ||
return this.txHost.tx.one(`SELECT * FROM user WHERE id = $1`); | ||
// highlight-end | ||
} | ||
|
||
async createUser(name: string, email: string) { | ||
return this.txHost.tx.none( | ||
`INSERT INTO user (name, email) VALUES ($1, $2)`, | ||
[name, email], | ||
); | ||
} | ||
} | ||
``` |
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