From 47307032eb9fcb9d6d88a909bfdcfc3b8a00a4cd Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Mon, 6 Sep 2021 12:11:49 +0300 Subject: [PATCH] add possibility to omit config (#42) --- README.md | 27 +++++++++++++++++++++++++-- index.js | 9 ++++++++- test.js | 14 ++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c446caa..3f97616 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![Greenkeeper badge](https://badges.greenkeeper.io/inthepocket/fastify-typeorm-plugin.svg)](https://greenkeeper.io/) [![Coverage Status](https://coveralls.io/repos/github/inthepocket/fastify-typeorm-plugin/badge.svg?branch=master)](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. +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 @@ -16,7 +16,7 @@ npm install fastify-typeorm-plugin ## Usage -Add it to your project with `register` and you are done! +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 @@ -43,6 +43,29 @@ fastify.listen(3000, 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 diff --git a/index.js b/index.js index b7ee7b4..f479670 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,14 @@ async function typeormConnector (fastify, options) { const { namespace } = options delete options.namespace - const connection = options.connection || await createConnection(options) + let connection + if (options.connection) { + connection = options.connection + } else if (Object.keys(options).length) { + connection = await createConnection(options) + } else { + connection = await createConnection() + } if (namespace) { if (!fastify.orm) { diff --git a/test.js b/test.js index a2fe896..a6ac8b6 100644 --- a/test.js +++ b/test.js @@ -21,6 +21,20 @@ test('Postgres available', async t => { await fastify.close() }) +test('Postgres available through env variables', async t => { + process.env.TYPEORM_CONNECTION = 'postgres' + process.env.TYPEORM_HOST = 'localhost' + process.env.TYPEORM_USERNAME = 'postgres' + process.env.TYPEORM_DATABASE = 'postgres' + process.env.TYPEORM_PORT = '5432' + const fastify = Fastify() + fastify.register(fastifyORM) + + await fastify.ready() + t.strictEqual(fastify.orm.name, 'default') + await fastify.close() +}) + test('with unreachable db', async t => { const fastify = Fastify() fastify.register(fastifyORM, { host: 'localhost', type: 'orm' })