Skip to content

Repository files navigation

lightning-pool

NPM Version NPM Downloads CI Tests Test Coverage

About

High performance resource pool written with TypeScript.

  • A fast Resource Pool implementation for JavaScript - see doc/BENCHMARKS.md for the numbers against generic-pool, or benchmark/README.md to reproduce them yourself
  • Advanced configuration options, suits for enterprise level applications
  • Configuration can be changed while pool running
  • Promise based factory supported
  • Supports validation and resource reset
  • Fully tested. (%100 coverage)

Installation

  • npm install lightning-pool --save

Example

import {Pool} from 'lightning-pool';
import dbDriver from 'some-db-driver';

/**
 * Step 1 - Create a factory object
 */
const factory = {
    create: async function(opts) {
        const client = await DbDriver.createClient();
        return client;
    },
    destroy: async function(client) {  
       await client.close();       
    },
    reset: async function(client){   
       await client.rollback();       
    },
    validate: async function(client) {
       await client.query('select 1');       
    }    
};

/**
 * Step 2 - Create a the pool object
 */
const pool = new Pool(factory, {  
    max: 10,    // maximum size of the pool
    min: 2,     // minimum size of the pool
    minIdle: 2  // minimum idle resources
});

pool.start();

/**
 * Step 3 - Use pool in your code to acquire/release resources
 */
// acquire connection - Promise is resolved
const client = await pool.acquire();
// once a resource becomes available
// Use resource
await client.query("select * from foo");
// return object back to pool
await pool.releaseAsync(client);

/**
 * Step 4 - Shutdown pool (optional)
 * Call close(force) when you need to shutdown the pool
 */

// Wait for active resource for 5 sec than force shutdown
await pool.close(5000);

Documentation

See doc/API.md for the full API reference - factory/options shape, every method and property, emitted events, and the PoolState/ResourceState enums.

Node Compatibility

  • node >= 16.0;

License

MIT

About

High performance resource pool written with TypeScript

Topics

Resources

Stars

31 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages