core: fix: upsert each user when setting roles in bulk (#1224)

* fix(domain): upsert each user when setting roles in bulk

Use per-UID bulk upserts for batch role assignment and skip empty batches while preserving cache invalidation and autojoin behavior.

Fixes hydro-dev/Hydro#1223

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* perf(domain): deduplicate UIDs before bulk role updates

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
AntaresandCopilot authored and GitHub committed 2026-10-14 09:09:29 +00:00
1 parent 52933cef13
commit 851e1672e7
1 file changed
+8 -1
+8 -1
View File
@@ -160,11 +160,18 @@ class DomainModel {
deleteUserCache(udoc);
return res;
}
if (!uid.length) return undefined;
const affected = await UserModel.getMulti({ _id: { $in: uid } })
.project<{ _id: number, mail: string, uname: string }>({ mail: 1, uname: 1 })
.toArray();
for (const udoc of affected) deleteUserCache(udoc);
return await collUser.updateMany({ domainId, uid: { $in: uid } }, update, { upsert: true });
return await collUser.bulkWrite([...new Set(uid)].map((id) => ({
updateOne: {
filter: { domainId, uid: id },
update,
upsert: true,
},
})));
}
static async setJoin(domainId: string, uid: MaybeArray<number>, join: boolean) {