Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions __test__/unit/NaroFiler/NaroFiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
14 changes: 14 additions & 0 deletions __test__/unit/base/Naro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})



14 changes: 14 additions & 0 deletions __test__/unit/core/Core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
18 changes: 18 additions & 0 deletions src/base/Naro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>} 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<void> {
const { collectionName, collectionId } = NaroPath.validate(path);
if (collectionId) throw new Error("Collection ID detected. Use delete method instead.");
this.core.removeCollection(collectionName);
}
}


Expand Down
6 changes: 6 additions & 0 deletions src/core/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
6 changes: 5 additions & 1 deletion src/manage/files/NaroFiler.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
}
Loading