Documentation

c-treeEDGE IoT Database

Previous Topic

Next Topic

Node.js Client Library for c-treeEDGE

This is a tutorial for connecting to Node.js using the c-treeEDGE Node.js module. For more about the FairCom support for Node.js, see FairCom Node.js Support in the on-line documentation.

This Node.js module has a dependency on Python 2.7.

Installation

  1. This Node.js module is included with your c-treeACE or c-treeEDGE download. Look for the restapi.nodejs folder in the sdk folder.
  2. Using the command line, create and then change into a new folder.
  3. Issue the following command, adapting it to the location of your restapi.nodejs folder:

    npm install C:\path\to\restapi.nodejs

Note: You may be required to update to the latest npm version. (For example, many Raspberry Pi Raspbian devices come pre-installed with an old version.) npm is easily updated as followed:
> sudo npm install -g npm@latest

Usage

The module currently contains 5 methods:

  • createTable
  • deleteTable
  • createIndex
  • createRecord
  • query

Basic usage of the module starts with the following two lines:

var ctree = require("ctree");

ctree.setAuth("localhost", 8443, "admin", "ADMIN", false);

Then you can call any of the 5 methods.

Create Table

createTable(db, table, fields, callback)

  • db: your existing database name
  • table: your not existing table name
  • fields: an array with objects containing the fields: name, type and length
  • callback: a callback function that will be called with two parameters once the table is created: err and res. If err is false, the table was created successfully.

Valid field types:

  • "BOOL"
  • "TINYINT"
  • "UTINYINT"
  • "SMALLINT"
  • "USMALLINT"
  • "INTEGER"
  • "UINTEGER"
  • "BIGINT"
  • "UBIGINT"
  • "MONEY"
  • "DATE"
  • "TIME"
  • "FLOAT"
  • "DOUBLE"
  • "TIMESTAMP"
  • "EFLOAT"
  • "BINARY"
  • "CHARS"
  • "FPSTRING"
  • "F2STRING"
  • "F4STRING"
  • "NUMBER"
  • "CURRENCY"
  • "PSTRING"
  • "VARBINARY"
  • "LVB"
  • "VARCHAR"
  • "LVC"
  • "NCHAR"
  • "NVARCHAR"

Example:

ctree.createTable("ctreeSQL", "books", [

{

"name": "id",

"type": "VARCHAR",

"length": 0

},

{

"name": "name",

"type": "VARCHAR",

"length": 128

},

{

"name": "author",

"type": "VARCHAR",

"length": 128

},

{

"name": "country",

"type": "VARCHAR",

"length": 48

}

]

, function(err, res){

if (err) {

console.log('error:', err);

} else {

console.log("table created");

}

});

Delete Table

deleteTable(db, table, callback)

  • db: your existing database name
  • table: your existing table name
  • callback: a callback function that will be called with two parameters once the table is deleted: err and res. If err is false, the table was deleted successfully.

Create Index

createIndex(db, table, index, unique, fields, callback)

  • db: your existing database name
  • table: your existing table name
  • index: your not existing index name
  • unique: true or false depending if you want the index to allow duplicate values
  • fields: an array with objects containing the fields: name and ascending
  • callback: a callback function that will be called with two parameters once the index is created: err and res. If err is false, the index was created successfully.

Example:

ctree.createIndex("ctreeSQL", "books", "myindex", false, [{

"name": "name",

"ascending": true

}], function(err, res){

if (err) {

console.log('error:', err);

} else {

console.log('response', res);

}

});

Create Record

createRecord( db, table, record, callback)

  • db: your existing database name
  • table: your existing table name
  • record: an object containing any or all the fields of your table
  • callback: a callback function that will be called with two parameters once the record is created: err and res. If err is false, the record was created successfully.

Example:

ctree.createRecord("ctreeSQL", "books", {

"name": "Moby Dick",

"author": "Herman Melville",

"country": "United States"

}

, function(err, res){

if (err) {

console.log('error:', err);

} else {

console.log("record created: ", res);

}

});

Query

query( db, table, index, operator, fields, callback)

  • db: your existing database name
  • table: your existing table name
  • index: your existing index name
  • operator: one of “=”, “>=”, “<=”, “>” or “<”
  • fields: an object containing the fields and search values as keys and values respectively.
  • callback: a callback function that will be called with two parameters once the query is executed: err and res. If err is false, the query was executed successfully and the res object contains the query search result.

Example:

ctree.query("ctreeSQL", "books", "myindex", "=", {name:"testn"}, function(err, res){

if (err) {

console.log('error:', err);

} else {

console.log('response', res);

}

});

TOCIndex