domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
41 lines • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RefByPrimary = void 0;
/* eslint-disable @typescript-eslint/no-redeclare */
const helpful_errors_1 = require("helpful-errors");
const refByPrimary_1 = require("../reference/refByPrimary");
const DomainLiteral_1 = require("./DomainLiteral");
const isOfDomainEntity_1 = require("./inherit/isOfDomainEntity");
const isOfDomainEvent_1 = require("./inherit/isOfDomainEvent");
// extend the domain literal into a custom class, so that we can rename it into RefByUnique, without mutating the global DomainLiteral class
class RefByPrimaryBase extends DomainLiteral_1.DomainLiteral {
constructor(props) {
// if props itself is a domain entity or domain event, extract its reference
if (props &&
typeof props === 'object' &&
((0, isOfDomainEntity_1.isOfDomainEntity)(props) || (0, isOfDomainEvent_1.isOfDomainEvent)(props))) {
super((0, refByPrimary_1.refByPrimary)(props));
}
else {
// validate that no primary key values are undefined at runtime
if (props && typeof props === 'object') {
for (const key of Object.keys(props)) {
if (props[key] === undefined) {
throw new helpful_errors_1.BadRequestError(`RefByPrimary: primary key '${key}' is undefined; primary keys must have defined values at reference time.`, { props });
}
}
}
super(props);
}
}
}
RefByPrimaryBase.metadata = [];
// create a constructor for RefByPrimary, 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.RefByPrimary = RefByPrimaryBase;
// Set the constructor name to 'RefByPrimary' for better developer experience
Object.defineProperty(RefByPrimaryBase, 'name', {
value: 'RefByPrimary',
writable: false,
});
//# sourceMappingURL=RefByPrimary.js.map