@andrew_l/search-query-language 0.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +94 -0
- package/dist/index.cjs +711 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +473 -0
- package/dist/index.d.mts +473 -0
- package/dist/index.d.ts +473 -0
- package/dist/index.mjs +697 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Andrew L. <andrew.io.dev@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
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://men232.github.io/toolkit/reference/@andrew_l/search-query-language/)
|
|
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
|
+
```
|