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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | 1x 1x 1x 1x 1x 1x 248x 1x 1x 1x 1x 28x 1x 1x 5x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getValueBySqlColumn = exports.getNewWith = exports.getMatchingPartsObject = exports.getMatchingParts = exports.getSqlUpdateParts = exports.getSqlInsertParts = exports.createManyFromDatabase = exports.createOneOrNoneFromDatabase = exports.createOneFromDatabase = exports.createFromDatabase = exports.objectifyDatabaseResult = exports.mapToBos = exports.clumpIntoGroups = exports.nestClump = exports.getIdForEntity = exports.getCollectionDisplayNameForEntity = exports.getReferencesForEntity = exports.getTableNameForEntity = exports.getPrimaryKeyForEntity = exports.getColumnNamesForEntity = exports.getPropertyNamesForEntity = exports.getSelectColumnsClauseForEntity = exports.getDisplayNameForEntity = exports.getPureORMDataByEntity = exports.getPureORMDataByTableName = exports.getSelectColumnsClause = exports.getPrefixedColumnNames = exports.getReferences = exports.getColumnNames = exports.getPrimaryKey = void 0;
const camelCase = require('camelcase');
const getPrimaryKey = (data) => {
const pkColumnsData = data.columns.filter((x) => x.primaryKey);
const primaryKeys = pkColumnsData.map((x) => x.column);
return primaryKeys.length > 0 ? primaryKeys : ['id'];
};
exports.getPrimaryKey = getPrimaryKey;
const getColumnNames = (data) => {
return data.columns.map((x) => x.column);
};
exports.getColumnNames = getColumnNames;
const getReferences = (data) => {
return data.columns
.filter((x) => x.references)
.reduce((accum, item) => Object.assign({}, accum, {
[item.property]: item.references
}), {});
};
exports.getReferences = getReferences;
const getPrefixedColumnNames = (data) => {
return (0, exports.getColumnNames)(data).map((col) => `${data.tableName}#${col}`);
};
exports.getPrefixedColumnNames = getPrefixedColumnNames;
const getSelectColumnsClause = (data) => {
return (0, exports.getPrefixedColumnNames)(data)
.map((prefixed, index) => `"${data.tableName}".${(0, exports.getColumnNames)(data)[index]} as "${prefixed}"`)
.join(', ');
};
exports.getSelectColumnsClause = getSelectColumnsClause;
const getPureORMDataByTableName = (tableName, pureORMDataArray) => {
const pureORMData = pureORMDataArray.find(data => data.tableName == tableName);
if (!pureORMData) {
throw new Error(`Could not find pureORMData for table ${tableName}`);
}
return pureORMData;
};
exports.getPureORMDataByTableName = getPureORMDataByTableName;
const getPureORMDataByEntity = (entity, pureORMDataArray) => {
const pureORMData = pureORMDataArray.find(data => data.entityClass == entity.constructor);
if (!pureORMData) {
throw new Error(`Could not find pureORMData for class ${entity.constructor}`);
}
return pureORMData;
};
exports.getPureORMDataByEntity = getPureORMDataByEntity;
const getDisplayNameForEntity = (entity, pureORMDataArray) => {
const pureORMData = (0, exports.getPureORMDataByEntity)(entity, pureORMDataArray);
return pureORMData.displayName;
};
exports.getDisplayNameForEntity = getDisplayNameForEntity;
const getSelectColumnsClauseForEntity = (entity, pureORMDataArray) => {
const pureORMData = (0, exports.getPureORMDataByEntity)(entity, pureORMDataArray);
return (0, exports.getSelectColumnsClause)(pureORMData);
};
exports.getSelectColumnsClauseForEntity = getSelectColumnsClauseForEntity;
const getPropertyNamesForEntity = (entity, pureORMDataArray) => {
const pureORMData = (0, exports.getPureORMDataByEntity)(entity, pureORMDataArray);
return pureORMData.propertyNames;
};
exports.getPropertyNamesForEntity = getPropertyNamesForEntity;
const getColumnNamesForEntity = (entity, pureORMDataArray) => {
const pureORMData = (0, exports.getPureORMDataByEntity)(entity, pureORMDataArray);
return (0, exports.getColumnNames)(pureORMData);
};
exports.getColumnNamesForEntity = getColumnNamesForEntity;
const getPrimaryKeyForEntity = (entity, pureORMDataArray) => {
const pureORMData = (0, exports.getPureORMDataByEntity)(entity, pureORMDataArray);
return (0, exports.getPrimaryKey)(pureORMData);
};
exports.getPrimaryKeyForEntity = getPrimaryKeyForEntity;
const getTableNameForEntity = (entity, pureORMDataArray) => {
const pureORMData = (0, exports.getPureORMDataByEntity)(entity, pureORMDataArray);
return pureORMData.tableName;
};
exports.getTableNameForEntity = getTableNameForEntity;
const getReferencesForEntity = (entity, pureORMDataArray) => {
const pureORMData = (0, exports.getPureORMDataByEntity)(entity, pureORMDataArray);
return (0, exports.getReferences)(pureORMData);
};
exports.getReferencesForEntity = getReferencesForEntity;
const getCollectionDisplayNameForEntity = (entity, pureORMDataArray) => {
const pureORMData = (0, exports.getPureORMDataByEntity)(entity, pureORMDataArray);
return pureORMData.collectionDisplayName;
};
exports.getCollectionDisplayNameForEntity = getCollectionDisplayNameForEntity;
// Returns unique identifier of entity (the values of the primary keys)
const getIdForEntity = (entity, pureORMDataArray) => {
const pureORMData = (0, exports.getPureORMDataByEntity)(entity, pureORMDataArray);
return (0, exports.getPrimaryKey)(pureORMData)
.map((key) => entity[key])
.join('');
};
exports.getIdForEntity = getIdForEntity;
/*
* In:
* [
* [Article {id: 32}, ArticleTag {id: 54}]
* [Article {id: 32}, ArticleTag {id: 55}]
* ]
* Out:
* Article {id: 32, ArticleTags articleTags: [ArticleTag {id: 54}, ArticleTag {id: 55}]
*/
const nestClump = (clump, pureORMDataArray) => {
clump = clump.map((x) => Object.values(x));
const root = clump[0][0];
clump = clump.map((row) => row.filter((item, index) => index !== 0));
const built = { [(0, exports.getDisplayNameForEntity)(root, pureORMDataArray)]: root };
let nodes = [root];
// Wowzer is this both CPU and Memory inefficient
clump.forEach((array) => {
array.forEach((_entity) => {
const nodeAlreadySeen = nodes.find((x) => x.constructor.name === _entity.constructor.name
&& (0, exports.getIdForEntity)(x, pureORMDataArray) === (0, exports.getIdForEntity)(_entity, pureORMDataArray));
const entity = nodeAlreadySeen || _entity;
const isNodeAlreadySeen = !!nodeAlreadySeen;
const nodePointingToIt = nodes.find(node => {
const indexes = Object.values((0, exports.getReferencesForEntity)(node, pureORMDataArray))
.map((x, i) => (x === entity.constructor ? i : null))
.filter((x, i) => x != null);
if (!indexes.length) {
return false;
}
for (const index of indexes) {
const property = Object.keys((0, exports.getReferencesForEntity)(node, pureORMDataArray))[index];
if (node[property] === entity.id) {
return true;
}
}
return false;
});
// For first obj type which is has an instance in nodes array,
// get its index in nodes array
const indexOfOldestParent = array.reduce((answer, obj) => {
if (answer != null) {
return answer;
}
const index = nodes.findIndex(n => n.constructor === obj.constructor);
if (index !== -1) {
return index;
}
return null;
}, null) || 0;
const parentHeirarchy = [
root,
...nodes.slice(0, indexOfOldestParent + 1).reverse()
];
const nodeItPointsTo = parentHeirarchy.find(parent => {
const index = Object.values((0, exports.getReferencesForEntity)(entity, pureORMDataArray)).indexOf(parent.constructor);
if (index === -1) {
return false;
}
const property = Object.keys((0, exports.getReferencesForEntity)(entity, pureORMDataArray))[index];
return entity[property] === parent.id;
});
if (isNodeAlreadySeen) {
if (nodeItPointsTo && !nodePointingToIt) {
nodes = [entity, ...nodes];
return;
}
// If the nodePointingToIt (eg, parcel_event) is part of an
// existing collection on this node (eg, parcel) which is a
// nodeAlreadySeen, early return so we don't create it (parcel) on
// the nodePointingToIt (parcel_event), since it (parcel) has been
// shown to be the parent (of parcel_events).
if (nodePointingToIt) {
const ec = entity[(0, exports.getCollectionDisplayNameForEntity)(nodePointingToIt, pureORMDataArray)];
if (ec && ec.models.find((m) => m === nodePointingToIt)) {
nodes = [entity, ...nodes];
return;
}
}
}
if (nodePointingToIt) {
nodePointingToIt[(0, exports.getDisplayNameForEntity)(entity, pureORMDataArray)] = entity;
}
else if (nodeItPointsTo) {
let collection = nodeItPointsTo[(0, exports.getCollectionDisplayNameForEntity)(entity, pureORMDataArray)];
if (collection) {
collection.models.push(entity);
}
else {
nodeItPointsTo[(0, exports.getCollectionDisplayNameForEntity)(entity, pureORMDataArray)] = new entity.BoCollection({
models: [entity]
});
}
}
else {
if (!(0, exports.getIdForEntity)(entity, pureORMDataArray)) {
// If the join is fruitless; todo: add a test for this path
return;
}
throw Error(`Could not find how this BO fits: ${JSON.stringify(entity)}`);
}
nodes = [entity, ...nodes];
});
});
return built;
};
exports.nestClump = nestClump;
/*
* Clump array of flat objects into groups based on id of root
* In:
* [
* [Article {id: 32}, ArticleTag {id: 54}]
* [Article {id: 32}, ArticleTag {id: 55}]
* [Article {id: 33}, ArticleTag {id: 56}]
* ]
* Out:
* [
* [
* [Article {id: 32}, ArticleTag {id: 54}]
* [Article {id: 32}, ArticleTag {id: 55}]
* ]
* [
* [Article {id: 33}, ArticleTag {id: 56}]
* ]
* ]
*/
const clumpIntoGroups = (processed, pureORMDataArray) => {
const root = processed[0][0];
const rootBo = root.constructor;
const clumps = processed.reduce((accum, item) => {
const id = (0, exports.getPrimaryKeyForEntity)(root, pureORMDataArray)
.map((key) => { var _a; return (_a = item.find((x) => x.constructor === rootBo)) === null || _a === void 0 ? void 0 : _a[key]; })
.join('@');
if (accum.has(id)) {
accum.set(id, [...accum.get(id), item]);
}
else {
accum.set(id, [item]);
}
return accum;
}, new Map());
return [...clumps.values()];
};
exports.clumpIntoGroups = clumpIntoGroups;
const mapToBos = (objectified, pureORMDataArray) => {
return Object.keys(objectified).map(tableName => {
const pureORMData = (0, exports.getPureORMDataByTableName)(tableName, pureORMDataArray);
const propified = Object.keys(objectified[tableName]).reduce((obj, column) => {
let propertyName = pureORMData.propertyNames[(0, exports.getColumnNames)(pureORMData).indexOf(column)];
if (!propertyName) {
if (column.startsWith('meta_')) {
propertyName = camelCase(column);
}
else {
throw Error(`No property name for "${column}" in business object "${pureORMData.displayName}". Non-spec'd columns must begin with "meta_".`);
}
}
obj[propertyName] = objectified[tableName][column];
return obj;
}, {});
return new pureORMData.entityClass(propified);
});
};
exports.mapToBos = mapToBos;
/*
* Make objects (based on special table#column names) from flat database
* return value.
*/
const objectifyDatabaseResult = (result) => {
return Object.keys(result).reduce((obj, text) => {
const tableName = text.split('#')[0];
const column = text.split('#')[1];
obj[tableName] = obj[tableName] || {};
obj[tableName][column] = result[text];
return obj;
}, {});
};
exports.objectifyDatabaseResult = objectifyDatabaseResult;
const createFromDatabase = (_result, pureORMDataArray) => {
const result = Array.isArray(_result) ? _result : [_result];
const objectified = result.map(exports.objectifyDatabaseResult);
const boified = objectified.map((x) => (0, exports.mapToBos)(x, pureORMDataArray));
const clumps = (0, exports.clumpIntoGroups)(boified, pureORMDataArray);
const nested = clumps.map(x => (0, exports.nestClump)(x, pureORMDataArray));
const models = nested.map(n => Object.values(n)[0]);
return models.length ? new models[0].BoCollection({ models }) : void 0;
};
exports.createFromDatabase = createFromDatabase;
const createOneFromDatabase = (_result, pureORMDataArray) => {
const collection = (0, exports.createFromDatabase)(_result, pureORMDataArray);
if (!collection || collection.models.length === 0) {
throw Error('Did not get one.');
}
else if (collection.models.length > 1) {
throw Error('Got more than one.');
}
return collection.models[0];
};
exports.createOneFromDatabase = createOneFromDatabase;
const createOneOrNoneFromDatabase = (_result, pureORMDataArray) => {
if (!_result) {
return _result;
}
const collection = (0, exports.createFromDatabase)(_result, pureORMDataArray);
if (collection && collection.models.length > 1) {
throw Error('Got more than one.');
}
return collection && collection.models[0];
};
exports.createOneOrNoneFromDatabase = createOneOrNoneFromDatabase;
const createManyFromDatabase = (_result, pureORMDataArray) => {
const collection = (0, exports.createFromDatabase)(_result, pureORMDataArray);
if (!collection || collection.models.length === 0) {
throw Error('Did not get at least one.');
}
return collection;
};
exports.createManyFromDatabase = createManyFromDatabase;
const getSqlInsertParts = (entity, pureORMDataArray) => {
const columns = (0, exports.getColumnNamesForEntity)(entity, pureORMDataArray)
.filter((column, index) => entity[(0, exports.getPropertyNamesForEntity)(entity, pureORMDataArray)[index]] !== void 0)
.map((col) => `"${col}"`)
.join(', ');
const values = (0, exports.getPropertyNamesForEntity)(entity, pureORMDataArray)
.map((property) => entity[property])
.filter((value) => value !== void 0);
const valuesVar = values.map((value, index) => `$${index + 1}`);
return { columns, values, valuesVar };
};
exports.getSqlInsertParts = getSqlInsertParts;
const getSqlUpdateParts = (entity, pureORMDataArray, on = 'id') => {
const clauseArray = (0, exports.getColumnNamesForEntity)(entity, pureORMDataArray)
.filter((sqlColumn, index) => entity[(0, exports.getPropertyNamesForEntity)(entity, pureORMDataArray)[index]] !== void 0)
.map((sqlColumn, index) => `"${sqlColumn}" = $${index + 1}`);
const clause = clauseArray.join(', ');
const idVar = `$${clauseArray.length + 1}`;
const _values = (0, exports.getPropertyNamesForEntity)(entity, pureORMDataArray)
.map((property) => entity[property])
.filter((value) => value !== void 0);
const values = [..._values, entity[on]];
return { clause, idVar, values };
};
exports.getSqlUpdateParts = getSqlUpdateParts;
const getMatchingParts = (entity, pureORMDataArray) => {
const whereClause = (0, exports.getPropertyNamesForEntity)(entity, pureORMDataArray)
.map((property, index) => entity[property] != null
? `"${(0, exports.getTableNameForEntity)(entity, pureORMDataArray)}"."${(0, exports.getColumnNamesForEntity)(entity, pureORMDataArray)[index]}"`
: null)
.filter((x) => x != null)
.map((x, i) => `${x} = $${i + 1}`)
.join(' AND ');
const values = (0, exports.getPropertyNamesForEntity)(entity, pureORMDataArray)
.map((property) => (entity[property] != null ? entity[property] : null))
.filter((x) => x != null);
return { whereClause, values };
};
exports.getMatchingParts = getMatchingParts;
// This one returns an object, which allows it to be more versatile.
// To-do: make this one even better and use it instead of the one above.
const getMatchingPartsObject = (entity, pureORMDataArray) => {
const whereClause = (0, exports.getPropertyNamesForEntity)(entity, pureORMDataArray)
.map((property, index) => entity[property] != null
? `"${(0, exports.getTableNameForEntity)(entity, pureORMDataArray)}"."${(0, exports.getColumnNamesForEntity)(entity, pureORMDataArray)[index]}"`
: null)
.filter((x) => x != null)
.map((x, i) => `${x} = $(${i + 1})`)
.join(' AND ');
const values = (0, exports.getPropertyNamesForEntity)(entity, pureORMDataArray)
.map((property) => (entity[property] != null ? entity[property] : null))
.filter((x) => x != null)
.reduce((accum, val, index) => Object.assign({}, accum, { [index + 1]: val }), {});
return { whereClause, values };
};
exports.getMatchingPartsObject = getMatchingPartsObject;
const getNewWith = (entity, sqlColumns, values, pureORMDataArray) => {
const Constructor = entity.constructor;
const entityKeys = sqlColumns.map((key) => (0, exports.getPropertyNamesForEntity)(entity, pureORMDataArray)[(0, exports.getColumnNamesForEntity)(entity, pureORMDataArray).indexOf(key)]);
const entityData = entityKeys.reduce((data, key, index) => {
data[key] = values[index];
return data;
}, {});
return new Constructor(entityData);
};
exports.getNewWith = getNewWith;
const getValueBySqlColumn = (entity, sqlColumn, pureORMDataArray) => {
return entity[(0, exports.getPropertyNamesForEntity)(entity, pureORMDataArray)[(0, exports.getColumnNamesForEntity)(entity, pureORMDataArray).indexOf(sqlColumn)]];
};
exports.getValueBySqlColumn = getValueBySqlColumn;
|