SPB Git

spb/rareindex

Public
1.2 KB · 31 lines typescript
1import { newId } from '@rareindex/shared';2import { events } from '@rareindex/database';3import { db } from './db.ts';45export type EventType =6  | 'page_discovered'7  | 'page_crawled'8  | 'listing_created'9  | 'listing_updated'10  | 'sale_detected'11  | 'entity_matched'12  | 'entity_created'13  | 'valuation_updated'14  | 'index_updated'15  | 'connector_run_finished'16  | 'radar_finding';1718/** Append domain events (§140). Buffered per call site; flush in batches to keep inserts cheap. */19export async function emit(type: EventType, entity: { type?: string; id?: string } = {}, payload: Record<string, unknown> = {}): Promise<void> {20  await db().insert(events).values({ id: newId('event'), type, entityType: entity.type ?? null, entityId: entity.id ?? null, payload });21}2223export async function emitMany(rows: Array<{ type: EventType; entityType?: string; entityId?: string; payload?: Record<string, unknown> }>): Promise<void> {24  if (rows.length === 0) return;25  for (let i = 0; i < rows.length; i += 500) {26    await db()27      .insert(events)28      .values(rows.slice(i, i + 500).map((r) => ({ id: newId('event'), type: r.type, entityType: r.entityType ?? null, entityId: r.entityId ?? null, payload: r.payload ?? {} })));29  }30}31
Files · 78cf9f7f3b34154bdac4f0af831574da262502f2
Menu

Theme