rich-domain
Version:
This package provide utils file and interfaces to assistant build a complex application with domain driving design
46 lines • 2.27 kB
TypeScript
import { IClass, CreateManyDomain, CreateManyResult, _ManyData } from "../types";
/**
* @description Helper function to create a data structure for constructing domain instances.
* Use this function to pair a domain class with its corresponding properties
* before calling `createManyDomainInstances`.
*
* @param domainClass The domain class that implements a static `create` method.
* @param props The properties needed to create an instance of `domainClass`.
* @returns An `_ManyData` object containing the class and props to be used by `createManyDomainInstances`.
*/
export declare const Class: <Props>(domainClass: IClass, props: Props) => _ManyData;
/**
* @description Creates multiple domain instances at once from an array of class-property pairs.
* Each element in the input data should contain a domain class and the props required to create an instance of it.
*
* - If the domain class does not have a static `create` method, a failure result is returned for that class.
* - Otherwise, `createManyDomainInstances` attempts to create each instance, collecting the results.
*
* @param data An array of objects, each containing a domain class and properties for instance creation.
* @returns A `CreateManyResult` object containing:
* - `data`: An iterator over the results of each instance creation attempt.
* - `result`: A combined `Result` indicating if all instances were created successfully or if any failed.
*
* @example
* ```typescript
* const classes = [
* Class(User, { name: "Alice", age: 30 }),
* Class(Product, { title: "Book", price: 9.99 }),
* Class(Order, { userId: "some-user-id", items: [] })
* ];
*
* const { data, result } = createManyDomainInstances(classes);
* if (result.isOk()) {
* const userResult = data.next().value;
* const productResult = data.next().value;
* const orderResult = data.next().value;
*
* // userResult, productResult, and orderResult are all successful `Result` instances.
* } else {
* console.error("Failed to create some domain instances:", result.error);
* }
* ```
*/
export declare const createManyDomainInstances: (data: CreateManyDomain) => CreateManyResult;
export default createManyDomainInstances;
//# sourceMappingURL=create-many-domain-instance.d.ts.map