https://github.com/web-server-userland/fastify-typeorm-query-runner
fastify typeorm QueryRunner plugin
https://github.com/web-server-userland/fastify-typeorm-query-runner
fastify fastify-plugin nodejs typeorm
Last synced: over 1 year ago
JSON representation
fastify typeorm QueryRunner plugin
- Host: GitHub
- URL: https://github.com/web-server-userland/fastify-typeorm-query-runner
- Owner: web-server-userland
- License: mit
- Created: 2022-07-11T03:20:21.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-11-26T07:12:37.000Z (over 3 years ago)
- Last Synced: 2025-03-10T20:42:00.535Z (over 1 year ago)
- Topics: fastify, fastify-plugin, nodejs, typeorm
- Language: JavaScript
- Homepage:
- Size: 57.6 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @web-server-userland/fastify-typeorm-query-runner

[](https://www.npmjs.com/package/@web-server-userland/fastify-typeorm-query-runner)
[](https://standardjs.com/)
[](https://www.npmjs.com/package/@web-server-userland/fastify-typeorm-query-runner)
[](https://coveralls.io/github/web-server-userland/fastify-typeorm-query-runner?branch=main)
A plugin for Fastify that adds support for `typrorm QueryRunner and Transaction`.
Supports Fastify versions 4.x.
> Support TypeScript
## Install
```shell
# npm
npm i @web-server-userland/fastify-typeorm-query-runner
# pnpm
pnpm add @web-server-userland/fastify-typeorm-query-runner
# yarn
yarn add @web-server-userland/fastify-typeorm-query-runner
```
## Usage
```JavaScript
const fastify = require('fastify')()
fastify.register(require('@web-server-userland/fastify-typeorm-query-runner'), {
dataSource: dataSource,
transaction: true,
match: request => request.routerPath.startsWith('/v2'),
respIsError: (respStr) => respStr === '{"status":false}'
})
fastify.get('/', async (req, reply) => {
console.log(req.queryRunner)
console.log(req.queryRunner.manager)
await req.queryRunner.manager.insert();
reply.send({ hello: 'world' })
})
fastify.listen(3000)
```
### Options
* `dataSource`(Required): TypeORM dataSource
* `transaction`(Optional): Whether to bind the life cycle of a thing to a request. - **default: `false`**
- Receiving requests: opening transaction
- Return response: close transaction
- Requesting an error: rolling back transaction
* `match`(Optional): Only matching requests will enable the plugin. - **default: `() => true`**
- Receiving requests: opening transaction
- Return response: close transaction
- Requesting an error: rolling back transaction
```javascript
match: request => {
return request.routerPath.startsWith('/v2')
}
```
* `respIsError`(Optional): When the response matches the condition, it is considered an error - **default: `() => false`**
```javascript
respIsError: (respStr) => {
return respStr === '{"status":false}'
}
```
## License
Licensed under [MIT](./LICENSE).