domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
41 lines (40 loc) • 2.95 kB
TypeScript
import type { DomainObjectShape, Refable } from '../reference/Refable';
import type { RefByPrimary as RefByPrimaryType } from '../reference/RefByPrimary.type';
/**
* In Domain Driven Design, a Reference is a special type of Domain Literal that represents a reference to another Domain Object.
*
* A Domain Reference:
* - contains only the identifying properties of the referenced object
* - is immutable (like all Domain Literals)
* - is used to refer to Domain Entities or Domain Events without including their full data
*
* The purpose of a Domain Reference is to enable lightweight references between domain objects without circular dependencies or data duplication.
*
* For example:
* - A `SeaTurtleRefByPrimary { uuid: string }` is a reference to a SeaTurtle by its primary key
*
* Note:
* - Domain References are typically created automatically via `DomainEntity.RefByPrimary` or `DomainEntity.RefByPrimary`
* - You rarely need to extend a DomainRef directly
*/
export type RefByPrimary<TDobj extends Refable<TShape, TPrimary, TUnique>, TShape extends DomainObjectShape = any, // todo: update DomainObjectShape -> DomainReferenceableInstance to enable extraction of primary and unique keys via types, when typescript supports constructor inference from instances
TPrimary extends readonly string[] = any, TUnique extends readonly string[] = any> = RefByPrimaryType<TDobj, TShape, TPrimary, TUnique>;
export declare const RefByPrimary: {
new <TDobj extends Refable<TShape, TPrimary, TUnique>, TShape extends DomainObjectShape = any, TPrimary extends readonly string[] = any, TUnique extends readonly string[] = any>(props: RefByPrimary<TDobj, TShape, TPrimary, TUnique>): RefByPrimary<TDobj, TShape, TPrimary, TUnique>;
/**
* .what = an interface via which to construct instances w/ immute operations
*
* .why =
* - immute operations such as .clone produce more maintainable code by preventing unexpected mutations
* - these immute operations provide a safe pit of success for common operations
*/
build<TDobj extends Refable<TShape, TPrimary, TUnique>, TShape extends DomainObjectShape = any, TPrimary extends readonly string[] = any, TUnique extends readonly string[] = any>(props: RefByPrimary<TDobj, TShape, TPrimary, TUnique>): RefByPrimary<TDobj, TShape, TPrimary, TUnique>;
/**
* .what = an interface via which to construct instances w/ immute operations
*
* .why =
* - immute operations such as .clone produce more maintainable code by preventing unexpected mutations
* - these immute operations provide a safe pit of success for common operations
*/
as<TDobj extends Refable<TShape, TPrimary, TUnique>, TShape extends DomainObjectShape = any, TPrimary extends readonly string[] = any, TUnique extends readonly string[] = any>(props: RefByPrimary<TDobj, TShape, TPrimary, TUnique>): RefByPrimary<TDobj, TShape, TPrimary, TUnique>;
};