Merge pull request #919 from Hexastack/536-issue---n-1-query-problem-preupdatemany-blockrepositoryts

fix(api): optimize preUpdateMany
This commit is contained in:
Yassine 2025-04-12 06:39:33 +01:00 committed by GitHub
commit 46aaffcbc7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 35 deletions

View File

@ -171,42 +171,38 @@ describe('BlockRepository', () => {
describe('prepareBlocksInCategoryUpdateScope', () => {
it('should update blocks within the scope based on category and ids', async () => {
jest.spyOn(blockRepository, 'findOne').mockResolvedValue({
id: blockValidIds[0],
category: 'oldCategory',
nextBlocks: [blockValidIds[1]],
attachedBlock: blockValidIds[1],
} as Block);
const mockUpdateOne = jest.spyOn(blockRepository, 'updateOne');
jest.spyOn(blockRepository, 'find').mockResolvedValue([
{
id: blockValidIds[0],
category: category.id,
nextBlocks: [blockValidIds[1]],
attachedBlock: blockValidIds[2],
},
] as Block[]);
jest.spyOn(blockRepository, 'updateOne');
await blockRepository.prepareBlocksInCategoryUpdateScope(
validCategory,
blockValidIds,
);
expect(mockUpdateOne).toHaveBeenCalledWith(blockValidIds[0], {
expect(blockRepository.updateOne).toHaveBeenCalledWith(blockValidIds[0], {
nextBlocks: [blockValidIds[1]],
attachedBlock: blockValidIds[1],
attachedBlock: blockValidIds[2],
});
});
it('should not update blocks if the category already matches', async () => {
jest.spyOn(blockRepository, 'findOne').mockResolvedValue({
id: validIds[0],
category: validCategory,
nextBlocks: [],
attachedBlock: null,
} as unknown as Block);
const mockUpdateOne = jest.spyOn(blockRepository, 'updateOne');
jest.spyOn(blockRepository, 'find').mockResolvedValue([]);
jest.spyOn(blockRepository, 'updateOne');
await blockRepository.prepareBlocksInCategoryUpdateScope(
validCategory,
validIds,
category.id,
blockValidIds,
);
expect(mockUpdateOne).not.toHaveBeenCalled();
expect(blockRepository.find).toHaveBeenCalled();
expect(blockRepository.updateOne).not.toHaveBeenCalled();
});
});

View File

@ -172,22 +172,24 @@ export class BlockRepository extends BaseRepository<
category: string,
ids: string[],
): Promise<void> {
for (const id of ids) {
const oldState = await this.findOne(id);
if (oldState && oldState.category !== category) {
const updatedNextBlocks = oldState.nextBlocks?.filter((nextBlock) =>
ids.includes(nextBlock),
);
const blocks = await this.find({
_id: { $in: ids },
category: { $ne: category },
});
const updatedAttachedBlock = ids.includes(oldState.attachedBlock || '')
? oldState.attachedBlock
: null;
for (const { id, nextBlocks, attachedBlock } of blocks) {
const updatedNextBlocks = nextBlocks.filter((nextBlock) =>
ids.includes(nextBlock),
);
await this.updateOne(id, {
nextBlocks: updatedNextBlocks,
attachedBlock: updatedAttachedBlock,
});
}
const updatedAttachedBlock = ids.includes(attachedBlock || '')
? attachedBlock
: null;
await this.updateOne(id, {
nextBlocks: updatedNextBlocks,
attachedBlock: updatedAttachedBlock,
});
}
}