Skip to content

Usage examples (NodeJS)

Dmitry Kochin edited this page Jul 10, 2018 · 4 revisions

As of Alpha release you can connect to the Ties.DB node, make INSERTs, UPDATEs and SELECTs. INSERTs and UPDATEs currently can be done via API only, however SELECTs can be expressed in SQL-like language TiQL.

The requests to Ties.DB are mostly asynchronous. You can use Promises or async/await syntax to make calls to the API.

We will further use async/await syntax for the sake of clearness. We presume that all the code written is wrapped into async function such as

(async function(){
   //Sample code here
})();

Establishing connection to the Ties.DB node

const Client = require('tiesdb-client');

let c = new Client.Connection();
await c.connect('ws://<IP address>:<port>/<path>');

console.log('Connected!');

Inserting a row into the table

ATTENTION! The Ties.DB has schema so the data must comply with the schema. Create your tablespace first!

    //... - the connection code from the Establishing connection example

    const uuidv4 = require('uuid/v4');

    //Create empty record for tablespace 'client-dev.test' and table 'all_types'
    let record = new Client.Record('client-dev.test', 'all_types');

    //Suppose the table 'all_types` has the following fields and types
    let types = {
        Id: 'uuid',
        fBinary: 'binary',
        fBoolean: 'boolean',
        fDecimal: 'decimal',
        fDouble: 'double',
        fDuration: 'duration',
        fFloat: 'float',
        fInteger: 'integer',
        fLong: 'long',
        fString: 'string',
        fTime: 'time'
    };

    record.putFields({
        Id: uuidv4(),
        fBinary: new Buffer("e0a61e5ad74f", 'hex'),
        fBoolean: false,
        fDecimal: new Client.BD.BigDecimal("-1.235e-8"),
        fDouble: 158.234e200,
        fDuration: 20*86400,
        fFloat: -42803.234e-8,
        fInteger: 423424432,
        fLong: -278374928374,
        fString: "This is UTF-8 строка",
        fTime: new Date()
    }, types);

    //Issue modification request with the new record. You should also specify 
    //the private key of your user to sign your record
    let response = await c.modify([record], Buffer.from('e0a61e5ad74fc154927e8412c7f03528134f755e7eb45554eb7a99c2744ac34e', 'hex'));

    //Check for errors

You can also fill the record with fields one by one

    //... - the connection code from the Establishing connection example

    const uuidv4 = require('uuid/v4');

    //Create empty record for tablespace 'client-dev.test' and table 'all_types'
    let record = new Client.Record('client-dev.test', 'all_types');

    record.putField('Id', uuidv4(), 'uuid');
    record.putField('fBoolean', false, 'boolean');
    record.putField('fDouble', 158.234e200, 'double');
    //...

    //Issue modification request with the new record. You should also specify 
    //the private key of your user to sign your record
    let privatekey = Buffer.from('e0a61e5ad74fc154927e8412c7f03528134f755e7eb45554eb7a99c2744ac34e', 'hex');
    let response = await c.modify([record], privatekey);

    //Check response for errors    

Retrieving records

    let uuid = uuidv4();

    //Insert a row with the Id=uuid
    //... skipped (see an example of inserting a row)

    //Now let us retrieve it
    let records = await c.recollect(
        `SELECT *
             FROM "client-dev.test"."all_types"
             WHERE
                 Id IN (${uuid.toString()})`
    );

    //Now `record` variable holds the array of the records found.

Modifying the record

Since all the records should be signed you must first retrieve current version of the record before modifying it.

    let records = await c.recollect(`SELECT Id, fLong FROM "client-dev.test"."all_types" WHERE Id=${uuid.toString()}`);
    
    records[0].putValue('fLong', '123', 'long');
    
    let response = await c.modify(records, privatekey);

    //Check response for errors

Checking response for errors

...

Clone this wiki locally