tornado-core/lib/Storage.js

40 lines
624 B
JavaScript
Raw Normal View History

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