domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
35 lines • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReadonlyKeys = void 0;
const isOfDomainEntity_1 = require("../instantiation/inherit/isOfDomainEntity");
const isOfDomainObject_1 = require("../instantiation/inherit/isOfDomainObject");
const getMetadataKeys_1 = require("./getMetadataKeys");
/**
* returns the readonly keys defined on the class of the domain object
*
* note,
* - this function returns the union of metadata keys and explicit readonly keys
* - only domain entities support non-metadata readonly attributes, due to their nature
*/
const getReadonlyKeys = (obj, options) => {
// make sure its an instance of DomainObject
if (!(0, isOfDomainObject_1.isOfDomainObject)(obj))
throw new Error('getReadonlyKeys called on object that is not an instance of a DomainObject. Are you sure you instantiated the object?');
// metadata is always readonly (applicable to all domain objects)
const metadataKeys = (0, getMetadataKeys_1.getMetadataKeys)(obj, options);
// for DomainEntity, include both metadata and explicit readonly keys
// (readonly = intrinsic attributes set by persistence layer, e.g., AWS-resolved host/port/status)
if ((0, isOfDomainEntity_1.isOfDomainEntity)(obj)) {
const domainObjectConstructor = obj.constructor;
const explicitReadonlyKeys = (domainObjectConstructor.readonly ??
[]);
// combine and dedupe
return [...new Set([...metadataKeys, ...explicitReadonlyKeys])];
}
// for DomainEvent, DomainLiteral, and other domain objects: only metadata is readonly
// - DomainEvent: immutable by nature, all properties known before persistence
// - DomainLiteral: immutable by nature, fully defined by intrinsic properties
return metadataKeys;
};
exports.getReadonlyKeys = getReadonlyKeys;
//# sourceMappingURL=getReadonlyKeys.js.map