rich-domain
Version:
This package provide utils file and interfaces to assistant build a complex application with domain driving design
76 lines • 3.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createManyDomainInstances = exports.Class = void 0;
const validator_1 = require("../utils/validator");
const iterator_1 = require("./iterator");
const result_1 = require("./result");
/**
* @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`.
*/
const Class = (domainClass, props) => {
return {
class: domainClass,
props
};
};
exports.Class = Class;
/**
* @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);
* }
* ```
*/
const createManyDomainInstances = (data) => {
const results = [];
if (validator_1.default.isArray(data)) {
for (let index = 0; index < data.length; index++) {
const domainInfo = data[index];
const domainClass = domainInfo.class;
const existsCreateMethod = typeof domainClass?.create === 'function';
if (!existsCreateMethod) {
results.push(result_1.default.fail(`No static 'create' method found in class ${domainClass?.name}.`));
continue;
}
const payload = domainClass.create(domainInfo.props);
results.push(payload);
}
}
const iterator = iterator_1.default.create({ initialData: results, returnCurrentOnReversion: true });
const result = result_1.default.combine(results);
return { data: iterator, result };
};
exports.createManyDomainInstances = createManyDomainInstances;
exports.default = exports.createManyDomainInstances;
//# sourceMappingURL=create-many-domain-instance.js.map