Home - aalfiann/jsonql-totaljs GitHub Wiki

Welcome to the jsonql-totaljs wiki!

Table Of Contents

About

This jsonql-totaljs will make you easier to use NoSQL Embedded with or without Total.js Framework. If you want to learn about NoSQL Embedded you can see the documentation from Total.js Framework at here.

Get Started

Install with NPM

$ npm install jsonql-totaljs

Usage

const JsonQL = require('jsonql-totaljs');
const jsonql = new JsonQL();
 
var q = [
    {
        select: {
            fields:['user_id','name'],
            from:'user',
            where:[
                ['name','==','budi']
            ]
        }
    }
];
 
// with callback
jsonql.query(q).exec(function(err,data) {
    console.log(data);        
});
 
// on top promise
jsonql.query(q).promise().then((data) => {
    console.log(data);        
});

Back to top

Structure

Query is using Json stucture so we can call it JsonQL. Every query must be inside array, so this will support to run multiple query as parallel in single execution.

Example multiple query

var q = [
  {
    select: {
      from:'user',
      where:[
        ['name','==','wawan']
      ]
    }
  },
  {
    select: {
      from:'profile',
      where:[
        ['address','==','jakarta']
      ]
    }
  }
];

jsonql.query(q).exec(function(err,data) {
  console.log(data);
});

Back to top

API

jsonql._odm

This is the layer of json ODM. See the documentation here.

jsonql.query(q)

  • q {array|string|json} - this is a query.

Example using array:

var q = [
  {
    select:{
      from:'user'
    }
  }
];
jsonql.query(q);

Example using object string:

var q = "[{select:{from:'user'}}]";
jsonql.query(q);

Example using json string:

var q = "[{"select":{"from":"user"}}]";
jsonql.query(q);

Back to API

jsonql.exec(callback)

  • callback - Callback will display error or data.

Example:

jsonql.query(obj).exec((err,data) => {
  if(err) return console.log(err);
  console.log(data);
});

Back to API

jsonql.promise()

This will execute query on top promise.

Example:

jsonql.query(obj).promise().then(data => {
  console.log(data);
}).catch(error => {
  console.log(error);
});

Back to API

Back to top

Output Format

This jsonql-totaljs output format query is an array, which is describe like this :

  • status {bool} : this is the status of query, if query success will return true.
  • response {object} : this is contains two nested property name, data and count. But this will not display if your query is error.
  • error {object} : this will display only when an error occured.

Example success query:

[
  { 
    status: true,
    response: {
      data: [
        {
          id: 1,
          name: 'aziz'
        },
        {
          id: 2,
          name: 'tika'
        }
      ]
    }, 
    count: 2
  }
]

Example error query:

[
  { 
    status: false,
    error: {
      // Error message will be appear here...
    }
  }
]

Back to top

Without Total.js Framework

This library is also supported if you are not using Total.js Framework.

Install total.js from NPM

$ npm install total.js@beta

In this current release version of total.js is still not supported, so you have to use total.js beta. See limitation.

Note:

  • from {string} : this should use absolute path.
  • into {string} : this should use absolute path.

Example select:

const JsonQL = require('jsonql-totaljs');
const jsonql = new JsonQL();
jsonql.query([
  {
    select: {
      fields:['user_id','name'],
      from:'~'+__dirname+'/fixtures/data1.nosql',
      where:[
        ['name','==','budi']
      ]
    }
  }
]).exec(function(err,data) {
  console.log(data);
});

Example insert:

const JsonQL = require('jsonql-totaljs');
const jsonql = new JsonQL();
jsonql.query([
  {
    insert: {
      into:'~'+__dirname+'/fixtures/data1.nosql',
      values:[
        {
          id:'1',
          name:'Aziz'
        }
      ]
    }
  }
]).exec(function(err,data) {
  console.log(data);
});

Limitation

NoSQL Embedded with absolute path will be supported in the next Total.js Framework version 3.3.3.

Back to top