Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue with dedupe function #79

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading