UNPKG

@decaf-ts/db-decorators

Version:

Agnostic database decorators and repository

79 lines 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findPrimaryKey = findPrimaryKey; exports.findModelId = findModelId; const constants_1 = require("./../model/constants.cjs"); const utils_1 = require("./../repository/utils.cjs"); const decorator_validation_1 = require("@decaf-ts/decorator-validation"); const errors_1 = require("./../repository/errors.cjs"); /** * @description Finds the primary key attribute for a model * @summary Searches in all the properties in the object for an {@link id} decorated property and returns the property key and metadata * @param {Model} model - The model object to search for primary key * @return {Object} An object containing the id property name and its metadata * @function findPrimaryKey * @mermaid * sequenceDiagram * participant Caller * participant findPrimaryKey * participant getAllPropertyDecoratorsRecursive * * Caller->>findPrimaryKey: model * findPrimaryKey->>getAllPropertyDecoratorsRecursive: get decorators * getAllPropertyDecoratorsRecursive-->>findPrimaryKey: decorators * findPrimaryKey->>findPrimaryKey: filter ID decorators * findPrimaryKey->>findPrimaryKey: validate single ID property * findPrimaryKey-->>Caller: {id, props} * @memberOf module:db-decorators */ function findPrimaryKey(model) { const decorators = (0, utils_1.getAllPropertyDecoratorsRecursive)(model, undefined, constants_1.DBKeys.REFLECT + constants_1.DBKeys.ID); const idDecorators = Object.entries(decorators).reduce((accum, [prop, decs]) => { const filtered = decs.filter((d) => d.key !== decorator_validation_1.ModelKeys.TYPE); if (filtered && filtered.length) { accum[prop] = accum[prop] || []; accum[prop].push(...filtered); } return accum; }, {}); if (!idDecorators || !Object.keys(idDecorators).length) throw new errors_1.InternalError("Could not find ID decorated Property"); if (Object.keys(idDecorators).length > 1) throw new errors_1.InternalError((0, decorator_validation_1.sf)(Object.keys(idDecorators).join(", "))); const idProp = Object.keys(idDecorators)[0]; if (!idProp) throw new errors_1.InternalError("Could not find ID decorated Property"); return { id: idProp, props: idDecorators[idProp][0].props, }; } /** * @description Retrieves the primary key value from a model * @summary Searches for the ID-decorated property in the model and returns its value * @param {Model} model - The model object to extract the ID from * @param {boolean} [returnEmpty=false] - Whether to return undefined if no ID value is found * @return {string | number | bigint} The primary key value * @function findModelId * @mermaid * sequenceDiagram * participant Caller * participant findModelId * participant findPrimaryKey * * Caller->>findModelId: model, returnEmpty * findModelId->>findPrimaryKey: model * findPrimaryKey-->>findModelId: {id, props} * findModelId->>findModelId: extract model[id] * findModelId->>findModelId: validate ID exists if required * findModelId-->>Caller: ID value * @memberOf module:db-decorators */ function findModelId(model, returnEmpty = false) { const idProp = findPrimaryKey(model).id; const modelId = model[idProp]; if (typeof modelId === "undefined" && !returnEmpty) throw new errors_1.InternalError(`No value for the Id is defined under the property ${idProp}`); return modelId; } //# sourceMappingURL=utils.js.map