From cb61909ae4439e3e61c5318e85e2a3f913b3bcec Mon Sep 17 00:00:00 2001 From: Bryan Lundberg Date: Sun, 11 May 2025 21:36:38 -0600 Subject: [PATCH 1/6] feat: Add method to remove directories in NaroFiler Introduce a `removeDirectory` method in the `NaroFiler` class, leveraging `fs-jetpack`'s `remove` function. This provides functionality to delete directories, extending the file management capabilities of the class. --- src/manage/files/NaroFiler.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/manage/files/NaroFiler.ts b/src/manage/files/NaroFiler.ts index 3eeeb6e..20cdfd3 100644 --- a/src/manage/files/NaroFiler.ts +++ b/src/manage/files/NaroFiler.ts @@ -1,5 +1,5 @@ import { decode, encode } from "notepack.io"; -import { dir, inspectTree, read, write } from "fs-jetpack"; +import { dir, inspectTree, read, write, remove } from "fs-jetpack"; export class NaroFiler { static ensureDirectory(path: string): void { @@ -20,4 +20,8 @@ export class NaroFiler { if (!directory) return []; return directory.children.filter((child) => child.type === "dir").map((child) => child.name); } + + static removeDirectory(path: string): void { + return remove(path); + } } From c0aa393dc0bfc0befa687898406fb2c1dc3130aa Mon Sep 17 00:00:00 2001 From: Bryan Lundberg Date: Sun, 11 May 2025 21:36:51 -0600 Subject: [PATCH 2/6] test: Add unit test for NaroFiler.removeDirectory function This commit adds a new test to validate the behavior of the removeDirectory method in NaroFiler. Ensures directories are properly deleted and their existence status is correctly evaluated. --- __test__/unit/NaroFiler/NaroFiler.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/__test__/unit/NaroFiler/NaroFiler.test.ts b/__test__/unit/NaroFiler/NaroFiler.test.ts index 6cbfbcd..b4a0f9d 100644 --- a/__test__/unit/NaroFiler/NaroFiler.test.ts +++ b/__test__/unit/NaroFiler/NaroFiler.test.ts @@ -42,3 +42,11 @@ test("listDirectories", async () => { const result = NaroFiler.listDirectories(DIRNAME_MOCK); expect(result).toEqual(["subdir1", "subdir2"]); }); + +test("removeDirectory", async () => { + const subDir = `${DIRNAME_MOCK}/subdir`; + await dirAsync(subDir); + NaroFiler.removeDirectory(subDir); + const existsDir = exists(subDir); + expect(existsDir).toBe(false); +}); From be197ce5268f73c85903cad3fd14ce9b40462f07 Mon Sep 17 00:00:00 2001 From: Bryan Lundberg Date: Sun, 11 May 2025 21:37:11 -0600 Subject: [PATCH 3/6] feat: Add method to remove a collection Introduce a `removeCollection` method to delete a collection directory and its reference in memory. This enhances the ability to manage collections dynamically. --- src/core/Core.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/Core.ts b/src/core/Core.ts index 217fb3c..086192d 100644 --- a/src/core/Core.ts +++ b/src/core/Core.ts @@ -53,4 +53,10 @@ export class Core { const dataPath = `${collectionPath}/${this.logFileName}`; NaroFiler.writeBinaryFile(dataPath, this.collections[path]); } + + removeCollection(path: string): void { + const collectionPath = `${this.rootPath}/${path}`; + NaroFiler.removeDirectory(collectionPath); + delete this.collections[path]; + } } From a3a8628cbcaa337ddcdfe39a3303143bb3497a4f Mon Sep 17 00:00:00 2001 From: Bryan Lundberg Date: Sun, 11 May 2025 21:37:19 -0600 Subject: [PATCH 4/6] test: Add unit test for removeCollection method Add a test case to ensure the removeCollection method correctly removes a specified collection directory. This improves test coverage and helps validate the functionality of collection removal. --- __test__/unit/core/Core.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/__test__/unit/core/Core.test.ts b/__test__/unit/core/Core.test.ts index 50f9025..9690616 100644 --- a/__test__/unit/core/Core.test.ts +++ b/__test__/unit/core/Core.test.ts @@ -99,3 +99,17 @@ test("writeCollection overwrites existing data in the collection", () => { const writtenData = NaroFiler.readBinaryFile(filePath); expect(writtenData).toEqual(updatedData); }); + +test("removeCollection removes the collection directory", () => { + const core = new Core(DIRNAME_MOCK); + const collectionName = "testCollection"; + const data = [...USERS_MOCK]; + + core.updateCollection(collectionName, data); + core.writeCollection(collectionName); + + core.removeCollection(collectionName); + + const filePath = `${DIRNAME_MOCK}/${collectionName}`; + expect(NaroFiler.listDirectories(DIRNAME_MOCK)).not.toContain(filePath); +}); From 299db2892cb784a1844bbe8476a08a0d5951005c Mon Sep 17 00:00:00 2001 From: Bryan Lundberg Date: Sun, 11 May 2025 21:37:34 -0600 Subject: [PATCH 5/6] test: Add unit tests for the clear method in Naro Added tests to ensure the clear method correctly clears all documents in a collection and throws an error when a collection identifier is used. These tests improve code reliability and edge-case handling for the clear functionality. --- __test__/unit/base/Naro.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/__test__/unit/base/Naro.test.ts b/__test__/unit/base/Naro.test.ts index a61873c..f398578 100644 --- a/__test__/unit/base/Naro.test.ts +++ b/__test__/unit/base/Naro.test.ts @@ -335,5 +335,19 @@ test("set, should throw an error if trying to set a document with an invalid ID" await expect(async () => await db.set(`users/invalid/id/fake`, { name: "Jane Doe" })).rejects.toThrowError(); }); +test("clear, should clear all documents in the users collection", async () => { + const db = new Naro(dbName); + await db.add("users", { name: faker.person.fullName(), phone: faker.phone.number() }); + await db.add("users", { name: faker.person.fullName(), phone: faker.phone.number() }); + await db.clear("users"); + const users = await db.getAll("users"); + expect(users).toHaveLength(0); +}) + +test("clear, should throw an error if has collection identifier", async () => { + const db = new Naro(dbName); + await expect(async () => await db.clear("users/123")).rejects.toThrowError(); +}) + From f9671bb4c443b16bc78654f0cdb417652be8fb37 Mon Sep 17 00:00:00 2001 From: Bryan Lundberg Date: Sun, 11 May 2025 21:37:42 -0600 Subject: [PATCH 6/6] feat: Add method to clear all documents in a collection Introduce the `clear` method to remove all documents from a specified collection. This functionality includes validation to prevent misuse with collection IDs, ensuring safer operations. Added documentation and an example for clarity. --- src/base/Naro.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/base/Naro.ts b/src/base/Naro.ts index 168692e..cada89c 100644 --- a/src/base/Naro.ts +++ b/src/base/Naro.ts @@ -408,6 +408,24 @@ export class Naro { const collection = this.core.getCollection(collectionName); return collection.length; } + + /** + * Deletes all documents in a specified collection. + * + * @param {string} path - The name of the collection to clear. + * @return {Promise} A promise that resolves when the collection is cleared. + * + * @example + * const db = new Naro("myDatabase"); + * + * await db.clear("users"); + * console.log(await db.getAll("users")); // Output: [] + */ + async clear(path: string): Promise { + const { collectionName, collectionId } = NaroPath.validate(path); + if (collectionId) throw new Error("Collection ID detected. Use delete method instead."); + this.core.removeCollection(collectionName); + } }