24 lines
608 B
TypeScript
24 lines
608 B
TypeScript
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
|
|
/**
|
|
* The actor for the current request, used by the Prisma audit extension to
|
|
* attribute database mutations. Populated in `hooks.server.ts`.
|
|
*/
|
|
export type AuditContext = {
|
|
userId: string | null;
|
|
email: string | null;
|
|
roleId: number | null;
|
|
page: string;
|
|
ip: string | null;
|
|
};
|
|
|
|
const storage = new AsyncLocalStorage<AuditContext>();
|
|
|
|
export function runWithAuditContext<T>(context: AuditContext, fn: () => T): T {
|
|
return storage.run(context, fn);
|
|
}
|
|
|
|
export function getAuditContext(): AuditContext | undefined {
|
|
return storage.getStore();
|
|
}
|