Hello guys, Today I would like to make some little research about JsonQL. So if you are first time hearing about JsonQL, the definitions about JsonQL is maybe “Json Query Language”. Why I said maybe? because before I write this post, I have research through google and found nothing about this kind technology. Actualy, I just found about jasperreports which is a Free Java Reporting Library. But we will not talk about making report guys. Jasperreports is not what I want to learn about JsonQL. In this article, we will talk about what is JsonQL? How to use and what is the benefit for us?

NoSQL Embedded

It was 6 months already I made some projects will the power of Total.js Framework. And surely, I love this Total.js Framework because so simple and truly a rapid development framework and one of reasons why I choose TotalJS is because they have built in NoSQL Embedded. NoSQL Embedded is a database management which your data is saved as json as filebased (json file).

NoSQL Embedded is very good for small database or creating small prototype application. But actualy I was use NoSQL Embedded for 1 million rows and trustme, it still so fast. But when you are in big database, you will have high risk to get Javascript Heap Out of Memory. To avoid this, you should read carefully about my previous article at here.

NoSQL Embedded TotalJS has many features which is almost like the other of nosql databases. It query is very strong, accurate and easy to configure in multiple conditions.

Example simple query:

1
2
3
4
5
6
7
8
9
10
11
// set database users
var user = NOSQL('users');

// query find users
user.find().make(function(builder) {
builder.where('age', '>', 20);
builder.where('confirmed', true);
builder.callback(function(err, response, count) {
console.log(response);
});
});

For more detail you can see in their documentation.

JsonQL

JsonQL is the Json Query Language. which means that you are able to query with the power of json structure.

JsonQL idea was comes up from my mind, After I using NoSQL Embedded more and more often in growing application. My code is getting big and the readability is going harder slowly. So I was thinking that, how to make query is eficient, simple and clean like in mostly RDBMS? So I created jsonql-totaljs library which is to help you make easier to query with NoSQL Embedded.

Query Like SQL

If you are familiar with SQL Query, don’t worry, my jsonql-totaljs library is using same SQL format in JsonQL way.

JsonQL Example:

  • Select

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    var query = [
    {
    select:{
    from:'dt_user',
    where:[
    ['age','>',10]
    ]
    }
    }
    ];
  • Insert

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    var query = [
    {
    insert: {
    into:'dt_user',
    values:[
    {
    id:'10',
    name:'aziz'
    }
    ]
    }
    }
    ];
  • Update

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    var query = [
    {
    update: {
    from:'dt_user',
    where:[
    ['name','==','aziz']
    ],
    set:{
    id:'10',
    name:'aziz alfian'
    }
    }
    }
    ];
  • Modify

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    var query = [
    {
    modify: {
    from:'dt_user',
    where:[
    ['name','==','aziz']
    ],
    set:{
    name:'M ABD AZIZ ALFIAN'
    }
    }
    }
    ];
  • Delete

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    var query = [
    {
    delete: {
    from:'dt_user',
    where:[
    ['name','==','aziz']
    ]
    }
    }
    ];

After you see the example above, it is similar with SQL Query, more easier and very readable right?

Parallel Execution

Please look at the example above, you will see that the JsonQL is under an element of array. It is because the library of jsonql-totaljs has features parallel or multiple request query by single execution.

Example Multiple Query

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var query = [
{
select: {
from:'dt_user',
where:[
['name','==','wawan']
]
}
},
{
select: {
from:'dt_profile',
where:[
['address','==','jakarta']
]
}
}
];

The process behind of this parallel execution is on top Promise.all and the output is in array format.

Multiple Join or As Nested

Join is the most common required things in building query. The fact is to create join with using NoSQL embedded way is little bit dificult and confusing. But when you are in JsonQL way, everything will going simple.

  • Join
    This is the very simple join.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    var query = [
    {
    select: {
    from:'dt_user',
    where:[
    ['name','==','budi']
    ],
    join:[
    {
    name:'dt_profile',
    from:'dt_profile',
    on:['id','id'],
    first:true
    }
    ]
    }
    }
    ];
  • Join Nested
    Sometime we want to create join in nested way.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    var query = [
    {
    select: {
    from:'dt_user',
    where:[
    ['name','==','budi']
    ],
    join:[
    {
    name:'dt_profile',
    from:'dt_profile',
    on:['id','id'],
    first:true,
    join:[
    {
    name:'additional',
    from:'user_other',
    on:['id','id'],
    first:false
    }
    ]
    }
    ]
    }
    }
    ];
  • Join Nested Manually
    Here is to create join nested manually, so we can set the sequence of the data to be nested.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    var query = [
    {
    select: {
    from:'dt_user',
    where:[
    ['name','==','budi']
    ],
    join:[
    {
    name:'dt_profile',
    from:'dt_profile',
    on:['id','id'],
    first:true
    },
    {
    name:'additional',
    from:'user_other',
    on:['id','id'],
    first:false
    }
    ],
    nested:['dt_profile','additional']
    }
    }
    ];

Multiple Query Format

For easier to implement the JsonQL into your development code. jsonql-totaljs has multiple query format.
Which is you can use array-object string, json string, or array-object to build queries.

Example:

  • array-object string

    1
    var query = "[{select:{from:'dt_user'}}]";
  • json string

    1
    var query = "[{"select":{"from":"dt_user"}}]";
  • array-object
    array object is same like the others example above.

Can be use outside TotalJS Framework

I’ve created this library is for TotalJS Framework only at the first time. But today you can use this library in another NodeJS framework. But the requirement for this is you have to use the latest version of TotalJS. You know that TotalJS framework is can be use as helper library. This is the one of TotalJS pros than other framework.

Complete Documentation

jsonql-totaljs has complete documentation which is available in their Wiki. I will not rewrite all things from documentation in this article, because I know, it will Too Long Didn’t Read.

So here is the basic tutorial how to use jsonql-totaljs :

  • Install by NPM

    1
    $ npm install jsonql-totaljs
  • Get Started

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    const JsonQL = require('jsonql-totaljs');
    // create new object jsonql
    const jsonql = new JsonQL();

    // build query
    var query = [
    {
    select: {
    fields:['user_id','name'],
    from:'dt_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);
    });

Conclusion

JsonQL is will make you easier to create query with NoSQL embedded database. With JsonQL, you long query will still readable. No spagethi code because JsonQL is already having nice structure.

So here is the benefit of using JsonQL :

  • Easier and readable query
  • Your source code is more cleaner
  • Simple multiple join table
  • Support parallel query in single execution
  • Support Promise, so you have no worry about callback hell
  • Saving your time to make builder of query function
  • Query support in string which means the query can be saved into database
  • etc

Actualy there is more benefit of using JsonQL, but remember that not all application is suitable to use JsonQL. Just use JsonQL if your application is require this.

Thank You.