From 7de882bcf1fd8bed374631d6bfb3c6e1a4233a12 Mon Sep 17 00:00:00 2001 From: serge watchou Date: Thu, 21 Nov 2024 22:29:02 +0100 Subject: [PATCH] git commit -m "Fixes issue with dedupe function. See details: https://github.com/imjuni/erdia/issues/77" --- src/modules/commands/building.ts | 4 ++-- .../__tests__/dedupe.relation.test.ts | 4 ++-- ...d.ts => dedupeManyToManyRelationRecord.ts} | 20 ++++++++++--------- 3 files changed, 15 insertions(+), 13 deletions(-) rename src/typeorm/relations/{dedupeManaToManyRelationRecord.ts => dedupeManyToManyRelationRecord.ts} (69%) diff --git a/src/modules/commands/building.ts b/src/modules/commands/building.ts index 0add62b..aef9b13 100644 --- a/src/modules/commands/building.ts +++ b/src/modules/commands/building.ts @@ -31,7 +31,7 @@ import { getColumnRecord } from '#/typeorm/columns/getColumnRecord'; import { getEntityRecords } from '#/typeorm/entities/getEntityRecords'; import { getDataSource } from '#/typeorm/getDataSource'; import { getIndexRecords } from '#/typeorm/indices/getIndexRecords'; -import { dedupeManaToManyRelationRecord } from '#/typeorm/relations/dedupeManaToManyRelationRecord'; +import { dedupeManyToManyRelationRecord } from '#/typeorm/relations/dedupeManyToManyRelationRecord'; import { getRelationRecords } from '#/typeorm/relations/getRelationRecords'; import { asValue } from 'awilix'; import chalk from 'chalk'; @@ -89,7 +89,7 @@ export async function building(option: SetOptional relationRecord.pass) .flat(); - const dedupedRelations = dedupeManaToManyRelationRecord(passRelations); + const dedupedRelations = dedupeManyToManyRelationRecord(passRelations); const records = [...entities, ...columns, ...dedupedRelations, ...indicesRecords]; logger.success('complete extraction'); diff --git a/src/typeorm/relations/__tests__/dedupe.relation.test.ts b/src/typeorm/relations/__tests__/dedupe.relation.test.ts index b094ad7..a70553f 100644 --- a/src/typeorm/relations/__tests__/dedupe.relation.test.ts +++ b/src/typeorm/relations/__tests__/dedupe.relation.test.ts @@ -1,6 +1,6 @@ import { CE_CHANGE_KIND } from '#/databases/const-enum/CE_CHANGE_KIND'; import type { IRelationRecord } from '#/databases/interfaces/IRelationRecord'; -import { dedupeManaToManyRelationRecord } from '#/typeorm/relations/dedupeManaToManyRelationRecord'; +import { dedupeManyToManyRelationRecord } from '#/typeorm/relations/dedupeManyToManyRelationRecord'; import fastSafeStringify from 'fast-safe-stringify'; import { parse } from 'jsonc-parser'; import fs from 'node:fs'; @@ -192,7 +192,7 @@ const relations: IRelationRecord[] = [ describe('dedupeManaToManyRelationRecord', () => { test('dedupe relations', async () => { const expectFileName = 'expect-07.json'; - const deduped = dedupeManaToManyRelationRecord(relations); + const deduped = dedupeManyToManyRelationRecord(relations); if (share.expect) { fs.writeFileSync(pathe.join(__dirname, 'expects', `${expectFileName}`), fastSafeStringify(deduped, undefined, 2)); diff --git a/src/typeorm/relations/dedupeManaToManyRelationRecord.ts b/src/typeorm/relations/dedupeManyToManyRelationRecord.ts similarity index 69% rename from src/typeorm/relations/dedupeManaToManyRelationRecord.ts rename to src/typeorm/relations/dedupeManyToManyRelationRecord.ts index a146788..ad30246 100644 --- a/src/typeorm/relations/dedupeManaToManyRelationRecord.ts +++ b/src/typeorm/relations/dedupeManyToManyRelationRecord.ts @@ -1,7 +1,7 @@ import type { IRelationRecord } from '#/databases/interfaces/IRelationRecord'; import { atOrThrow } from 'my-easy-fp'; -export function dedupeManaToManyRelationRecord(relations: IRelationRecord[]) { +export function dedupeManyToManyRelationRecord(relations: IRelationRecord[]) { const otherRelations = relations.filter((relation) => relation.relationType !== 'many-to-many'); const manyToManyRelations = relations.filter((relation) => relation.relationType === 'many-to-many'); @@ -21,16 +21,18 @@ export function dedupeManaToManyRelationRecord(relations: IRelationRecord[]) { 0, new Error(`Cannot found relation: ${sortedRelations.at(0)?.entity} - ${sortedRelations.at(1)?.entity}`), ); - const secondRelation = atOrThrow( - sortedRelations, - 1, - new Error(`Cannot found relation: ${sortedRelations.at(0)?.entity} - ${sortedRelations.at(1)?.entity}`), - ); - const firstNext = { ...firstRelation, inverseJoinColumnName: secondRelation.joinColumnName }; - const secondNext = { ...secondRelation, inverseJoinColumnName: firstRelation.joinColumnName, isDuplicate: true }; + const secondRelation = sortedRelations.length > 1 ? sortedRelations[1] : null; + + if (secondRelation) { + const firstNext = { ...firstRelation, inverseJoinColumnName: secondRelation.joinColumnName }; + const secondNext = { ...secondRelation, inverseJoinColumnName: firstRelation.joinColumnName, isDuplicate: true }; + + return [firstNext, secondNext]; + } - return [firstNext, secondNext]; + const firstNext = { ...firstRelation }; + return [firstNext]; }); return [...otherRelations, ...nextRelations.flat()];