All files / src/dao base-dao.js

2.27% Statements 1/44
0% Branches 0/14
0% Functions 0/21
2.27% Lines 1/44

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 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      2x                                                                                                                                                                                                                                                                                              
/*
 * A wrapper function returning the base data access abstraction.
 */
module.exports = ({ db: closureDB, logError: closureLogError }) =>
  class BaseDAO {
    constructor({ db, logError } = {}) {
      this.db = db || closureDB;
      this.logError = logError || closureLogError;
      this.ensureExists = this.getOrCreate; // alias
      this.manyOrNone = this.any; // alias
      this.errorHandler = this.errorHandler.bind(this);
    }
 
    /* Nice abstractions over this.db ---------------------------------------*/
    /* ----------------------------------------------------------------------*/
 
    one(query, values, errorHandler = this.errorHandler) {
      return this.db
        .many(query, values)
        .then(rows => this.Bo.createOneFromDatabase(rows))
        .catch(errorHandler);
    }
 
    oneOrNone(query, values, errorHandler = this.errorHandler) {
      return this.db
        .any(query, values)
        .then(rows => this.Bo.createOneOrNoneFromDatabase(rows))
        .catch(errorHandler);
    }
 
    many(query, values, errorHandler = this.errorHandler) {
      return this.db
        .any(query, values)
        .then(rows => this.Bo.createManyFromDatabase(rows))
        .catch(errorHandler);
    }
 
    any(query, values, errorHandler = this.errorHandler) {
      return this.db
        .any(query, values)
        .then(rows => this.Bo.createFromDatabase(rows))
        .catch(errorHandler);
    }
 
    none(query, values, errorHandler = this.errorHandler) {
      return this.db
        .none(query, values)
        .then(() => null)
        .catch(errorHandler);
    }
 
    /* Piecemeal endings if using this.db directly --------------------------*/
    /* ----------------------------------------------------------------------*/
 
    errorHandler(err) {
      if (!err.name === 'QueryResultError') {
        this.logError(err);
      }
      throw err;
    }
 
    /* Built-in basic DAO methods -------------------------------------------*/
    /* ----------------------------------------------------------------------*/
 
    // Standard create
    create(bo) {
      const { columns, values, valuesVar } = bo.getSqlInsertParts();
      const query = `
        INSERT INTO "${bo.constructor.tableName}" ( ${columns} )
        VALUES ( ${valuesVar} )
        RETURNING ${bo.constructor.getSQLSelectClause()};
      `;
      return this.one(query, values);
    }
 
    // Standard update
    update(bo, { on = 'id' } = {}) {
      const { clause, idVar, values } = bo.getSqlUpdateParts(on);
      const query = `
        UPDATE "${bo.constructor.tableName}"
        SET ${clause}
        WHERE "${bo.constructor.tableName}".${on} = ${idVar}
        RETURNING ${bo.constructor.getSQLSelectClause()};
      `;
      return this.one(query, values);
    }
 
    // Standard delete
    delete(bo) {
      const id = bo.id;
      const query = `
        DELETE FROM "${bo.constructor.tableName}"
        WHERE "${bo.constructor.tableName}".id = ${id}
      `;
      return this.none(query);
    }
 
    deleteMatching(bo) {
      const { whereClause, values } = bo.getMatchingParts();
      const query = `
        DELETE FROM "${bo.constructor.tableName}"
        WHERE ${whereClause};
      `;
      return this.none(query, values);
    }
 
    getMatching(bo) {
      const { whereClause, values } = bo.getMatchingParts();
      const query = `
        SELECT ${bo.constructor.getSQLSelectClause()}
        FROM "${bo.constructor.tableName}"
        WHERE ${whereClause};
      `;
      return this.one(query, values);
    }
 
    getOneOrNoneMatching(bo) {
      const { whereClause, values } = bo.getMatchingParts();
      const query = `
        SELECT ${bo.constructor.getSQLSelectClause()}
        FROM "${bo.constructor.tableName}"
        WHERE ${whereClause};
      `;
      return this.oneOrNone(query, values);
    }
 
    getAnyMatching(bo) {
      const { whereClause, values } = bo.getMatchingParts();
      const query = `
        SELECT ${bo.constructor.getSQLSelectClause()}
        FROM "${bo.constructor.tableName}"
        WHERE ${whereClause};
      `;
      return this.any(query, values);
    }
 
    getAllMatching(bo) {
      const { whereClause, values } = bo.getMatchingParts();
      const query = `
        SELECT ${bo.constructor.getSQLSelectClause()}
        FROM "${bo.constructor.tableName}"
        WHERE ${whereClause};
      `;
      return this.many(query, values);
    }
  };