Create named wrapper for database

master
Bel LaPointe 2021-02-26 22:04:44 -06:00
parent 76aab678b5
commit 52c7b2222e
1 changed files with 47 additions and 1 deletions

View File

@ -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