From 2b36f36f0c805949e2e09ee85e9707b5d63613d9 Mon Sep 17 00:00:00 2001 From: A-Chronicle Date: Mon, 25 May 2026 20:44:59 +0530 Subject: [PATCH] perf(pluto): batch-fetch keys in getAllPrismDIDs to eliminate N+1 queries Signed-off-by: A-Chronicle --- packages/lib/sdk/src/pluto/Pluto.ts | 40 ++++++++++++----------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/packages/lib/sdk/src/pluto/Pluto.ts b/packages/lib/sdk/src/pluto/Pluto.ts index 012ad2889..4ad3bb370 100644 --- a/packages/lib/sdk/src/pluto/Pluto.ts +++ b/packages/lib/sdk/src/pluto/Pluto.ts @@ -483,30 +483,24 @@ export class Pluto extends Domain.Startable.Controller implements Domain.Pluto { * @returns Array of {@link Domain.PrismDID} instances. */ async getAllPrismDIDs(): Promise { - const dids = await this.Repositories.DIDs.find({ method: "prism" }); - const prismDIDS: Domain.PrismDID[] = []; - for (const did of dids) { - const dbDids = await this.getPrismDIDS(did.uuid); - for (const prismDID of dbDids) { - prismDIDS.push(prismDID); - } - } - return prismDIDS; - } + const allDids = await this.Repositories.DIDs.find({ method: "prism" }); + const allLinks = await this.Repositories.DIDKeyLinks.getModels({ + selector: { $or: allDids.map(x => ({ didId: x.uuid })) } + }); + const allKeys = await this.Repositories.Keys.get({ + selector: { $or: allLinks.map(x => ({ uuid: x.keyId })) } + }); - private async getPrismDIDS(didId: string) { - const links = await this.Repositories.DIDKeyLinks.getModels({ selector: { didId } }); - return Promise.all( - links.map(async (link) => { - const did = await this.Repositories.DIDs.byUUID(link.didId); - const key = await this.Repositories.Keys.byUUID(link.keyId); - if (!did || !key) { - throw new Error("PrismDID not found"); - } - const prismDID = new Domain.PrismDID(did, key, link.alias); - return prismDID; - }) - ); + return allDids.flatMap(did => { + const links = allLinks.filter(x => x.didId === did.uuid); + return links + .map(link => { + const key = allKeys.find(x => x.uuid === link.keyId); + if (!key) return null; + return new Domain.PrismDID(did, key, link.alias); + }) + .filter((x): x is Domain.PrismDID => x !== null); + }); }