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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | 2x 2x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 55x 55x | const {
createOneFromDatabase,
createOneOrNoneFromDatabase,
createManyFromDatabase,
createFromDatabase,
getSqlUpdateParts,
getSqlInsertParts,
getTableName,
getColumns,
getDisplayName
} = require('./business-object');
module.exports.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 => createOneFromDatabase(rows, getBusinessObjects))
.catch(errorHandler);
};
const oneOrNone = (query, values, errorHandler = defaultErrorHandler) => {
return db
.any(query, values)
.then(rows => createOneOrNoneFromDatabase(rows, getBusinessObjects))
.catch(errorHandler);
};
const many = (query, values, errorHandler = defaultErrorHandler) => {
return db
.any(query, values)
.then(rows => createManyFromDatabase(rows, getBusinessObjects))
.catch(errorHandler);
};
const any = (query, values, errorHandler = defaultErrorHandler) => {
return db
.any(query, values)
.then(rows => 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 } = getSqlInsertParts(bo);
const query = `
INSERT INTO "${getTableName(bo)}" ( ${columns} )
VALUES ( ${valuesVar} )
RETURNING ${getColumns(bo)};
`;
return one(query, values);
};
// Standard update
const update = (bo, { on = 'id' } = {}) => {
const { clause, idVar, values } = getSqlUpdateParts(bo, on);
const query = `
UPDATE "${getTableName(bo)}"
SET ${clause}
WHERE "${getTableName(bo)}".${on} = ${idVar}
RETURNING ${getColumns(bo)};
`;
return one(query, values);
};
// Standard delete
const _delete = bo => {
const id = bo.id;
const query = `
DELETE FROM "${getTableName(bo)}"
WHERE "${getTableName(bo)}".id = ${id}
`;
return none(query);
};
const deleteMatching = bo => {
const { whereClause, values } = bo.getMatchingParts();
const query = `
DELETE FROM "${getTableName(bo)}"
WHERE ${whereClause};
`;
return none(query, values);
};
const getMatching = bo => {
const { whereClause, values } = bo.getMatchingParts();
const query = `
SELECT ${getColumns(bo)}
FROM "${getTableName(bo)}"
WHERE ${whereClause};
`;
return one(query, values);
};
const getOneOrNoneMatching = bo => {
const { whereClause, values } = bo.getMatchingParts();
const query = `
SELECT ${getColumns(bo)}
FROM "${getTableName(bo)}"
WHERE ${whereClause};
`;
return oneOrNone(query, values);
};
const getAnyMatching = bo => {
const { whereClause, values } = bo.getMatchingParts();
const query = `
SELECT ${getColumns(bo)}
FROM "${getTableName(bo)}"
WHERE ${whereClause};
`;
return any(query, values);
};
const getAllMatching = bo => {
const { whereClause, values } = bo.getMatchingParts();
const query = `
SELECT ${getColumns(bo)}
FROM "${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[getDisplayName(Bo)] = getColumns(Bo);
return accum;
}, {})
};
};
|