@digitize/query-builder
v0.1.1
Published
## Usage
Downloads
3
Readme
query-builder
Usage
import {QueryBuilder} from "@digitize/query-builder";
const {Filter, And, Or, Not} = QueryBuilder();
const filter = And(
Or(
Filter('field1').gt(123),
Filter('field2').eq('value2')
),
Filter('field3').eq('ADFADFAD'),
Not(
Or(
Filter('field4').eq(234234),
And(
Filter('field5').between(134, 3456),
Not(Filter('field5').between(134, 3456)),
)
)
),
Filter('field6').between(333, 888)
);
// Get syntax agnostic JSON representation of the query
console.dir(filter.toJSON(), {depth: 12});
// Get serialized query string
console.log(filter.toString({
operators: {
eq: '=',
ne: '<>',
lt: '<',
le: '<=',
gt: '>',
ge: '>=',
in: ({formatValue}, ...values) => `IN (${values.map(formatValue).join(", ")})`,
between: ({formatValue}, v1, v2) => `BETWEEN ${formatValue(v1)} AND ${formatValue(v2)}`,
},
and: 'AND',
or: 'OR',
not: 'NOT',
formatValue: (v: any) => JSON.stringify(v),
formatIdentifier: (v: string) => v,
}));