|
| 1 | +# Search Query Language <!-- omit in toc --> |
| 2 | + |
| 3 | + <!-- omit in toc --> |
| 4 | + <!-- omit in toc --> |
| 5 | + <!-- omit in toc --> |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +[Documentation](https://your-docs-link.com) |
| 10 | + |
| 11 | +<!-- install placeholder --> |
| 12 | + |
| 13 | +## ✨ Features |
| 14 | + |
| 15 | +- **Expression Parsing**: Convert query strings into an AST for structured processing. |
| 16 | +- **MongoDB Query Conversion**: Transform expressions into MongoDB-compatible query objects. |
| 17 | +- **Flexible Syntax Support**: Supports comparison operators like `=`, `!=`, `>`, `<`, `>=`, `<=`. |
| 18 | + |
| 19 | +## 🔍 Search Syntax |
| 20 | + |
| 21 | +The following table lists the syntax that you can use to construct a query. |
| 22 | + |
| 23 | +| Syntax | Usage | Description | Examples | |
| 24 | +| ------ | ------------------------------------------- | -------------------------------------------- | ----------------------------------------------- | |
| 25 | +| `=` | `field="value"` | Exact match | `name="andrew"` | |
| 26 | +| `!=` | `field!="value"` | Not equal to | `status!="active"` | |
| 27 | +| `<` | `field<value` | Less than | `amount<5000` | |
| 28 | +| `<=` | `field<=value` | Less than or equal to | `amount<=10000` | |
| 29 | +| `>` | `field>value` | Greater than | `created>1672531200` | |
| 30 | +| `>=` | `field>=value` | Greater than or equal to | `created>=1672531200` | |
| 31 | +| `AND` | `condition1 AND condition2` | Combine conditions (both must be true) | `status="active" AND age>=18` | |
| 32 | +| `OR` | `condition1 OR condition2` | Combine conditions (either can be true) | `status="stop" OR age>40` | |
| 33 | +| `()` | `(condition1 OR condition2) AND condition3` | Group conditions to control logic precedence | `(status="active" OR status="stop") AND age>18` | |
| 34 | + |
| 35 | +## 📌 Notes |
| 36 | + |
| 37 | +- 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). |
| 38 | +- Support **comparison operators (`<`, `<=`, `>`, `>=`)**. |
| 39 | +- Use **`AND`** and **`OR`** to combine conditions. |
| 40 | +- Parentheses **`()`** help group conditions for better control over query logic. |
| 41 | + |
| 42 | +## 🚀 Example: Minimal |
| 43 | + |
| 44 | +```ts |
| 45 | +import { parseToMongo } from '@andrew_l/search-query-language'; |
| 46 | + |
| 47 | +const clients = db.collection('clients'); |
| 48 | + |
| 49 | +// GET /clients?search="age>18" |
| 50 | +app.get('/clients', async (req, res) => { |
| 51 | + const filter = parseToMongo(req.query.search); |
| 52 | + const items = await clients.find(filter).toArray(); |
| 53 | + res.json(items); |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +## 🚀 Example: Usage MongoDB |
| 58 | + |
| 59 | +```ts |
| 60 | +import { parseToMongo } from '@andrew_l/search-query-language'; |
| 61 | + |
| 62 | +// GET /clients?search="active=true AND age>18" |
| 63 | +app.get('/clients', async (req, res) => { |
| 64 | + const filter = parseToMongo(req.query.search, { |
| 65 | + transform: { |
| 66 | + _id: [MONGO_TRANSFORM.OBJECT_ID, MONGO_TRANSFORM.NOT_NULLABLE], |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + const items = await db.collection('clients').find(filter).toArray(); |
| 71 | + |
| 72 | + res.json(items); |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +## 🚀 Example: Usage Mongoose |
| 77 | + |
| 78 | +```ts |
| 79 | +import mongoose from 'mongoose'; |
| 80 | +import { parseToMongoose } from '@andrew_l/search-query-language'; |
| 81 | + |
| 82 | +const Client = mongoose.Model('Clients', new mongoose.Schema({ |
| 83 | + email: String; |
| 84 | + active: Boolean; |
| 85 | +})) |
| 86 | + |
| 87 | +// GET /clients?search="email="andrew.io.dev@gmail.com" AND active=true" |
| 88 | +app.get('/clients', async (req, res) => { |
| 89 | + const filter = parseToMongoose(Client, req.query.search); |
| 90 | + const items = await Client.find(filter).lean(); |
| 91 | + |
| 92 | + res.json(items); |
| 93 | +}); |
| 94 | +``` |
0 commit comments