domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
43 lines • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMetadataKeys = void 0;
const isOfDomainEntity_1 = require("../instantiation/inherit/isOfDomainEntity");
const isOfDomainEvent_1 = require("../instantiation/inherit/isOfDomainEvent");
const isOfDomainObject_1 = require("../instantiation/inherit/isOfDomainObject");
const DomainEntityUniqueKeysMustBeDefinedError_1 = require("./DomainEntityUniqueKeysMustBeDefinedError");
const DEFAULT_METADATA_KEYS = [
'id',
'uuid',
'createdAt',
'updatedAt',
'effectiveAt',
];
/**
* returns the metadata keys defined on the class of the domain object
*/
const getMetadataKeys = (obj, options) => {
// make sure its an instance of DomainObject
if (!(0, isOfDomainObject_1.isOfDomainObject)(obj))
throw new Error('getMetadataKeys called on object that is not an instance of a DomainObject. Are you sure you instantiated the object?');
// see if metadata was explicitly defined
const metadataKeysDeclared = obj.constructor
.metadata;
if (metadataKeysDeclared)
return metadataKeysDeclared;
// if it wasn't explicitly declared and its a DomainEntity or DomainEvent, then check to see if uuid is part of the unique key and augment default keys based on that
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: options?.nameOfFunctionNeededFor ?? 'getMetadataKeys',
});
if (uniqueKeys.flat().includes('uuid'))
return DEFAULT_METADATA_KEYS.filter((key) => key !== 'uuid'); // if the unique key includes uuid, then uuid is not metadata
}
// otherwise, return the defaults
return DEFAULT_METADATA_KEYS;
};
exports.getMetadataKeys = getMetadataKeys;
//# sourceMappingURL=getMetadataKeys.js.map