Skip to content

Search Query Language

licensenpm versionnpm bundle size

Search Query Language is a lightweight utility that converts human-readable query strings into structured representations. It supports parsing expressions into an abstract syntax tree (AST) and transforming them into queries.

Documentation

🔧 Installation

sh
$ npm add -D @andrew_l/search-query-language
sh
$ pnpm add -D @andrew_l/search-query-language
sh
$ yarn add -D @andrew_l/search-query-language

✨ Features

  • Expression Parsing: Convert query strings into an AST for structured processing.
  • MongoDB Query Conversion: Transform expressions into MongoDB-compatible query objects.
  • Flexible Syntax Support: Supports comparison operators like =, !=, >, <, >=, <=.

🔍 Search Syntax

The following table lists the syntax that you can use to construct a query.

SyntaxUsageDescriptionExamples
=field="value"Exact matchname="andrew"
!=field!="value"Not equal tostatus!="active"
<field<valueLess thanamount<5000
<=field<=valueLess than or equal toamount<=10000
>field>valueGreater thancreated>1672531200
>=field>=valueGreater than or equal tocreated>=1672531200
ANDcondition1 AND condition2Combine conditions (both must be true)status="active" AND age>=18
ORcondition1 OR condition2Combine conditions (either can be true)status="stop" OR age>40
()(condition1 OR condition2) AND condition3Group conditions to control logic precedence(status="active" OR status="stop") AND age>18

📌 Notes

  • The left operand must always be an identifier (e.g., a field name), while the right operand must always be a literal value (e.g., a string, number, or boolean).
  • Support comparison operators (<, <=, >, >=).
  • Use AND and OR to combine conditions.
  • Parentheses () help group conditions for better control over query logic.

🚀 Example: Minimal

ts
import { parseToMongo } from '@andrew_l/search-query-language';

const clients = db.collection('clients');

// GET /clients?search="age>18"
app.get('/clients', async (req, res) => {
  const filter = parseToMongo(req.query.search);
  const items = await clients.find(filter).toArray();
  res.json(items);
});

🚀 Example: Usage MongoDB

ts
import { parseToMongo } from '@andrew_l/search-query-language';

// GET /clients?search="active=true AND age>18"
app.get('/clients', async (req, res) => {
  const filter = parseToMongo(req.query.search, {
    transform: {
      _id: [MONGO_TRANSFORM.OBJECT_ID, MONGO_TRANSFORM.NOT_NULLABLE],
    },
  });

  const items = await db.collection('clients').find(filter).toArray();

  res.json(items);
});

🚀 Example: Usage Mongoose

ts
import mongoose from 'mongoose';
import { parseToMongoose } from '@andrew_l/search-query-language';

const Client = mongoose.Model('Clients', new mongoose.Schema({
  email: String;
  active: Boolean;
}))

// GET /clients?search="email="andrew.io.dev@gmail.com" AND active=true"
app.get('/clients', async (req, res) => {
  const filter = parseToMongoose(Client, req.query.search);
  const items = await Client.find(filter).lean();

  res.json(items);
});