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); +}); 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(); +}) + 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); +}); 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); + } } 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]; + } } 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); + } }