From 2281be755703fe84868c53a5215b86ccd960de25 Mon Sep 17 00:00:00 2001 From: Glenn Bostoen Date: Fri, 20 Dec 2019 09:58:58 +0100 Subject: [PATCH 1/4] Fix link to TypeORM --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bc6a89f..a79277f 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![Build Status](https://travis-ci.org/inthepocket/fastify-typeorm-plugin.svg?branch=master)](https://travis-ci.org/inthepocket/fastify-typeorm-plugin) 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 From a19fd2e997a8f392734eb0f71ef679c404258f32 Mon Sep 17 00:00:00 2001 From: Glenn Bostoen Date: Sat, 21 Dec 2019 17:43:32 +0100 Subject: [PATCH 2/4] Set typeorm configuration on root options --- index.js | 5 ++--- test.js | 34 +++++++++++++++------------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 9f480e1..ea2ca97 100644 --- a/index.js +++ b/index.js @@ -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) { diff --git a/test.js b/test.js index 88a674a..f7345f3 100644 --- a/test.js +++ b/test.js @@ -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() @@ -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() @@ -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() }) From 23dbd5a28a83a1be842a7d28fd60a1a3e08c3b1a Mon Sep 17 00:00:00 2001 From: Glenn Bostoen Date: Sat, 21 Dec 2019 17:44:15 +0100 Subject: [PATCH 3/4] Add more information on how to use --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a79277f..1e2ec0f 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,56 @@ [![Build Status](https://travis-ci.org/inthepocket/fastify-typeorm-plugin.svg?branch=master)](https://travis-ci.org/inthepocket/fastify-typeorm-plugin) 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. +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 From 2bdf3791cc6765571ffdbb926781e9e4d414c66e Mon Sep 17 00:00:00 2001 From: Glenn Bostoen Date: Sat, 21 Dec 2019 17:51:54 +0100 Subject: [PATCH 4/4] Add npm badge --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1e2ec0f..eca6037 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # fastify-typeorm-plugin +[![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) Fastify plugin for TypeORM for sharing the same TypeORM connection in every part of your server.