node-handlersocket is a pure JavaScript client for HandlerSocket.
HandlerSocket is a NoSQL plugin for MySQL. See Matsunobu's blog for more information.
- Node.js (>= v0.6.0, tested with v0.6.10)
- HandlerSocket Plugin for MySQL (>= v1.1.0, tested with v1.1.0)
npm install node-handlersocket
var hs = require('node-handlersocket');
var con = hs.connect(function() {
con.openIndex('test', 'EMPLOYEE', 'PRIMARY',
['EMPLOYEE_ID', 'EMPLOYEE_NO', 'EMPLOYEE_NAME'],
function(err, index) {
index.find('=', [1], function(err, records) {
console.log(records[0]);
con.close();
});
});
});var hs = require('node-handlersocket');
var con = hs.connect(function() {
con.openIndex('test', 'EMPLOYEE', 'PRIMARY',
['EMPLOYEE_ID', 'EMPLOYEE_NO', 'EMPLOYEE_NAME'],
function(err, index) {
index.find('>=', [1], {limit: 10, offset: 0}, function(err, records) {
records.forEach(function(record) {
console.log(record);
});
con.close();
});
});
});var hs = require('node-handlersocket');
var con = hs.connect(function() {
con.openIndex('test', 'EMPLOYEE', 'PRIMARY',
['EMPLOYEE_ID', 'EMPLOYEE_NO', 'EMPLOYEE_NAME'],
function(err, index) {
index.find('=', [hs.in(1, 2, 3)], {limit: 10}, function(err, records) {
records.forEach(function(record) {
console.log(record);
});
con.close();
});
});
});var hs = require('node-handlersocket');
var con = hs.connect(function() {
con.openIndex('test', 'EMPLOYEE', 'PRIMARY',
['EMPLOYEE_ID', 'EMPLOYEE_NO', 'EMPLOYEE_NAME'],
['EMPLOYEE_NO'],
function(err, index) {
index.find('>=', [1], {
filters: [hs.filter('EMPLOYEE_NO', '<', 7800)],
limit: 10
},
function(err, records) {
records.forEach(function(record) {
console.log(record);
});
con.close();
});
});
});var hs = require('node-handlersocket');
var con = hs.connect({port: 9999});
con.on('connect', function() {
con.openIndex('test', 'EMPLOYEE', 'PRIMARY',
['EMPLOYEE_ID', 'EMPLOYEE_NO', 'EMPLOYEE_NAME'],
function(err, index) {
index.insert([100, 9999, 'KOICHIK'], function(err) {
con.close();
});
});
});
var hs = require('node-handlersocket');
var con = hs.connect({port: 9999});
con.on('connect', function() {
con.openIndex('test', 'EMPLOYEE', 'PRIMARY',
['EMPLOYEE_ID', 'EMPLOYEE_NO', 'EMPLOYEE_NAME'],
function(err, index) {
index.update('=', 100, [100, 9999, 'EBIYURI'], function(err, numRecords) {
console.log(numRecords);
con.close();
});
});
});
var hs = require('node-handlersocket');
var con = hs.connect({port: 9999});
con.on('connect', function() {
con.openIndex('test', 'EMPLOYEE', 'PRIMARY',
['EMPLOYEE_ID', 'EMPLOYEE_NO', 'EMPLOYEE_NAME'],
function(err, index) {
index.delete('=', 100, function(err, numRecords) {
console.log(numRecords);
con.close();
});
});
});
Open a connection to HandlerSocket server.
- Parameters
-
options: (optional) an object with the following properties:-
host: a host name or address (default is'localhost'). -
port: a port number (default is9998). -
auth: an authentication key.Note, the port 9998 only allows read operations, and the port 9999 allows write operations also. See HandlerSocket installation document for more information.
-
-
connectListener: (optional) It is automatically set as a listener for the'connect'event.
-
- Returns
- a new
Connectionobject.
- a new
Creates and returns an IN criterion object.
- Parameters
values: values of index.
- Returns
- a new IN criterion object.
Creates and returns a filter criterion object.
- Parameters
column: a column name used by this filter. It must be included infilterColumnsspecifiedConnection.openIndex().op: a filter operation, one of'=','>','>=','<'and'<='.value: a value.
- Returns
- a new filter object.
Creates and returns a while filter criterion object.
- Parameters
column: a column name used by this filter. It must be included infilterColumnsspecifiedConnection.openIndex().op: a filter operation, one of'=','>','>=','<'and'<='.value: a value.
- Returns
- a new while filter object.
An object representing connection to the HandlerSocket server. It is an instance of EventEmitter.
Emitted when a connection is established.
- Callback function:
function()
Emitted once the connection is fully closed.
- Callback function:
function()
Emitted when an error occurs.
- Callback function:
function(err)- Parameters
err: an error that occurred.
- Parameters
Open an index.
- Parameters
database: a database name.table: a table name.index: an index name. If 'PRIMARY' is specified, the primary index is open.columns: an array of column names.filterColumns: (optional) an array of column names used by a filter.callback: a function to be called when the response received.
- Callback function:
function(err, index)- Parameters
err: anErrorobject when the request failed, otherwisenull.index: a newIndexobject.
- Parameters
Close the connection.
An object representing MySQL's index.
To read records from a table using the index.
- Parameters
op: a search operation, one of'=','>','>=','<'and'<='.keys: an array of index values. It can include an IN criterion returned fromhs.in().options: (optional) an object which specifies several options.filters: an array of filters returned fromhs.filter()and/orhs.while().limit: a maximum number of records to be retrieved (defaults to 1).offset: a number of records skipped before retrieving records (defaults to 0).
callback: a function to be called when the response received.
- Callback Function:
function(err, records)- Parameters
err: anErrorobject when the request failed, otherwisenull.records: an array of records. Each record is array of column values which corresponds tocolumnsparameter ofConnection.openIndex().
- Parameters
To add records.
- Parametes
- values: an array of new column values which corresponds to
columnsparameter ofConnection.openIndex(). callback: a function to be called when the response received.
- values: an array of new column values which corresponds to
- Callback Function:
function(err)- Parameters
err: anErrorobject when the request failed, otherwisenull.
- Parameters
To update records.
- Parametes
op: a search operation, one of'=','>','>=','<'and'<='.keys: an array of index values. It can include an IN criterion returned fromhs.in().options: (optional) an object which specifies several options.filters: an array of filter returned fromhs.filter()and/orhs.while().limit: a maximum number of records to be retrieved (defaults to 1).offset: a number of records skipped before retrieving records (defaults to 0).
- values: an array of new column values which corresponds to
columnsparameter ofConnection.openIndex(). callback: a function to be called when the response received.
- Callback Function:
function(err, numRecords)- Parameters
err: anErrorobject when the request failed, otherwisenull.numRecords: a number of updated records.
- Parameters
To update records and get values before they are updated.
- Parametes
op: a search operation, one of'=','>','>=','<'and'<='.keys: an array of index values. It can include an IN criterion returned fromhs.in().options: (optional) an object which specifies several options.filters: an array of filter returned fromhs.filter()and/orhs.while().limit: a maximum number of records to be retrieved (defaults to 1).offset: a number of records skipped before retrieving records (defaults to 0).
- values: an array of new column values which corresponds to
columnsparameter ofConnection.openIndex(). callback: a function to be called when the response received.
- Callback Function:
function(err, records)- Parameters
err: anErrorobject when the request failed, otherwisenull.records: an array of records before modification. Each record is an array of column values which corresponds tocolumnsparameter ofConnection.openIndex().
- Parameters
To increment records.
- Parametes
op: a search operation, one of'=','>','>=','<'and'<='.keys: an array of index values. It can include an IN criterion returned fromhs.in().options: (optional) an object which specifies several options.filters: an array of filter returned fromhs.filter()and/orhs.while().limit: a maximum number of records to be retrieved (defaults to 1).offset: a number of records skipped before retrieving records (defaults to 0).
- values: an array of incremental values which corresponds to
columnsparameter ofConnection.openIndex(). callback: a function to be called when the response received.
- Callback Function:
function(err, numRecords)- Parameters
err: anErrorobject when the request failed, otherwisenull.numRecords: a number of updated records.
- Parameters
To increment records and get values before they are updated.
- Parametes
op: a search operation, one of'=','>','>=','<'and'<='.keys: an array of index values. It can include an IN criterion returned fromhs.in().options: (optional) an object which specifies several options.filters: an array of filter returned fromhs.filter()and/orhs.while().limit: a maximum number of records to be retrieved (defaults to 1).offset: a number of records skipped before retrieving records (defaults to 0).
- values: an array of incremental values which corresponds to
columnsparameter ofConnection.openIndex(). callback: a function to be called when the response received.
- Callback Function:
function(err, records)- Parameters
err: anErrorobject when the request failed, otherwisenull.records: an array of records before modification. Each record is an array of column values which corresponds tocolumnsparameter ofConnection.openIndex().
- Parameters
To decrement records.
- Parametes
op: a search operation, one of'=','>','>=','<'and'<='.keys: an array of index values. It can include an IN criterion returned fromhs.in().options: (optional) an object which specifies several options.filters: an array of filter returned fromhs.filter()and/orhs.while().limit: a maximum number of records to be retrieved (defaults to 1).offset: a number of records skipped before retrieving records (defaults to 0).
- values: an array of decremental values which corresponds to
columnsparameter ofConnection.openIndex(). callback: a function to be called when the response received.
- Callback Function:
function(err, numRecords)- Parameters
err: anErrorobject when the request failed, otherwisenull.numRecords: a number of updated records.
- Parameters
To decrement records and get values before they are updated.
- Parametes
op: a search operation, one of'=','>','>=','<'and'<='.keys: an array of index values. It can include an IN criterion returned fromhs.in().options: (optional) an object which specifies several options.filters: an array of filter returned fromhs.filter()and/orhs.while().limit: a maximum number of records to be retrieved (defaults to 1).offset: a number of records skipped before retrieving records (defaults to 0).
- values: an array of decremental values which corresponds to
columnsparameter ofConnection.openIndex(). callback: a function to be called when the response received.
- Callback Function:
function(err, records)- Parameters
err: anErrorobject when the request failed, otherwisenull.records: an array of records before modification. Each record is an array of column values which corresponds tocolumnsparameter ofConnection.openIndex().
- Parameters
To delete records.
- Parametes
op: a search operation, one of'=','>','>=','<'and'<='.keys: an array of index values. It can include an IN criterion returned fromhs.in().options: (optionaal) an object which specifies several options.filters: an array of filter returned fromhs.filter()and/orhs.while().limit: a maximum number of records to be retrieved (defaults to 1).offset: a number of records skipped before retrieving records (defaults to 0).
callback: a function to be called when the response received.
- Callback Function:
function(err, numRecords)- Parameters
err: anErrorobject when the request failed, otherwisenull.numRecords: a number of deleted records.
- Parameters
To delete records and get values before they are deleted.
- Parametes
op: a search operation, one of'=','>','>=','<'and'<='.keys: an array of index values. It can include an IN criterion returned fromhs.in().options: (optional) an object which specifies several options.filters: an array of filter returned fromhs.filter()and/orhs.while().limit: a maximum number of records to be retrieved (defaults to 1).offset: a number of records skipped before retrieving records (defaults to 0).
callback: a function to be called when the response received.
- Callback Function:
function(err, records)- Parameters
err: anErrorobject when the request failed, otherwisenull.records: an array of records before deletion. Each is an array of column values which corresponds tocolumnsparameter ofConnection.openIndex().
- Parameters
node-handlersocket depends on Vows for unit testing.
npm test
The encoding of MySQL server (default-character-set parameter in [mysqld] section)
which node-handlersocket supports is only UTF-8.
Binary data types are not supported.
node-handlersocket is licensed under the MIT license.