Liveblocks query parser
Largely inspired by
Stripe's search query language.
Step 1: Configure your parser
The purpose of this library is to safely parse any user-provided input string,
and returning a Query instance, which is the parsed result. You can then use
this parse result to turn it into an arbitrary search query.
The parser definition defines what queries are considered legal for your use
case. Invalid queries will get rejected.
import { QueryParser } from "@liveblocks/query-parser";
const parser = new QueryParser({
fields: {
id: "token",
name: "string",
description: "string",
email: "string",
age: "numeric",
resolved: "boolean",
},
indexableFields: {
meta: "mixed",
},
});
The following field types are possible:
token | String | : (exact) |
boolean | Boolean | : (exact) |
string | String | : (exact), ^ (prefix) |
numeric | Number | : (exact), >, <, >=, <= (comparison) |
mixed | String or number | : (exact), ^ (prefix), >, <, >=, <= (comparison) |
Step 2: Use it
parser.parse('id:"abc123"');
parser.parse('id^"abc123"');
parser.parse('resolved:true');
parser.parse('resolved:false');
parser.parse('email:"vincent@liveblocks.io"');
parser.parse('email^"vincent@"');
parser.parse("age:42");
parser.parse("age<42");
parser.parse("age<=42");
parser.parse("age>42");
parser.parse("age>=42");
parser.parse('metadata["color"]:"red"');
parser.parse('metadata["last-updated"]>1709122155');
parser.parse('metadata["resolved"]:true');
parser.parse('metadata["org"]^"liveblocks:engineering"');
parser.parse('id:"abc123" metadata["color"]:"red"');
parser.parse('id:"abc123" AND email:"vincent@liveblocks.io"');
parser.parse('id:"abc123" OR email:"vincent@liveblocks.io"');
parser.parse('id:"abc123" AND name:"Vincent" OR email:"vincent@liveblocks.io"');
parser.parse('id:"abc123" -name:"Vincent");
Step 3: Use the output
The output is a fully-typed AST. TypeScript auto-completion is your best friend.
Here is a
high-level example output.