|
| 1 | +import type { |
| 2 | + Adapter, |
| 3 | + BaseEventType, |
| 4 | + InferEventFromSchema, |
| 5 | + InferStateFromSchema, |
| 6 | +} from "../types"; |
| 7 | + |
| 8 | +type BaseEventTypeRow = Omit<BaseEventType, "eventCreatedAt" | "body"> & { |
| 9 | + eventCreatedAt: Date; |
| 10 | + body: unknown; |
| 11 | +}; |
| 12 | + |
| 13 | +export type VentydAdapterOptions< |
| 14 | + $$Schema, |
| 15 | + $$PrismaEventRow extends BaseEventTypeRow, |
| 16 | + $$PrismaEventRowInput, |
| 17 | + $$PrismaSnapshotRowInput, |
| 18 | +> = { |
| 19 | + prisma: { |
| 20 | + $transaction: (commands: unknown[]) => Promise<unknown[]>; |
| 21 | + }; |
| 22 | + tables: { |
| 23 | + event: { |
| 24 | + findMany(args?: { |
| 25 | + where: { entityName: string; entityId: string }; |
| 26 | + }): Promise<$$PrismaEventRow[]>; |
| 27 | + createMany(args?: { data: $$PrismaEventRowInput[] }): Promise<unknown>; |
| 28 | + }; |
| 29 | + snapshot: { |
| 30 | + upsert(args: { |
| 31 | + where: { id?: string }; |
| 32 | + update: $$PrismaSnapshotRowInput; |
| 33 | + create: $$PrismaSnapshotRowInput; |
| 34 | + }): Promise<unknown>; |
| 35 | + }; |
| 36 | + }; |
| 37 | + entityToRow(args: { |
| 38 | + entityId: string; |
| 39 | + state: InferStateFromSchema<$$Schema>; |
| 40 | + }): $$PrismaSnapshotRowInput; |
| 41 | +}; |
| 42 | + |
| 43 | +export default function prismaAdapter< |
| 44 | + $$Schema, |
| 45 | + $$PrismaEventRow extends BaseEventTypeRow, |
| 46 | + $$PrismaEventRowInput, |
| 47 | + $$PrismaSnapshotRowInput, |
| 48 | +>( |
| 49 | + options: VentydAdapterOptions< |
| 50 | + $$Schema, |
| 51 | + $$PrismaEventRow, |
| 52 | + $$PrismaEventRowInput, |
| 53 | + $$PrismaSnapshotRowInput |
| 54 | + >, |
| 55 | +): Adapter<$$Schema> { |
| 56 | + function eventToRow(_event: InferEventFromSchema<$$Schema>) { |
| 57 | + const event = _event as BaseEventType; |
| 58 | + |
| 59 | + const row = { |
| 60 | + eventId: event.eventId, |
| 61 | + eventName: event.eventName, |
| 62 | + entityId: event.entityId, |
| 63 | + entityName: event.entityName, |
| 64 | + eventCreatedAt: new Date(event.eventCreatedAt), |
| 65 | + body: event.body, |
| 66 | + } as $$PrismaEventRowInput; |
| 67 | + |
| 68 | + return row; |
| 69 | + } |
| 70 | + |
| 71 | + function rowToEvent(row: $$PrismaEventRow) { |
| 72 | + const event = { |
| 73 | + eventId: row.eventId, |
| 74 | + eventName: row.eventName, |
| 75 | + entityId: row.entityId, |
| 76 | + entityName: row.entityName, |
| 77 | + eventCreatedAt: row.eventCreatedAt.toISOString(), |
| 78 | + body: row.body, |
| 79 | + } as InferEventFromSchema<$$Schema>; |
| 80 | + |
| 81 | + return event; |
| 82 | + } |
| 83 | + |
| 84 | + return { |
| 85 | + async getEventsByEntityId({ entityName, entityId }) { |
| 86 | + const rows = await options.tables.event.findMany({ |
| 87 | + where: { |
| 88 | + entityName, |
| 89 | + entityId, |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + return rows.map(rowToEvent); |
| 94 | + }, |
| 95 | + async commitEvents({ events, entityId, state }) { |
| 96 | + const snapshotRow = options.entityToRow({ |
| 97 | + entityId, |
| 98 | + state, |
| 99 | + }); |
| 100 | + |
| 101 | + await options.prisma.$transaction([ |
| 102 | + options.tables.event.createMany({ |
| 103 | + data: events.map(eventToRow), |
| 104 | + }), |
| 105 | + options.tables.snapshot.upsert({ |
| 106 | + where: { id: entityId }, |
| 107 | + update: snapshotRow, |
| 108 | + create: snapshotRow, |
| 109 | + }), |
| 110 | + ]); |
| 111 | + }, |
| 112 | + }; |
| 113 | +} |
0 commit comments