UNPKG

domain-objects

Version:

A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.

56 lines 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUniqueIdentifier = void 0; const helpful_errors_1 = require("helpful-errors"); const type_fns_1 = require("type-fns"); const assertDomainObjectIsSafeToManipulate_1 = require("../constraints/assertDomainObjectIsSafeToManipulate"); const isOfDomainEntity_1 = require("../instantiation/inherit/isOfDomainEntity"); const isOfDomainEvent_1 = require("../instantiation/inherit/isOfDomainEvent"); const isOfDomainLiteral_1 = require("../instantiation/inherit/isOfDomainLiteral"); const isOfDomainObject_1 = require("../instantiation/inherit/isOfDomainObject"); const refByUnique_1 = require("../reference/refByUnique"); const DomainEntityUniqueKeysMustBeDefinedError_1 = require("./DomainEntityUniqueKeysMustBeDefinedError"); /** * Extracts an object that identifies the domain object via unique key, for DomainEntity, DomainEvent, or DomainLiteral. * * Uses the definition of a DomainEntity, DomainEvent, or DomainLiteral in order to extract the properties that uniquely define the domain object, generically. */ const getUniqueIdentifier = (obj) => { // make sure its an instance of DomainObject if (!(0, isOfDomainObject_1.isOfDomainObject)(obj)) throw new helpful_errors_1.BadRequestError('getUniqueIdentifier called on object that is not an instance of a DomainObject. Are you sure you instantiated the object? (Related: see `DomainObject.nested`)', { obj, }); // handle DomainEntity if ((0, isOfDomainEntity_1.isOfDomainEntity)(obj) || (0, isOfDomainEvent_1.isOfDomainEvent)(obj)) { const className = obj.constructor.name; const uniqueKeys = obj.constructor.unique; if (!uniqueKeys) throw new DomainEntityUniqueKeysMustBeDefinedError_1.DomainEntityUniqueKeysMustBeDefinedError({ entityName: className, nameOfFunctionNeededFor: 'getUniqueIdentifier', }); // make sure that the unique keys are safe to manipulate (0, assertDomainObjectIsSafeToManipulate_1.assertDomainObjectIsSafeToManipulate)(obj, { onKeys: uniqueKeys.flat(), }); // use refByUnique to extract the unique identifier return (0, refByUnique_1.refByUnique)(obj); } // handle DomainLiteral if ((0, isOfDomainLiteral_1.isOfDomainLiteral)(obj)) { const ignoreList = obj.constructor.metadata ?? [ 'id', 'uuid', ]; // these are keys we should ignore, because literals are unique on their natural keys - not metadata keys (e.g., id or uuid) const uniqueKeys = Object.keys(obj).filter((key) => !ignoreList.includes(key)); // literals are unique on all keys, other than the metadata ones we ignore return (0, type_fns_1.pick)(obj, uniqueKeys); } // throw error we get here, this is unexpected throw new helpful_errors_1.UnexpectedCodePathError('unexpected domain object type for getUniqueIdentifier. expected DomainLiteral, DomainEntity, or DomainEvent', { dobjClass: obj?.constructor?.name, dobj: obj, }); }; exports.getUniqueIdentifier = getUniqueIdentifier; //# sourceMappingURL=getUniqueIdentifier.js.map