summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Van Doorn <vandoorn.nick@gmail.com>2019-01-19 20:09:02 -0800
committerNick Van Doorn <vandoorn.nick@gmail.com>2019-01-19 20:09:02 -0800
commit8504ed682cbed88fa0b14e21b9072216db02368a (patch)
tree3cfae8f1d145c5e3c2c302d8fef244a2ad233d4a
parente9c557656aa034aab1ca7356162d97c6116334af (diff)
Document methods
-rw-r--r--src/database.model.ts34
-rw-r--r--src/database.ts14
2 files changed, 48 insertions, 0 deletions
diff --git a/src/database.model.ts b/src/database.model.ts
index 08e91ab..3b8fa1b 100644
--- a/src/database.model.ts
+++ b/src/database.model.ts
@@ -1,7 +1,41 @@
export interface DatabaseInterface {
+ /**
+ * Init the database with state
+ * stored in the cache file.
+ *
+ * Calling init is not mandatory,
+ * but is required for any form
+ * of non-volatile storage
+ */
init(): Promise<void>;
+
+ /**
+ * Read data from the "node" described by path
+ * where path uses "/" to separate nodes
+ *
+ * null is returned if there is no data
+ * at path
+ */
read(path: string): Promise<Object>;
+
+ /**
+ * Write data to the node described by path
+ * where path once again uses "/" separate nodes
+ *
+ * toWrite can be any plain JavaScript object
+ * (e.g anything that can be serialized with JSON.stringify)
+ *
+ * An exception is thrown if the write fails
+ */
write(path: string, toWrite: Object): Promise<void>;
+
+ /**
+ * Empty the database
+ */
flush(): Promise<void>;
+
+ /**
+ * String representation (JSON) of the database
+ */
toString(): Object;
}
diff --git a/src/database.ts b/src/database.ts
index 867e3c9..9a342fc 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -16,6 +16,8 @@ export const DB_CACHE_PATH = `${__dirname}/db.json`;
/**
* Implementation of NoSQL DB that uses paths and objects.
*
+ * See DatabaseInterface for docs on public API
+ *
* Uses a plain object as a buffer and reads/writes to a
* plain JSON file. A better implementation could be backed
* by somethig a little nicer and not hold the buffer
@@ -59,6 +61,18 @@ export class Database implements DatabaseInterface {
return JSON.stringify(this.buff);
}
+ /**
+ * Resolve the object located at path.
+ *
+ * If isRead == true, no new nodes will
+ * be created, and the function will return
+ * null if a null node is encountered on the path.
+ * Else, we create each node on the path.
+ *
+ * Level is used to determine how deep
+ * to recurse on path. Callers interested in
+ * writing may wish to stop higher up the tree.
+ */
private resolve(
pathParts: string[],
isRead: boolean = true,