2019-07-11 06:38:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
class JsStorage {
|
|
|
|
constructor() {
|
|
|
|
this.db = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
get(key) {
|
|
|
|
return this.db[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
get_or_element(key, defaultElement) {
|
|
|
|
const element = this.db[key];
|
|
|
|
if (element === undefined) {
|
|
|
|
return defaultElement;
|
|
|
|
} else {
|
|
|
|
return element
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
put(key, value) {
|
2019-07-11 10:04:36 -04:00
|
|
|
if (key === undefined || value === undefined) {
|
|
|
|
throw Error('key or value is undefined')
|
|
|
|
}
|
2019-07-11 06:38:22 -04:00
|
|
|
this.db[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
del(key) {
|
|
|
|
delete this.db[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
put_batch(key_values) {
|
|
|
|
key_values.forEach(element => {
|
|
|
|
this.db[element.key] = element.value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = JsStorage;
|