SPB Git forge

spb/rareindex

Public
54commits 1branches 0releases
7.1 MBsize
maindefault branch
10 days agolast push
TypeScript 61.9% HTML 37.2% SQL 0.7%
1.2 KB · 31 lines typescript
Raw Blame History
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