Skip to content
This repository was archived by the owner on Jun 3, 2018. It is now read-only.

db hint #1

Open
vitaly-t opened this issue Apr 11, 2016 · 1 comment
Open

db hint #1

vitaly-t opened this issue Apr 11, 2016 · 1 comment

Comments

@vitaly-t
Copy link

on your code here:

this.db = pgp(connectionOptions);

    connect(connectionOptions) {
        this.db = pgp(connectionOptions);
    }

You should know that that code does nothing really. It doesn't connect to the database, merely creates a new database object, as this library uses lazy connection. This is why in all the examples db is created once, globally.

See also related: http://stackoverflow.com/questions/36120435/verify-database-connection-with-pg-promise-when-starting-an-app

And since event extend is triggered for each such object, it would only reduce the performance, trying to extend every new instance created, while quite pointlessly.

@vitaly-t
Copy link
Author

You are not the first one to make that mistake, and I just updated the documentation to underline this.

From https://github.com/vitaly-t/pg-promise#connection

Object db represents the database protocol, with lazy database connection, i.e. only the actual query methods acquire and release the connection. Therefore, you should create only one global/shared db object per connection details.

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant