domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
32 lines (31 loc) • 1.54 kB
TypeScript
import type { DomainObjectShape, Refable } from './Refable';
import type { RefByUnique } from './RefByUnique.type';
/**
* creates a reference to a domain object by its unique key
*
* extracts only the unique key properties from a domain object instance
*
* note
* - you may need to explicitly annotate the type for proper inference
* - e.g., `const ref = refByUnique<typeof SeaTurtle>(turtle)`
* - automatic resolution of the relationship between instance and class.static properties is not yet possible in TypeScript
* - recursively extracts references from nested domain objects
* - if a unique key property is itself a domain object, it will recursively call refByUnique on it
*
* @example
* ```ts
* const turtle = new SeaTurtle({ uuid: '1', seawaterSecurityNumber: '821', name: 'Crush' });
* const ref = refByUnique<typeof SeaTurtle>(turtle);
* // ref = { seawaterSecurityNumber: '821' }
* ```
*
* @example
* ```ts
* // with nested domain objects
* const turtle = new SeaTurtle({ seawaterSecurityNumber: '821', name: 'Crush' });
* const shell = new SeaTurtleShell({ turtle, algea: 'ALIL' });
* const ref = refByUnique<typeof SeaTurtleShell>(shell);
* // ref = { turtle: { seawaterSecurityNumber: '821' } }
* ```
*/
export declare const refByUnique: <TDobj extends Refable<TShape, TPrimary, TUnique>, TShape extends DomainObjectShape = any, TPrimary extends readonly string[] = any, TUnique extends readonly string[] = any>(instance: InstanceType<TDobj>) => RefByUnique<TDobj, TShape, TPrimary, TUnique>;