Skip to content

Commit 415cf6f

Browse files
committed
Initial (functional) commit of node.dbslayer.js!
0 parents  commit 415cf6f

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
node.dbslayer.js
2+
=================
3+
4+
node.dbslayer.js is a very basic and easy-to-use library to connect to a DBSlayer server, which effectively provides non-blocking and scalable MySQL support for Node.JS.
5+
6+
DBSlayer benefits include:
7+
8+
* It's Node.JS/V8/JavaScript friendly, since the the messages are delivered in JSON format over HTTP.
9+
10+
* Developed by the New York Times, it's designed with scalability in mind, doing connection pooling for you. This is what makes DBSlayer arguably better than implementing an async MySQL client directly into Node (through mysac for example).
11+
12+
Requirements
13+
------------
14+
15+
16+
* [Node.js](http://nodejs.org/) (tested with v0.1.21)
17+
* [DBSlayer](http://code.nytimes.com/projects/dbslayer/) (tested with beta-12)
18+
19+
How to Use
20+
----------
21+
22+
From your node.js script, require the `dbslayer` package
23+
24+
var db = require('dbslayer');
25+
26+
Initialize a connection
27+
28+
var connection = db.Server('localhost', 9090);
29+
30+
and then perform a query:
31+
32+
connection.query("SELECT * FROM table");
33+
34+
To be truly non-blocking, `Server::fetch` has to return a promise and not the result immediately. This means that in order to be able to perform queries in a designated order or access the result, you'll have to use callbacks:
35+
36+
connection.query("SELECT * FROM TABLE").addCallback(function(result){
37+
// insert ready
38+
for (var i = 0, l = result.rows.length; )
39+
});
40+
41+
If you want to capture MySQL errors, subscribe to the 'error' event
42+
43+
connection.query("...").addErrback(function(){
44+
alert('mysql error!');
45+
});
46+
47+
Aside from query, the commands `stat`, `client_info`, `host_info`, `server_version` and `client_version` are available, which provide the corresponding information.
48+
49+
Installing DBSlayer
50+
-------------------
51+
52+
Compile it according to the instructions [here](http://code.nytimes.com/projects/dbslayer/wiki).
53+
54+
Then create a /etc/dbslayer.conf file defining a database. Here I'm defining the `cool` server which connects to my `mysql` database
55+
56+
[cool]
57+
database=mysql
58+
host=localhost
59+
user=root
60+
pass=1234
61+
62+
Then run DBSlayer for that connection:
63+
64+
dbslayer -c /etc/dbslayer.conf -s cool
65+
66+
Test it by running test.js like this:
67+
68+
node test.js "SELECT * FROM help_category"
69+
70+
If you get a bunch of entries like in this [screenshot](http://cld.ly/9aosh) then dbslayer (and node.dbslayer.js) work!
71+
72+
Author
73+
------
74+
75+
Guillermo Rauch <[http://devthought.com](http://devthought.com)>

dbslayer.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
---
3+
name: dbslayer.js
4+
5+
description: Interface to DBSlayer for Node.JS
6+
7+
author: [Guillermo Rauch](http://devthought.com)
8+
...
9+
*/
10+
11+
var sys = require('sys'),
12+
http = require('http'),
13+
14+
booleanCommands = ['STAT', 'CLIENT_INFO', 'HOST_INFO', 'SERVER_VERSION', 'CLIENT_VERSION'],
15+
16+
Server = this.Server = function(host, port, timeout){
17+
this.host = host || 'localhost';
18+
this.port = port || 9090;
19+
this.timeout = timeout;
20+
};
21+
22+
Server.prototype.fetch = function(object, key){
23+
var connection = http.createClient(this.port, this.host),
24+
request = connection.get('/db?' + escape(JSON.stringify(object)), {'host': this.host}),
25+
promise = new process.Promise();
26+
27+
promise.timeout(this.timeout);
28+
29+
request.finish(function(response){
30+
response.addListener('body', function(data){
31+
try {
32+
var object = JSON.parse(data);
33+
} catch(e){
34+
return promise.emitError(e);
35+
}
36+
37+
if (object.MYSQL_ERROR !== undefined){
38+
promise.emitError(object.MYSQL_ERROR, object.MYSQL_ERRNO);
39+
} else if (object.ERROR !== undefined){
40+
promise.emitError(object.ERROR);
41+
} else {
42+
promise.emitSuccess(key ? object[key] : object);
43+
}
44+
});
45+
});
46+
47+
return promise;
48+
};
49+
50+
Server.prototype.query = function(query){
51+
return this.fetch({SQL: query}, 'RESULT');
52+
};
53+
54+
for (var i = 0, l = booleanCommands.length; i < l; i++){
55+
Server.prototype[booleanCommands[i].toLowerCase()] = (function(command){
56+
return function(){
57+
var obj = {};
58+
obj[command] = true;
59+
return this.fetch(obj, command);
60+
};
61+
})(booleanCommands[i]);
62+
}

test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
---
3+
name: tools.js
4+
5+
description: <
6+
This is a demonstration of how dbslayer.js can be used.
7+
It takes three parameters from the SQL query, a host
8+
9+
author: [Guillermo Rauch](http://devthought.com)
10+
...
11+
*/
12+
13+
var sys = require('sys')
14+
dbslayer = require('./dbslayer'),
15+
sql = process.ARGV[2],
16+
db = new dbslayer.Server();
17+
18+
if (!sql){
19+
sys.puts('Please provide the SQL query');
20+
return;
21+
}
22+
23+
db.query(sql)
24+
// on success
25+
.addCallback(function(result){
26+
sys.puts('-------------------------');
27+
for (var i = 0, l = result.ROWS.length; i < l; i++){
28+
sys.puts('Row ' + i + ': ' + result.ROWS[i].join(' '));
29+
}
30+
})
31+
32+
// on error :(
33+
.addErrback(function(error, errno){
34+
sys.puts('-------------------------');
35+
sys.puts('MySQL error (' + (errno || '') + '): ' + error);
36+
});
37+
38+
['stat', 'client_info', 'host_info', 'server_version', 'client_version'].forEach(function(command){
39+
db[command]().addCallback(function(result){
40+
sys.puts('-------------------------');
41+
sys.puts(command.toUpperCase() + ' ' + result);
42+
});
43+
});

0 commit comments

Comments
 (0)