mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
Merge pull request #919 from Hexastack/536-issue---n-1-query-problem-preupdatemany-blockrepositoryts
fix(api): optimize preUpdateMany
This commit is contained in:
commit
46aaffcbc7
@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user