Giter Club home page Giter Club logo

fastify-postgres's Introduction

CI Package Manager CI Web SIte js-standard-style CII Best Practices

NPM version NPM downloads Security Responsible Disclosure Discord Contribute with Gitpod Open Collective backers and sponsors


An efficient server implies a lower cost of the infrastructure, a better responsiveness under load and happy users. How can you efficiently handle the resources of your server, knowing that you are serving the highest number of requests as possible, without sacrificing security validations and handy development?

Enter Fastify. Fastify is a web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. It is inspired by Hapi and Express and as far as we know, it is one of the fastest web frameworks in town.

The main branch refers to the Fastify v4 release. Check out the v3.x branch for v3.

Table of Contents

Quick start

Create a folder and make it your current working directory:

mkdir my-app
cd my-app

Generate a fastify project with npm init:

npm init fastify

Install dependencies:

npm i

To start the app in dev mode:

npm run dev

For production mode:

npm start

Under the hood npm init downloads and runs Fastify Create, which in turn uses the generate functionality of Fastify CLI.

Install

To install Fastify in an existing project as a dependency:

Install with npm:

npm i fastify

Install with yarn:

yarn add fastify

Example

// Require the framework and instantiate it

// ESM
import Fastify from 'fastify'

const fastify = Fastify({
  logger: true
})
// CommonJs
const fastify = require('fastify')({
  logger: true
})

// Declare a route
fastify.get('/', (request, reply) => {
  reply.send({ hello: 'world' })
})

// Run the server!
fastify.listen({ port: 3000 }, (err, address) => {
  if (err) throw err
  // Server is now listening on ${address}
})

with async-await:

// ESM
import Fastify from 'fastify'

const fastify = Fastify({
  logger: true
})
// CommonJs
const fastify = require('fastify')({
  logger: true
})

fastify.get('/', async (request, reply) => {
  reply.type('application/json').code(200)
  return { hello: 'world' }
})

fastify.listen({ port: 3000 }, (err, address) => {
  if (err) throw err
  // Server is now listening on ${address}
})

Do you want to know more? Head to the Getting Started.

Note

.listen binds to the local host, localhost, interface by default (127.0.0.1 or ::1, depending on the operating system configuration). If you are running Fastify in a container (Docker, GCP, etc.), you may need to bind to 0.0.0.0. Be careful when deciding to listen on all interfaces; it comes with inherent security risks. See the documentation for more information.

Core features

  • Highly performant: as far as we know, Fastify is one of the fastest web frameworks in town, depending on the code complexity we can serve up to 76+ thousand requests per second.
  • Extensible: Fastify is fully extensible via its hooks, plugins and decorators.
  • Schema based: even if it is not mandatory we recommend to use JSON Schema to validate your routes and serialize your outputs, internally Fastify compiles the schema in a highly performant function.
  • Logging: logs are extremely important but are costly; we chose the best logger to almost remove this cost, Pino!
  • Developer friendly: the framework is built to be very expressive and help the developer in their daily use, without sacrificing performance and security.

Benchmarks

Machine: EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD.

Method:: autocannon -c 100 -d 40 -p 10 localhost:3000 * 2, taking the second average

Framework Version Router? Requests/sec
Express 4.17.3 14,200
hapi 20.2.1 42,284
Restify 8.6.1 50,363
Koa 2.13.0 54,272
Fastify 4.0.0 77,193
-
http.Server 16.14.2 74,513

Benchmarks taken using https://github.com/fastify/benchmarks. This is a synthetic, "hello world" benchmark that aims to evaluate the framework overhead. The overhead that each framework has on your application depends on your application, you should always benchmark if performance matters to you.

Documentation

中文文档地址

Ecosystem

  • Core - Core plugins maintained by the Fastify team.
  • Community - Community supported plugins.
  • Live Examples - Multirepo with a broad set of real working examples.
  • Discord - Join our discord server and chat with the maintainers.

Support

Please visit Fastify help to view prior support issues and to ask new support questions.

Contributing

Whether reporting bugs, discussing improvements and new ideas or writing code, we welcome contributions from anyone and everyone. Please read the CONTRIBUTING guidelines before submitting pull requests.

Team

Fastify is the result of the work of a great community. Team members are listed in alphabetical order.

Lead Maintainers:

Fastify Core team

Fastify Plugins team

Great Contributors

Great contributors on a specific area in the Fastify ecosystem will be invited to join this group by Lead Maintainers.

Past Collaborators

Hosted by

We are a At-Large Project in the OpenJS Foundation.

Sponsors

Support this project by becoming a SPONSOR! Fastify has an Open Collective page where we accept and manage financial contributions.

Acknowledgements

This project is kindly sponsored by:

Past Sponsors:

This list includes all companies that support one or more of the team members in the maintenance of this project.

License

Licensed under MIT.

For your convenience, here is a list of all the licenses of our production dependencies:

  • MIT
  • ISC
  • BSD-3-Clause
  • BSD-2-Clause

fastify-postgres's People

Contributors

0xvbetsun avatar bdun94 avatar billouboq avatar cemremengu avatar darkgl0w avatar delvedor avatar dependabot-preview[bot] avatar dependabot[bot] avatar eomm avatar fdawgs avatar frikille avatar github-actions[bot] avatar greenkeeper[bot] avatar jsumners avatar lependu avatar lucarainone avatar mcollina avatar michieldemey avatar pavish avatar salmanm avatar stefee avatar tahseenio avatar temsa avatar toni-sharpe avatar uzlopak avatar voxpelli avatar wedgwood avatar zekth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fastify-postgres's Issues

suggest: use pg-native

Hi guys,

I suggest to use pg-native instead of pg, for high performance. It comes with the same interface of pg.

connect is not function

connect is not function

i try to follow the documentation but i got error .. how can i fix it thanks

MY controller

const fastify = require('fastify')()
fastify.register(require('fastify-postgres'), {
  connectionString: 'postgres://username:pass@localhost:5432/db'
})

exports.getAllProduct = async ( req, reply ) => {
	fastify.pg.connect(onConnect)

	function onConnect ( err, client, release ) {
		if ( err ) return reply.send( err )

		client.query(
			'SELECT id, first_name, last_name FROM person',
			function onResult ( err, result ) {
				release()
				reply.send(err || result)
			}
		)
	}
}

routes

async function routes( fastify, options ) {
	const ProductController = require('../controllers/product')
	fastify.get('/', ProductController.getAllProduct)
}

module.exports = routes;
const fastify = require('fastify')({
	logger: true
})

fastify.register(require('./api/routes/product'), {
	prefix: '/product'
});


fastify.listen(3000, ( err, address ) => {
	if ( err ) {
		fastify.log.error( err )
		process.exit( 1 )
	}
	fastify.log.info(`server listening on address ${ address }`)
})

THE ERROR

{
    "statusCode": 500,
    "error": "Internal Server Error",
    "message": "Cannot read property 'connect' of undefined"
}

Typescript types missing for FastifyRequest and RouteShorthandOptions when using the transact route option

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.0.0

Plugin version

5.0.0

Node.js version

16.16.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.4

Description

When attempting to use the transact route option using typescript, passing in the transact route options and referencing pg from the request object throws typescript type errors. This is because because pg has not been added to FastifyRequest and

interface FastifyPostgresRouteOptions {
  transact: boolean | string;
}

has not been added for pg for RouteShorthandOptions

Steps to Reproduce

fastify.post(
    '/',
    {
      pg: { transact: true }, // Throws type error for RouteShorthandOptions
    },
    async (req, _reply) => {
      req.pg.query('SELECT * FROM table') // Throws type error for FastifyRequest
    }
  );

Expected Behavior

Using the route transact option should not cause compile errors in typescript

connect / release on high frequency endpoints

I am writing a web mapping framework which uses fastify-postgres in many of the data endpoints. Certain endpoints are hit very frequently (hundreds of times from a single user).

Would you recommend to connect, query and release?

Or would you recommend to connect and store the connection in a global and then just query, query, query.

Both methods are working fine for me but I am concerned the connect handshake is taking time while with the always connected solution I am worried what happens if a connection errs.

An in-range update of tap is breaking the build 🚨

The devDependency tap was updated from 12.1.1 to 12.1.2.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

tap is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 7 commits.

  • cf8a2e1 12.1.2
  • 04d27df Add attributes file
  • 3620c76 Remove some tests of things that can't be done in v11+
  • f61181a Run snapshots in -J mode, remove an extraneous test
  • 699b179 (synonym) added 'identical' to same to create symmetry to 'equivalent' synonym on equal
  • fe466f5 test: don't break when running in buffered mode
  • f1979a9 typo in usage.txt

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of tap is breaking the build 🚨

The devDependency tap was updated from 12.1.2 to 12.1.3.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

tap is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 2 commits.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Mistake in the example

The example below does not work because the query result is in the property rows, not result.
So it really should be const { rows } = await client.query(

fastify.get('/user/:id', async (req, reply) => {
  const client = await fastify.pg.connect()
  const { result } = await client.query(
    'SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id],
  )
  client.release()
  return result
})

An in-range update of fastify is breaking the build 🚨

The devDependency fastify was updated from 2.4.1 to 2.5.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

fastify is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v2.5.0

Features:

  • Add ability to get response time on reply - #1697
  • Alias schema.query to schema.querystring (#1690) - #1694

Fixes:

  • Added case sensitive option to definition file - #1691
  • fix: use reply instead of original res - #1704
  • Removes double call to afterRouteAdded when ignoreTrailingSlash === true - #1675
  • Fix http2SecureServer test - #1681
  • Adds defaults to TS generics definitions - #1669

Documentation:

  • docs: fix typos for logging - #1701
  • Add RBAC plugin - #1692
  • Update docs to fix req.body inside logger req - #1695
Commits

The new version differs by 12 commits.

  • 901911c Bumped v2.5.0
  • 1ad7566 add ability to get response time on reply (#1697)
  • fda8448 Added case sensitive option to definition file. (#1691)
  • d8730a2 fix: use reply instead of original res (#1704)
  • b53f037 chore: greenkeeper ignore autocannon (#1696)
  • 00d0e23 docs: fix typos (#1701)
  • bdc137e Alias schema.query to schema.querystring (#1690) (#1694)
  • 41a94c3 docs: fix req.body inside logger req (#1695)
  • ecea232 Removes double call to afterRouteAdded when ignoreTrailingSlash === true (#1675)
  • fc52dc3 Add RBAC plugin to Ecosystem (#1692)
  • 7f274d1 Update node types, typescript, and fix broken test (#1681)
  • 1958c74 Adds defaults to TS generics definitions (#1669)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

There is no documentation for how to work with SSL

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.0.2

Plugin version

5.0.0

Node.js version

16.15.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.4

Description

I was looking for how to disable rejectUnauthorized (or how to work with SSL in general) and found nothing in the documentation.

Enabling SSL by passing ssl=true as a query parameter worked, but only got me to the database rejecting a self-signed cert.

Further, I imagined that passing the standard structure for parameters into the config hash might work... I got as far as this (somewhat janky) config... which unfortunately didn't work.

fastify.register(require('@fastify/postgres'), {
  connectionString: process.env.DATABASE_URL + '?ssl=true',
  ssl: {
    rejectUnauthorized: false
  }
})

The specific use case is 'connect to a Heroku-hosted DB from a Heroku-hosted Fastify app' which is likely a common use case. Since the latest Postgres library defaults to rejectUnauthorized being 'true' I'm guessing this is what 'broke' things. (Though of course this behavior is more correct.)

Steps to Reproduce

Try to configure SSL.

Expected Behavior

I'd find some documentation for how to configure SSL.

Add pg as a peer dependency

Might be better suited for this package to add pg as a peer dependency to allow developers to use their preferred version of pg and to avoid mixing pg versions.

Any specific thoughts on this?

problem with multiple db connections

The plugin hardcode the name decorated as "pg", then you could not add any other postgres instance into it.

Maybe optional name field in option could override the decorating name is nice.

tests failing on Node 11 on travis

The tests are failing on Node 11 on travis.

I cannot understand why, so I'm disabling them.

It would be fantastic if someone could pick this up.

fastify-postgres: Reason why we cannot use a named connection and an unnamed connection at the same time

I have been upgrading fastify v2 to v3, and along with it, all the plugins we use.

In fastify-postgres v2, we were able to use both a named connection and an unnamed connection at the same time. We used fastify.pg for the primary db and fastify.pg.read for the read replica.

In the latest version, I noticed that there is a restriction placed to prevent such usage. While we are perfectly fine with changing the former to a named connection, I would like to know if there was any reason behind this change. Perhaps a design pattern that I am unaware of, that I would like to know.

Thanks.

Allow to override `pg` library

I'd like to use https://www.npmjs.com/package/pg-range with my fastify-postgres.

By default, pg-range is patching the pg in order to work.

var pg = require("pg");
require("pg-range").install(pg);

Currently, I can't override the pg (at least properly) used by fastify-postgres

Could we make an option for overriding it ?

Is the pg-native recommendation outdated?

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

In the README, there is a section about pg-native support which states the following:

If you want maximum performance you can install pg-native, and pass native: true to the plugin options.

Is this claim of maximum performance still true in 2023? Does it still make sense to recommend pg-native to users?

My own findings:

According to this benchmark, the difference in performance is minimal, and actually js pg beats pg-native in 3 out of 4 tests. I've also heard anecdotal evidence from several engineers that they saw no difference in real life performance when switching between pg and pg-native.

On the other hand, there is a clear downside to using pg-native: setup can be a real pain.

Unless there's some evidence that pg-native is indeed still better, I propose changing the README to reflect the current situation, so users won't think they need pg-native for good performance.

"Cannot read properties of null (reading 'release')" when schema validation fails together with transact:true flag

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.0.0

Plugin version

5.2.0

Node.js version

19.4

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

13

Description

Currently, fastify schema validation and the pg: {transact: true} option are not compatible.

The validation schema is executed before the transaction initialization, which is expected behavior. However, when an error response with code FST_ERR_VALIDATION is sent, it triggers the onSend handler of the fastify-postgres hook, which assumes the initialization of a client.

This cause the Cannot read properties of null (reading 'release') error.

Steps to Reproduce

Configure your server:

fastify.post('/strictValidateFail', {
      schema: {
        body: {
          type: 'object',
          properties: {
            hello: { type: 'string' }
          },
          required: ['hello']
        }
      },
      pg: { transact: true }
    }, async (req, reply) => {
      await req.pg.query('THIS QUERY SHOULD NEVER BE EXECUTED WITH WRONG BODY', [])
      return 'never catched'
    });

Send a wrong body post

const response = await fastify.inject({
      url: '/strictValidateFail',
      method: 'POST',
      body: { notValid: 'json input' }
    })

the result is:

{"statusCode":400,"error":"Bad Request","message":"Cannot read properties of null (reading 'release')"}

Expected Behavior

The response should be

{"statusCode":400,"code":"FST_ERR_VALIDATION","error":"Bad Request","message":"body must have required property 'hello'"}

Dependencies problem

The plugin does not work with the latest version of Fastify.

throw new Error(fastify-plugin - expected '${version}' fastify version, '${fastifyVersion}' is installed) Error: fastify-plugin - expected '>=0.13.1' fastify version, '1.0.0-rc.2' is installed at plugin (.../node_modules/fastify-plugin/index.js:24:13)

Automatic transactions

🚀 Feature Proposal

In most applications I've seen:

fastify.get('/something', function (req, reply) {
  return this.pg.transact(async (client) => {
      const { rows } = await client.query('SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id])
      return rows
  })
})

I think we could greatly simplify this by having some dedicated syntax to avoid developer errors

3rd argument

fastify.get('/something', { pg: { transact: true } }, function (req, reply, client) {
  const { rows } = await client.query('SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id])
  return rows
})

or even better

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

fastify.get('/something', function (req, reply, client) {
  const { rows } = await client.query('SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id])
  return rows
})

This can be implemented by having an 'onRoute' hook that wraps the handler.

Decorator

fastify.get('/something', { pg: { transact: true } }, function (req, reply) {
  const { rows } = await req.pg.query('SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id])
  return rows
})

or even better

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

fastify.get('/something', function (req, reply) {
  const { rows } = await req.pg.query('SELECT id, username, hash, salt FROM users WHERE id=$1', [req.params.id])
  return rows
})

This can be implemented by having a 'preHandler' hook that starts the transaction and an 'onSend' hook that commits or roll back it.


What do you prefer? The decorator is probably the more idiomatic approach, even it it will require a bit more work to implement.

Readme.md documentation

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Why should i also install the pg module in addition to the @fastify/postgres module?

postgres Connection Timeout

We are seeing this issue recently, where PostgreSQL has not been under much load but all DB statements are getting connection time-outs. We have set a DB_CONNECTTION_TIMEOUT set at 10 secs.

host: DB_HOST,
    port: DB_PORT,
    database: DB_NAME,
    user: DB_USER,
    password: DB_PASSWORD,
    pg: pg,
    max: 15,
    connectionTimeoutMillis: 10000,
    statementTimeout: 10000,
   keepAlive: true,
   idleTimeoutMillis: 10000

err: {
message: "timeout exceeded when trying to connect"
stack: "Error: timeout exceeded when trying to connect
at Timeout.setTimeout [as _onTimeout] (/app/node_modules/pg-pool/index.js:199:27)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)"
type: "Error"
}

Documented Transactions doesnt work

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.26.2

Plugin version

5.2.2

Node.js version

v18.20.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

12

Description

I am working on a file structure consisting of server.js, route.js and profile.js and am trying to implement a PATCH request within profile.js to save data into the database.
Originally, I had the following code:

fastify.patch('/', {pg: {transact: true}}, async (request) => {
// Code
}

And attempted to implement a query:

try {
    // ...
    const edit = await request.pg.query(`UPDATE wallets
                                         SET username = $1
                                         WHERE phonenumber = $2
                                         AND useractive = true;`, [requestBody.username, accountId]);
    if (edit.rowCount === 0) {
        return {
            success: false,
            error: [
                {
                    message: "Account not found",
                    code: 400
                }
            ]
        }
    }
    // ...
} catch (e) {
    await request.pg.query("ROLLBACK;");
}

However, I am encountering a TypeError: Cannot read properties of null (reading 'query').
I've tried various alternatives, such as renaming {pg: {transact: true}} to {pg: {transact: 'accEdit'}} and changing the query structure from request.pg.query(...) to request.pg['accEdit'].query(...).
Despite these changes, I'm either receiving no errors from fastify-pg (though the test fails and changes are still made) or I'm getting an error stating that fastify.transact doesn't exist.
In addition to assistance with these errors, I would appreciate a practical documentation with Error Handling.

Steps to Reproduce

'use strict'

/**
 *
 * @param fastify
 * @return {Promise<void>}
 */
const editAccount = async (fastify) => {
    fastify.patch('/', {pg: {transact: 'accEdit'}}, async (request) => {
        try {
            const param = request.params.accountId;
            const authKey = request.headers["authorization"] || null;
            const authUsername = request.headers["x-auth-username"] || null;
            const requestBody = request.body || null;
            if (!authKey || !authUsername || !param || !requestBody) {
                return {
                    success: false,
                    error: [
                        {
                            message: "Account not found",
                            code: 400
                        }
                    ]
                };

            const edit = await request.pg.query(`UPDATE wallets
                                                 SET username = $1
                                                 WHERE phonenumber = $2
                                                   AND accountactive = true;`, [requestBody.username, accountId]);
            if (edit.rowCount === 0) {
                return {
                    success: false,
                    error: [
                        {
                            message: "Account not found",
                            code: 400
                        }
                    ]
                }
            }
            fastify.done(); // Thats the thing that makes the test error
            await request.pg.query("COMMIT;")
        } catch (e) {
            await fastify.pg.query("ROLLBACK;");
            fastify.log.error("There was an error updating the Data!\n"+e)

        } finally {
            await fastify.pg.release;
        }
    })
}

module.exports = editAccount;

Expected Behavior

It should get the error and handle it and not commit it to the Database

Tests are not passing on `master`

As of 772ff91 ( version 1.2.1 ), this script would not lead to working tests:

git clone https://github.com/fastify/fastify-postgres.git
cd fastify-postgres
npm i
npm run postgres &
sleep 10
npm test

Note: my docker pg image is cached and instantiated for a while before tests starts.

The tests logs:

> [email protected] test /path/to/fastify-postgres
> standard && tap test.js

test.js ...............................pg-native not installed, can't use native option - fallback to pg module
...ERROR:  relation "users" does not exist at character 13
STATEMENT:  INSERT INTO users(username) VALUES($1) RETURNING id
test.js ............................................. 35/40 11s
 fastify.pg.test use transact util with promise
 not ok (unnamed test)
   stack: |-
     Error: relation "users" does not exist
         at module.exports.Client._readError (/path/to/fastify-postgres/node_modules/pg-native/index.js:155:13)
         at module.exports.Client._emitResult (/path/to/fastify-postgres/node_modules/pg-native/index.js:178:12)
         at module.exports.Client._read (/path/to/fastify-postgres/node_modules/pg-native/index.js:220:31)
         at PQ.emit (events.js:182:13)
         at PQ.EventEmitter.emit (domain.js:442:20)
   message: relation "users" does not exist
   severity: ERROR
   code: 42P01
   position: '13'
   file: parse_relation.c
   line: '1160'
   routine: parserOpenTable
 
 fastify.pg.test use transact util with promise
 not ok test unfinished
   stack: |
     Object.<anonymous> (test.js:257:1)
   test: fastify.pg.test use transact util with promise
   at:
     line: 257
     column: 1
     file: test.js
     function: Object.<anonymous>
   source: |
     test('fastify.pg.test use transact util with promise', t => {
 
 not ok child test left in queue: t.test fastify.pg.test use transact util with callback
 not ok child test left in queue: t.test fastify.pg.test use transact util with commit callback
 not ok test count !== plan
   +++ found                                                          
   --- wanted                                                          
   -12                                                                
   +14                                                                
   results:
     ok: false
     count: 14
     pass: 11
     fail: 3
     bailout: false
     todo: 0
     skip: 0
     plan:
       start: 1
       end: 12
       skipAll: false
       skipReason: ''
       comment: ''
     failures:
       - ok: false
         id: 12
         time: 10028.298
         name: fastify.pg.test use transact util with promise
       - ok: false
         id: 13
         name: >-
           child test left in queue: t.test fastify.pg.test use transact util with
           callback
         parent: ''
       - ok: false
         id: 14
         name: >-
           child test left in queue: t.test fastify.pg.test use transact util with
           commit callback
         parent: ''

total ............................................... 35/40
 

 35 passing (11s)
 5 failing

pg-pool force closing fastify instance

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.x.x

Plugin version

5.2.2

Node.js version

18.x

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

22.04

Description

Hi, I just found out this weird behavior about pg-pool and it turned out a known issue on its repo.
When we shutdown/restart PostgreSQL instance, this module will force close our Fastify app! As my understanding, this happens due to lack of error handler in pg-pool, resulting unexpected termination event when we wrap the query logic using try catch. Due to my poor explanation, please read this issue: brianc/node-postgres#2439.

Steps to Reproduce

  1. Deploy PostgreSQL
  2. Deploy Fastify Instance with PostgreSQL support
  3. Shutdown PostgreSQL

Expected Behavior

No response

Typo in section: Custom Postgres approach

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.0.0

Plugin version

No response

Node.js version

16.16

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

11.3

Description

There seems to be a typo in the Custom Postgres approach section where there "in" is typed as "oin"

Steps to Reproduce

N/A

Expected Behavior

N/A

fastify-postgres is inserting twice into db

🐛 Bug Report

I'm using async/await while inserting values into the database, but it's inserting the values twice for one table. I'm also receiving this error message in console Error: Release called on client which has already been released to the pool.

To Reproduce

Steps to reproduce the behavior:

  1. I've created the project using fastify-cli fastify lang=ts generate
  2. Installed fastify-postgres with npm i fastify-postgres
  3. Added code in support.ts
import fastifyPostgres from "fastify-postgres";

fastify.register(fastifyPostgres, {
  connectionString: 'postgres://postgres@localhost/postgres'
})
  1. Added code in routes\organization\create.ts (only added the handler portion)
handler: function (request, reply) {
      const {
        phone,
        organization_name,
        email,
        full_name,
        password,
        aadhar_number,
      }: any = request.body;

      bcrypt.genSalt(10, function (err, salt) {
        bcrypt.hash(password, salt, async function (err, hash) {
          if (err) {
            return reply.code(500).send({ success: false, error: err });
          }

          try {
            client = await fastify.pg.connect();

            // First create the organization
            const { rows, rowCount } = await client.query(
              "SELECT id FROM public.organizations ORDER BY id DESC LIMIT 1"
            );

            let org_id = 10000;
            if (rowCount > 0) {
              const temp = parseInt(rows[0].id);
              org_id = temp + 1;
            }

            // The below query executes twice
            await client.query(
              SQL`INSERT INTO public.organizations(id, title, phone, email) VALUES(${org_id}, ${organization_name}, ARRAY['91', ${phone}], ${email})`
            );

            // Second create the manager account
            const empid = "KTGEMP".concat(org_id.toString()).concat("01");
            const res = await client.query(
              "SELECT id FROM public.roles WHERE role = 'manager'"
            );
            await client.query(
              SQL`INSERT INTO public.employees(id, full_name, phone, email, password, role_id, aadhar_number, access_level, org_id) VALUES(${empid}, ${full_name}, ARRAY['91', ${phone}], ${email}, ${hash}, ${res.rows[0].id}, ${aadhar_number}, 20, ${org_id})`
            );

            client.release();
            return reply
              .code(201)
              .setCookie("token", fastify.createTokens({ id: empid }))
              .send({
                success: true,
                user: { email, org_id, role: "manager" },
              });
          } catch (error) {
            client.release();
            return reply.code(500).send({
              success: false,
              error,
            });
          }
        });
      });
    },
  });

Expected behavior

I expect organizations table to have 1 record and employees table to have one record. But right now, organization table has 2 records, while employees table has 1 record.

Your Environment

  • node version: v14.15.4
  • fastify version: >=3.10.1
  • os: Windows, Linux
  • fastify postgres: >=3.3.0
  • any other relevant information

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.