domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
44 lines • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMetadataKeys = void 0;
const DomainEntity_1 = require("../instantiation/DomainEntity");
const DomainEvent_1 = require("../instantiation/DomainEvent");
const DomainObject_1 = require("../instantiation/DomainObject");
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) => {
var _a;
// make sure its an instance of DomainObject
if (!(obj instanceof DomainObject_1.DomainObject))
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 (obj instanceof DomainEntity_1.DomainEntity || obj instanceof DomainEvent_1.DomainEvent) {
const className = obj.constructor.name;
const uniqueKeys = obj.constructor.unique;
if (!uniqueKeys)
throw new DomainEntityUniqueKeysMustBeDefinedError_1.DomainEntityUniqueKeysMustBeDefinedError({
entityName: className,
nameOfFunctionNeededFor: (_a = options === null || options === void 0 ? void 0 : options.nameOfFunctionNeededFor) !== null && _a !== void 0 ? _a : '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