From 52c7b2222e11a7105690735e17e89ba9ffa90da5 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Fri, 26 Feb 2021 22:04:44 -0600 Subject: [PATCH] Create named wrapper for database --- nullboard.html | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/nullboard.html b/nullboard.html index c84937b..b652c8a 100644 --- a/nullboard.html +++ b/nullboard.html @@ -1237,6 +1237,52 @@ } } + class NamespacedDatabase extends Database { + constructor(namespace, database) + { + super(); + this.database = database + this.namespace = namespace; + } + + deprefix(key) + { + if (key.startsWith(this.namespace + ".")) + return key.slice(this.namespace.length + 1); + return key; + } + + prefix(key) + { + return this.namespace + "." + key; + } + + get(key) { + var k = this.prefix(key); + return this.database.get(k); + } + + set(key, value) + { + var k = this.prefix(key); + return this.database.set(k, value); + } + + del(key) + { + var k = this.prefix(key); + return this.database.del(k); + } + + forEach(foo) + { + this.database.forEach((key) => { + var k = this.deprefix(key); + foo(k); + }); + } + } + class Local extends Database { constructor() { @@ -1275,7 +1321,7 @@ } } - globalStorage = new Local(); + globalStorage = new NamespacedDatabase("4", new Local()); /* * poor man's error handling -- $fixme