项目作者: inthepocket

项目描述 :
Fastify plugin for TypeORM
高级语言: JavaScript
项目地址: git://github.com/inthepocket/fastify-typeorm-plugin.git
创建时间: 2019-12-19T12:47:18Z
项目社区:https://github.com/inthepocket/fastify-typeorm-plugin

开源协议:MIT License

下载


fastify-typeorm

Package Version
Build Status
Greenkeeper badge
Coverage Status

Fastify plugin for TypeORM for sharing the same TypeORM connection in every part of your server.
Under the hood the official TypeORM module is used.

Install

  1. npm install fastify-typeorm-plugin

Usage

Add it to your project with register and you are done!
The plugin accepts the same connection options as the TypeORM client.

  1. const fastify = require('fastify')();
  2. const user = require('./entity/user');
  3. fastify.register(require('fastify-typeorm-plugin'), {
  4. type: 'sqlite',
  5. database: './mydb.sql',
  6. });
  7. fastify.get('/users', async function(req, reply) {
  8. const users = await this.orm
  9. .getRepository(User)
  10. .createQueryBuilder('user')
  11. .getMany();
  12. return users;
  13. });
  14. fastify.listen(3000, err => {
  15. if (err) throw err;
  16. });

If you won’t pass config, it will use typeorm default createConnection mechanism:

  1. const fastify = require('fastify')();
  2. const user = require('./entity/user');
  3. fastify.register(require('fastify-typeorm-plugin'));
  4. fastify.get('/users', async function(req, reply) {
  5. const users = await this.orm
  6. .getRepository(User)
  7. .createQueryBuilder('user')
  8. .getMany();
  9. return users;
  10. });
  11. fastify.listen(3000, err => {
  12. if (err) throw err;
  13. });

You can also pass in an existing connection:

  1. const { createConnection } = require('typeorm');
  2. const fastify = require('fastify')();
  3. const connection = await createConnection({
  4. type: 'sqlite',
  5. database: './mydb.sql',
  6. });
  7. fastify.register(require('fastify-typeorm-plugin'), {
  8. connection,
  9. });

Team

License

Licensed under MIT.