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