|
| 1 | +<p align="center"> |
| 2 | + <img src="https://raw.githubusercontent.com/beekeeper-studio/queryleaf/main/logo-transparent-bg-green-shape.png" width="100" height="100" alt="QueryLeaf Logo"> |
| 3 | +</p> |
| 4 | + |
| 5 | +<h1 align="center">@queryleaf/lib</h1> |
| 6 | + |
| 7 | +<p align="center">SQL to MongoDB query translator - Core library</p> |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +`@queryleaf/lib` is the core library for QueryLeaf, a tool that translates SQL queries into MongoDB commands. It provides the foundation for all QueryLeaf packages by parsing SQL using node-sql-parser, transforming it into an abstract command set, and executing those commands against the MongoDB Node.js driver. |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +- Parse SQL statements into an abstract syntax tree |
| 16 | +- Compile SQL AST into MongoDB commands |
| 17 | +- Execute MongoDB commands using the official driver |
| 18 | +- Support for basic SQL operations (SELECT, INSERT, UPDATE, DELETE) |
| 19 | +- Advanced querying features: |
| 20 | + - Nested field access (e.g., `address.zip`) |
| 21 | + - Array element access (e.g., `items[0].name`) |
| 22 | + - GROUP BY with aggregation functions (COUNT, SUM, AVG, MIN, MAX) |
| 23 | + - JOINs between collections |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +```bash |
| 28 | +npm install @queryleaf/lib |
| 29 | +# or |
| 30 | +yarn add @queryleaf/lib |
| 31 | +``` |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```typescript |
| 36 | +import { QueryLeaf } from '@queryleaf/lib'; |
| 37 | +import { MongoClient } from 'mongodb'; |
| 38 | + |
| 39 | +// Your existing MongoDB client |
| 40 | +const mongoClient = new MongoClient('mongodb://localhost:27017'); |
| 41 | +await mongoClient.connect(); |
| 42 | + |
| 43 | +// Create QueryLeaf with your MongoDB client |
| 44 | +const queryLeaf = new QueryLeaf(mongoClient, 'mydatabase'); |
| 45 | + |
| 46 | +// Execute SQL queries against your MongoDB database |
| 47 | +const results = await queryLeaf.execute('SELECT * FROM users WHERE age > 21'); |
| 48 | +console.log(results); |
| 49 | + |
| 50 | +// When you're done, close your MongoDB client |
| 51 | +await mongoClient.close(); |
| 52 | +``` |
| 53 | + |
| 54 | +### Testing with DummyQueryLeaf |
| 55 | + |
| 56 | +For testing or debugging without a real database, use DummyQueryLeaf: |
| 57 | + |
| 58 | +```typescript |
| 59 | +import { DummyQueryLeaf } from '@queryleaf/lib'; |
| 60 | + |
| 61 | +// Create a DummyQueryLeaf instance for testing |
| 62 | +const queryLeaf = new DummyQueryLeaf('mydatabase'); |
| 63 | + |
| 64 | +// Operations will be logged to console but not executed |
| 65 | +await queryLeaf.execute('SELECT * FROM users WHERE age > 21'); |
| 66 | +// [DUMMY MongoDB] FIND in mydatabase.users with filter: { "age": { "$gt": 21 } } |
| 67 | +``` |
| 68 | + |
| 69 | +## SQL Query Examples |
| 70 | + |
| 71 | +```sql |
| 72 | +-- Basic SELECT with WHERE |
| 73 | +SELECT name, email FROM users WHERE age > 21 |
| 74 | + |
| 75 | +-- Nested field access |
| 76 | +SELECT name, address.city FROM users WHERE address.zip = '10001' |
| 77 | + |
| 78 | +-- Array access |
| 79 | +SELECT items[0].name FROM orders WHERE items[0].price > 100 |
| 80 | + |
| 81 | +-- GROUP BY with aggregation |
| 82 | +SELECT status, COUNT(*) as count FROM orders GROUP BY status |
| 83 | + |
| 84 | +-- JOIN between collections |
| 85 | +SELECT u.name, o.total FROM users u JOIN orders o ON u._id = o.userId |
| 86 | +``` |
| 87 | + |
| 88 | +## Links |
| 89 | + |
| 90 | +- [Website](https://queryleaf.com) |
| 91 | +- [Documentation](https://queryleaf.com/docs) |
| 92 | +- [GitHub Repository](https://github.com/beekeeper-studio/queryleaf) |
| 93 | + |
| 94 | +## License |
| 95 | + |
| 96 | +QueryLeaf is dual-licensed: |
| 97 | + |
| 98 | +- [AGPL-3.0](https://github.com/beekeeper-studio/queryleaf/blob/main/LICENSE.md) for open source use |
| 99 | +- [Commercial license](https://github.com/beekeeper-studio/queryleaf/blob/main/COMMERCIAL_LICENSE.md) for commercial use |
| 100 | + |
| 101 | +For commercial licensing options, visit [queryleaf.com](https://queryleaf.com). |
0 commit comments