New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

any-db

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

any-db

Database-agnostic connection pooling, querying, and result sets

  • 2.1.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Any-DB

Build Status

The less-opinionated Node.js database abstraction layer

Synopsis

module.exports := {
  createConnection: (Url, Continuation<Connection>?) => Connection
  createPool: (Url, PoolConfig) => ConnectionPool
}

Url := String | { adapter: String }

PoolConfig := {
  min: Number,
  max: Number,
  onConnect: (Connection, ((Error) => void) => void
  reset: (Connection, ((Error) => void) => void
}

Establish a connection:

// Takes an optional callback
var conn = anyDB.createConnection('driver://user:pass@hostname/database')

Make queries:

var sql = 'SELECT * FROM my_table'
conn.query(sql).on('row', function (row) {})  // evented
conn.query(sql, function (error, result) {})  // or callback

Use bound parameters:

sql += ' WHERE my_column = ?'
conn.query(sql, [42]).on('row', ...)           // again, evented
conn.query(sql, [42], function (err, res) {})  // or callback

Close a connection:

conn.end()

Start a transaction:

var tx = conn.begin()             // Can also take a callback
tx.on('error', function (err) {}) // Emitted for unhandled query errors
tx.query(...)                     // same interface as connections, plus...
tx.commit()                       // takes an optional callback for errors
tx.rollback()                     // this too

Create a connection pool that maintains 2-20 connections

var pool = anyDB.createPool(dbURL, {min: 2, max: 20})

pool.query(...)       // perform a single query, same API as connection
var tx = pool.begin() // start a transaction, again, same API as connection
pool.close()          // close the pool (call when your app should exit)

Description

The purpose of this library is to provide a consistent API for the commonly used functionality of SQL database drivers, while avoiding altering driver behaviour as much as possible.

Installation

For Applications

npm install --save any-db-{postgres,mysql,sqlite3}

All of the adapter libraries have any-db as a peerDependency, which means that require('any-db') will work even though you don't install it directly or add it to your package.json.

For Libraries

Add any-db to peerDependencies in package.json. This allows users of your library to satisfy the any-db dependency by installing the adapter of their choice.

API

The API of [Connection][] and [Query][] objects is fully described in the [adapter-spec][], while [Transaction][] and ConnectionPool objects have their own documentation. Connections, transactions and pools all have a query method that behaves consistently between drivers.

Both exported functions require an Url as their first parameter. This can either be a string of the form adapter://user:password@host/database (which will be parsed by [parse-db-url][]) or an object. When an object is used, it must have an adapter property, and any other properties required by the specified adapters createConnection method.

See also: README notes for your chosen adapter (MySQL, Postgres, and SQLite3)

License

MIT

Keywords

FAQs

Package last updated on 26 Dec 2013

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc