Storage.js 624 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. class JsStorage {
  2. constructor() {
  3. this.db = {}
  4. }
  5. get(key) {
  6. return this.db[key]
  7. }
  8. get_or_element(key, defaultElement) {
  9. const element = this.db[key]
  10. if (element === undefined) {
  11. return defaultElement
  12. } else {
  13. return element
  14. }
  15. }
  16. put(key, value) {
  17. if (key === undefined || value === undefined) {
  18. throw Error('key or value is undefined')
  19. }
  20. this.db[key] = value
  21. }
  22. del(key) {
  23. delete this.db[key]
  24. }
  25. put_batch(key_values) {
  26. key_values.forEach(element => {
  27. this.db[element.key] = element.value
  28. })
  29. }
  30. }
  31. module.exports = JsStorage