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.14 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-redeclare */
Object.defineProperty(exports, "__esModule", { value: true });
exports.RefByUnique = void 0;
const refByUnique_1 = require("../reference/refByUnique");
const DomainLiteral_1 = require("./DomainLiteral");
const isOfDomainEntity_1 = require("./inherit/isOfDomainEntity");
const isOfDomainEvent_1 = require("./inherit/isOfDomainEvent");
const Ref_1 = require("./Ref");
// extend the domain literal into a custom class, so that we can rename it into RefByUnique, without mutating the global DomainLiteral class
class RefByUniqueBase extends DomainLiteral_1.DomainLiteral {
constructor(props) {
// if props itself is a domain entity or domain event, extract its reference
if ((0, isOfDomainEntity_1.isOfDomainEntity)(props) || (0, isOfDomainEvent_1.isOfDomainEvent)(props)) {
super((0, refByUnique_1.refByUnique)(props));
return;
}
// if props is not itself a domain entity/event, check each property
// and apply refByUnique on any nested domain entities/events
const transformedProps = {};
for (const key of Object.keys(props)) {
const value = props[key];
if (value &&
typeof value === 'object' &&
((0, isOfDomainEntity_1.isOfDomainEntity)(value) || (0, isOfDomainEvent_1.isOfDomainEvent)(value))) {
transformedProps[key] = Ref_1.Ref.as(value);
}
else {
transformedProps[key] = value;
}
}
super(transformedProps);
}
}
RefByUniqueBase.metadata = [];
// create a constructor for RefByUnique, so that we can instantiate references
// todo: actually use a class and extend DomainLiteral, when typescript supports more specialized types for classes; for now, we create a constructor via a procedure
exports.RefByUnique = RefByUniqueBase;
// overwrite the constructor name to 'RefByUnique', to keep things consistent
Object.defineProperty(RefByUniqueBase, 'name', {
value: 'RefByUnique',
writable: false,
});
//# sourceMappingURL=RefByUnique.js.map