From 1f2729da36d763460edbfb718540d1ac68b5ec8e Mon Sep 17 00:00:00 2001 From: rizmyabdulla Date: Thu, 18 Jan 2024 20:01:32 +0530 Subject: [PATCH] Updated to v1.1.0 --- dist/JsonFlexDB.js | 37 +++++++++++++++++++++++++++++++++++++ readme.md | 10 ++++++++++ 2 files changed, 47 insertions(+) diff --git a/dist/JsonFlexDB.js b/dist/JsonFlexDB.js index 05f8bbe..fe386f5 100644 --- a/dist/JsonFlexDB.js +++ b/dist/JsonFlexDB.js @@ -261,6 +261,43 @@ class JsonFlexDB { } } + /** + * Finds a single document based on a query. + * @async + * @param {Object} query - The query object. + * @returns {Promise} A promise that resolves to the matching document or null if not found. + * @throws {Error} If there's an issue executing the findOne operation. + */ + async findOne(query) { + try { + await this.ensureLoaded(); + + const results = await this.find(query); + return results.length > 0 ? this.data[results[0]] : null; + } catch (error) { + throw new Error(`Failed to execute findOne operation: ${error.message}`); + } + } + + /** + * Gets the next available auto-incremented ID. + * @async + * @returns {Promise} A promise that resolves to the next available ID. + */ + async getAutoIncrementId() { + await this.ensureLoaded(); + + let maxId = 0; + for (const key in this.data) { + const id = parseInt(key); + if (!isNaN(id) && id > maxId) { + maxId = id; + } + } + + return maxId + 1; + } + /** * Gets all documents in the database. * @returns {Object} An object representing all documents in the database. diff --git a/readme.md b/readme.md index cfc216d..172a466 100644 --- a/readme.md +++ b/readme.md @@ -78,6 +78,12 @@ Finds documents based on a query. - `query`: The query object. +### `async findOne(query: Object): Promise` + +Finds a single document based on a query. + +- `query` (Object): The query object. + ### `async insert(document: Object): Promise` Inserts a document into the database. @@ -101,6 +107,10 @@ Removes documents based on a query. Gets all documents in the database. +### `async getAutoIncrementId(): Promise` + +Gets the next available auto-incremented ID. + ### `visualize(): void` Visualizes the data in the console using `console.table`.