36 lines
926 B
TypeScript
36 lines
926 B
TypeScript
import { randomUUID } from "node:crypto";
|
|
import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
import * as schema from "./schema";
|
|
import { events } from "./schema";
|
|
|
|
type Database = PostgresJsDatabase<typeof schema>;
|
|
|
|
export interface RecordEventInput {
|
|
type: string;
|
|
source: string;
|
|
subject?: string;
|
|
data?: Record<string, unknown>;
|
|
actorUserId?: string;
|
|
ipAddress?: string;
|
|
correlationId?: string;
|
|
time?: Date;
|
|
}
|
|
|
|
export async function recordEvent(db: Database, input: RecordEventInput) {
|
|
const id = randomUUID();
|
|
await db.insert(events).values({
|
|
id,
|
|
specVersion: "1.0",
|
|
source: input.source,
|
|
type: input.type,
|
|
subject: input.subject,
|
|
time: input.time ?? new Date(),
|
|
dataContentType: "application/json",
|
|
data: input.data ?? {},
|
|
actorUserId: input.actorUserId,
|
|
ipAddress: input.ipAddress,
|
|
correlationId: input.correlationId,
|
|
});
|
|
return id;
|
|
}
|