All files / src/storages indexeddb.js

46.67% Statements 7/15
0% Branches 0/1
44.44% Functions 4/9
46.67% Lines 7/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54              3x   3x 3x   3x           1x               1x               4x                                      
// I decied to keep it simple, it's just a storage
 
import Dexie from 'dexie'
import BaseStorage from './base'
 
export default class Storage extends BaseStorage {
  constructor (storageName = 'default') {
    super(storageName)
 
    this.name = 'indexeddb'
    this.storage = new Dexie(storageName)
 
    this.storage.version(1).stores({
      files: 'path,node,parentId'
    })
  }
 
  create (path, node, parentId) {
    return this.put(path, node, parentId)
  }
 
  async remove (path) {
    await this.storage.files.where({ path: path }).delete()
  }
 
  put (path, node, parentId) {
    return this.storage.files.put({ path: path, node: node, parentId: parentId })
  }
 
  transaction (mode, cb) {
    return this.storage.transaction(mode, this.storage.files, cb)
  }
 
  get (path) {
    return this.storage.files.get({ path: path })
  }
 
  getBy (key, value) {
    const params = {}
    params[key] = value
 
    return this.storage.files.where(params).toArray()
  }
 
  where (params) {
    return this.storage.files.where(params).toArray()
  }
 
  async isEmpty (parentId) {
    const count = await this.storage.files.where({ parentId: parentId }).count()
    return count === 0
  }
}