summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Van Doorn <vandoorn.nick@gmail.com>2019-01-19 21:45:54 -0800
committerNick Van Doorn <vandoorn.nick@gmail.com>2019-01-19 21:45:54 -0800
commitf5c35a0b3519ff5df54eec0a10c9b16eb76c5d5f (patch)
tree9b26053a69595221dc7b690359ab2a8421ffe1d2 /src
parentc73f95e9de812e06d36bc4950edb0bd8c383331b (diff)
Add remove method
Diffstat (limited to 'src')
-rw-r--r--src/database.model.ts5
-rw-r--r--src/database.test.ts12
-rw-r--r--src/database.ts6
3 files changed, 22 insertions, 1 deletions
diff --git a/src/database.model.ts b/src/database.model.ts
index 3b8fa1b..dc39fda 100644
--- a/src/database.model.ts
+++ b/src/database.model.ts
@@ -30,6 +30,11 @@ export interface DatabaseInterface {
write(path: string, toWrite: Object): Promise<void>;
/**
+ * Remove data at path
+ */
+ remove(path: string): Promise<void>;
+
+ /**
* Empty the database
*/
flush(): Promise<void>;
diff --git a/src/database.test.ts b/src/database.test.ts
index 9f583b9..de05b2c 100644
--- a/src/database.test.ts
+++ b/src/database.test.ts
@@ -49,6 +49,18 @@ describe("Database module", () => {
expect(s).toBe(toWrite);
});
+ test("it should remove data", async () => {
+ const path = "/this/is/fun";
+ await db.write(path, {
+ foxNews: {
+ stories: ["AOC", "aoc", "aOC", "aOc"]
+ }
+ });
+ await db.remove(path);
+ const s = await db.read(path);
+ expect(s).toBeNull();
+ });
+
test("it should be empty after a flush", async () => {
await db.write("/hello/world", {
my: "object"
diff --git a/src/database.ts b/src/database.ts
index 4e263cd..4e85057 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -50,13 +50,17 @@ export class Database implements DatabaseInterface {
return this.resolve(pathParts);
}
- async write(path: string, toWrite: Object): Promise<void> {
+ async write(path: string, toWrite: any): Promise<void> {
const pathParts = splitPath(path);
const writeTo = this.resolve(pathParts, false, 1);
writeTo[last(pathParts)] = toWrite;
await this.serialize();
}
+ remove(path: string): Promise<void> {
+ return this.write(path, null);
+ }
+
async flush(): Promise<void> {
this.buff = {};
await this.serialize();