Skip to content
Browse files

More consistent API.

  • Loading branch information...
1 parent 7354961 commit ff6ab0647b3e0f2b266f02d643a2c6c1d7da8645 @Kijewski committed Mar 12, 2011
Showing with 61 additions and 54 deletions.
  1. +25 −17 Readme.md
  2. +36 −37 lib/mysql-pool/pool.js
View
42 Readme.md
@@ -44,7 +44,7 @@ Otherwise the requirements are the same as for
client.properties.user = 'root';
client.properties.password = 'root';
- client.connectPool(4);
+ client.connect(4);
client.query("SELECT 'Hello, World!' AS hello", function(err, rows, fields) {
if(err) throw err;
@@ -60,16 +60,16 @@ Otherwise the requirements are the same as for
## API
-The API of this module is as similar to node-mysql as possible, with three exceptions:
+The API of this module is as similar to node-mysql as possible, with two exceptions:
* You must always supply a callback function. Using listeners is not supported.
-* You connect using `connectPool([poolsize], [cb])` and shut the pool down using
- `endPool([cb])`.
* Property `x`, when not supplied while creation, are to be set to `instance.properties.x`.
+When called back, `this` will be the used connection.
+
### new mysqlPool.Pool([options])
-Creates a new, currently empty, pool. Any property for the signle connections or
+Creates a new, currently empty, pool. Any property for the single connections or
the connectionpool, resp., can be set using the `options` object.
### client.poolSize = 1
@@ -88,38 +88,46 @@ Property `xyz` of the `Client` object.
See the [original documentation](https://github.com/felixge/node-mysql/blob/master/Readme.md)
of node-mysql for more property related information.
-### client.connectPool([poolsize], [cb])
+### client.connect([poolsize], [cb])
+
+ 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.
- cb = function(err, result={ [connections: Number], [errors: Array] })
-
Only if all connection attemps failed `err` is supplied.
If some connections failed, `result.error` will contain a list of Errors.
If some or all connections succeeded, `results.connections` will contains the pool's size.
-### client.endPool([cb])
+### client.end([cb])
Shuts down all connections, not waiting for any enqueued and waiting queries.
Active queries won't be aborted, though.
-`cb` will be called back once for every shut down connection. (Subject to change!)
+`cb` will be called once for every shut down connection. (Subject to change!)
+
+### client.useDatabase(database, cb)
+
+Changes the database for every connection.
+
+### client.destroy()
+
+Kills every connection. You do not want do use this method!
### client.xzy(..., cb)
-All methods of the `Client` object will be supported.
+All methods of the `Client` object will be supported -- with `connect(...)`, `end(...)`,
+`useDatabase(...)` and `destroy(...)` being overwritten.
See the [original documentation](https://github.com/felixge/node-mysql/blob/master/Readme.md)
-of node-mysql for information.
+of node-mysql for method related information.
Beware:
-* Twink twice before calling `useDatabase(...)`: It will only affect a single random
- connection. Do you really want that?
-* Invoking `destroy` kills a random connection. While it won't couse harm right away,
- you may observe errors while shutting down.
+* You must supply a callback method, if you have any parameters.
+* No events are emitted but error.
### event: 'error' (err)
@@ -128,7 +136,7 @@ You should always supply a callback function!
## Todo
-* The `endPool([cb])` method has a strange API.
+* The `end([cb])`, `destroy([cb])` and `useDatabase(db, cb)` methods have a strange API.
## Licence
View
73 lib/mysql-pool/pool.js
@@ -3,14 +3,6 @@
var util = require("util");
var EventEmitter = require("events").EventEmitter;
-function argumentsArray(args) {
- var result = [];
- for(var i in args) {
- result.push(args[i]);
- }
- return result;
-}
-
function MySQLPool(properties) {
if(!(this instanceof MySQLPool)) {
return new MySQLPool(properties);
@@ -54,26 +46,10 @@ MySQLPool.prototype._avail = function _avail(client) {
}
}
-MySQLPool.prototype.connect = function connect(cb) {
- var err = new Error("MySQLPool.connect() is not available. Use connectPool()!");
- if(cb) {
- cb(err);
- } else {
- throw err;
- }
-}
-
-MySQLPool.prototype.end = function connect(cb) {
- var err = new Error("MySQLPool.end() is not available. Use endPool()!");
- if(cb) {
- cb(err);
- } else {
- throw err;
- }
-}
-
-MySQLPool.prototype.connectPool = function connectPool(n, cb) {
+MySQLPool.prototype.connect = function connect(n, cb) {
var pool = this;
+ this._idleQueue = [];
+ this._connectionPool = [];
if(!this.Client) {
this.Client = require('mysql').Client;
@@ -130,34 +106,57 @@ MySQLPool.prototype.connectPool = function connectPool(n, cb) {
client.connect(mkCallback(client));
}
- delete this.Client;
return this;
}
-MySQLPool.prototype.endPool = function endPool(cb) {
+MySQLPool.prototype._forEach = function _forEach(method, destroying, cb /*, ...args */) {
// TODO: callback _once_
var pool = this;
- this._avail = [];
- for(var i in this._connectionPool) {
- var client = this._connectionPool[i];
- client.end(function(err) {
- --pool._poolSize;
+
+ function mkCallback(client) {
+ return function(err) {
if(cb) {
cb.apply(client, arguments);
} else if(err) {
pool.emit("error", err);
}
- });
+ };
}
- this._connectionPool = [];
+
+ var args = Array.prototype.slice.call(arguments).slice(3);
+ for(var i in this._connectionPool) {
+ var client = this._connectionPool[i];
+ method.apply(client, args.concat(mkCallback(client)));
+ }
+
+ if(destroying) {
+ this._poolSize = 0;
+ this._connectionPool = [];
+ this._idleQueue = [];
+ this._idleQueue.push = function() {
+ return 0
+ };
+ }
+}
+
+MySQLPool.prototype.end = function end(cb) {
+ return this._forEach(this.Client.prototype.end, true, cb);
+}
+
+MySQLPool.prototype.destroy = function destroy() {
+ return this._forEach(this.Client.prototype.destroy, true);
+}
+
+MySQLPool.prototype.useDatabase = function useDatabase(db, cb) {
+ return this._forEach(this.Client.prototype.useDatabase, false, cb, db);
}
MySQLPool.prototype._populate = function _populate() {
var pool = this;
function mkPrototypeMethod(method) {
return function wrapperMethod() {
- var args = argumentsArray(arguments);
+ var args = Array.prototype.slice.call(arguments);
while(typeof args[args.length-1] == "undefined") {
args.pop();
}

0 comments on commit ff6ab06

Please sign in to comment.
Something went wrong with that request. Please try again.