lukasmartinelli / postgis-editor

An accessible PostGIS query editor and visualizer.
MIT License
193 stars 29 forks source link

db hint #1

Open vitaly-t opened 8 years ago

vitaly-t commented 8 years ago

on your code here: https://github.com/lukasmartinelli/postgis-editor/blob/b13ab81b99dc49d486fd62067f56651a10e78208/src/database.js#L25

    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 commented 8 years ago

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.