all files / collab/ SnapshotStore.js

82.76% Statements 24/29
76.19% Branches 16/21
100% Functions 6/6
88.89% Lines 24/27
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            13×                             10×     10× 10× 10× 10×                                                                  
/*
  Implements Substance SnapshotStore API. This is just a dumb store.
  No integrity checks are made, as this is the task of SnapshotEngine
*/
class SnapshotStore {
  constructor(seed) {
    this._snapshots = seed || {}
  }
 
  /*
    Get all available versions for a document
  */
  getVersions(documentId, cb) {
    let versions = this._getVersions(documentId)
    cb(null, versions)
  }
 
  /*
    Get Snapshot by documentId and version.
 
    Returns snapshot data and snaphot version
  */
  getSnapshot(documentId, version, cb) {
    Iif (!arguments.length === 3) {
      throw new Error('Invalid Arguments')
    }
    let docEntry = this._snapshots[documentId]
    Iif (!docEntry) return cb(null, undefined)
    let snapshot = docEntry[version]
    if (snapshot) {
      cb(null, snapshot, version)
    } else {
      cb(null, undefined)
    }
  }
 
  /*
    Saves a snapshot for a given documentId and version.
 
    Please note that an existing snapshot will be overwritten.
  */
  saveSnapshot(documentId, version, data, cb) {
    Iif (!documentId || !version || !data) {
      throw new Error('Invalid arguments')
    }
    let docEntry = this._snapshots[documentId]
    if (!docEntry) {
      docEntry = this._snapshots[documentId] = {}
    }
    docEntry[version] = data
    cb(null, docEntry[version])
  }
 
  /*
    Removes a snapshot for a given documentId + version
  */
  deleteSnapshot(documentId, version, cb) {
    let docEntry = this._snapshots[documentId]
    Iif (!docEntry || !docEntry[version]) {
      return cb(new Error('Snapshot does not exist and can not be deleted'))
    }
    let snapshot = this._snapshots[documentId][version]
    delete this._snapshots[documentId][version]
    cb(null, snapshot)
  }
 
  /*
    Get versions for a given document
  */
  _getVersions(documentId) {
    let docEntry = this._snapshots[documentId]
    Iif (!docEntry) return [] // no versions available
    return Object.keys(docEntry)
  }
 
}
 
export default SnapshotStore