UNPKG

@elsikora/nestjs-crud-automator

Version:

A library for automating the creation of CRUD operations in NestJS.

103 lines (99 loc) 4.17 kB
'use strict'; var autoContext_constant = require('../constant/dto/auto-context.constant.js'); var autoDtoContextQueue_utility = require('./auto-dto-context-queue.utility.js'); const AUTO_DTO_CHILDREN = new WeakMap(); /** * Returns registered child DTO constructors for the provided parent prototype. * @param {object} parentPrototype - Parent DTO prototype. * @returns {Set<Type<unknown>> | undefined} Set of registered child constructors. */ function GetRegisteredAutoDtoChildren(parentPrototype) { return AUTO_DTO_CHILDREN.get(parentPrototype); } /** * Returns all registered child DTO constructors for the provided parent prototype recursively. * @param {object} parentPrototype - Parent DTO prototype. * @returns {Array<Type<unknown>>} Registered child constructors including nested descendants. */ function GetRegisteredAutoDtoChildrenRecursive(parentPrototype) { const collected = new Set(); collectRegisteredAutoDtoChildren(parentPrototype, collected); return [...collected]; } /** * Registers DTO constructors as children of the provided parent prototype. * Used to propagate auto DTO context to nested manual DTOs. * @param {object} parentPrototype - Parent DTO prototype. * @param {unknown} child - Child constructor or array of constructors. */ function RegisterAutoDtoChild(parentPrototype, child) { if (!parentPrototype || !child) return; if (Array.isArray(child)) { for (const entry of child) { RegisterAutoDtoChild(parentPrototype, entry); } return; } if (typeof child === "function") { const childConstructor = child; if (childConstructor === Object) { return; } let children = AUTO_DTO_CHILDREN.get(parentPrototype); if (!children) { children = new Set(); AUTO_DTO_CHILDREN.set(parentPrototype, children); } if (!children.has(childConstructor)) { children.add(childConstructor); inheritExistingContexts(parentPrototype, childConstructor.prototype); } } } /** * Collects registered child DTO constructors recursively. * @param {object} parentPrototype - Parent DTO prototype. * @param {Set<Type<unknown>>} collected - Accumulator for discovered constructors. * @returns {void} */ function collectRegisteredAutoDtoChildren(parentPrototype, collected) { const children = AUTO_DTO_CHILDREN.get(parentPrototype); if (!children) { return; } for (const child of children) { if (collected.has(child)) { continue; } collected.add(child); collectRegisteredAutoDtoChildren(child.prototype, collected); } } /** * Copies currently active auto DTO contexts from parent prototype to child prototype. * @param {object} parentPrototype - Parent DTO prototype. * @param {object} childPrototype - Child DTO prototype. */ function inheritExistingContexts(parentPrototype, childPrototype) { const parentStack = Reflect.getMetadata?.(autoContext_constant.AUTO_CONTEXT_DTO_CONSTANT.METADATA_KEY, parentPrototype); if (!parentStack || parentStack.length === 0) { return; } const childStack = Reflect.getMetadata?.(autoContext_constant.AUTO_CONTEXT_DTO_CONSTANT.METADATA_KEY, childPrototype) ?? []; if (childStack.length < parentStack.length) { const updatedStack = [...childStack, ...parentStack.slice(childStack.length)]; Reflect.defineMetadata?.(autoContext_constant.AUTO_CONTEXT_DTO_CONSTANT.METADATA_KEY, updatedStack, childPrototype); autoDtoContextQueue_utility.FlushAutoDtoContextExecutions(childPrototype); } const grandchildren = AUTO_DTO_CHILDREN.get(childPrototype); if (!grandchildren) return; for (const grandchild of grandchildren) { inheritExistingContexts(childPrototype, grandchild.prototype); } } exports.GetRegisteredAutoDtoChildren = GetRegisteredAutoDtoChildren; exports.GetRegisteredAutoDtoChildrenRecursive = GetRegisteredAutoDtoChildrenRecursive; exports.RegisterAutoDtoChild = RegisterAutoDtoChild; //# sourceMappingURL=register-auto-dto-child.utility.js.map