All files / src/storages memory.js

100% Statements 37/37
90.91% Branches 10/11
100% Functions 12/12
100% Lines 33/33

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79        22x   22x   22x     1x 1x         22x       83x 83x       11x 11x   11x       35x       1x       200x   200x 588x 117x     83x       8x 8x   8x 51x 51x 51x 51x   51x   8x       16x 16x   16x 242x 11x     16x      
import BaseStorage from './base'
 
export default class MemoryStorage extends BaseStorage {
  constructor (storageName = 'default') {
    super(storageName)
 
    this.name = 'memory'
    // for unit testing
    this.storage = {
      files: {},
      transaction: function (mode, table, cb) {
        return new Promise((resolve, reject) => {
          resolve(cb())
        })
      }
    }
 
    this.data = {}
  }
 
  async create (path, node, parentId) {
    this.data[path] = { path: path, node: node, parentId: parentId }
    return path
  }
 
  async remove (path) {
    Eif (path in this.data) {
      delete this.data[path]
    }
    return undefined
  }
 
  async put (path, node, parentId) {
    return this.create(path, node, parentId)
  }
 
  async transaction (mode, cb) {
    return this.storage.transaction(mode, this.storage.files, cb)
  }
 
  async get (path) {
    const keys = Object.keys(this.data)
 
    for (let i = 0; i < keys.length; i++) {
      if (this.data[keys[i]].path === path) {
        return this.data[keys[i]]
      }
    }
    return undefined
  }
 
  async where (params) {
    const paramsKeys = Object.keys(params)
    const ret = []
 
    Object.keys(this.data).forEach((d) => {
      let canBe = true
      const object = this.data[d]
      paramsKeys.forEach((param) => {
        if (object[param] !== params[param]) canBe = false
      })
      if (canBe) ret.push(object)
    })
    return ret
  }
 
  async isEmpty (parentId) {
    let count = 0
    const keys = Object.keys(this.data)
 
    for (let i = 0; i < keys.length; i++) {
      if (this.data[keys[i]].parentId === parentId) {
        count += 1
      }
    }
    return count === 0
  }
}