https://github.com/inthepocket/fastify-typeorm-plugin
Fastify plugin for TypeORM
https://github.com/inthepocket/fastify-typeorm-plugin
fastify fastify-plugin typeorm
Last synced: 4 days ago
JSON representation
Fastify plugin for TypeORM
- Host: GitHub
- URL: https://github.com/inthepocket/fastify-typeorm-plugin
- Owner: inthepocket
- License: mit
- Created: 2019-12-19T12:47:18.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-08-04T08:48:06.000Z (over 2 years ago)
- Last Synced: 2025-04-13T04:36:22.899Z (14 days ago)
- Topics: fastify, fastify-plugin, typeorm
- Language: JavaScript
- Homepage:
- Size: 29.3 KB
- Stars: 25
- Watchers: 5
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-typeorm
[](https://npm.im/fastify-typeorm-plugin)
[](https://travis-ci.org/inthepocket/fastify-typeorm-plugin)
[](https://greenkeeper.io/)
[](https://coveralls.io/github/inthepocket/fastify-typeorm-plugin?branch=master)Fastify plugin for TypeORM for sharing the same TypeORM connection in every part of your server.
Under the hood the official [TypeORM](https://www.npmjs.com/package/typeorm) module is used.## Install
```sh
npm install fastify-typeorm-plugin
```## Usage
Add it to your project with `register` and you are done!
The plugin accepts the [same connection options](https://typeorm.io/#/connection-options) as the TypeORM client.```js
const fastify = require('fastify')();const user = require('./entity/user');
fastify.register(require('fastify-typeorm-plugin'), {
type: 'sqlite',
database: './mydb.sql',
});fastify.get('/users', async function(req, reply) {
const users = await this.orm
.getRepository(User)
.createQueryBuilder('user')
.getMany();return users;
});fastify.listen(3000, err => {
if (err) throw err;
});
```If you won't pass config, it will use `typeorm` default [createConnection](https://typeorm.io/#/connection/creating-a-new-connection) mechanism:
```js
const fastify = require('fastify')();const user = require('./entity/user');
fastify.register(require('fastify-typeorm-plugin'));
fastify.get('/users', async function(req, reply) {
const users = await this.orm
.getRepository(User)
.createQueryBuilder('user')
.getMany();return users;
});fastify.listen(3000, err => {
if (err) throw err;
});
```You can also pass in an existing connection:
```js
const { createConnection } = require('typeorm');const fastify = require('fastify')();
const connection = await createConnection({
type: 'sqlite',
database: './mydb.sql',
});
fastify.register(require('fastify-typeorm-plugin'), {
connection,
});
```## Team
- Glenn Bostoen
- Jonas Goderis## License
Licensed under [MIT](./LICENSE).