Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
any-db-pool
Advanced tools
var ConnectionPool = require('any-db-pool')
var mysql = require('mysql')
var adapter = {
createConnection: function (opts, callback) {
var conn = mysql.createConnection(opts, callback)
conn.connect(function (err) {
if (err) callback(err)
else callback(null, conn)
})
return conn
},
createQuery: mysql.createQuery
}
var pool = new ConnectionPool(adapter, {user: 'scott', password: 'tiger'}, {
min: 5,
max: 15,
reset: function (conn, done) { conn.query('ROLLBACK', done) }
})
// Proxies to mysql's connection.query
var q = pool.query('SELECT 1', function (err, res) { })
This module contains a database connection pool that can be used with any driver, though it is designed to integrate well with any-db, a minimal database abstraction layer. If you are writing a library that needs to support multiple database backends (e.g. SQLite3 or Postgres or MySQL) then it's highly encouraged that you use any-db and not this module.
If, on the other hand, you just need a connection pool for your application this should work for you with very little fuss.
generic-pool
?generic-pool is awesome, but it's very generic. This is a Good Thing
for a library with "generic" in the name, but not so good for the very common
but slightly more specialized case of pooling stateful database connection. This
library uses generic-pool
and simply augments it with some added niceties:
query
method that allows queries to be performed without the user needing
a reference to a connection object (and potentially leaking that reference).npm install any-db-pool
var ConnectionPool = require('any-db-pool')
var pool = new ConnectionPool(adapter, connectionParams, options)
The module exports a single constructor function, conventionally named
ConnectionPool
. This constructor expects 3 arguments:
adapter
- An object with createConnection
and createQuery
methods. See
adapter interface below for details.
connectionParams
- An argument for adapter.createConnection
to create connections.
options
- an object with any of the following keys (defaults shown):
min: 2
The minimum number of connections to keep open in the pool.
max: 10
The maximum number of connections to allow in the pool.
onConnect: function (conn, done) { done(null, conn) }
Called immediately after a connection is first established. Use this to do
one-time setup of new connections. You must call done(error, connection)
for the connection to actually make it into the pool.
reset: function (conn, done) { done(null) }
,
Called each time the connection is returned to the pool. Use this to restore your connection to it's original state (e.g. rollback transactions, set the user or encoding).
Adapter objects must support the following method signatures:
adapter.createConnection(connectionParams, callback)
- Create a new database
connection using connectionParams
. Callback must be called with either an
error (callback(error)
) or the connection itself (callback(null, conn)
). The
connection is expected to adhere to the any-db Connection
interface.
adapter.createQuery(statement, params, callback)
- Create and return a new
query object that can be later executed with connection.query.
(note that this requires the connection objects query method to support being
called with a pre-constructed query object).
ConnectionPool instances are created with createPool. They have also been moved into their own module any-db-pool
var query = pool.query(stmt, [params], [callback])
Perform a SQL query using the first available connection, and automatically release the connection after the query has completed.
pool.acquire(function (err, conn) { ... })
Remove a connection from the pool. If you use this method you must return the connection back to the pool using ConnectionPool.release.
pool.release(conn)
Return a connection to the pool. This should only be called with connections you've manually acquired, and you must not continue to use the connection after releasing it.
Stop giving out new connections, and close all existing database connections as they are returned to the pool.
'close'
- emitted when the connection pool has closed all of it
connections after a call to close()
.MIT
FAQs
Any-DB connection pool
The npm package any-db-pool receives a total of 1,984 weekly downloads. As such, any-db-pool popularity was classified as popular.
We found that any-db-pool demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.