Database Access optimisations (#14544)

* Optimize database access during spell casting

* load less data when casting spells

* Begin migrating update calls to updateOne and updateMany

* Only update user objects that don’t have notification yet

* fix test

* fix spy

* Don’t unnecessarily update user when requesting invalid guild

* fix sort order for middlewares to not load user twice every request

* fix tests

* fix integration test

* fix skill usage not always deducting mp

* addtest case for blessing spell

* fix healAll

* fix lint

* Fix error for when some spells are used outside of party

* Add check to not run bulk spells in web client

* fix(tags): change const to let

---------

Co-authored-by: SabreCat <sabe@habitica.com>
This commit is contained in:
Phillip Thelen
2023-05-16 19:21:45 +02:00
committed by GitHub
parent f0637dcf49
commit 8150fef993
20 changed files with 223 additions and 133 deletions

View File

@@ -93,7 +93,7 @@ api.inviteToQuest = {
user.party.quest.RSVPNeeded = false;
user.party.quest.key = questKey;
await User.update({
await User.updateMany({
'party._id': group._id,
_id: { $ne: user._id },
}, {
@@ -101,7 +101,7 @@ api.inviteToQuest = {
'party.quest.RSVPNeeded': true,
'party.quest.key': questKey,
},
}, { multi: true }).exec();
}).exec();
_.each(members, member => {
group.quest.members[member._id] = null;
@@ -409,10 +409,9 @@ api.cancelQuest = {
const [savedGroup] = await Promise.all([
group.save(),
newChatMessage.save(),
User.update(
User.updateMany(
{ 'party._id': groupId },
Group.cleanQuestParty(),
{ multi: true },
).exec(),
]);
@@ -467,12 +466,11 @@ api.abortQuest = {
});
await newChatMessage.save();
const memberUpdates = User.update({
const memberUpdates = User.updateMany({
'party._id': groupId,
}, Group.cleanQuestParty(),
{ multi: true }).exec();
}, Group.cleanQuestParty()).exec();
const questLeaderUpdate = User.update({
const questLeaderUpdate = User.updateOne({
_id: group.quest.leader,
}, {
$inc: {

View File

@@ -227,7 +227,7 @@ api.deleteTag = {
const tagFound = find(user.tags, tag => tag.id === req.params.tagId);
if (!tagFound) throw new NotFound(res.t('tagNotFound'));
await user.update({
await user.updateOne({
$pull: { tags: { id: tagFound.id } },
}).exec();
@@ -237,13 +237,13 @@ api.deleteTag = {
user._v += 1;
// Remove from all the tasks TODO test
await Tasks.Task.update({
await Tasks.Task.updateMany({
userId: user._id,
}, {
$pull: {
tags: tagFound.id,
},
}, { multi: true }).exec();
}).exec();
res.respond(200, {});
},

View File

@@ -840,7 +840,7 @@ api.moveTask = {
// Cannot send $pull and $push on same field in one single op
const pullQuery = { $pull: {} };
pullQuery.$pull[`tasksOrder.${task.type}s`] = task.id;
await owner.update(pullQuery).exec();
await owner.updateOne(pullQuery).exec();
let position = to;
if (to === -1) position = order.length - 1; // push to bottom
@@ -850,7 +850,7 @@ api.moveTask = {
$each: [task._id],
$position: position,
};
await owner.update(updateQuery).exec();
await owner.updateOne(updateQuery).exec();
// Update the user version field manually,
// it cannot be updated in the pre update hook
@@ -1434,7 +1434,7 @@ api.deleteTask = {
const pullQuery = { $pull: {} };
pullQuery.$pull[`tasksOrder.${task.type}s`] = task._id;
const taskOrderUpdate = (challenge || user).update(pullQuery).exec();
const taskOrderUpdate = (challenge || user).updateOne(pullQuery).exec();
// Update the user version field manually,
// it cannot be updated in the pre update hook