-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from pinax-network/fix/write-row
improve writeRow logic
- Loading branch information
Showing
12 changed files
with
201 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,29 @@ | ||
import { Clock } from "@substreams/core/proto" | ||
import { EntityChange } from "@substreams/sink-entity-changes/zod"; | ||
import { parseClock, parseTimestamp } from "./parseClock.js"; | ||
import { Timestamp } from "@bufbuild/protobuf"; | ||
|
||
export function applyReservedFields( values: Record<string, unknown>, entityChange: EntityChange, cursor: string, clock: Clock ) { | ||
const { block_number, block_id, timestamp, seconds, milliseconds, nanos } = parseClock(clock); | ||
|
||
// **Reserved field names** to be used to expand the schema | ||
if ( !values["id"] ) values["id"] = entityChange.id; | ||
if ( !values["operation"] ) values["operation"] = entityChange.operation; | ||
if ( !values["cursor"] ) values["cursor"] = cursor; | ||
if ( !values["block"] ) values["block"] = block_number; | ||
if ( !values["block_num"] ) values["block_num"] = block_number; | ||
if ( !values["block_number"] ) values["block_number"] = block_number; | ||
if ( !values["block_id"] ) values["block_id"] = block_id; | ||
if ( !values["seconds"] ) values["seconds"] = seconds; | ||
if ( !values["milliseconds"] ) values["milliseconds"] = milliseconds; | ||
if ( !values["millis"] ) values["millis"] = milliseconds; | ||
if ( !values["nanos"] ) values["nanos"] = nanos; | ||
if ( !values["nanoseconds"] ) values["nanoseconds"] = nanos; | ||
if ( !values["timestamp"] ) values["timestamp"] = timestamp; | ||
|
||
// exception parsing timestamp | ||
if ( values["timestamp"] ) values["timestamp"] = parseTimestamp(Timestamp.fromDate(new Date(values["timestamp"] as string))); | ||
else values["timestamp"] = timestamp; | ||
|
||
return values; | ||
} |
This file contains 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,8 @@ | ||
import { expect, test } from "bun:test"; | ||
import { parseTimestamp } from "./parseClock.js"; | ||
import { Timestamp } from "@bufbuild/protobuf"; | ||
|
||
test("parseTimestamp", () => { | ||
// expect(parseTimestamp(timestamp)).toBe("2015-07-30T15:26:57.000Z"); | ||
expect(parseTimestamp(Timestamp.fromDate(new Date(1438270017000)))).toBe("2015-07-30 15:26:57"); | ||
}) |
This file contains 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 |
---|---|---|
@@ -1,11 +1,22 @@ | ||
import { Clock } from "@substreams/core/proto"; | ||
import { Timestamp } from "@bufbuild/protobuf"; | ||
|
||
export function parseClock(clock: Clock) { | ||
if ( !clock.timestamp ) throw new Error("Clock has no timestamp"); | ||
return { | ||
block_num: Number(clock.number), | ||
block_id: clock.id, | ||
seconds: Number(clock.timestamp?.seconds), | ||
timestamp: clock.timestamp?.toDate().toISOString(), | ||
} | ||
} | ||
if ( !clock.timestamp ) throw new Error("Clock has no timestamp"); | ||
const seconds = Number(clock.timestamp?.seconds); | ||
const nanos = Number(clock.timestamp?.nanos); | ||
const milliseconds = seconds * 1000 + nanos / 1000000; | ||
|
||
return { | ||
block_number: Number(clock.number), | ||
block_id: clock.id, | ||
seconds, | ||
milliseconds, | ||
nanos, | ||
timestamp: parseTimestamp(clock.timestamp) | ||
} | ||
} | ||
|
||
export function parseTimestamp(timestamp: Timestamp) { | ||
return timestamp.toDate().toISOString().replace("T", " ").split(".")[0] | ||
} |
This file contains 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,14 @@ | ||
import { expect, test } from "bun:test"; | ||
import { formatValue } from "./writeRow.js"; | ||
|
||
test("formatValue", () => { | ||
const options = {delimiter: ","}; | ||
// expect(formatValue("a", options)).toBe("a"); | ||
expect(formatValue("'a'", options)).toBe("'a'"); | ||
expect(formatValue("foo bar", options)).toBe("foo bar"); | ||
expect(formatValue("foo \" bar", options)).toBe("foo \"\" bar"); | ||
expect(formatValue(undefined, options)).toBe(""); | ||
expect(formatValue(null, options)).toBe(""); | ||
expect(formatValue("a,b", options)).toBe("\"a,b\""); | ||
expect(formatValue("a,\"b", options)).toBe("\"a,\"\"b\""); | ||
}) |
This file contains 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,20 @@ | ||
import fs from "fs"; | ||
|
||
interface WriteRowOptions { | ||
delimiter: string; | ||
} | ||
|
||
export function writeRow(writer: fs.WriteStream, columns: any[], options: WriteRowOptions): void { | ||
columns = columns.map(value => formatValue(value, options)); | ||
writer.write(columns.join(options.delimiter) + '\n'); | ||
} | ||
|
||
export function formatValue(value: string|undefined|null, options: WriteRowOptions): string { | ||
if (value === undefined || value === null) return ""; | ||
|
||
if (typeof value == "string") { | ||
value = value.replace(/"/g, "\"\"") | ||
if ( value.includes(options.delimiter) ) value = `"${value}"`; // escape commas | ||
} | ||
return value; | ||
} |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
export const version = "0.2.9"; | ||
export const version = "0.2.10"; |