Giter Club home page Giter Club logo

node.dbslayer.js's Introduction

node.dbslayer.js

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.

DBSlayer benefits include:

  • It's Node.JS/V8/JavaScript friendly, since the the messages are delivered in JSON format over HTTP.

  • 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).

Requirements

How to Use

From your node.js script, require the dbslayer package

var db = require('dbslayer');

Initialize a connection

var connection = db.Server('localhost', 9090);

and then perform a query:

connection.query("SELECT * FROM table");

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:

connection.query("SELECT * FROM TABLE").addCallback(function(result){
	for (var i = 0, l = result.ROWS.length; i < l; i++){
		var row = result.ROWS[i];
		// do something with the data
	}
});

If you want to capture MySQL errors, subscribe to the 'error' event

connection.query("SELECT * FROM inexistent_table").addErrback(function(error, errno){
	alert('mysql error! + ' error);
});

Aside from query, the commands stat, client_info, host_info, server_version and client_version are available, which provide the respective information about the server.

Installing DBSlayer

Compile it according to the instructions here.

Then create a /etc/dbslayer.conf file defining a database. Here I'm defining the cool server which connects to my mysql database

[cool]
database=mysql
host=localhost
user=root
pass=1234

Then run DBSlayer for that connection:

dbslayer -c /etc/dbslayer.conf -s cool

Test it by running test.js like this:

node test.js "SELECT * FROM help_category"

If you get a bunch of entries like in this screenshot then dbslayer (and node.dbslayer.js) work!

Author

Guillermo Rauch <http://devthought.com>

node.dbslayer.js's People

Contributors

rauchg avatar shoeman22 avatar apemsel avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.