Files
HealthLog/src/lib/settings/operator-row.ts
T
Marc-André Bombeck f8a88d1361 perf(auth): read the operator settings once per request
An app boot's /api/auth/me read the AppSettings singleton six times, each
reader with its own few columns: the MFA policy, the Assistant switches,
the module availability, the provider presence, the AI usage bounds and
the reasoning controls. They now take their columns from one read,
memoised for the request and read afresh inside a background job like the
readers themselves. No answer changes.
2026-10-22 07:25:00 +02:00

57 lines
1.9 KiB
TypeScript

/**
* The operator's settings that every app boot reads, in one statement per
* request.
*
* `/api/auth/me` resolved six answers out of the `AppSettings` singleton (the
* MFA policy, the Assistant switches, the module availability, the provider
* presence, the AI usage bounds and the reasoning controls), each with its own
* `SELECT` of a few columns, so one client boot read the same row six times.
* Each reader now takes its columns from this one read, memoised for the
* request (and read afresh for every call inside a background job, whose one
* event spans a whole multi-user pass, like the readers it serves).
*
* A reader keeps its own error handling: a failed read rejects here, and the
* memo forgets a rejected read so the next caller tries again.
*/
import type { Prisma } from "@/generated/prisma/client";
import { prisma } from "@/lib/db";
import { memoizePerRequest } from "@/lib/request-cache";
export const OPERATOR_ROW_SELECT = {
mfaRequired: true,
assistantEnabled: true,
assistantCoachEnabled: true,
assistantBriefingEnabled: true,
assistantInsightStatusEnabled: true,
assistantDocumentAiEnabled: true,
moduleAvailabilityJson: true,
adminAiKeyEncrypted: true,
adminAiModel: true,
adminAiBaseUrl: true,
adminCodexConnectionStatus: true,
adminCodexAccessTokenEncrypted: true,
adminCodexRefreshTokenEncrypted: true,
adminCodexAccountIdEncrypted: true,
aiUsageMaxLevel: true,
aiUsageDefaultLevel: true,
aiSyncRegenGapHours: true,
aiReasoningEnabled: true,
aiReasoningMaxEffort: true,
} as const satisfies Prisma.AppSettingsSelect;
export type OperatorRow = Prisma.AppSettingsGetPayload<{
select: typeof OPERATOR_ROW_SELECT;
}>;
export function readOperatorRow(): Promise<OperatorRow | null> {
return memoizePerRequest(
"operator-settings-row",
() =>
prisma.appSettings.findUnique({
where: { id: "singleton" },
select: OPERATOR_ROW_SELECT,
}),
{ freshInBackground: true },
);
}