Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions indexer/export-csv.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,36 @@ import { createReadStream, existsSync } from "node:fs";
const IN = process.argv[2] ?? "events.ndjson";
const COLUMNS = ["at", "ledger", "type", "split", "amount", "token", "creator", "txHash"];

if (!existsSync(IN)) {
console.error(`${IN} not found. Run the indexer first.`);
process.exit(1);
}

function cell(value) {
if (value === undefined || value === null) return "";
const s = String(value);
return /[",\n]/.test(s) ? `"${s.replaceAll('"', '""')}"` : s;
return /[,"\n]/.test(s) ? `""${s.replaceAll('"', '""')}"`" : s;
}

if (process.argv[2] === "--test") {
const fixture = [
{ at: "2023-01-01T00:00:00Z", ledger: 1, type: "issue", split: "100", amount: "1000", token: "TOKEN", creator: "alice", txHash: "abc" },
{ at: "2023-01-02T00:00:00Z", ledger: 2, type: "transfer", split: "50", amount: "500", token: "TOKEN", creator: "bob", txHash: "def" }
];
const lines = [COLUMNS.join(",")];
for (const record of fixture) {
lines.push(COLUMNS.map((c) => cell(record[c])).join(","));
}
const output = lines.join("\n");
const expected = `at,ledger,type,split,amount,token,creator,txHash
\n2023-01-01T00:00:00Z,1,issue,100,1000,TOKEN,alice,abc
\n2023-01-02T00:00:00Z,2,transfer,50,500,TOKEN,bob,def`;
if (output !== expected) {
console.error("CSV export test failed");
process.exit(1);
}
console.log("CSV export test passed");
process.exit(0);
}

if (!existsSync(IN)) {
console.error(`${IN} not found. Run the indexer first.`);
process.exit(1);
}

console.log(COLUMNS.join(","));
Expand Down
1 change: 1 addition & 0 deletions indexer/export-csv.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import{test}from'node:test';import{execFileSync}from'node:child_process';import{fileURLtoPath}from'node:url';test('csv',()=>{const o=execFileSync(process.execPath,[fileURLToPath(new URL('./export-csv.mjs',import.meta.url)),fileURLToPath(new URL('./fix.ndjson',import.meta.url))]).toString();if(o!=='at,ledger,type,split,amount,token,creator,txHash\n',,,,,,,,\n')throw Error(1)});
1 change: 1 addition & 0 deletions indexer/fix.ndjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}