Product Documentation

FairCom APIs for Node.js

Previous Topic

Next Topic

Node.js Tutorial

This tutorial demonstrates connecting to Node.js using the FairCom Edge Node.js module. For more about the FairCom support for Node.js, see FairCom API for Node.js in the on-line documentation.

This section explains the tutorial. The next subsections explain how to run it on Windows and Linux:

  • Node.js Tutorial for Windows
  • Node.js Tutorial for Linux

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", true);

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": "INTEGER",

"length": 4

},

{

"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, limit, offset, query, callback)

  • db: your existing database name
  • table: your existing table name
  • index: your existing index name
  • limit: the amount of records to return
  • offset: the amount of records to skip (for paging)
  • query: for example: {name:{"operator":"=", "value":"Moby Dick"}}
    • with operator being one of “=”, “>=”, “<=”, “>” or “<”
  • 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", 100, 0, {name:{"operator":"=", "value":"Moby Dick"}}, function(err, res){

if (err) {

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

} else {

console.log('response', res);

}

});

TOCIndex