2.2.1 • Published 2 years ago

fastify-typeorm-plugin v2.2.1

Weekly downloads
44
License
MIT
Repository
github
Last release
2 years ago

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

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.

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 mechanism:

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:

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

License

Licensed under MIT.

2.2.1

2 years ago

2.2.0

3 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.0.0

4 years ago