mysql-pool
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -13,6 +13,2 @@ "use strict"; | ||
this.poolSize = 1; | ||
this.Client = null; | ||
this.properties = {}; | ||
// connections waiting for a query to execute | ||
@@ -28,2 +24,4 @@ this._idleQueue = []; | ||
case "Client": | ||
throw new Error("Deprecated: specify `library` instead of `Client`."); | ||
case "mysql": | ||
case "poolSize": | ||
@@ -38,2 +36,19 @@ this[key] = properties[key]; | ||
if(!this.poolsize) { | ||
this.poolsize = 1; | ||
} | ||
if(!this.mysql) { | ||
this.mysql = require("mysql"); | ||
} else if(typeof this.mysql == "string") { | ||
this.mysql = require(this.library); | ||
} | ||
this._populate(); | ||
for(var i = 0; i < this.poolSize; ++i) { | ||
var client = this.mysql.createClient(this.properties); | ||
this._connectionPool.push(client); | ||
this._avail(client); | ||
} | ||
return this; | ||
@@ -53,62 +68,9 @@ } | ||
MySQLPool.prototype.connect = function connect(n, cb) { | ||
var pool = this; | ||
this._idleQueue = []; | ||
this._connectionPool = []; | ||
if(!this.Client) { | ||
this.Client = require('mysql').Client; | ||
} | ||
this._populate(); | ||
if(typeof n == "function") { | ||
cb = n; | ||
n = undefined; | ||
} | ||
var poolSize = n || this.poolSize; | ||
var calledBack = 0; | ||
var availableConnections = []; | ||
var errors = []; | ||
function mkCallback(client) { | ||
return function(err) { | ||
if(err) { | ||
errors.push(err); | ||
--pool.poolSize; | ||
} else { | ||
availableConnections.push(client); | ||
pool._connectionPool.push(client); | ||
} | ||
if(++calledBack >= poolSize) { | ||
if(availableConnections.length > 0) { | ||
if(cb) { | ||
if(errors.length == 0) { | ||
cb(null, {connections:availableConnections.length}); | ||
} else { | ||
cb(null, {connections:availableConnections.length, errors:errors}); | ||
} | ||
} | ||
for(var i in availableConnections) { | ||
pool._avail(availableConnections[i]); | ||
} | ||
} else { | ||
var err = new Error("All connections failed."); | ||
if(cb) { | ||
cb(err, {errors:errors}); | ||
} else { | ||
pool.emit("error", err); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
for(var i = 0; i < poolSize; ++i) { | ||
var client = this.Client(this.properties); | ||
client.connect(mkCallback(client)); | ||
} | ||
return this; | ||
} | ||
throw new Error('Deprecated: specify `poolSize` in constructor options.'); | ||
}; | ||
MySQLPool.prototype.createClient = function createClient(options) { | ||
return new MySQLPool(options); | ||
}; | ||
MySQLPool.prototype._forEach = function _forEach(params) { | ||
@@ -198,7 +160,8 @@ // TODO: callback _once_ | ||
for(var key in this.Client.prototype) { | ||
var Client = this.mysql.Client; | ||
for(var key in Client.prototype) { | ||
if(!key.match(/^[_A-Z]/) && !(key in this) && !(key in EventEmitter.prototype)) { | ||
this[key] = mkPrototypeMethod(this.Client.prototype[key], key); | ||
this[key] = mkPrototypeMethod(Client.prototype[key], key); | ||
} | ||
} | ||
} |
{ | ||
"name": "mysql-pool", | ||
"description": "MySQL connection pool for node.js on top of node-mysql.", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/Kijewski/node-mysql-pool", | ||
@@ -19,4 +19,4 @@ "repository": { | ||
"devDependencies": { | ||
"mysql": ">=0.9.0" | ||
"mysql": ">=0.9.2" | ||
} | ||
} |
145
Readme.md
@@ -1,4 +0,6 @@ | ||
<h1 id="Readme">node-mysql-pool</h1> | ||
<a name="Readme"></a> | ||
<h1>node-mysql-pool</h1> | ||
<h2 id="Purpose">Purpose</h2> | ||
<a name="Purpose"></a> | ||
<h2>Purpose</h2> | ||
@@ -12,4 +14,50 @@ node-mysql-pool is a MySQL [connection pool](http://en.wikipedia.org/wiki/Connection_pool) | ||
<h2 id="Status">Current status</h2> | ||
<a name="TOC"></a> | ||
<h2>TOC</h2> | ||
* [Tutorial](#Tutorial) | ||
* [Current status](#Status) | ||
* [Contributors](#Contributors) | ||
* [Compatibility](#Compatibility) | ||
* [Installation](#Installation) | ||
* [API](#API) | ||
* [Creation of a new pool](#NewPool) | ||
* [Options](#Options) | ||
* [Methods affecting all connections](#AllConnections) | ||
* [Methods invoked on a single connection](#SingleConnection) | ||
* [Methods unrelated to connections](#NoConnection) | ||
* [event: 'error' \(err\)](#EventError) | ||
* [Todo](#Todo) | ||
* [Licence](#Licence) | ||
<a name="Tutorial"></a> | ||
<h2>Tutorial</h2> | ||
var MySQLPool = require("mysql-pool").MySQLPool; | ||
var pool = new MySQLPool({ | ||
poolSize: 4, | ||
user: 'root', | ||
password: 'root', | ||
database: 'test' | ||
}); | ||
pool.query("SELECT 'Hello, World!' AS hello", function(err, rows, fields) { | ||
if(err) throw err; | ||
console.log(rows[0].hello); | ||
}); | ||
for(var i = 0; i < 10; ++i) { | ||
pool.query("SELECT SLEEP(2), ? AS i", [i], function(err, rows, fields) { | ||
if(err) throw err; | ||
console.log("Slept: " + rows[0].i); | ||
}); | ||
} | ||
You probably do not have to change anything if you already used | ||
[node-mysql](https://github.com/felixge/node-mysql/) | ||
or any of [its forks](https://github.com/felixge/node-mysql/network)! | ||
<a name="Status"></a> | ||
<h2>Current status</h2> | ||
This module is currently not backed by proper unit testing. Nevertheless I found | ||
@@ -20,12 +68,16 @@ it stable for my testings. | ||
<h2 id="Contributors">Contributors</h2> | ||
<a name="Contributors"></a> | ||
<h2>Contributors</h2> | ||
* [René Kijewski](https://github.com/Kijewski) | ||
* [Michael Lai](https://github.com/melin) | ||
(fixed [issue #1](https://github.com/Kijewski/node-mysql-pool/issues#issue/1)) | ||
(fixed [issue #1](https://github.com/Kijewski/node-mysql-pool/pull/1)) | ||
* [Daniel Dickison](https://github.com/danieldickison) | ||
(fixed [issue #3](https://github.com/Kijewski/node-mysql-pool/pull/3)) | ||
<h2 id="Compatibility">Compatibility</h2> | ||
<a name="Compatibility"></a> | ||
<h2>Compatibility</h2> | ||
This module was only tested using node >= 0.4.x. It may work for older versions, | ||
but I am not going to actively support them. | ||
This module was only tested using node >= 0.4.x. It does not work with older | ||
versions of node.js. | ||
@@ -41,30 +93,5 @@ The node-mysql-pool even works with unknown forks of node-mysql, as long as | ||
<h2 id="Tutorial">Tutorial</h2> | ||
<a name="Installation"></a> | ||
<h2>Installation</h2> | ||
var MySQLPool = require("mysql-pool").MySQLPool, | ||
pool = new MySQLPool({database: "test"}); | ||
pool.properties.user = 'root'; | ||
pool.properties.password = 'root'; | ||
pool.connect(4); | ||
pool.query("SELECT 'Hello, World!' AS hello", function(err, rows, fields) { | ||
if(err) throw err; | ||
console.log(rows[0].hello); | ||
}); | ||
for(var i = 0; i < 10; ++i) { | ||
pool.query("SELECT SLEEP(2), ? AS i", [i], function(err, rows, fields) { | ||
if(err) throw err; | ||
console.log("Slept: " + rows[0].i); | ||
}); | ||
} | ||
You probably do not have to change anything if you already used | ||
[node-mysql](https://github.com/felixge/node-mysql/) | ||
or any of [its forks](https://github.com/felixge/node-mysql/network)! | ||
<h2 id="Installation">Installation</h2> | ||
* Using [npm](http://npmjs.org/): `npm install mysql-pool` | ||
@@ -76,3 +103,4 @@ * Using git: | ||
<h2 id="API">API</h2> | ||
<a name="API"></a> | ||
<h2>API</h2> | ||
@@ -87,16 +115,10 @@ The API of this module is as similar to node-mysql as possible, with two exceptions: | ||
<h3 id="NewPool">Creation of a new pool</h3> | ||
<a name="NewPool"></a> | ||
<h3>Creation of a new pool</h3> | ||
new mysqlPool.Pool([options]) | ||
mysqlPool.Pool(options) | ||
creates a new, currently empty, pool. Any property for the single connections or | ||
creates a new, currently empty. Any property for the single connections or | ||
the connectionpool, resp., can be set using the `options` object. | ||
client.connect([poolsize], [cb]) | ||
// with: | ||
cb = function(err, result) | ||
result = { [connections: Number], [errors: Array] } | ||
Establishes a new connection pool with the size of `poolsize`. | ||
If the parameter `poolsize` is omitted, the value of `client.poolsize`, or 1 is used. | ||
@@ -108,3 +130,4 @@ | ||
<h3 id="Options">Options</h3> | ||
<a name="Options"></a> | ||
<h3>Options</h3> | ||
@@ -114,15 +137,16 @@ Defaults: | ||
pool.poolSize = 1 | ||
pool.Client = require("mysql").Client | ||
pool.mysql = require("mysql") | ||
* `pool.poolSize`: | ||
* The number of connections to establish to the server. | ||
* `pool.Client`: | ||
* `pool.mysql`: | ||
* If you do not want the npm version of node-mysql—e.g. because you forked and | ||
tweaked it for your purposes—you can supply a different `Client` object. | ||
tweaked it for your purposes—you can supply a different library to use. | ||
* `pool.properties.xyz = undefined`: | ||
* Property `xyz` of the `Client` object. | ||
* Property `xyz` of the `mysql.Client` object. | ||
See the [original documentation](https://github.com/felixge/node-mysql/blob/master/Readme.md) | ||
of node-mysql for more property related information. | ||
<h3 id="AllConnections">Methods affecting all connections</h3> | ||
<a name="AllConnections"></a> | ||
<h3>Methods affecting all connections</h3> | ||
@@ -147,3 +171,4 @@ client.useDatabase(database, cb) | ||
<h3 id="SingleConnection">Methods invoked on a single connection</h3> | ||
<a name="SingleConnection"></a> | ||
<h3>Methods invoked on a single connection</h3> | ||
@@ -167,3 +192,4 @@ All methods of the `Client` object will be supported—with `connect(...)`, `end(...)`, | ||
<h3 id="NoConnection">Methods unrelated to connections</h3> | ||
<a name="NoConnection"></a> | ||
<h3>Methods unrelated to connections</h3> | ||
@@ -176,3 +202,4 @@ format(sql, params) | ||
<h3 id="EventError">event: 'error' (err)</h3> | ||
<a name="EventError"></a> | ||
<h3>event: 'error' (err)</h3> | ||
@@ -182,11 +209,13 @@ Emitted if and only if an error occurred and no callback function was supplied. | ||
<h2 id="Todo">Todo</h2> | ||
<a name="Todo"></a> | ||
<h2>Todo</h2> | ||
* The methods effecting all connections have a strange API. `cb` should be called | ||
* The methods affecting all connections have a strange API. `cb` should be called | ||
only once. | ||
<h2 id="Licence">Licence</h2> | ||
<a name="Licence"></a> | ||
<h2>Licence</h2> | ||
node-mysql-pool is licensed under the | ||
[MIT license](https://github.com/Kijewski/node-mysql-pool/blob/master/License). |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
12970
8
211
142
1