node-mariadb

A pure javascript client for mariadb

node-mariadb

Node-mariadb is a pure javascript client for mariadb.
Some Drivers have compatibility with mysql.
Our goal is supporting almost functions and protocols of mariadb.

The latest version of node-mariadb supported only HandlerSocket Driver(ver 0.1.1).

  • HandlerSocket
  • Regular mysql protocol
  • Galera cluster
  • Non-blocking api client
$ npm install node-mariadb
var nodeMaria = require('node-mariadb');
 
//hs readable configuration. 
var connection = nodeMaria.createConnection({
  driverType: nodeMaria.DRIVER_TYPE_HANDLER_SOCKET,
  host:'localhost',
  port:9998
});
 
connection.on('erorr', function(err){
  console.log(err);
  process.exit(1);
});
 
connection.on('connect', function(){
  
  ### Basic find.
  connection.openIndex('CTU', 'employee', nodeMaria.HandlerSocket.PRIMARY, ['id', 'name', 'age']
  , function(errhs){
    hs.find([1], {limit:1},function(errdata){
      console.log(data);   =>  [{id: '1', name: 'jack', age: '40'}]
    });
  });
 
 
  ### Find with in clause
  connection.openIndex('CTU', 'employee', nodeMaria.HandlerSocket.PRIMARY, ['id', 'name', 'age'], ['id']
  , function(errhs){
    hs.find({in: [1, 2, 3]}, {limit: 3}, function(errdata){
      console.log(data);   =>  [{id: '2', name: 'Tonny', age: '38'}, .... ]
    });
  });
 
 
  ### Find with filter
  connection.openIndex('CTU', 'employee', 'age', ['id', 'name', 'age'], ['age']
  , function(errhs){
    hs.find([26], {operator: '>', limit:5, filter:['age', '<=', 28]}, function(errdata){
      console.log(data);   =>  [{id: '2', name: 'Chloe', age: '35'}, ..... ]
    });
  });
  
});
var nodeMaria = require('node-mariadb');
 
//hs writeable configuration. 
var connection = nodeMaria.createConnection({
  driverType: nodeMaria.DRIVER_TYPE_HANDLER_SOCKET,
  host:'localhost',
  port:9999
});
 
connection.on('erorr', function(err){
  console.log(err);
  process.exit(1);
});
 
connection.on('connect', function(){
  
  ### Insert
  connection.openIndex('CTU', 'employee', nodeMaria.HandlerSocket.PRIMARY, ['id', 'name', 'age']
  , function(errhs){
    hs.insert([10, 'Edgar', 35],function(errdata){
      console.log(data);   => true
    });
  });
 
 
  ### Update
  connection.openIndex('CTU', 'employee', nodeMaria.HandlerSocket.PRIMARY, ['id', 'name', 'age'], ['id']
  , function(errhs){
    hs.update([12], {set:[12, 'Bill', 50]}, function(erraffectedNum){
      console.log(affectedNum);   =>  1
    });
  });
  
  ### Delete
  connection.openIndex('CTU', 'employee', nodeMaria.HandlerSocket.PRIMARY, ['id']
  , function(errhs){
    hs.delete([1], function(erraffectedNum){
      console.log(affectedNum);   =>  1
    });
  });
 
});

Api Reference

  • settings:
  • driverType: A driver type name. Currently suppoerted only HandlerSocket.
  • host: A host name or address for mariadb server.
  • port: A port number for mariadb server.
  • auth: {key: 'Your authnetication key'}
  • options: Options(debug, logging, etc...). (Not implemented yet.)


Emitted when a connection is established.

Emitted when an error is occured.

####close Emitted when a connection is closed.


open index to prepare to handle records.

  • config
  1. A database name
  2. A table name
  3. An index name. If you would like to use 'Primary', you can use constant of Handlersocket.PRIMARY.
  4. An array of column names.
  5. optional [An array of column names use for filter or while filter.]
  • callback: Callback function

Find records by values and options.

  • values : [value1, value2, ... ] or {in: [value1, value2, ... ]}
  • options:
  • limit: num (default 1)
  • offset: num (default 0)
  • operator: =,>,>=,<and<=
  • filter: ['indexname', 'operator', 'value']
  • while: ['indexname', 'operator', 'value']
  • callback: callback function.

Insert record.

  • values : [value1, value2, ... ]
  • callback: callback function.

Update rows by values and options.
It arguments are similar to find. A only different is 'set' option which is used to set updating values like as 'update' clause of sql.

  • values : [value1, value2, ... ] or {in: [value1, value2, ... ]}
  • options:
  • set(required): [value1, value2, .... ]
  • limit: num (default 1)
  • offset: num (default 0)
  • operator: =,>,>=,<and<=
  • filter: ['indexname', 'operator', 'value']
  • while: ['indexname', 'operator', 'value']
  • callback: callback function.

Delete records by values and options. It arguments are same as find.

  • values : [value1, value2, ... ] or {in: [value1, value2, ... ]}
  • options:
  • limit: num (default 1)
  • offset: num (default 0)
  • operator: =,>,>=,<and<=
  • filter: ['indexname', 'operator', 'value']
  • while: ['indexname', 'operator', 'value']
  • callback: callback function.

Increment records '+' options num.

  • values : [value1, value2, ... ] or {in: [value1, value2, ... ]}
  • options:
  • '+': num
  • limit: num (default 1)
  • offset: num (default 0)
  • operator: =,>,>=,<and<=
  • filter: ['indexname', 'operator', 'value']
  • while: ['indexname', 'operator', 'value']
  • callback: callback function.

Decrement records '-' options num.

  • values : [value1, value2, ... ] or {in: [value1, value2, ... ]}
  • options:
  • '-': num
  • limit: num (default 1)
  • offset: num (default 0)
  • operator: =,>,>=,<and<=
  • filter: ['indexname', 'operator', 'value']
  • while: ['indexname', 'operator', 'value']
  • callback: callback function.

[Modifier]Get behavior is same as normal modifier(Such as update, delete, increment and decrement).
But, their return values are not affected num but records which are contents of the records before modification.

$ npm test

(The MIT License)

Copyright (c) 2013 Yuki Takei(Noppoman) yuki@miketokyo.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.