{"version":3,"file":"process-unidirectional-join-tables.mjs","sources":["../../../src/repairs/operations/process-unidirectional-join-tables.ts"],"sourcesContent":["import type { Database } from '../..';\n\n/**\n * Iterates over all models and their unidirectional relations, invoking a provided operation on each join table.\n *\n * This function does not perform any cleaning or modification itself. Instead, it identifies all unidirectional\n * relations (relations without inversedBy or mappedBy) that use join tables, and delegates any join table operation\n * (such as cleaning, validation, or analysis) to the provided operateOnJoinTable function.\n *\n * @param db - The database instance\n * @param operateOnJoinTable - A function to execute for each unidirectional join table relation\n * @returns The sum of results returned by operateOnJoinTable for all processed relations\n */\nexport const processUnidirectionalJoinTables = async (\n db: Database,\n operateOnJoinTable: (\n db: Database,\n joinTableName: string,\n relation: any,\n sourceModel: any\n ) => Promise\n): Promise => {\n let totalResult = 0;\n\n const mdValues = db.metadata.values();\n const mdArray = Array.from(mdValues);\n\n if (mdArray.length === 0) {\n return 0;\n }\n\n db.logger.debug('Starting unidirectional join table operation');\n\n for (const model of mdArray) {\n const unidirectionalRelations = getUnidirectionalRelations(model.attributes || {});\n\n for (const relation of unidirectionalRelations) {\n if (hasJoinTable(relation) && hasTarget(relation)) {\n const result = await operateOnJoinTable(db, relation.joinTable.name, relation, model);\n totalResult += result;\n }\n }\n }\n\n db.logger.debug(\n `Unidirectional join table operation completed. Processed ${totalResult} entries.`\n );\n\n return totalResult;\n};\n\n/**\n * Identifies unidirectional relations (relations without inversedBy or mappedBy)\n * Uses same logic as prevention fix in unidirectional-relations.ts:54-61\n */\nconst getUnidirectionalRelations = (attributes: Record): any[] => {\n return Object.values(attributes).filter((attribute) => {\n if (attribute.type !== 'relation') {\n return false;\n }\n\n // Check if it's unidirectional (no inversedBy or mappedBy) - same as prevention logic\n return !attribute.inversedBy && !attribute.mappedBy;\n });\n};\n\n/**\n * Type guard to check if a relation has a joinTable property\n */\nconst hasJoinTable = (relation: any): boolean => {\n return 'joinTable' in relation && relation.joinTable != null;\n};\n\n/**\n * Type guard to check if a relation has a target property\n */\nconst hasTarget = (relation: any): boolean => {\n return 'target' in relation && typeof relation.target === 'string';\n};\n"],"names":["processUnidirectionalJoinTables","db","operateOnJoinTable","totalResult","mdValues","metadata","values","mdArray","Array","from","length","logger","debug","model","unidirectionalRelations","getUnidirectionalRelations","attributes","relation","hasJoinTable","hasTarget","result","joinTable","name","Object","filter","attribute","type","inversedBy","mappedBy","target"],"mappings":"AAEA;;;;;;;;;;AAUC,IACM,MAAMA,+BAAkC,GAAA,OAC7CC,EACAC,EAAAA,kBAAAA,GAAAA;AAOA,IAAA,IAAIC,WAAc,GAAA,CAAA;AAElB,IAAA,MAAMC,QAAWH,GAAAA,EAAAA,CAAGI,QAAQ,CAACC,MAAM,EAAA;IACnC,MAAMC,OAAAA,GAAUC,KAAMC,CAAAA,IAAI,CAACL,QAAAA,CAAAA;IAE3B,IAAIG,OAAAA,CAAQG,MAAM,KAAK,CAAG,EAAA;QACxB,OAAO,CAAA;AACT;IAEAT,EAAGU,CAAAA,MAAM,CAACC,KAAK,CAAC,8CAAA,CAAA;IAEhB,KAAK,MAAMC,SAASN,OAAS,CAAA;AAC3B,QAAA,MAAMO,uBAA0BC,GAAAA,0BAAAA,CAA2BF,KAAMG,CAAAA,UAAU,IAAI,EAAC,CAAA;QAEhF,KAAK,MAAMC,YAAYH,uBAAyB,CAAA;YAC9C,IAAII,YAAAA,CAAaD,QAAaE,CAAAA,IAAAA,SAAAA,CAAUF,QAAW,CAAA,EAAA;gBACjD,MAAMG,MAAAA,GAAS,MAAMlB,kBAAmBD,CAAAA,EAAAA,EAAIgB,SAASI,SAAS,CAACC,IAAI,EAAEL,QAAUJ,EAAAA,KAAAA,CAAAA;gBAC/EV,WAAeiB,IAAAA,MAAAA;AACjB;AACF;AACF;IAEAnB,EAAGU,CAAAA,MAAM,CAACC,KAAK,CACb,CAAC,yDAAyD,EAAET,WAAY,CAAA,SAAS,CAAC,CAAA;IAGpF,OAAOA,WAAAA;AACT;AAEA;;;IAIA,MAAMY,6BAA6B,CAACC,UAAAA,G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