Skip to content

Commit

Permalink
Merge branch 'feature/non-nested-config'
Browse files Browse the repository at this point in the history
  • Loading branch information
gboston committed Dec 21, 2019
2 parents d2b905d + 2bdf379 commit 8ebead7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 25 deletions.
52 changes: 49 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,60 @@
# fastify-typeorm-plugin

[![Build Status](https://travis-ci.org/inthepocket/fastify-typeorm-plugin.svg?branch=master)](https://travis-ci.org/inthepocket/fastify-typeorm-plugin) [![Greenkeeper badge](https://badges.greenkeeper.io/inthepocket/fastify-typeorm-plugin.svg)](https://greenkeeper.io/)
[![Package Version](https://img.shields.io/npm/v/fastify-typeorm-plugin.svg)](https://npm.im/fastify-typeorm-plugin)
[![Build Status](https://travis-ci.org/inthepocket/fastify-typeorm-plugin.svg?branch=master)](https://travis-ci.org/inthepocket/fastify-typeorm-plugin)
[![Greenkeeper badge](https://badges.greenkeeper.io/inthepocket/fastify-typeorm-plugin.svg)](https://greenkeeper.io/)

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/typeorm) module is used.
Under the hood the official [TypeORM](https://www.npmjs.com/package/typeorm) module is used.

## Install

```sh
npm i fastify-typeorm-plugin
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;
});
```

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,
});
```

## License
Expand Down
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ const fp = require('fastify-plugin')
const { createConnection } = require('typeorm')

async function typeormConnector (fastify, options) {
const { namespace, config } = options
const { namespace } = options
delete options.namespace
delete options.config

const connection = options.connection || await createConnection(config)
const connection = options.connection || await createConnection(options)

if (namespace) {
if (!fastify.orm) {
Expand Down
34 changes: 15 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ const fastifyORM = require('./index')
test('Postgres available', async t => {
const fastify = Fastify()
fastify.register(fastifyORM, {
config: {
host: 'localhost',
type: 'postgres',
port: '5432',
password: '',
database: 'postgres',
username: 'postgres'
}
host: 'localhost',
type: 'postgres',
port: '5432',
password: '',
database: 'postgres',
username: 'postgres'
})

await fastify.ready()
Expand All @@ -25,7 +23,7 @@ test('Postgres available', async t => {

test('with unreachable db', async t => {
const fastify = Fastify()
fastify.register(fastifyORM, { config: { host: 'localhost', type: 'orm' } })
fastify.register(fastifyORM, { host: 'localhost', type: 'orm' })

try {
await fastify.ready()
Expand All @@ -39,19 +37,17 @@ test('with unreachable db', async t => {
test('namespaced', async t => {
const fastify = Fastify()
fastify.register(fastifyORM, {
config: {
host: 'localhost',
type: 'postgres',
port: '5432',
password: '',
database: 'postgres',
username: 'postgres'
},
namespace: 'cluster'
host: 'localhost',
type: 'postgres',
port: '5432',
password: '',
database: 'postgres',
username: 'postgres',
namespace: 'con1'
})

await fastify.ready()
t.strictEqual(fastify.orm.cluster.name, 'default')
t.strictEqual(fastify.orm.con1.name, 'default')
await fastify.close()
})

Expand Down

0 comments on commit 8ebead7

Please sign in to comment.