| 67 | } |
| 68 | |
| 69 | async function seed(): Promise<void> { |
| 70 | const { rows } = await client.query<{ count: number }>('SELECT COUNT(*)::int AS count FROM events') |
| 71 | const count = rows[0]?.count ?? 0 |
| 72 | if (count >= EVENTS) { |
| 73 | console.log(`seed: skipping (events=${count} >= target=${EVENTS})`) |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | console.log(`seed: inserting ${EVENTS - count} events across ${PUBKEYS} pubkeys…`) |
| 78 | |
| 79 | const pubkeys = Array.from({ length: PUBKEYS }, randPubkey) |
| 80 | const now = Math.floor(Date.now() / 1000) |
| 81 | const BATCH = 2000 |
| 82 | const toInsert = EVENTS - count |
| 83 | |
| 84 | await client.query('BEGIN') |
| 85 | await client.query('ALTER TABLE events DISABLE TRIGGER insert_event_tags') |
| 86 | try { |
| 87 | for (let i = 0; i < toInsert; i += BATCH) { |
| 88 | const values: string[] = [] |
| 89 | const params: unknown[] = [] |
| 90 | const size = Math.min(BATCH, toInsert - i) |
| 91 | for (let j = 0; j < size; j++) { |
| 92 | const idx = params.length |
| 93 | const pk = pubkeys[(i + j) % PUBKEYS] |
| 94 | const kind = kinds[(i + j) % kinds.length] |
| 95 | const created = now - Math.floor(Math.random() * 60 * 86400) |
| 96 | const deleted = Math.random() < 0.02 ? new Date(created * 1000) : null |
| 97 | params.push( |
| 98 | randomBytes(32), |
| 99 | pk, |
| 100 | created, |
| 101 | kind, |
| 102 | '[]', |
| 103 | '', |
| 104 | randomBytes(64), |
| 105 | null, |
| 106 | null, |
| 107 | deleted, |
| 108 | ) |
| 109 | values.push( |
| 110 | `($${idx + 1}, $${idx + 2}, $${idx + 3}, $${idx + 4}, $${idx + 5}::jsonb, $${idx + 6}, $${idx + 7}, $${idx + 8}, $${idx + 9}, $${idx + 10})`, |
| 111 | ) |
| 112 | } |
| 113 | await client.query( |
| 114 | `INSERT INTO events (event_id, event_pubkey, event_created_at, event_kind, event_tags, event_content, event_signature, event_deduplication, expires_at, deleted_at) |
| 115 | VALUES ${values.join(',')} ON CONFLICT DO NOTHING`, |
| 116 | params, |
| 117 | ) |
| 118 | if ((i / BATCH) % 10 === 0) { |
| 119 | process.stdout.write(` inserted ${i + size}/${toInsert}\r`) |
| 120 | } |
| 121 | } |
| 122 | } finally { |
| 123 | await client.query('ALTER TABLE events ENABLE TRIGGER insert_event_tags') |
| 124 | await client.query('COMMIT') |
| 125 | } |
| 126 | |