All files meta.ts

100% Statements 36/36
100% Branches 7/7
100% Functions 6/6
100% Lines 30/30

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 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          20x                                                     20x             20x 6x 6x 6x   6x                 20x 25x 25x 25x   25x                 20x 3x         3x 2x             2x   1x 1x     6x               20x 2x 2x 4x               20x 1x 1x 1x   1x   20x  
/**
 * Services for the meta table.
 * @packageDocumentation
 */
 
import { BaseService } from "./util";
 
/**
 * Meta architecture.
 */
export interface Meta {
  name: string;
  value: string;
}
 
/**
 * Meta with only name architecture.
 */
interface MetaName {
  name: string;
}
 
/**
 * Meta with only value architecture.
 */
interface MetaValue {
  value: string;
}
 
/**
 * Meta services.
 */
export class MetaService extends BaseService {
  /**
   * Check if a key name exists.
   *
   * @param name The key name.
   * @returns Whether or not the name exists.
   */
  public async exists(name: string): Promise<boolean> {
    const sql = `SELECT name FROM Meta WHERE name = ?;`;
    const params = [name];
    const rows: MetaName[] = await this.dbm.execute(sql, params);
 
    return rows.length > 0;
  }
 
  /**
   * Get the value given the key name.
   *
   * @param name The key name.
   * @returns The value.
   */
  public async get(name: string): Promise<string> {
    const sql = `SELECT value FROM Meta WHERE name = ?;`;
    const params = [name];
    const rows: MetaValue[] = await this.dbm.execute(sql, params);
 
    return rows[0]?.value;
  }
 
  /**
   * Set the key name and value.
   *
   * @param name The key name.
   * @param value The value.
   */
  public async set(name: string, value: string): Promise<void> {
    const nameExists = await this.exists(name);
 
    let sql: string;
    let params: string[];
 
    if (!nameExists) {
      sql = `
				INSERT INTO Meta (
					name, value
				) VALUES (
					?, ?
				);
			`;
      params = [name, value];
    } else {
      sql = `UPDATE Meta SET value = ? WHERE name = ?;`;
      params = [value, name];
    }
 
    await this.dbm.execute(sql, params);
  }
 
  /**
   * Delete the key name and value.
   *
   * @param name The key name.
   */
  public async remove(name: string): Promise<void> {
    const sql = `DELETE FROM Meta WHERE name = ?;`;
    const params = [name];
    await this.dbm.execute(sql, params);
  }
 
  /**
   * Get all key names and values.
   *
   * @returns Key names and values.
   */
  public async getAll(): Promise<Meta[]> {
    const sql = `SELECT * FROM Meta;`;
    const params = [];
    const rows: Meta[] = await this.dbm.execute(sql, params);
 
    return rows;
  }
}