Skip to content

Commit

Permalink
add possibility to omit config (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-popov-tech authored Sep 6, 2021
1 parent 0a9de49 commit 4730703
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down

0 comments on commit 4730703

Please sign in to comment.