rollun-ts-rql
v0.10.0
Published
RQL query object and serializer written in TypeScript
Downloads
2,493
Readme
rollun-ts-rql
RQL library written in Typescript. This library contains:
- Set of objects that represent RQL nodes
- Stringifier that converts object tree into RQL string
Installation
preferred way to install this library is via npm. Run
npm install rollun-ts-rql
Basic usage
import { QueryStringifier } from 'rollun-ts-rql';
import { Query } from 'rollun-ts-rql';
import { Select } from 'rollun-ts-rql';
import { And } from 'rollun-ts-rql';
import { Eq } from 'rollun-ts-rql';
import { Ge } from 'rollun-ts-rql';
const query = new Query({
select: new Select(['id', 'name', 'age', 'city']),
query: new And([
new Eq('name', 'John'),
new Ge('age', 18)
])
});
const rqlString = QueryStringifier.stringify(query);
console.log(rqlString);
// using builder pattern
const query = new Query()
.addSelect(new Select(['id', 'name', 'age', 'city']))
.addQuery(new And([
new Eq('name', 'John'),
new Ge('age', 18)
]))
// {
// select: new Select(['id', 'name', 'age', 'city']),
// query: new And([
// new Eq('name', 'John'),
// new Ge('age', 18)
// ])
// });
const rqlString = QueryStringifier.stringify(query);
console.log(rqlString);
//output: select(id,name,age,city)&and(eq(name,John),ge(age,18))
Nodes
Scalar nodes:
- eq - new Eq(<field>,<value>)
- ne - new Ne(<field>,<value>)
- lt - new Lt(<field>,<value>)
- gt - new Gt(<field>,<value>)
- le - new Le(<field>,<value>)
- ge - new Ge(<field>,<value>)
- like - new Like(<field>,<value>)
- alike - new Alike(<field>,<value>)
Array Nodes
- in - new In(<field>,<array of values>)
- out - new Out(<field>,<array of values>)
Logic operators
- and - new And(<array of nodes>)
- or - new Or(<array of nodes>)
- not - new Not(<array of nodes>)
Aggregate nodes
- groupby - new GroupBy(<array of values>)
Query to string
QueryStringifier exposes static method stringify
, that takes a node
and returns a string representation of that node
import { QueryStringifier } from 'rollun-ts-rql';
const rqlString = QueryStringifier.stringify(
new Query({
query: new And([
new Eq('status', 'active'),
new Eq('age', 33)
])
})
);
console.log(rqlString);
//output: and(eq(status,active),eq(age,33))
Contributing
Before contributing to this lib, make sure you have correct node.js version. It is specified in .nvmrc
file
You can use NVM to manage node versions.
For example, run following command to automatically use correct node version:
nvm use