summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Van Doorn <vandoorn.nick@gmail.com>2019-01-19 19:54:59 -0800
committerNick Van Doorn <vandoorn.nick@gmail.com>2019-01-19 19:54:59 -0800
commit9ecb417c79362111a438fa81b307a943ea2547d5 (patch)
tree6057d2a00498444857ee741777696242e1b65e31
parented879f6c8c0d500590fec851a858f0240111d370 (diff)
Use "read" instead of "fetch"
-rw-r--r--src/database.model.ts2
-rw-r--r--src/database.test.ts16
-rw-r--r--src/database.ts8
3 files changed, 13 insertions, 13 deletions
diff --git a/src/database.model.ts b/src/database.model.ts
index 89e3a44..08e91ab 100644
--- a/src/database.model.ts
+++ b/src/database.model.ts
@@ -1,6 +1,6 @@
export interface DatabaseInterface {
init(): Promise<void>;
- fetch(path: string): Promise<Object>;
+ read(path: string): Promise<Object>;
write(path: string, toWrite: Object): Promise<void>;
flush(): Promise<void>;
toString(): Object;
diff --git a/src/database.test.ts b/src/database.test.ts
index 2c0588b..ff4b6d8 100644
--- a/src/database.test.ts
+++ b/src/database.test.ts
@@ -14,19 +14,19 @@ describe("Database module", () => {
formula: "hello world"
});
});
- test("it should fetch data", () => {
- return db.fetch("/hello/world");
+ test("it should read data", () => {
+ return db.read("/hello/world");
});
test("it should have a string representation", () => {
expect(db.toString()).not.toHaveLength(0);
});
- test("it should write and then fetch data", async () => {
+ test("it should write and then read data", async () => {
const path = "/hello/world";
const toWrite = {
secret: "stuff"
};
await db.write(path, toWrite);
- const s = await db.fetch(path);
+ const s = await db.read(path);
expect(s).toBe(toWrite);
});
test("it should be empty after a flush", async () => {
@@ -36,18 +36,18 @@ describe("Database module", () => {
await db.flush();
expect(db.toString()).toHaveLength(2); // empty object
});
- test("it should fetch null on an empty node", async () => {
+ test("it should read null on an empty node", async () => {
await db.flush();
- const s = await db.fetch("/any/path/should/work");
+ const s = await db.read("/any/path/should/work");
expect(s).toBeNull();
});
- test("it should not change on fetch", async () => {
+ test("it should not change on read", async () => {
await db.flush();
await db.write("/my/data/lives/here", {
whiteHouseDinner: `America's Finest McDouble's`
});
const before = db.toString();
- const s = await db.fetch("/no/data/here/silly");
+ const s = await db.read("/no/data/here/silly");
expect(s).toBeNull();
expect(before).toEqual(db.toString());
});
diff --git a/src/database.ts b/src/database.ts
index 57907f2..867e3c9 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -38,14 +38,14 @@ export class Database implements DatabaseInterface {
// this currently runs synchronously,
// but only because we hold the entire DB in memory
// (which obviously becomes a bad idea at some point)
- async fetch(path: string): Promise<Object> {
+ async read(path: string): Promise<Object> {
const pathParts = splitPath(path);
return this.resolve(pathParts);
}
async write(path: string, toWrite: Object): Promise<void> {
const pathParts = splitPath(path);
- const writeTo = this.resolve(pathParts, false, -1);
+ const writeTo = this.resolve(pathParts, false, 1);
writeTo[last(pathParts)] = toWrite;
await this.serialize();
}
@@ -62,12 +62,12 @@ export class Database implements DatabaseInterface {
private resolve(
pathParts: string[],
isRead: boolean = true,
- offset: number = 0
+ level: number = 0
): any {
// start at the root of our buffer
let lastNode = this.buff;
let node;
- for (let i = 0; i < pathParts.length + offset; i++) {
+ for (let i = 0; i < pathParts.length - level; i++) {
const part: string = pathParts[i];
// handle null node
if (!lastNode[part]) {