Product Documentation

c-treeDB API for Node.js

Previous Topic

Next Topic

Navigational Node.js Methods

The ctdbsdk.js module exposes almost all (668 in total) c-treeDB functions from the FairCom DB client library. It exposes them in two ways:

  • They can be called using an enhanced version that throws an exception in case of an error:

    ctdb.AllocSession(ctdb.CTSESSION_CTDB);

  • They can be called using the direct version that does NOT throw an exception in case of an error (it returns null instead):

    ctdb.ctdbAllocSession(ctdb.CTSESSION_CTDB);

Notice the difference is the ctdb.ctdb in front of AllocSession.

Example

The following is an example using the enhanced version:

const ctdb = require('./ctdbsdk');

var ref = require('ref');

var hSession = ctdb.AllocSession(ctdb.CTSESSION_CTDB);

ctdb.Logon(hSession, "FAIRCOMS", "ADMIN", "ADMIN");

var hDatabase = ctdb.AllocDatabase(hSession);

ctdb.Connect(hDatabase, "test");

var hTable = ctdb.AllocTable(hDatabase);

ctdb.OpenTable(hTable, "custmast", ctdb.CTOPEN_NORMAL);

hRecord = ctdb.AllocRecord(hTable);

var custnumb = Buffer.alloc(4+1);

var custname = Buffer.alloc(47+1);

var retval = ctdb.FirstRecord(hRecord);

while (retval != ctdb.END_OF_FILE) {

retval = 0;

retval |= ctdb.GetFieldAsString(hRecord, 0, custnumb, custnumb.length);

retval |= ctdb.GetFieldAsString(hRecord, 4, custname, custname.length);

if (retval) {

console.log("Display_Records(): ctdbGetFieldAsString()");

break;

}

let custnumbStr = ref.readCString(custnumb, 0);

let custnameStr = ref.readCString(custname, 0);

console.log(custnumbStr + ", " + custnameStr);

/* read next record */

retval = ctdb.NextRecord(hRecord);

if (retval == ctdb.END_OF_FILE)

break; /* reached end of file */

if (retval) {

console.log("error using ctdbNextRecord: " + retval);

break;

}

}

ctdb.CloseTable(hTable);

ctdb.Logout(hSession);

ctdb.FreeRecord(hRecord);

ctdb.FreeTable(hTable);

ctdb.FreeDatabase(hDatabase);

ctdb.FreeSession(hSession);

As you can see, the Node.js module is a faithful wrapper on top of the c-treeDB API. Therefore, the c-treeDB Developer Guide will be a fairly accurate guide on how to use the module.

TOCIndex