UNPKG

domain-objects

Version:

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

42 lines 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUpdatableProperties = 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 isOfDomainObject_1 = require("../instantiation/inherit/isOfDomainObject"); const DomainEntityUpdatablePropertiesMustBeDefinedError_1 = require("./DomainEntityUpdatablePropertiesMustBeDefinedError"); /** * Extracts an object that contains only the updatable properties of the domain object, for DomainEntity * * Uses the definition of a DomainEntity in order to extract the properties that are updatable for the domain object generically * * note * - this should not be called on DomainLiterals or DomainEvents, since by definition they can not have updatable properties */ const getUpdatableProperties = (dobj) => { // make sure its an instance of DomainObject if (!(0, isOfDomainObject_1.isOfDomainObject)(dobj)) throw new Error('getUpdatableProperties called on object that is not an instance of a DomainObject. Are you sure you instantiated the object? (Related: see `DomainObject.nested`)'); // make sure that its safe to manipulate (0, assertDomainObjectIsSafeToManipulate_1.assertDomainObjectIsSafeToManipulate)(dobj); // handle DomainEntity if ((0, isOfDomainEntity_1.isOfDomainEntity)(dobj)) { const className = dobj.constructor.name; const updatableProps = dobj.constructor.updatable; if (!updatableProps) throw new DomainEntityUpdatablePropertiesMustBeDefinedError_1.DomainEntityUpdatablePropertiesMustBeDefinedError({ entityName: className, nameOfFunctionNeededFor: 'getUpdatableProperties', }); return (0, type_fns_1.pick)(dobj, updatableProps.flat()); } // throw error we get here, this is unexpected throw new helpful_errors_1.UnexpectedCodePathError('unexpected domain object type for getUpdatableProperties. expected DomainEntity', { dobjClass: dobj?.constructor?.name, dobj, }); }; exports.getUpdatableProperties = getUpdatableProperties; //# sourceMappingURL=getUpdatableProperties.js.map