-
Notifications
You must be signed in to change notification settings - Fork 217
FAQ
- WARNING: Creating a duplicate database object for the same connection
- Can this library work with Native Bindings?
- How to access the instance of node-postgres that's used?
- How to format queries without executing them?
- Can I use only the query formatting from pg-promise, and nothing else?
- Why use method
batch
instead ofpromise.all
?
As per the Database API, you should not create more than one Database object for the same connection, as it will only slow down your application and consume more memory.
If you keep creating a new Database object for the same connection, you will be seeing this warning in development environment:
WARNING: Creating a duplicate database object for the same connection.
Can this library work with Native Bindings?
Yes.
Similar to the original instructions, you need to add pg-native to your project's dependencies.
And then you can pass in initialization option pgNative=true
to activate use of Native Bindings.
How to access the instance of node-postgres that's used?
The library exposes the instance of node-postgres via property pgp.pg
that's available after initializing the library.
For example, if you want to add your custom parser via pg-types, you can do:
const pgp = require('pg-promise')(/*options*/);
const types = pgp.pg.types;
types.setTypeParser(...);
or to change the default size of the connection pool:
const pgp = require('pg-promise')(/*options*/);
pgp.pg.defaults.max = 20;
The reason property pg
is available after initialization and not from the root of the library is because it depends on initialization option pgNative
, which when set, automatically changes property pg
to represent the Native Bindings.
The library exposes all its query formatting methods to the client via namespace pgp.as
:
const pgp = require('pg-promise');
const s = pgp.as.format("SELECT * FROM table WHERE field1 = $1", 123);
console.log(s);
Output:
SELECT * FROM table WHERE field1 = 123
Can I use only the query formatting from pg-promise, and nothing else?
Yes. You can load and use the formatting module from the library independently:
const format = require('pg-promise/lib/formatting').as.format;
const s = format("SELECT * FROM table WHERE field1 = $1 AND field2 = $2", ['one', 123]);
console.log(s);
Output:
SELECT * FROM table WHERE field1 = 'one' AND field2 = 123
When executing queries via methods task or tx, it is done with a shared connection those methods automatically allocate and release.
If your call into promise.all
rejects, the containing method (task or tx) releases the shared connection at once. Any query request that hasn't been settled yet will continue executing against the released connection, resulting in errors.
The most typical error in this case is: Querying against a released or lost connection.
It is quintessential to settle all the promises-queries created within your task or transaction, before the connection is released, which is exactly what method batch makes sure of, by settling all the promises.
pg-promise