tornado-core/lib/Storage.js

38 lines
624 B
JavaScript
Raw Normal View History

2019-07-11 12:38:22 +02:00
class JsStorage {
constructor() {
2019-07-16 19:27:20 +02:00
this.db = {}
2019-07-11 12:38:22 +02:00
}
get(key) {
2019-07-16 19:27:20 +02:00
return this.db[key]
2019-07-11 12:38:22 +02:00
}
get_or_element(key, defaultElement) {
2019-07-16 19:27:20 +02:00
const element = this.db[key]
2019-07-11 12:38:22 +02:00
if (element === undefined) {
2019-07-16 19:27:20 +02:00
return defaultElement
2019-07-11 12:38:22 +02:00
} else {
2019-07-16 19:27:20 +02:00
return element
2019-07-11 12:38:22 +02:00
}
}
put(key, value) {
2019-07-11 16:04:36 +02:00
if (key === undefined || value === undefined) {
2019-07-16 19:27:20 +02:00
throw Error('key or value is undefined')
2019-07-11 16:04:36 +02:00
}
2019-07-16 19:27:20 +02:00
this.db[key] = value
2019-07-11 12:38:22 +02:00
}
del(key) {
2019-07-16 19:27:20 +02:00
delete this.db[key]
2019-07-11 12:38:22 +02:00
}
put_batch(key_values) {
2021-02-11 07:23:18 +01:00
key_values.forEach((element) => {
2019-07-16 19:27:20 +02:00
this.db[element.key] = element.value
})
2019-07-11 12:38:22 +02:00
}
}
2019-07-16 19:27:20 +02:00
module.exports = JsStorage