generic-pool
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Generic-pool is an npm package delivering a versatile resource pool with a Promise-based API. As developer, it enables you to conveniently reuse or throttle usage of resource-intensive components such as database connections, hence increasing your application's performance and decreasing resource expenses.
This npm package is highly intuitive to use. To start with, install the package with the following command:
$ npm install generic-pool [--save]
Afterward, you can utilize the package in your code, using the create
and destroy
functions in a factory object. Here's an example code snippet on how to use generic-pool:
// import the needed packages
const genericPool = require('generic-pool');
const DbDriver = require('some-db-driver');
// Set the step 1 - Create pool using a factory object
const factory = {
create: function() {
return DbDriver.createClient();
},
destroy: function(client) {
client.disconnect();
}
};
// Define the options
const opts = {
max: 10, // maximum size of the pool
min: 2 // minimum size of the pool
};
// Create the pool
const myPool = genericPool.createPool(factory, opts);
// Step 2 - Use pool in your code to obtain/release resources
// Acquire connection - Promise is resolved once a resource becomes accessible
const resourcePromise = myPool.acquire();
resourcePromise
.then(function(client) {
client.query("select * from foo", [], function() {
// return object back to the pool
myPool.release(client);
});
})
.catch(function(err) {
// handle error - this is generally a timeout or maxWaitingClients error
});
// Step 3 - Drain pool during shutdown (optional)
// Only call this once in your application -- at the point you want to shutdown and stop utilizing this pool
myPool.drain().then(function() {
myPool.clear();
});
The documentation for the usage of generic-pool
is provided in this package's README file on its GitHub page. The documentation includes all methods and functions you can use with generic-pool
, as well as various examples and notes on its use. It provides in-depth information about factory object functions (create
, destroy
, validate
), pool configuration options, and pool methods (acquire
, release
, isBorrowedResource
, destroy
, on
, start
, ready
, use
, and others), which enable developers to adapt generic-pool
to their specific needs. Detailed error handling and unit testing instructions are also provided.