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

100% Statements 58/58
78.57% Branches 11/14
100% Functions 14/14
100% Lines 58/58
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                                      46x         46x         46x         46x         46x               46x 46x 44x 42x               32x   32x 32x 32x           32x 28x                 10x 6x 6x 4x               9x               8x 6x 6x 4x                   2x               8x 6x 6x 4x               1x 1x 1x 1x 1x 1x         1x   1x 1x   1x 1x   2x 2x 2x   1x 1x 2x                 4x 4x 4x   4x 4x   2x     4x   4x 4x   2x     4x       3x  
"use strict";
 
/**
 * Should be the class that programs interact with.
 * 
 * It should take care of syncing data between storage and search.
 */
class Collection
{
 
  /**
   * @param {object} options; see public fields for details
   */
  constructor(options)
  {
    /**
     * where data is stored
     * @type {Storage}
     */
    this.storage = options.storage;
    /**
     * instance of Search
     * @type {Search}
     */
    this.search = options.search;
    /**
     * name of collection
     * @type {?string}
     */
    this.collectionName = options.collectionName || options.storage.collectionName || 'unspecified'; // optional
    /**
     * id field of records
     * @type {string}
     */
    this.primaryKey = options.primaryKey || 'id';
    /**
     * search definition
     * @type {SearchMeta}
     */
    this.searchMeta = options.searchMeta || {};
  }
 
  /**
   * Call this to connect to storage units and search
   */
  async initialise()
  {
    await this.storage.connect();
    const records = await this.storage.readAllRecords();
    await this.search.initialise(this.searchMeta, records);
    return records;
  }
 
  /**
   * Helper creation method
   */
  static create(storage, search, Class)
  {
    return new Promise((resolve, reject) =>
    {
      let pimaryKey = Class.pimaryKey;
      let searchMeta = Class.searchMeta;
      let collection = new Class({
        storage,
        search,
        pimaryKey,
        searchMeta
      });
      collection.initialise()
        .then(() => resolve(collection), reject);
    });
  }
 
  /**
   * Add a new record to collection.
   */
  async createRecord(record)
  {
    record = await this.storage.createRecord(record);
    console.log('CREATED', this.collectionName, record[this.primaryKey]);
    await this.search.createRecord(record);
    return record;
  }
 
  /**
   * Read an existing record.
   */
  readRecord(record)
  {
    return this.storage.readRecord(record);
  }
 
  /**
   * Update an existing record.
   */
  async updateRecord(record)
  {
    record = await this.storage.updateRecord(record);
    console.log('UPDATED', this.collectionName, record[this.primaryKey]);
    await this.search.updateRecord(record);
    return record;
  }
 
  /**
   * Update field of a record.
   * 
   * Override this to inject custom processing rules.
   */
  updateField(record, field, value)
  {
    record[field] = value;
  }
 
  /**
   * Delete a record from collection
   */
  async deleteRecord(record)
  {
    record = await this.storage.deleteRecord(record);
    console.log('DELETED', this.collectionName, record[this.primaryKey]);
    await this.search.deleteRecord(record);
    return record
  }
 
  /**
   * Search for records in collection using query.
   */
  async searchRecords(query)
  {
    let offset = query.offset;
    delete query.offset;
    let limit = query.limit;
    delete query.limit;
    let records = await this.search.searchRecords(query);
    let results = {
      results: records,
      total: records.length,
      offset: offset,
    };
    Eif (query.sort)
    {
      results.sort = query.sort;
      results.order = query.order;
    }
    records = records.splice(offset, limit);
    records = records.map(id =>
    {
      const query = {};
      query[this.primaryKey] = id;
      return this.readRecord(query);
    });
    records = await Promise.all(records);
    return {
      results: records.map(x => this.stripRecord(x, query.extra))
    };
  }
 
  /**
   * Strips a record to return as search result.
   */
  stripRecord(record, extra = [])
  {
    let out = {};
    out[this.primaryKey] = record[this.primaryKey];
    for (let field in this.searchMeta.fields)
    {
      let value = record[field];
      if (value !== undefined)
      {
        out[field] = value;
      }
    }
    for (let field of extra)
    {
      let value = record[field];
      if (value !== undefined)
      {
        out[field] = value;
      }
    }
    return out;
  }
}
 
module.exports = Collection;