mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
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:
1 parent
52933cef13
commit
851e1672e7
1 file changed
+8
-1
@@ -160,11 +160,18 @@ class DomainModel {
|
|||||||
deleteUserCache(udoc);
|
deleteUserCache(udoc);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
if (!uid.length) return undefined;
|
||||||
const affected = await UserModel.getMulti({ _id: { $in: uid } })
|
const affected = await UserModel.getMulti({ _id: { $in: uid } })
|
||||||
.project<{ _id: number, mail: string, uname: string }>({ mail: 1, uname: 1 })
|
.project<{ _id: number, mail: string, uname: string }>({ mail: 1, uname: 1 })
|
||||||
.toArray();
|
.toArray();
|
||||||
for (const udoc of affected) deleteUserCache(udoc);
|
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) {
|
static async setJoin(domainId: string, uid: MaybeArray<number>, join: boolean) {
|
||||||
|
|||||||
Reference in new issue
Block a user