1 2
import Config from './config'
2
import reduct = require('reduct')
3
import { StoreConstructor, StoreInstance } from '../types/store'
4

5 2
import { loadModuleOfType } from '../lib/utils'
6

7 2
export default class Store {
8
  protected config: Config
9
  protected store: StoreInstance
10

11
  constructor (deps: reduct.Injector) {
12 2
    this.config = deps(Config)
13

14 2
    const Store: StoreConstructor = loadModuleOfType('store', this.config.store)
15 2
    this.store = new Store(Object.assign({
16
      path: this.config.storePath
17
    }, this.config.storeConfig), {})
18
  }
19

20
  getPluginStore (name: string) {
21 2
    if (!name.match(/^[A-Za-z0-9_\-~.]+$/)) {
22 2
      throw new Error('"' + name + '" includes forbidden characters.')
23
    }
24

25 2
    return {
26
      get: (key: string) => {
27 0
        return this.store.get(name + key)
28
      },
29

30
      put: (key: string, value: string) => {
31 0
        return this.store.put(name + key, value)
32
      },
33

34
      del: (key: string) => {
35 0
        return this.store.del(name + key)
36
      }
37
    }
38
  }
39

40
  async get (key: string) {
41 2
    return this.store.get(key)
42
  }
43

44
  async put (key: string, value: string) {
45 2
    return this.store.put(key, value)
46
  }
47

48
  async del (key: string) {
49 2
    return this.store.del(key)
50
  }
51
}

Read our documentation on viewing source code .

Loading