All files / services storage.ts

100% Statements 24/24
100% Branches 6/6
100% Functions 11/11
100% Lines 23/23
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                    3x                           10x             1x                         19x 19x 19x 10x     19x 19x                             4x 12x                       11x 11x 10x   1x                         2x   2x 6x 6x 4x 4x           2x                       10x      
/**
 * A storage class to store information on a device
 *
 * It follows a structure similar to relational databases
 *
 * Basic usage example:
 * ```js
 *  let storage = new Storage('tableName');
 * ```
 */
export default class Storage {
 
  /**
   * The name of the table where all the
   * records is be saved
   */
  tableName: string;
 
  /**
   * Storage constructor
   *
   * @param table Table name
   */
  constructor(tableName: string) {
    this.tableName = tableName;
  }
 
  /**
   * Returns the table name
   */
  getTableName(): string {
    return this.tableName;
  }
 
  /**
   *
   * @param value Any object or Array of object to save
   *
   * Example:
   * ```js
   *  storage.insert({ id: 1, name: 'John', lastname: 'Doe'});
   * ```
   */
  insert(value: any): void {
    let table = window.localStorage.getItem(this.tableName);
    let records = [];
    if(table) {
      records = JSON.parse(table);
    }
 
    records.push(value);
    window.localStorage.setItem(this.tableName, JSON.stringify(records));
  }
 
  /**
   * Retreives a record from the table
   *
   * @param key Unique identifier of the record
   * @param value The expected value
   *
   * Example:
   * ```js
   *  let result = storage.select('id', 1);
   * ```
   */
  select(key: string, value: string): Array<any> {
    let records = this.list();
    return records.filter(record => record[key] === value);
  }
 
  /**
   * Returns all records in table
   *
   * Example:
   * ```js
   *  let result = storage.list();
   * ```
   */
  list(): Array<any> {
    let table = window.localStorage.getItem(this.tableName);
    if(table) {
      return JSON.parse(table);
    } else {
      return [];
    }
  }
 
  /**
   * Updates records in table
   *
   * Example:
   * ```js
   *  this.storage.update([{key: 'name', value: 'James'}], [{key: 'id', value: '1'}]);
   * ```
   */
  update(values, where) {
    let records = this.list();
 
    records.forEach(record => {
      where.forEach(w => {
        if(record[w.key] === w.value) {
          values.forEach(v => {
            record[v.key] = v.value;
          });
        }
      });
    });
 
    window.localStorage.setItem(this.tableName, JSON.stringify(records));
  }
 
  /**
   * Clears table of all records
   *
   * Example:
   * ```js
   *  storage.clear();
   * ```
   */
  clear() {
    window.localStorage.removeItem(this.tableName);
  }
 
}