From d40e5514407f2dd66d6ddf6f1922ce485fb99d1a Mon Sep 17 00:00:00 2001 From: Mark Wylde Date: Thu, 4 Jul 2024 21:46:11 +0100 Subject: [PATCH] Implement complex query --- README.md | 84 +++++++++++++++++++++++++++++------------------ package-lock.json | 4 +-- package.json | 2 +- 3 files changed, 55 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 3ec79f5..9eff0c6 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,4 @@ # doubledb -![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/markwylde/doubledb?style=flat-square) -[![GitHub package.json version](https://img.shields.io/github/package-json/v/markwylde/doubledb?style=flat-square)](https://github.com/markwylde/doubledb/blob/master/package.json) -[![GitHub](https://img.shields.io/github/license/markwylde/doubledb?style=flat-square)](https://github.com/markwylde/doubledb/blob/master/LICENSE) -[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square)](https://github.com/standard/semistandard) An on disk database that indexes everything for fast querying. @@ -11,21 +7,10 @@ An on disk database that indexes everything for fast querying. npm install --save doubledb ``` -## Features -- [x] read -- [x] insert -- [x] replace -- [x] patch -- [x] remove -- [x] find -- [x] filter -- [x] query (not optimized) -- [ ] query (optimized with indexes) - ## Usage ```javascript import createDoubledb from 'doubledb'; -const doubledb = createDoubledb('./data'); +const doubledb = await createDoubledb('./data'); doubledb.insert({ id: undefined, // defaults to uuid, must be unique @@ -146,25 +131,60 @@ The final record will be: ``` ### `.remove(key)` -Completely remove the key and it's value from the database. - -### Query -This has been implemented but it is very inefficient right now, as it doesn't use indexes. Inside, it iteratives over all keys then matches the operators. +Query the database using a complex query object. This method allows for advanced querying using a combination of fields and operators. -Supported operators can be found in the [applyMqlOperators](./index.js#L302) function. +### `.query(queryObject)` +Query the database using a complex query object. This method allows for advanced querying using a combination of fields and operators. +**Example:** ```javascript -const record = doubledb.query({ +const records = await doubledb.query({ location: 'London', category: 'b', + $or: [ + { firstName: { $eq: 'Joe' } }, + { firstName: { $eq: 'joe' } } + ] +}); +``` + +The `queryObject` can contain various fields and operators to filter the records. The following operators are supported: + +#### Operators: +- `$eq`: Equal to a value. +- `$ne`: Not equal to a value. +- `$gt`: Greater than a value. +- `$gte`: Greater than or equal to a value. +- `$lt`: Less than a value. +- `$lte`: Less than or equal to a value. +- `$in`: Value is in the provided array. +- `$nin`: Value is not in the provided array. +- `$all`: Array contains all the provided values. +- `$exists`: Field exists or does not exist. +- `$not`: Negates the condition. + +**Example Usage of Operators:** +```javascript +const records = await doubledb.query({ + age: { $gte: 18, $lt: 30 }, + status: { $in: ['active', 'pending'] }, + $or: [ + { role: { $eq: 'admin' } }, + { role: { $eq: 'user' } } + ], + preferences: { $exists: true } +}); +``` - $or: { - firstName: { - $eq: 'Joe', - }, - firstName: { - $eq: 'joe', - } - } -}) -``` \ No newline at end of file +### How Operators Work: +- **$eq**: Matches documents where the field is equal to the specified value. +- **$ne**: Matches documents where the field is not equal to the specified value. +- **$gt / $gte**: Matches documents where the field is greater than (or greater than or equal to) the specified value. +- **$lt / $lte**: Matches documents where the field is less than (or less than or equal to) the specified value. +- **$in**: Matches documents where the field value is in the specified array. +- **$nin**: Matches documents where the field value is not in the specified array. +- **$all**: Matches documents where the array field contains all the specified values. +- **$exists**: Matches documents where the field exists (or does not exist if set to false). +- **$not**: Matches documents that do not match the specified condition. + +This query method is powerful and allows combining multiple conditions and operators to fetch the desired records from the database. diff --git a/package-lock.json b/package-lock.json index d8bf6ee..f6a2000 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "doubledb", - "version": "2.3.0", + "version": "3.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "doubledb", - "version": "2.3.0", + "version": "3.0.0", "license": "MIT", "dependencies": { "level": "^8.0.1", diff --git a/package.json b/package.json index 8f8f929..abb842b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "doubledb", - "version": "2.3.0", + "version": "3.0.0", "type": "module", "description": "An on disk database that indexes everything for fast querying.", "main": "index.js",