Comparing version 1.0.1 to 1.0.3
50
index.js
var ConnectionPool = require('any-db-pool') | ||
var parseDbUrl = require('./lib/parse-url') | ||
var Transaction = require('./lib/transaction') | ||
var parseDbUrl = require('parse-db-url') | ||
@@ -13,5 +12,2 @@ Object.defineProperty(exports, 'adapters', { | ||
// Re-export Transaction for adapters | ||
exports.Transaction = Transaction | ||
exports.createConnection = function connect (dbUrl, callback) { | ||
@@ -21,44 +17,10 @@ var adapterConfig = parseDbUrl(dbUrl) | ||
var conn = adapter.createConnection(adapterConfig, callback); | ||
conn.adapter = adapterConfig.adapter; | ||
return conn; | ||
conn.adapter = adapterConfig.adapter | ||
return conn | ||
} | ||
exports.createPool = function createPool (dbUrl, poolConfig) { | ||
poolConfig = poolConfig || {} | ||
if (poolConfig.create || poolConfig.destroy) { | ||
throw new Error( | ||
"Use onConnect/reset options instead of create/destroy." | ||
) | ||
} | ||
var adapterConfig = parseDbUrl(dbUrl); | ||
if (adapterConfig.adapter === 'sqlite3' && /:memory:$/i.test(adapterConfig.database)) { | ||
if (poolConfig.min > 1 || poolConfig.max > 1) { | ||
console.warn( | ||
"Pools of more than 1 connection do not work for in-memory SQLite3 databases\n" + | ||
"The specified minimum (%d) and maximum (%d) connections have been overridden" | ||
) | ||
} | ||
if (poolConfig.min) poolConfig.min = 1; | ||
poolConfig.max = 1; | ||
} | ||
var adapter = getAdapter(adapterConfig.adapter); | ||
var pool = new ConnectionPool(adapter, adapterConfig, poolConfig); | ||
pool.adapter = adapterConfig.adapter; | ||
pool.begin = Transaction.createBeginMethod(adapter.createQuery, function (tx) { | ||
// Proxy query events from the transaction to the pool | ||
tx.on('query', this.emit.bind(this, 'query')) | ||
this.acquire(function (err, connection) { | ||
if (err) return tx.emit('error', err); | ||
var release = pool.release.bind(pool, connection) | ||
tx.once('rollback:complete', release) | ||
.once('commit:complete', release) | ||
.setConnection(connection) | ||
}) | ||
}); | ||
return pool | ||
var adapter = getAdapter(adapterConfig.adapter) | ||
return new ConnectionPool(adapter, adapterConfig, poolConfig) | ||
} | ||
@@ -68,3 +30,3 @@ | ||
var name = protocol.replace(':', '').split('+').shift() | ||
return require('any-db-' + name); | ||
return require('any-db-' + name) | ||
} |
{ | ||
"name": "any-db", | ||
"version": "1.0.1", | ||
"version": "1.0.3", | ||
"description": "Database-agnostic connection pooling, querying, and result sets", | ||
"main": "index.js", | ||
"scripts": { | ||
"prepublish": "cp ../*.md .", | ||
"test": "tap test/*.test.js" | ||
"test": "node test.js" | ||
}, | ||
@@ -24,9 +23,9 @@ "repository": { | ||
"dependencies": { | ||
"any-db-pool": ">= 0.0.6" | ||
"any-db-pool": "~1.0.1", | ||
"parse-db-url": "0.0.0" | ||
}, | ||
"devDependencies": { | ||
"any-db-sqlite3": "*", | ||
"any-db-postgres": "*", | ||
"any-db-mysql": "*" | ||
"tape": "~2.3.2", | ||
"any-db-fake": "0.0.1" | ||
} | ||
} |
107
README.md
# Any-DB - a less-opinionated database abstraction layer. | ||
This is the main entry point for Any-DB. Users of the library will | ||
`require('any-db')` to make use of the [API](API.md) it exposes. | ||
[![Build Status](https://secure.travis-ci.org/grncdr/node-any-db.png?branch=master)](http://travis-ci.org/grncdr/node-any-db) | ||
_The less-opinionated Node.js database abstraction layer_ | ||
## Synopsis | ||
```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 | ||
} | ||
``` | ||
Establish a connection: | ||
```javascript | ||
// Takes an optional callback | ||
var conn = anyDB.createConnection('driver://user:pass@hostname/database') | ||
``` | ||
Make queries: | ||
```javascript | ||
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 | ||
Do not install this library directly. Instead, install one or more of the | ||
database adapters, which will pull in `any-db` as a peerDependency. For example: | ||
### For Applications | ||
npm install --save any-db-mysql | ||
npm install --save-dev any-db-sqlite3 | ||
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](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 | ||
[createConnection]: https://github.com/grncdr/node-any-db-adapter-spec#adapter-createconnection | ||
[ConnectionPool]: https://github.com/grncdr/node-any-db-pool#api |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
8
112
8912
2
46
1
+ Addedparse-db-url@0.0.0
+ Addedany-db-pool@1.0.3(transitive)
+ Addedany-db-transaction@0.0.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedparse-db-url@0.0.0(transitive)
+ Addedyafsm@0.0.0(transitive)
- Removedany-db-pool@2.3.0(transitive)
Updatedany-db-pool@~1.0.1