What is sequelize-pool?
The sequelize-pool npm package is a standalone library for managing resource pools, such as database connections. It is designed to be used with Sequelize, a popular ORM for Node.js, but can also be used independently. The package provides a robust and efficient way to manage and reuse resources, ensuring optimal performance and resource utilization.
What are sequelize-pool's main functionalities?
Creating a Pool
This code demonstrates how to create a new resource pool using sequelize-pool. The pool is configured with a name, create and destroy functions for managing resources, and parameters for maximum and minimum resources, as well as idle timeout.
const Pool = require('sequelize-pool').Pool;
const pool = new Pool({
name: 'my-pool',
create: function() {
return new Promise((resolve) => {
resolve({}); // Simulate resource creation
});
},
destroy: function(resource) {
return new Promise((resolve) => {
resolve(); // Simulate resource destruction
});
},
max: 10,
min: 2,
idle: 10000
});
Acquiring a Resource
This code demonstrates how to acquire a resource from the pool. Once the resource is acquired, it can be used for various operations. After usage, the resource is released back to the pool.
pool.acquire().then((resource) => {
console.log('Resource acquired:', resource);
// Use the resource
pool.release(resource);
});
Releasing a Resource
This code demonstrates how to release a resource back to the pool after it has been used. Releasing resources ensures they can be reused, optimizing resource management.
pool.acquire().then((resource) => {
console.log('Resource acquired:', resource);
// Use the resource
pool.release(resource).then(() => {
console.log('Resource released');
});
});
Draining the Pool
This code demonstrates how to drain the pool, which means no new resources will be created. After draining, all existing resources are destroyed. This is useful for gracefully shutting down applications.
pool.drain().then(() => {
console.log('Pool drained');
return pool.destroyAllNow();
}).then(() => {
console.log('All resources destroyed');
});
Other packages similar to sequelize-pool
generic-pool
generic-pool is a generic resource pool library for Node.js. It provides similar functionality to sequelize-pool, including resource creation, acquisition, release, and destruction. However, generic-pool is more general-purpose and can be used for a wider range of resource types.
sequelize-pool
Resource pool. Can be used to reuse or throttle expensive resources such as
database connections.
This is a fork from generic-pool@v2.5.
Installation
$ npm install --save sequelize-pool
$ yarn add sequelize-pool
Example
Step 1 - Create pool using a factory object
var Pool = require('sequelize-pool').Pool;
var mysql2 = require('mysql2/promise');
var pool = new Pool({
name : 'mysql',
create : function() {
return mysql2.createConnection({
user: 'scott',
password: 'tiger',
database:'mydb'
});
},
destroy : function(client) { client.end(); },
max : 10,
min : 2,
idleTimeoutMillis : 30000,
acquireTimeoutMillis: 30000,
log : true
});
Step 2 - Use pool in your code to acquire/release resources
pool.acquire().then(connection => {
client.query("select * from foo", [], function() {
pool.release(client);
});
});
Step 3 - Drain pool during shutdown (optional)
If you are shutting down a long-lived process, you may notice
that node fails to exit for 30 seconds or so. This is a side
effect of the idleTimeoutMillis behaviour -- the pool has a
setTimeout() call registered that is in the event loop queue, so
node won't terminate until all resources have timed out, and the pool
stops trying to manage them.
This behaviour will be more problematic when you set factory.min > 0,
as the pool will never become empty, and the setTimeout calls will
never end.
In these cases, use the pool.drain() function. This sets the pool
into a "draining" state which will gracefully wait until all
idle resources have timed out. For example, you can call:
pool.drain().then(() => pool.destroyAllNow());
If you do this, your node process will exit gracefully.
Draining
If you know would like to terminate all the resources in your pool before
their timeouts have been reached, you can use destroyAllNow()
in conjunction
with drain()
:
pool.drain().then(() => pool.destroyAllNow());
One side-effect of calling drain()
is that subsequent calls to acquire()
will throw an Error.
Pool info
The following functions will let you get information about the pool:
pool.name
pool.size
pool.available
pool.waiting
pool.maxSize
pool.minSize
Run Tests
$ npm install
$ npm test