DbPool

DbPool

Create a new database pool with default methods and properties

Constructor

new DbPool(options, defaults)

Source:
Parameters:
Name Type Description
options object

an object with default pool properties, see Pool

Properties
Name Type Description
type string

pool type, this is the db driver name

pool string

actual pool name, overrides defaults.name because it is the driver module with generic name

min int

min number of open database connections

max int

max number of open database connections, all attempts to run more will result in clients waiting for the next available db connection, if set to 0 no pooling will be enabled and will result in the unlimited connections, this is default for DynamoDB

max_queue int

how many db requests can be in the waiting queue, above that all requests will be denied instead of putting in the waiting queue

defaults object

an object with default pool methods for init and shutdown and other properties

The db methods cover most use cases but in case native driver needs to be used this is how to get the client and use it with its native API, it is required to call pool.release at the end to return the connection back to the connection pool.

Example
var pool = db.getPool("pg");
pool.use((err, client) => {
   client.query("SELECT * from users", (err, rows) => {
      pool.release(client);
   });
});

// Or async version

const { client } = await pool.ause((err);
const { err, rows, info } = await client.aquery("SELECT * from users");
pool.release(client);

Methods

(async) aquery(client, req)

Description:
  • Async version of query, returns an object { err, rows, info }

Source:
Parameters:
Name Type Description
client object
req DbRequest

cacheColumns(client, options, callbackopt)

Description:
  • Cache columns for all tables

Source:
Parameters:
Name Type Attributes Description
client object
options object
callback function <optional>

cacheIndexes(options, callbackopt)

Description:
  • Cache indexes for all tables

Source:
Parameters:
Name Type Attributes Description
options object
callback function <optional>

closeDb(client, callbackopt)

Description:
  • Close a connection, default is do nothing

Source:
Parameters:
Name Type Attributes Description
client object
callback function <optional>

configure(optionsopt)

Description:
  • Reconfigure properties, only subset of properties are allowed here so it is safe to apply all of them directly, this is called during realtime config update

Source:
Parameters:
Name Type Attributes Description
options object <optional>

convertError(req, err) → {object|Error}

Description:
  • Converts native DB driver error into other human readable format

Source:
Parameters:
Name Type Description
req DbRequest
err object | Error
Returns:
Type Description
object | Error

exists(table) → {boolean}

Description:
  • Return true if given table exists, to be used in createTables

Source:
Parameters:
Name Type Description
table string
Returns:
Type Description
boolean

nextToken(client, req, rows)

Description:
  • Return next token from the client object

Source:
Parameters:
Name Type Description
client object
req DbRequest
rows Array.<object>

openDb(callbackopt)

Description:
  • Open a connection to the database, default is to return an empty object as a client

Source:
Parameters:
Name Type Attributes Description
callback function <optional>

prepare(req)

Description:
Source:
Parameters:
Name Type Description
req DbRequest

prepareOptions(options)

Description:
  • Update the options with pool config parameters if needed, the options is from the request

Source:
Parameters:
Name Type Description
options object

prepareQuery(req)

Description:
  • Perform pool specific actions for prepared query before passing it to the op specific columns filterting

Source:
Parameters:
Name Type Description
req DbRequest

query(client, req, callbackopt)

Description:
  • Query the database, always return an array as a result (i.e. the second argument for the callback)

Source:
Parameters:
Name Type Attributes Description
client object
req DbRequest
callback function <optional>

shutdown(optionsopt, callbackopt)

Description:
  • Close the database connection and cleanup

Source:
Parameters:
Name Type Attributes Description
options object <optional>
callback function <optional>