summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Van Doorn <vandoorn.nick@gmail.com>2019-01-19 20:10:12 -0800
committerNick Van Doorn <vandoorn.nick@gmail.com>2019-01-19 20:10:12 -0800
commit0ddd6bffb0bb838035d130053368b40b950abc33 (patch)
treeffeadfed43f63170ee1aa256ebe5b8d027b3b6cf /src
parent8504ed682cbed88fa0b14e21b9072216db02368a (diff)
Pass cache path in context
Diffstat (limited to 'src')
-rw-r--r--src/database.ts13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/database.ts b/src/database.ts
index 9a342fc..71ba7fc 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -11,7 +11,10 @@ const splitPath = (path: string): string[] => path.split("/").filter(k => k);
const write = promisify(writeFile);
const read = promisify(readFile);
-export const DB_CACHE_PATH = `${__dirname}/db.json`;
+export const DEFAULT_CTX = {
+ logger: console.log,
+ cachePath: `${__dirname}/db.json`
+};
/**
* Implementation of NoSQL DB that uses paths and objects.
@@ -25,11 +28,11 @@ export const DB_CACHE_PATH = `${__dirname}/db.json`;
*/
export class Database implements DatabaseInterface {
private buff: any = {};
- constructor(private ctx: Context = { logger: console.log }) {}
+ constructor(private ctx: Context = DEFAULT_CTX) {}
async init(): Promise<void> {
try {
- const buff = await read(DB_CACHE_PATH);
+ const buff = await read(this.ctx.cachePath);
this.buff = JSON.parse(buff.toString());
} catch (e) {
this.ctx.logger("Failed to init database, using empty object");
@@ -104,7 +107,9 @@ export class Database implements DatabaseInterface {
return node;
}
+ /**
+ */
private serialize(): Promise<void> {
- return write(DB_CACHE_PATH, this.toString());
+ return write(this.ctx.cachePath, this.toString());
}
}