mongo-query-dsl 1.0.0
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/README.md +339 -0
- package/dist/examples/usage.d.ts +13 -0
- package/dist/examples/usage.d.ts.map +1 -0
- package/dist/examples/usage.js +162 -0
- package/dist/examples/usage.js.map +1 -0
- package/dist/src/__tests__/executor.test.d.ts +2 -0
- package/dist/src/__tests__/executor.test.d.ts.map +1 -0
- package/dist/src/__tests__/executor.test.js +602 -0
- package/dist/src/__tests__/executor.test.js.map +1 -0
- package/dist/src/__tests__/integration.test.d.ts +2 -0
- package/dist/src/__tests__/integration.test.d.ts.map +1 -0
- package/dist/src/__tests__/integration.test.js +320 -0
- package/dist/src/__tests__/integration.test.js.map +1 -0
- package/dist/src/__tests__/parser.test.d.ts +2 -0
- package/dist/src/__tests__/parser.test.d.ts.map +1 -0
- package/dist/src/__tests__/parser.test.js +320 -0
- package/dist/src/__tests__/parser.test.js.map +1 -0
- package/dist/src/__tests__/resolver.test.d.ts +2 -0
- package/dist/src/__tests__/resolver.test.d.ts.map +1 -0
- package/dist/src/__tests__/resolver.test.js +178 -0
- package/dist/src/__tests__/resolver.test.js.map +1 -0
- package/dist/src/errors.d.ts +26 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +44 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/executor.d.ts +32 -0
- package/dist/src/executor.d.ts.map +1 -0
- package/dist/src/executor.js +214 -0
- package/dist/src/executor.js.map +1 -0
- package/dist/src/index.d.ts +43 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +120 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/parser.d.ts +42 -0
- package/dist/src/parser.d.ts.map +1 -0
- package/dist/src/parser.js +194 -0
- package/dist/src/parser.js.map +1 -0
- package/dist/src/resolver.d.ts +22 -0
- package/dist/src/resolver.d.ts.map +1 -0
- package/dist/src/resolver.js +55 -0
- package/dist/src/resolver.js.map +1 -0
- package/dist/src/types.d.ts +92 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/examples/usage.ts +190 -0
- package/jest.config.js +13 -0
- package/package.json +34 -0
- package/src/__tests__/executor.test.ts +694 -0
- package/src/__tests__/integration.test.ts +392 -0
- package/src/__tests__/parser.test.ts +377 -0
- package/src/__tests__/resolver.test.ts +218 -0
- package/src/errors.ts +47 -0
- package/src/executor.ts +276 -0
- package/src/index.ts +118 -0
- package/src/parser.ts +216 -0
- package/src/resolver.ts +58 -0
- package/src/types.ts +107 -0
- package/tsconfig.json +30 -0
package/README.md
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
# MongoDB Query DSL
|
|
2
|
+
|
|
3
|
+
A domain-specific language (DSL) for traversing and querying MongoDB documents with dynamic lookups. Supports both JSON-based queries and a custom string syntax.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Dual Query Syntax**: Use JSON for programmatic queries or a human-readable string DSL
|
|
8
|
+
- **Dynamic Value Resolution**: Reference document fields dynamically with `:fieldName` syntax
|
|
9
|
+
- **Enhanced GET Operations**: Use field aliases (`field as alias`), default fallbacks, and single-field extraction with `getOne`
|
|
10
|
+
- **Flexible Output Formats**: Get just values, full documents, or detailed execution traces
|
|
11
|
+
- **WHERE Conditions**: Filter documents with AND conditions and multiple value types
|
|
12
|
+
- **Type-Safe**: Written in TypeScript with strict mode enabled
|
|
13
|
+
- **Error Handling**: Custom error classes for better debugging
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install mongo-query-dsl
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { NoQL } from "mongo-query-dsl";
|
|
25
|
+
|
|
26
|
+
// Connect to MongoDB
|
|
27
|
+
const noql = await NoQL.connect("mongodb://localhost:27017");
|
|
28
|
+
|
|
29
|
+
// Execute a simple DSL query
|
|
30
|
+
const result = await noql.executeDSL(`
|
|
31
|
+
FROM users IN myDb WHERE _id = "user_123"
|
|
32
|
+
-> GET name, email
|
|
33
|
+
`);
|
|
34
|
+
|
|
35
|
+
console.log(result);
|
|
36
|
+
|
|
37
|
+
// Close connection
|
|
38
|
+
await noql.close();
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## String DSL Syntax
|
|
42
|
+
|
|
43
|
+
### Basic Structure
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
[WITH output: "value" | "document" | "trace"]
|
|
47
|
+
FROM collection IN database WHERE field = "value" [AND field2 = value2]
|
|
48
|
+
-> LOOKUP collection IN database BY field [WHERE condition]
|
|
49
|
+
-> GET field1, field2, ...
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Example
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
WITH output: "trace"
|
|
56
|
+
FROM documents IN myDb WHERE _id = "doc_123"
|
|
57
|
+
-> LOOKUP users IN myDb BY userId
|
|
58
|
+
-> GET name, email, role
|
|
59
|
+
-> LOOKUP :role IN permissions BY roleId
|
|
60
|
+
-> GET permissions
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## JSON Query Syntax
|
|
64
|
+
|
|
65
|
+
### Structure
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
{
|
|
69
|
+
output?: "value" | "document" | "trace",
|
|
70
|
+
start: {
|
|
71
|
+
collection: string,
|
|
72
|
+
db: string,
|
|
73
|
+
where: { field: value, ... }
|
|
74
|
+
},
|
|
75
|
+
steps: [
|
|
76
|
+
{ type: "lookup", collection: string, db: string, by: string, where?: {...} },
|
|
77
|
+
{ type: "get", fields: string[], default?: Record<string, any> },
|
|
78
|
+
{ type: "getOne", fields: string }
|
|
79
|
+
]
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Example
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
const query = {
|
|
87
|
+
output: "value",
|
|
88
|
+
start: {
|
|
89
|
+
collection: "Sales",
|
|
90
|
+
db: "smartTadbeer",
|
|
91
|
+
where: { _id: "693553b149c75fdf39495ead" },
|
|
92
|
+
},
|
|
93
|
+
steps: [
|
|
94
|
+
{
|
|
95
|
+
type: "get",
|
|
96
|
+
fields: ["clientId", "candidateId as candidateRef"],
|
|
97
|
+
default: { gender: "Female" },
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
type: "lookup",
|
|
101
|
+
collection: "Candidate",
|
|
102
|
+
db: "smartTadbeer",
|
|
103
|
+
by: "candidateRef",
|
|
104
|
+
where: { gender: ":gender" },
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
type: "getOne",
|
|
108
|
+
fields: "fullNameEnglish",
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const result = await noql.executeJson(query);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### GET and GETONE Notes
|
|
117
|
+
|
|
118
|
+
- `get.fields` supports aliases with `field as alias`.
|
|
119
|
+
- `get.fields` can also reference previous documents using `:fieldName`.
|
|
120
|
+
- `get.default` provides fallback values and is merged with extracted fields.
|
|
121
|
+
- `getOne.fields` extracts a single field as the direct result value.
|
|
122
|
+
|
|
123
|
+
## Dynamic Value Resolution
|
|
124
|
+
|
|
125
|
+
Use the colon prefix (`:fieldName`) to reference values from the current document context:
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
// If current document has { ownerType: "users", ownerSource: "mainDb" }
|
|
129
|
+
// Then :ownerType resolves to "users" and :ownerSource resolves to "mainDb"
|
|
130
|
+
|
|
131
|
+
const query = `
|
|
132
|
+
FROM documents IN myDb WHERE _id = "doc_123"
|
|
133
|
+
-> LOOKUP metadata IN myDb BY metadataId
|
|
134
|
+
-> GET ownerId, ownerType, ownerSource
|
|
135
|
+
-> LOOKUP :ownerType IN :ownerSource BY ownerId
|
|
136
|
+
-> GET name
|
|
137
|
+
`;
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Output Formats
|
|
141
|
+
|
|
142
|
+
### `"value"` (default)
|
|
143
|
+
|
|
144
|
+
Returns only the requested field(s):
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
{ success: true, result: { name: "John", email: "john@example.com" } }
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### `"document"`
|
|
151
|
+
|
|
152
|
+
Returns the entire final document:
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
{
|
|
156
|
+
success: true,
|
|
157
|
+
result: { _id: "123", name: "John", email: "john@example.com", ... }
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### `"trace"`
|
|
162
|
+
|
|
163
|
+
Returns result with full execution trace for debugging:
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
{
|
|
167
|
+
success: true,
|
|
168
|
+
result: { name: "John" },
|
|
169
|
+
trace: [
|
|
170
|
+
{
|
|
171
|
+
stepNumber: 0,
|
|
172
|
+
stepType: "start",
|
|
173
|
+
collection: "users",
|
|
174
|
+
db: "myDb",
|
|
175
|
+
query: { _id: "user_123" },
|
|
176
|
+
documentFound: { _id: "user_123", name: "John", ... }
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
stepNumber: 1,
|
|
180
|
+
stepType: "get",
|
|
181
|
+
fieldsExtracted: { name: "John" }
|
|
182
|
+
}
|
|
183
|
+
]
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## API Reference
|
|
188
|
+
|
|
189
|
+
### NoQL Class
|
|
190
|
+
|
|
191
|
+
#### `static async connect(connectionString: string): Promise<NoQL>`
|
|
192
|
+
|
|
193
|
+
Connects to MongoDB and returns a NoQL instance.
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
const noql = await NoQL.connect("mongodb://localhost:27017");
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
#### `async executeJson(query: JsonQuery): Promise<QueryResult>`
|
|
200
|
+
|
|
201
|
+
Executes a JSON query.
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
const result = await noql.executeJson({
|
|
205
|
+
start: { collection: "users", db: "myDb", where: { _id: "123" } },
|
|
206
|
+
steps: [{ type: "get", fields: ["name"] }],
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
#### `async executeDSL(dsl: string): Promise<QueryResult>`
|
|
211
|
+
|
|
212
|
+
Parses and executes a DSL string query.
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
const result = await noql.executeDSL(
|
|
216
|
+
'FROM users IN myDb WHERE _id = "123" -> GET name',
|
|
217
|
+
);
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
#### `parseDSL(dsl: string): JsonQuery`
|
|
221
|
+
|
|
222
|
+
Parses a DSL string into JsonQuery format without executing.
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
const query = noql.parseDSL('FROM users IN myDb WHERE _id = "123" -> GET name');
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
#### `async close(): Promise<void>`
|
|
229
|
+
|
|
230
|
+
Closes the database connection.
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
await noql.close();
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Error Handling
|
|
237
|
+
|
|
238
|
+
### DocumentNotFoundError
|
|
239
|
+
|
|
240
|
+
Thrown when a document is not found during query execution.
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
try {
|
|
244
|
+
await noql.executeDSL('FROM users IN myDb WHERE _id = "nonexistent"');
|
|
245
|
+
} catch (error) {
|
|
246
|
+
if (error instanceof DocumentNotFoundError) {
|
|
247
|
+
console.log(`Document not found in ${error.collection}`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### ParseError
|
|
253
|
+
|
|
254
|
+
Thrown when DSL syntax is invalid.
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
try {
|
|
258
|
+
noql.parseDSL("INVALID SYNTAX");
|
|
259
|
+
} catch (error) {
|
|
260
|
+
if (error instanceof ParseError) {
|
|
261
|
+
console.log(`Parse error: ${error.message}`);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### ResolutionError
|
|
267
|
+
|
|
268
|
+
Thrown when a dynamic field reference cannot be resolved.
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
try {
|
|
272
|
+
// If :ownerType field doesn't exist in the document
|
|
273
|
+
await noql.executeDSL(
|
|
274
|
+
'FROM docs IN myDb WHERE _id = "123" -> LOOKUP :ownerType IN myDb BY ownerId',
|
|
275
|
+
);
|
|
276
|
+
} catch (error) {
|
|
277
|
+
if (error instanceof ResolutionError) {
|
|
278
|
+
console.log(`Cannot resolve field: ${error.fieldName}`);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## Value Types in WHERE Conditions
|
|
284
|
+
|
|
285
|
+
The DSL supports multiple value types:
|
|
286
|
+
|
|
287
|
+
```
|
|
288
|
+
WHERE stringField = "text" # String
|
|
289
|
+
WHERE numberField = 42 # Number
|
|
290
|
+
WHERE boolField = true # Boolean
|
|
291
|
+
WHERE nullField = null # Null
|
|
292
|
+
WHERE dynamicField = :fieldName # Dynamic reference
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## Examples
|
|
296
|
+
|
|
297
|
+
See the [examples/usage.ts](examples/usage.ts) file for comprehensive examples.
|
|
298
|
+
|
|
299
|
+
## Use Cases
|
|
300
|
+
|
|
301
|
+
- **Data Traversal**: Navigate through related documents across collections
|
|
302
|
+
- **Dynamic Lookups**: Resolve collection and database names at runtime
|
|
303
|
+
- **Audit Trails**: Use trace mode to debug complex queries
|
|
304
|
+
- **Data Aggregation**: Collect data from multiple related documents
|
|
305
|
+
- **Reporting**: Build reports from interconnected data
|
|
306
|
+
|
|
307
|
+
## TypeScript Support
|
|
308
|
+
|
|
309
|
+
The package is written in TypeScript and provides full type definitions:
|
|
310
|
+
|
|
311
|
+
```typescript
|
|
312
|
+
import {
|
|
313
|
+
NoQL,
|
|
314
|
+
JsonQuery,
|
|
315
|
+
QueryResult,
|
|
316
|
+
DocumentNotFoundError,
|
|
317
|
+
} from "mongo-query-dsl";
|
|
318
|
+
|
|
319
|
+
const query: JsonQuery = {
|
|
320
|
+
start: { collection: "users", db: "myDb", where: { _id: "123" } },
|
|
321
|
+
steps: [],
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
const result: QueryResult = await noql.executeJson(query);
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
## Requirements
|
|
328
|
+
|
|
329
|
+
- Node.js 16+
|
|
330
|
+
- MongoDB 4.0+
|
|
331
|
+
- TypeScript 5.0+ (for development)
|
|
332
|
+
|
|
333
|
+
## License
|
|
334
|
+
|
|
335
|
+
MIT
|
|
336
|
+
|
|
337
|
+
## Contributing
|
|
338
|
+
|
|
339
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example usage of the MongoDB Query DSL
|
|
3
|
+
*
|
|
4
|
+
* This file demonstrates both JSON and DSL query syntax
|
|
5
|
+
*
|
|
6
|
+
* IMPORTANT: This is an example file. To run it, you need:
|
|
7
|
+
* 1. A running MongoDB instance
|
|
8
|
+
* 2. Sample data loaded into your database
|
|
9
|
+
* 3. Update the connection string below
|
|
10
|
+
*/
|
|
11
|
+
declare function main(): Promise<void>;
|
|
12
|
+
export { main };
|
|
13
|
+
//# sourceMappingURL=usage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage.d.ts","sourceRoot":"","sources":["../../examples/usage.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AAEH,iBAAe,IAAI,kBAsKlB;AAUD,OAAO,EAAE,IAAI,EAAE,CAAC"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.main = main;
|
|
4
|
+
const index_1 = require("../src/index");
|
|
5
|
+
/**
|
|
6
|
+
* Example usage of the MongoDB Query DSL
|
|
7
|
+
*
|
|
8
|
+
* This file demonstrates both JSON and DSL query syntax
|
|
9
|
+
*
|
|
10
|
+
* IMPORTANT: This is an example file. To run it, you need:
|
|
11
|
+
* 1. A running MongoDB instance
|
|
12
|
+
* 2. Sample data loaded into your database
|
|
13
|
+
* 3. Update the connection string below
|
|
14
|
+
*/
|
|
15
|
+
async function main() {
|
|
16
|
+
// Connect to MongoDB
|
|
17
|
+
const connectionString = process.env.MONGODB_URI || "mongodb://localhost:27017";
|
|
18
|
+
const noql = await index_1.NoQL.connect(connectionString);
|
|
19
|
+
console.log("MongoDB Query DSL Examples\n");
|
|
20
|
+
console.log("=".repeat(50));
|
|
21
|
+
// Example 1: Simple JSON Query
|
|
22
|
+
console.log("\n1. Simple JSON Query - Get document by _id");
|
|
23
|
+
console.log("-".repeat(50));
|
|
24
|
+
const jsonQuery1 = {
|
|
25
|
+
output: "value",
|
|
26
|
+
start: {
|
|
27
|
+
collection: "documents",
|
|
28
|
+
db: "myDb",
|
|
29
|
+
where: { _id: "doc_123" },
|
|
30
|
+
},
|
|
31
|
+
steps: [{ type: "get", fields: ["title", "content"] }],
|
|
32
|
+
};
|
|
33
|
+
const result1 = await noql.executeJson(jsonQuery1);
|
|
34
|
+
console.log("Query:", JSON.stringify(jsonQuery1, null, 2));
|
|
35
|
+
console.log("Result:", JSON.stringify(result1, null, 2));
|
|
36
|
+
// Example 2: JSON Query with LOOKUP
|
|
37
|
+
console.log("\n2. JSON Query with LOOKUP step");
|
|
38
|
+
console.log("-".repeat(50));
|
|
39
|
+
const jsonQuery2 = {
|
|
40
|
+
output: "document",
|
|
41
|
+
start: {
|
|
42
|
+
collection: "documents",
|
|
43
|
+
db: "myDb",
|
|
44
|
+
where: { _id: "doc_123" },
|
|
45
|
+
},
|
|
46
|
+
steps: [
|
|
47
|
+
{
|
|
48
|
+
type: "lookup",
|
|
49
|
+
collection: "users",
|
|
50
|
+
db: "myDb",
|
|
51
|
+
by: "userId",
|
|
52
|
+
},
|
|
53
|
+
{ type: "get", fields: ["name", "email"] },
|
|
54
|
+
],
|
|
55
|
+
};
|
|
56
|
+
const result2 = await noql.executeJson(jsonQuery2);
|
|
57
|
+
console.log("Query:", JSON.stringify(jsonQuery2, null, 2));
|
|
58
|
+
console.log("Result:", JSON.stringify(result2, null, 2));
|
|
59
|
+
// Example 3: JSON Query with Dynamic Lookups
|
|
60
|
+
console.log("\n3. JSON Query with Dynamic Lookups (using :prefixed values)");
|
|
61
|
+
console.log("-".repeat(50));
|
|
62
|
+
const jsonQuery3 = {
|
|
63
|
+
output: "trace",
|
|
64
|
+
start: {
|
|
65
|
+
collection: "documents",
|
|
66
|
+
db: "myDb",
|
|
67
|
+
where: { _id: "doc_123" },
|
|
68
|
+
},
|
|
69
|
+
steps: [
|
|
70
|
+
{
|
|
71
|
+
type: "lookup",
|
|
72
|
+
collection: "parentCollection",
|
|
73
|
+
db: "myDb",
|
|
74
|
+
by: "parentId",
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
type: "get",
|
|
78
|
+
fields: ["ownerId", "ownerType", "ownerSource"],
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
type: "lookup",
|
|
82
|
+
collection: ":ownerType",
|
|
83
|
+
db: ":ownerSource",
|
|
84
|
+
by: "ownerId",
|
|
85
|
+
},
|
|
86
|
+
{ type: "get", fields: ["salesId", "name"] },
|
|
87
|
+
],
|
|
88
|
+
};
|
|
89
|
+
const result3 = await noql.executeJson(jsonQuery3);
|
|
90
|
+
console.log("Query:", JSON.stringify(jsonQuery3, null, 2));
|
|
91
|
+
console.log("Result:", JSON.stringify(result3, null, 2));
|
|
92
|
+
// Example 4: DSL Query - Simple
|
|
93
|
+
console.log("\n4. DSL Query - Simple");
|
|
94
|
+
console.log("-".repeat(50));
|
|
95
|
+
const dslQuery1 = `
|
|
96
|
+
FROM documents IN myDb WHERE _id = "doc_123"
|
|
97
|
+
-> GET title, content
|
|
98
|
+
`.trim();
|
|
99
|
+
const result4 = await noql.executeDSL(dslQuery1);
|
|
100
|
+
console.log("DSL:", dslQuery1);
|
|
101
|
+
console.log("Result:", JSON.stringify(result4, null, 2));
|
|
102
|
+
// Example 5: DSL Query - With LOOKUP
|
|
103
|
+
console.log("\n5. DSL Query - With LOOKUP");
|
|
104
|
+
console.log("-".repeat(50));
|
|
105
|
+
const dslQuery2 = `
|
|
106
|
+
FROM documents IN myDb WHERE _id = "doc_123"
|
|
107
|
+
-> LOOKUP users IN myDb BY userId
|
|
108
|
+
-> GET name, email
|
|
109
|
+
`.trim();
|
|
110
|
+
const result5 = await noql.executeDSL(dslQuery2);
|
|
111
|
+
console.log("DSL:", dslQuery2);
|
|
112
|
+
console.log("Result:", JSON.stringify(result5, null, 2));
|
|
113
|
+
// Example 6: DSL Query - With Dynamic Lookups and Trace
|
|
114
|
+
console.log("\n6. DSL Query - With Dynamic Lookups and Trace");
|
|
115
|
+
console.log("-".repeat(50));
|
|
116
|
+
const dslQuery3 = `
|
|
117
|
+
WITH output: "trace"
|
|
118
|
+
FROM documents IN myDb WHERE _id = "doc_123"
|
|
119
|
+
-> LOOKUP parentCollection IN myDb BY parentId
|
|
120
|
+
-> GET ownerId, ownerType, ownerSource
|
|
121
|
+
-> LOOKUP :ownerType IN :ownerSource BY ownerId
|
|
122
|
+
-> GET salesId, name
|
|
123
|
+
`.trim();
|
|
124
|
+
const result6 = await noql.executeDSL(dslQuery3);
|
|
125
|
+
console.log("DSL:", dslQuery3);
|
|
126
|
+
console.log("Result:", JSON.stringify(result6, null, 2));
|
|
127
|
+
// Example 7: DSL Query - With WHERE conditions
|
|
128
|
+
console.log("\n7. DSL Query - With WHERE conditions in LOOKUP");
|
|
129
|
+
console.log("-".repeat(50));
|
|
130
|
+
const dslQuery4 = `
|
|
131
|
+
FROM documents IN myDb WHERE _id = "doc_123"
|
|
132
|
+
-> LOOKUP users IN myDb BY userId WHERE status = "active" AND verified = true
|
|
133
|
+
-> GET name, email
|
|
134
|
+
`.trim();
|
|
135
|
+
const result7 = await noql.executeDSL(dslQuery4);
|
|
136
|
+
console.log("DSL:", dslQuery4);
|
|
137
|
+
console.log("Result:", JSON.stringify(result7, null, 2));
|
|
138
|
+
// Example 8: Parse DSL without executing
|
|
139
|
+
console.log("\n8. Parse DSL (without executing)");
|
|
140
|
+
console.log("-".repeat(50));
|
|
141
|
+
const dslToParse = `
|
|
142
|
+
WITH output: "document"
|
|
143
|
+
FROM documents IN myDb WHERE _id = "doc_123"
|
|
144
|
+
-> LOOKUP users IN myDb BY userId
|
|
145
|
+
-> GET name, email
|
|
146
|
+
`.trim();
|
|
147
|
+
const parsedQuery = noql.parseDSL(dslToParse);
|
|
148
|
+
console.log("DSL:", dslToParse);
|
|
149
|
+
console.log("Parsed to JSON:", JSON.stringify(parsedQuery, null, 2));
|
|
150
|
+
// Close connection
|
|
151
|
+
await noql.close();
|
|
152
|
+
console.log("\n" + "=".repeat(50));
|
|
153
|
+
console.log("Connection closed");
|
|
154
|
+
}
|
|
155
|
+
// Run examples
|
|
156
|
+
if (require.main === module) {
|
|
157
|
+
main().catch((error) => {
|
|
158
|
+
console.error("Error running examples:", error);
|
|
159
|
+
process.exit(1);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=usage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage.js","sourceRoot":"","sources":["../../examples/usage.ts"],"names":[],"mappings":";;AA6LS,oBAAI;AA7Lb,wCAAoC;AAEpC;;;;;;;;;GASG;AAEH,KAAK,UAAU,IAAI;IACjB,qBAAqB;IACr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