import { listPublicKnowledgeBaseRlsIdentities, resolvePublicKnowledgeBaseRlsIdentity } from "@hivegrid/db/rls-resolvers.server"; import { runWithIsolatedPublicKnowledgeBaseRlsContext } from "@hivegrid/db/rls-context.server"; export async function withPublicKnowledgeBaseRlsContext(slug: string, operation: () => Promise): Promise { const identity = await resolvePublicKnowledgeBaseRlsIdentity(slug); if (!identity) { return null; } return runWithIsolatedPublicKnowledgeBaseRlsContext(identity.resourceId, operation); } export async function mapPublicKnowledgeBasesWithRlsContext(operation: (knowledgeBaseId: string) => Promise): Promise[]> { const identities = await listPublicKnowledgeBaseRlsIdentities(); const values = await Promise.all(identities.map(({ resourceId }) => runWithIsolatedPublicKnowledgeBaseRlsContext(resourceId, () => operation(resourceId)))); return values.flatMap((value) => (value === null ? [] : [value])); }