All files / dist factory.js

37.29% Statements 22/59
0% Branches 0/9
9.52% Functions 2/21
37.29% Lines 22/59

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  4x 4x 4x 4x 18x                 18x           18x           18x           18x           18x                   18x                   18x                     18x               18x               18x                 18x                 18x                 18x                 18x                                 110x 110x       4x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.create = void 0;
const business_object_1 = require("./business-object");
const create = ({ getBusinessObjects, db, logError }) => {
    const defaultErrorHandler = (err) => {
        if (!(err.name === 'QueryResultError')) {
            logError(err);
        }
        throw err;
    };
    /* ------------------------------------------------------------------------*/
    /* Query functions --------------------------------------------------------*/
    /* ------------------------------------------------------------------------*/
    const one = (query, values, errorHandler = defaultErrorHandler) => {
        return db
            .many(query, values)
            .then((rows) => (0, business_object_1.createOneFromDatabase)(rows, getBusinessObjects))
            .catch(errorHandler);
    };
    const oneOrNone = (query, values, errorHandler = defaultErrorHandler) => {
        return db
            .any(query, values)
            .then((rows) => (0, business_object_1.createOneOrNoneFromDatabase)(rows, getBusinessObjects))
            .catch(errorHandler);
    };
    const many = (query, values, errorHandler = defaultErrorHandler) => {
        return db
            .any(query, values)
            .then((rows) => (0, business_object_1.createManyFromDatabase)(rows, getBusinessObjects))
            .catch(errorHandler);
    };
    const any = (query, values, errorHandler = defaultErrorHandler) => {
        return db
            .any(query, values)
            .then((rows) => (0, business_object_1.createFromDatabase)(rows, getBusinessObjects))
            .catch(errorHandler);
    };
    const none = (query, values, errorHandler = defaultErrorHandler) => {
        return db
            .none(query, values)
            .then(() => null)
            .catch(errorHandler);
    };
    /* ------------------------------------------------------------------------*/
    /* Built-in basic CRUD functions ------------------------------------------*/
    /* ------------------------------------------------------------------------*/
    // Standard create
    const create = (bo) => {
        const { columns, values, valuesVar } = (0, business_object_1.getSqlInsertParts)(bo);
        const query = `
      INSERT INTO "${(0, business_object_1.getTableName)(bo)}" ( ${columns} )
      VALUES ( ${valuesVar} )
      RETURNING ${(0, business_object_1.getColumns)(bo)};
    `;
        return one(query, values);
    };
    // Standard update
    const update = (bo, { on = 'id' } = {}) => {
        const { clause, idVar, values } = (0, business_object_1.getSqlUpdateParts)(bo, on);
        const query = `
      UPDATE "${(0, business_object_1.getTableName)(bo)}"
      SET ${clause}
      WHERE "${(0, business_object_1.getTableName)(bo)}".${on} = ${idVar}
      RETURNING ${(0, business_object_1.getColumns)(bo)};
    `;
        return one(query, values);
    };
    // Standard delete
    const _delete = (bo) => {
        const id = bo.id;
        const query = `
      DELETE FROM "${(0, business_object_1.getTableName)(bo)}"
      WHERE "${(0, business_object_1.getTableName)(bo)}".id = $(id)
    `;
        return none(query, { id });
    };
    const deleteMatching = (bo) => {
        const { whereClause, values } = bo.getMatchingParts();
        const query = `
      DELETE FROM "${(0, business_object_1.getTableName)(bo)}"
      WHERE ${whereClause};
    `;
        return none(query, values);
    };
    const getMatching = (bo) => {
        const { whereClause, values } = bo.getMatchingParts();
        const query = `
      SELECT ${(0, business_object_1.getColumns)(bo)}
      FROM "${(0, business_object_1.getTableName)(bo)}"
      WHERE ${whereClause};
    `;
        return one(query, values);
    };
    const getOneOrNoneMatching = (bo) => {
        const { whereClause, values } = bo.getMatchingParts();
        const query = `
      SELECT ${(0, business_object_1.getColumns)(bo)}
      FROM "${(0, business_object_1.getTableName)(bo)}"
      WHERE ${whereClause};
    `;
        return oneOrNone(query, values);
    };
    const getAnyMatching = (bo) => {
        const { whereClause, values } = bo.getMatchingParts();
        const query = `
      SELECT ${(0, business_object_1.getColumns)(bo)}
      FROM "${(0, business_object_1.getTableName)(bo)}"
      WHERE ${whereClause};
    `;
        return any(query, values);
    };
    const getAllMatching = (bo) => {
        const { whereClause, values } = bo.getMatchingParts();
        const query = `
      SELECT ${(0, business_object_1.getColumns)(bo)}
      FROM "${(0, business_object_1.getTableName)(bo)}"
      WHERE ${whereClause};
    `;
        return many(query, values);
    };
    return {
        // Query Functions
        one,
        oneOrNone,
        many,
        any,
        none,
        // Built-in basic CRUD functions
        create,
        update,
        delete: _delete,
        deleteMatching,
        getMatching,
        getOneOrNoneMatching,
        getAnyMatching,
        getAllMatching,
        tables: getBusinessObjects().reduce((accum, Bo) => {
            accum[(0, business_object_1.getDisplayName)(Bo)] = (0, business_object_1.getColumns)(Bo);
            return accum;
        }, {})
    };
};
exports.create = create;