All files / node-collections-boilerplate-nahid CachedCollection.js

94.67% Statements 71/75
81.25% Branches 13/16
83.33% Functions 15/18
94.59% Lines 70/74
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198    2x                               23x         21x     21x   44x 44x       21x   1x 1x 1x     21x   1x 1x 1x       21x   1x 1x 1x           21x   21x               5x 5x               7x 7x   2x 1x   1x 1x 1x   1x       5x                 4x 4x               4x   4x           4x               1x   1x 1x 1x 1x 1x     1x           1x   1x 1x   2x   1x   3x 3x   6x 6x   2x 2x       4x   4x         1x     1x   1x 1x   1x 1x   2x 2x 2x   1x     2x 1x               2x   2x  
"use strict";
 
const Collection = require('./Collection');
 
/**
 * In memory version of collection.
 * 
 * Since all the data is in memory, it also does basic facets analysis.
 */
class CachedCollection extends Collection
{
  /**
   * Builds up records in mmory.
   * Sets up event listeners to keep everything up to date.
   * @override
   */
  async initialise()
  {
    let records = await super.initialise();
    /**
     * Mappint of primary key to records
     * @type {Map<string,Record>}
     */
    this.lookup = {};
 
    // populate lookup list
    for (let record of records)
    {
      let pkValue = record[this.primaryKey]
      this.lookup[pkValue] = record;
    }
 
    // set up listeners
    this.storage.on('create', record =>
    {
      console.log('NOTIFY CREATED', this.collectionName, record[this.primaryKey]);
      this.lookup[record[this.primaryKey]] = record;
      this.search.createRecord(record)
        .then(x => x, console.log.bind(console));
    });
    this.storage.on('update', record =>
    {
      console.log('NOTIFY UPDATED', this.collectionName, record[this.primaryKey]);
      this.lookup[record[this.primaryKey]] = record;
      this.search.updateRecord(record)
        .then(x => x, console.log.bind(console));
    });
 
    this.storage.on('delete', record =>
    {
      console.log('NOTIFY DELETED', this.collectionName, record[this.primaryKey]);
      delete this.lookup[record[this.primaryKey]];
      this.search.deleteRecord(record)
        .then(x => x, console.log.bind(console));
    });
 
    // install updater
    // see if storage has some clever way of receiving updates
    this.storage.startRecordUpdateCheck()
 
    return records;
  }
 
  /**
   * @override
   */
  createRecord(record)
  {
    this.lookup[record[this.primaryKey]] = record;
    return super.createRecord(record);
  }
 
  /**
   * @override
   */
  async readRecord(record)
  {
    let read = this.lookup[record[this.primaryKey]];
    if (!read)
    {
      record = await super.readRecord(record);
      Eif (record)
      {
        this.lookup[record[this.primaryKey]] = record;
        this.search.createRecord(record)
          .then(x => x, console.log.bind(console));
      }
      return record;
    }
    else
    {
      return read;
    }
  }
 
  /**
   * @override
   */
  updateRecord(record)
  {
    this.lookup[record[this.primaryKey]] = record;
    return super.updateRecord(record);
  }
 
  /**
   * @override
   */
  deleteRecord(record)
  {
    try
    {
      delete this.lookup[record[this.primaryKey]];
    }
    catch (e)
    {
      console.log(e)
    }
    return super.deleteRecord(record);
  }
 
  /**
   * @override
   */
  searchRecords(query)
  {
    return new Promise((resolve, reject) =>
    {
      let offset = query.offset;
      delete query.offset;
      let limit = query.limit;
      delete query.limit;
      this.search.searchRecords(query)
        .then(records =>
        {
          let results = {
            results: records,
            total: records.length,
            offset: offset,
          };
 
          Eif (query.returnFacets)
          {
            let facets = {};
            for (let facet of this.searchMeta.facets)
            {
              facets[facet] = {};
            }
            for (let id of records)
            {
              let record = this.lookup[id];
              for (let facet of this.searchMeta.facets)
              {
                let facetValues = (record[facet] || []);
                if (typeof facetValues === 'string')
                {
                  let value = facetValues;
                  facets[facet][value] = (facets[facet][value] || 0) + 1;
                }
                else
                {
                  for (let value of facetValues)
                  {
                    facets[facet][value] = (facets[facet][value] || 0) + 1;
                  }
                }
              }
            }
            results.facets = facets;
          }
 
          Eif (query.sort)
          {
            results.sort = query.sort;
            results.order = query.order;
          }
          records = records.splice(offset, limit);
          records = records.map(id =>
          {
            let query = {};
            query[this.primaryKey] = id;
            return this.readRecord(query);
          });
          Promise.all(records)
            .then(records =>
            {
              results.results = records.map(x => this.stripRecord(x, query.extra));
              resolve(results);
            }, reject);
        }, reject);
    });
  }
 
}
 
module.exports = CachedCollection;
 
CachedCollection.create = Collection.create;