Scratch space for learning atproto app development
1import type { Database } from '#/db'
2import { Firehose } from '#/firehose/firehose'
3import * as Status from '#/lexicon/types/com/example/status'
4
5export class Ingester {
6 firehose: Firehose | undefined
7 constructor(public db: Database) {}
8
9 async start() {
10 const firehose = new Firehose({})
11
12 for await (const evt of firehose.run()) {
13 if (evt.event === 'create') {
14 const record = evt.record
15 if (
16 evt.collection === 'com.example.status' &&
17 Status.isRecord(record) &&
18 Status.validateRecord(record).success
19 ) {
20 await this.db
21 .insertInto('status')
22 .values({
23 authorDid: evt.author,
24 status: record.status,
25 updatedAt: record.updatedAt,
26 indexedAt: new Date().toISOString(),
27 })
28 .onConflict((oc) => oc.doNothing())
29 .execute()
30 }
31 }
32 }
33 }
34
35 destroy() {
36 this.firehose?.destroy()
37 }
38}