{"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,+BAAAA,GAAkC,OAC7CC,EAAAA,EACAC,kBAAAA,GAAAA;AAOA,IAAA,IAAIC,WAAAA,GAAc,CAAA;AAElB,IAAA,MAAMC,QAAAA,GAAWH,EAAAA,CAAGI,QAAQ,CAACC,MAAM,EAAA;IACnC,MAAMC,OAAAA,GAAUC,KAAAA,CAAMC,IAAI,CAACL,QAAAA,CAAAA;IAE3B,IAAIG,OAAAA,CAAQG,MAAM,KAAK,CAAA,EAAG;QACxB,OAAO,CAAA;AACT,IAAA;IAEAT,EAAAA,CAAGU,MAAM,CAACC,KAAK,CAAC,8CAAA,CAAA;IAEhB,KAAK,MAAMC,SAASN,OAAAA,CAAS;AAC3B,QAAA,MAAMO,uBAAAA,GAA0BC,0BAAAA,CAA2BF,KAAAA,CAAMG,UAAU,IAAI,EAAC,CAAA;QAEhF,KAAK,MAAMC,YAAYH,uBAAAA,CAAyB;YAC9C,IAAII,YAAAA,CAAaD,QAAAA,CAAAA,IAAaE,SAAAA,CAAUF,QAAAA,CAAAA,EAAW;gBACjD,MAAMG,MAAAA,GAAS,MAAMlB,kBAAAA,CAAmBD,EAAAA,EAAIgB,SAASI,SAAS,CAACC,IAAI,EAAEL,QAAAA,EAAUJ,KAAAA,CAAAA;gBAC/EV,WAAAA,IAAeiB,MAAAA;AACjB,YAAA;AACF,QAAA;AACF,IAAA;IAEAnB,EAAAA,CAAGU,MAAM,CAACC,KAAK,CACb,CAAC,yDAAyD,EAAET,WAAAA,CAAY,SAAS,CAAC,CAAA;IAGpF,OAAOA,WAAAA;AACT;AAEA;;;IAI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