Skip to content

Commit

Permalink
git commit -m "Fixes issue with dedupe function. See details: #77"
Browse files Browse the repository at this point in the history
  • Loading branch information
d3bug-git authored and imjuni committed Dec 20, 2024
1 parent 54d4d43 commit 7de882b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/modules/commands/building.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -89,7 +89,7 @@ export async function building(option: SetOptional<IBuildCommandOption, 'config'
.map((relationRecord) => relationRecord.pass)
.flat();

const dedupedRelations = dedupeManaToManyRelationRecord(passRelations);
const dedupedRelations = dedupeManyToManyRelationRecord(passRelations);
const records = [...entities, ...columns, ...dedupedRelations, ...indicesRecords];

logger.success('complete extraction');
Expand Down
4 changes: 2 additions & 2 deletions src/typeorm/relations/__tests__/dedupe.relation.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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()];
Expand Down

0 comments on commit 7de882b

Please sign in to comment.