Skip to content

Commit 6c7d249

Browse files
committed
fix: add logging for record fetching and optimize foreign key updates in LinkService
1 parent 713add3 commit 6c7d249

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

‎apps/nestjs-backend/src/features/calculation/link.service.ts‎

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable sonarjs/cognitive-complexity */
22
/* eslint-disable sonarjs/no-duplicate-string */
3-
import { BadRequestException, Injectable } from '@nestjs/common';
3+
import { Injectable, Logger } from '@nestjs/common';
44
import type { ILinkCellValue, ILinkFieldOptions, IRecord } from '@teable/core';
55
import { FieldType, HttpErrorCode, Relationship } from '@teable/core';
66
import type { Field } from '@teable/db-main-prisma';
@@ -52,6 +52,7 @@ export interface ILinkCellContext {
5252

5353
@Injectable()
5454
export class LinkService {
55+
private logger = new Logger(LinkService.name);
5556
constructor(
5657
private readonly prismaService: PrismaService,
5758
private readonly batchService: BatchService,
@@ -826,6 +827,7 @@ export class LinkService {
826827
);
827828

828829
const nativeQuery = qb.whereIn('__id', recordIds).toQuery();
830+
this.logger.debug(`Fetch records with query: ${nativeQuery}`);
829831
const recordRaw = await this.prismaService
830832
.txClient()
831833
.$queryRawUnsafe<{ [dbTableName: string]: unknown }[]>(nativeQuery);
@@ -1361,32 +1363,26 @@ export class LinkService {
13611363
);
13621364
}
13631365
} else {
1364-
// One-many without order column implies one-way using a junction table.
1365-
// To preserve the explicit order provided by the client (e.g., typecast "id1,id2"),
1366-
// delete all existing rows for this source record and re-insert in the new order.
1367-
const oldArr = oldKey ?? [];
1368-
const newArr = newKey ?? [];
1369-
1370-
const needsReorder =
1371-
oldArr.length !== newArr.length || !oldArr.every((key, i) => key === newArr[i]);
1372-
1373-
if (needsReorder) {
1374-
// Delete all existing associations for this source record
1375-
const deleteAllQuery = this.knex(fkHostTableName)
1376-
.where(selfKeyName, recordId)
1377-
.delete()
1378-
.toQuery();
1379-
await this.prismaService.txClient().$executeRawUnsafe(deleteAllQuery);
1380-
1381-
// Re-insert in the specified order
1382-
if (newArr.length) {
1383-
const insertValues = newArr.map((foreignRecordId) => ({
1366+
// One-many without an order column stores the FK directly on the foreign table.
1367+
// Only update rows where the foreign key actually changes.
1368+
const toAdd = difference(newKey, oldKey);
1369+
1370+
if (toAdd.length > 0) {
1371+
const dbFields = [{ dbFieldName: selfKeyName, schemaType: SchemaType.String }];
1372+
1373+
const addData = toAdd.map((foreignRecordId) => ({
1374+
id: foreignRecordId,
1375+
values: {
13841376
[selfKeyName]: recordId,
1385-
[foreignKeyName]: foreignRecordId,
1386-
}));
1387-
const insertQuery = this.knex(fkHostTableName).insert(insertValues).toQuery();
1388-
await this.prismaService.txClient().$executeRawUnsafe(insertQuery);
1389-
}
1377+
},
1378+
}));
1379+
1380+
await this.batchService.batchUpdateDB(
1381+
fkHostTableName,
1382+
foreignKeyName,
1383+
dbFields,
1384+
addData
1385+
);
13901386
}
13911387
}
13921388
}

0 commit comments

Comments
 (0)