-
Notifications
You must be signed in to change notification settings - Fork 231
feat: Export events to JSON Lines #451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8cffb6
feat: add CLI script to export events to JSON Lines
Anshumancanrock 1d9b9a8
chore: add export npm script and ignore .jsonl output
Anshumancanrock 8ed6a89
feat: add streaming export of events to JSON Lines
Anshumancanrock 43f2eb1
fix: polish export-events review feedback
Anshumancanrock 9281d14
Merge branch 'main' into export-events
Anshumancanrock b011a66
Merge branch 'main' into export-events
Anshumancanrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import 'pg-query-stream' | ||
| import dotenv from 'dotenv' | ||
| dotenv.config() | ||
|
|
||
| import fs from 'fs' | ||
| import path from 'path' | ||
| import { pipeline } from 'stream/promises' | ||
| import { Transform } from 'stream' | ||
|
|
||
| import { getMasterDbClient } from '../database/client' | ||
|
|
||
| type EventRow = { | ||
| event_id: Buffer | ||
| event_pubkey: Buffer | ||
| event_kind: number | ||
| event_created_at: number | ||
| event_content: string | ||
| event_tags: unknown[] | null | ||
| event_signature: Buffer | ||
| } | ||
|
|
||
| async function exportEvents(): Promise<void> { | ||
| const filename = process.argv[2] || 'events.jsonl' | ||
| const outputPath = path.resolve(filename) | ||
| const db = getMasterDbClient() | ||
| const abortController = new AbortController() | ||
| let interruptedBySignal: NodeJS.Signals | undefined | ||
|
|
||
| const onSignal = (signal: NodeJS.Signals) => { | ||
| if (abortController.signal.aborted) { | ||
| return | ||
| } | ||
|
|
||
| interruptedBySignal = signal | ||
| process.exitCode = 130 | ||
| console.log(`${signal} received. Stopping export...`) | ||
| abortController.abort() | ||
| } | ||
|
|
||
| process | ||
| .on('SIGINT', onSignal) | ||
| .on('SIGTERM', onSignal) | ||
|
|
||
| try { | ||
| const firstEvent = await db('events') | ||
| .select('event_id') | ||
| .whereNull('deleted_at') | ||
| .first() | ||
|
|
||
| if (abortController.signal.aborted) { | ||
| return | ||
| } | ||
|
|
||
| if (!firstEvent) { | ||
| console.log('No events to export.') | ||
| return | ||
| } | ||
|
|
||
| console.log(`Exporting events to ${outputPath}`) | ||
|
|
||
| const output = fs.createWriteStream(outputPath) | ||
| let exported = 0 | ||
|
|
||
| const dbStream = db('events') | ||
| .select( | ||
| 'event_id', | ||
| 'event_pubkey', | ||
| 'event_kind', | ||
| 'event_created_at', | ||
| 'event_content', | ||
| 'event_tags', | ||
| 'event_signature', | ||
| ) | ||
| .whereNull('deleted_at') | ||
| .orderBy('event_created_at', 'asc') | ||
| .orderBy('event_id', 'asc') | ||
| .stream() | ||
|
|
||
| const toJsonLine = new Transform({ | ||
| objectMode: true, | ||
| transform(row: EventRow, _encoding, callback) { | ||
| const event = { | ||
| id: row.event_id.toString('hex'), | ||
| pubkey: row.event_pubkey.toString('hex'), | ||
| created_at: row.event_created_at, | ||
| kind: row.event_kind, | ||
| tags: Array.isArray(row.event_tags) ? row.event_tags : [], | ||
| content: row.event_content, | ||
| sig: row.event_signature.toString('hex'), | ||
| } | ||
|
|
||
| exported++ | ||
| if (exported % 10000 === 0) { | ||
| console.log(`Exported ${exported} events...`) | ||
| } | ||
|
|
||
| callback(null, JSON.stringify(event) + '\n') | ||
| }, | ||
| }) | ||
|
|
||
| await pipeline(dbStream, toJsonLine, output, { | ||
| signal: abortController.signal, | ||
| }) | ||
|
|
||
| console.log(`Export complete: ${exported} events written to ${outputPath}`) | ||
| } catch (error) { | ||
| if (abortController.signal.aborted) { | ||
| console.log(`Export interrupted by ${interruptedBySignal ?? 'signal'}.`) | ||
| process.exitCode = 130 | ||
| return | ||
| } | ||
|
|
||
| throw error | ||
| } finally { | ||
| process | ||
| .off('SIGINT', onSignal) | ||
| .off('SIGTERM', onSignal) | ||
|
|
||
| await db.destroy() | ||
| } | ||
| } | ||
|
|
||
| exportEvents().catch((error) => { | ||
| console.error('Export failed:', error.message) | ||
|
cameri marked this conversation as resolved.
|
||
| process.exit(1) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR description mentions cleaning up the DB connection “on exit”, but the script doesn’t currently trap
SIGINT/SIGTERM. If the process is interrupted mid-export, the transaction/stream and file descriptor may not be closed cleanly. Consider adding signal handlers to destroy the db stream, close the output stream, anddb.destroy()before exiting.