# any-db

> Database-agnostic connection pooling, querying, and result sets

Latest version **2.2.1** (published 2018-03-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install any-db
pnpm add any-db
yarn add any-db
bun add any-db
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.1 |
| Published | 2018-03-25 |
| First published | 2012-10-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/any-db) |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 9.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 210 |
| Author | Stephen Sugden |
| Maintainers | grncdr |
| Keywords | mysql, postgres, pg, sqlite, sqlite3 |

## Links

- npm: https://www.npmjs.com/package/any-db
- Repository: https://github.com/grncdr/node-any-db
- Homepage: https://github.com/grncdr/node-any-db#readme
- Issues: https://github.com/grncdr/node-any-db/issues
- npm.io page: https://npm.io/package/any-db

## Dependencies (2)

- [any-db-pool](https://npm.io/package/any-db-pool.md) ~2.2.0
- [parse-db-url](https://npm.io/package/parse-db-url.md) 0.0.0

## Recent versions

- 2.2.1 (latest) — 2018-03-25
- 2.2.0 — 2017-02-14
- 2.1.0 — 2013-12-26
- 1.0.3 — 2013-12-22
- 1.0.1 — 2013-11-28
- 1.0.0 — 2013-11-06
- 1.0.0-rc2 — 2013-11-01
- 1.0.0-rc1 — 2013-10-17
- 1.0.0-alpha2 — 2013-09-18
- 1.0.0-alpha1 — 2013-09-08
- 0.6.0 — 2013-06-14
- 0.5.1 — 2013-05-16
- 0.5.0 — 2013-05-12
- 0.4.4 — 2013-04-24
- 0.4.3 — 2013-04-24
- … 18 more at https://npm.io/package/any-db/versions

## README

# Any-DB

[![Build Status](https://secure.travis-ci.org/grncdr/node-any-db.svg?branch=master)](http://travis-ci.org/grncdr/node-any-db)

_The less-opinionated Node.js database abstraction layer_

## Synopsis

Establish a connection:

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

Make queries:

```javascript
var sql = 'SELECT * FROM questions'

// query() returns a readable stream
conn.query(sql).on('data', function (row) {})

// pass a callback to collect results
conn.query(sql, function (error, result) {})
```

Use bound parameters:

```javascript
sql += ' WHERE answer = ?'
conn.query(sql, [42], function (err, res) {})
```

Manage database transactions with [any-db-transaction][Transaction]:

```javascript
var begin = require('any-db-transaction')

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

Create a [connection pool][ConnectionPool] that maintains 2-20 connections:

```javascript
var pool = anyDB.createPool(dbURL, {min: 2, max: 20})
    
pool.query(...)       // perform a single query, same API as connection
var tx = begin(pool)  // create a transaction with the first available 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,mssql}

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

```ocaml
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
}

Continuation := (Maybe<Error>, Any) => void
```

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 for your chosen adapter
([MS SQL](https://github.com/Hypermediaisobar-admin/node-any-db-mssql),
 [MySQL](https://github.com/grncdr/node-any-db-mysql),
 [Postgres](https://github.com/grncdr/node-any-db-postgres), and
 [SQLite3](https://github.com/grncdr/node-any-db-sqlite3))

## License

MIT

[Connection]: https://github.com/grncdr/node-any-db-adapter-spec#connection
[Query]: https://github.com/grncdr/node-any-db-adapter-spec#query
[adapter-spec]: https://github.com/grncdr/node-any-db-adapter-spec
[createConnection]: https://github.com/grncdr/node-any-db-adapter-spec#adapter-createconnection
[ConnectionPool]: https://github.com/grncdr/node-any-db-pool#api
[Transaction]: https://github.com/grncdr/node-any-db-transaction#api
[parse-db-url]: https://github.com/grncdr/parse-db-url

---
_Source: https://npm.io/package/any-db · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
