Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,34 @@ fastify.listen(3000, err => {
})
```

### `pg` option
If you want to provide your own `pg` module, for example to support packages like [`pg-range`](https://www.npmjs.com/package/pg-range), you can provide an optional `pg` option with the patched library to use:

```js
const fastify = require('fastify')
const pg = require("pg");
require("pg-range").install(pg)

fastify.register(require('fastify-postgres'), {
connectionString: 'postgres://postgres@localhost/postgres',
pg: pg
})

fastify.get('/user/:id', (req, reply) => {
fastify.pg.query(
'SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id],
function onResult (err, result) {
reply.send(err || result)
}
)
})

fastify.listen(3000, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
```

## Development and Testing

First, start postgres with:
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const fp = require('fastify-plugin')
var pg = require('pg')
var defaultPg = require('pg')

function transactionUtil (pool, fn, cb) {
pool.connect((err, client, done) => {
Expand Down Expand Up @@ -56,6 +56,10 @@ function transact (fn, cb) {
}

function fastifyPostgres (fastify, options, next) {
let pg = defaultPg
if (options.pg) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this seems legit to be able to pass pg into it, but I don't think there's a good use case.

At this line pg === defaultPg is actually true even if you pass the pg instance after patching it with pg-range, so not sure if there's any benefit in passing it altogether.

All pg-range does is patch the pg, it doesn't return a new/different object. So just requireing pg-range before fastify-postgres should do the trick.

Or am I missing something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I am not a continuous postgres user, it looks like require('pg') returns a factory and plugins are installed to that factory instance only so you need to pass that factory instance in?

@temsa do you mind trying the suggestion here to see it works?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree @cemremengu.

Also, the main reason I'm skeptical about this is that fastify-postgres is a dedicated library for pg. We do not support any other similar postgres module. Which is why I think allowing the user to pass a pg instance is not so intuitive because the user is free to pass any object and we have no control over it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollina what do you think about this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also enables mocking, which might be a good feature to support.

pg = options.pg
}
if (options.native) {
delete options.native
if (!pg.native) {
Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,32 @@ test('use native module', t => {
})
})

test('use alternative pg module', t => {
const altPg = require('pg')
t.plan(2)

const fastify = Fastify()

fastify.register(fastifyPostgres, {
connectionString: 'postgres://postgres@localhost/postgres',
pg: altPg
})

fastify.ready(err => {
t.error(err)
fastify.pg
.query('SELECT 1 AS one')
.then(result => {
t.ok(result.rows[0].one === 1)
fastify.close()
})
.catch(err => {
t.fail(err)
fastify.close()
})
})
})

test('fastify.pg.test namespace should exist', t => {
t.plan(6)

Expand Down