-
Couldn't load subscription status.
- Fork 220
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?](#can-i-use-only-the-query-formatting-from-pg- promise-and-nothing-else)
- Why use method
batchinstead ofpromise.all?
Added in v.4.7.0:
- Global event listeners, to prevent application crash when the database server goes offline
- A warning when creating duplicate Database instances for the same connection
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 create needless event listeners.
If you keep creating a new Database object for the same connection, you will be seeing this warning in development environment, often accompanied by another one:
Warning: Possible EventEmitter memory leak detected. 11 error listeners added. Use emitter.setMaxListeners() to increase limit.
NOTE: You should not increase the limit, you should make your Database object global/shared.
Can this library work with Native Bindings?
Yes, starting with version 3.5.0.
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?
NOTE: This has been updated for version 3.7.0 and later.
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:
var pgp = require('pg-promise')(/*options*/);
var types = pgp.pg.types;
types.setTypeParser(...);or to change the default size of the connection pool:
var pgp = require('pg-promise')(/*options*/);
pgp.pg.defaults.poolSize = 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:
var pgp = require('pg-promise');
var 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:
var format = require('pg-promise/lib/formatting').as.format;
var 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: Loose request outside an expired 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