Files
diachron/express/services/index.ts
Michael Wolf 55f5cc699d Add request-scoped context for session.getUser()
Use AsyncLocalStorage to provide request context so services can access
the current user without needing Call passed through every function.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-11 14:56:10 -06:00

53 lines
950 B
TypeScript

// services.ts
import { AuthService } from "../auth";
import { getCurrentUser } from "../context";
import { db, migrate, migrationStatus, PostgresAuthStore } from "../database";
import { getLogs, log } from "../logging";
import type { MaybeUser } from "../user";
const database = {
db,
migrate,
migrationStatus,
};
const logging = {
log,
getLogs,
};
const random = {
randomNumber: () => {
return Math.random();
},
};
const misc = {
sleep: (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
},
};
const session = {
getUser: (): MaybeUser => {
return getCurrentUser();
},
};
// Initialize auth with PostgreSQL store
const authStore = new PostgresAuthStore();
const auth = new AuthService(authStore);
// Keep this asciibetically sorted
const services = {
auth,
database,
logging,
misc,
random,
session,
};
export { services };