@apibara/indexer 2.0.0-beta.5 → 2.0.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ import { Cursor } from '@apibara/protocol';
2
+ import { Options, Stringifier } from 'csv-stringify';
3
+ import { b as Sink, a as SinkCursorParams, S as SinkData } from '../shared/indexer.d227f25c.cjs';
4
+
5
+ type CsvArgs = {
6
+ /**
7
+ * csv-stringy options
8
+ * @reference https://csv.js.org/stringify/options/
9
+ */
10
+ csvOptions?: Options;
11
+ /**
12
+ * filepath for your csv file
13
+ */
14
+ filepath: string;
15
+ };
16
+ type CsvSinkOptions = {
17
+ /**
18
+ * An optional column name used to store the cursor value. If specified,
19
+ * the value of this column must match the `endCursor.orderKey` for each row.
20
+ */
21
+ cursorColumn?: string;
22
+ };
23
+ type TxnParams = {
24
+ writer: {
25
+ insert: (data: SinkData[]) => void;
26
+ };
27
+ };
28
+ /**
29
+ * A sink that writes data to a CSV file.
30
+ *
31
+ * @example
32
+ *
33
+ * ```ts
34
+ * const sink = csv({
35
+ * filepath: "./data.csv",
36
+ * csvOptions: {
37
+ * header: true,
38
+ * },
39
+ * });
40
+ *
41
+ * ...
42
+ * async transform({context, endCursor}){
43
+ * const { writer } = useSink(context);
44
+ * const insertHelper = writer(endCursor);
45
+ *
46
+ * insertHelper.insert([
47
+ * { id: 1, name: "John" },
48
+ * { id: 2, name: "Jane" },
49
+ * ]);
50
+ * }
51
+ *
52
+ * ```
53
+ */
54
+ declare class CsvSink extends Sink {
55
+ private _stringifier;
56
+ private _config;
57
+ constructor(_stringifier: Stringifier, _config: CsvSinkOptions);
58
+ private write;
59
+ transaction({ cursor, endCursor, finality }: SinkCursorParams, cb: (params: TxnParams) => Promise<void>): Promise<void>;
60
+ invalidate(cursor?: Cursor): Promise<void>;
61
+ private insertToCSV;
62
+ private processCursorColumn;
63
+ }
64
+ declare const csv: (args: CsvArgs & CsvSinkOptions) => CsvSink;
65
+
66
+ export { type CsvArgs, CsvSink, type CsvSinkOptions, csv };
@@ -0,0 +1,66 @@
1
+ import { Cursor } from '@apibara/protocol';
2
+ import { Options, Stringifier } from 'csv-stringify';
3
+ import { b as Sink, a as SinkCursorParams, S as SinkData } from '../shared/indexer.d227f25c.mjs';
4
+
5
+ type CsvArgs = {
6
+ /**
7
+ * csv-stringy options
8
+ * @reference https://csv.js.org/stringify/options/
9
+ */
10
+ csvOptions?: Options;
11
+ /**
12
+ * filepath for your csv file
13
+ */
14
+ filepath: string;
15
+ };
16
+ type CsvSinkOptions = {
17
+ /**
18
+ * An optional column name used to store the cursor value. If specified,
19
+ * the value of this column must match the `endCursor.orderKey` for each row.
20
+ */
21
+ cursorColumn?: string;
22
+ };
23
+ type TxnParams = {
24
+ writer: {
25
+ insert: (data: SinkData[]) => void;
26
+ };
27
+ };
28
+ /**
29
+ * A sink that writes data to a CSV file.
30
+ *
31
+ * @example
32
+ *
33
+ * ```ts
34
+ * const sink = csv({
35
+ * filepath: "./data.csv",
36
+ * csvOptions: {
37
+ * header: true,
38
+ * },
39
+ * });
40
+ *
41
+ * ...
42
+ * async transform({context, endCursor}){
43
+ * const { writer } = useSink(context);
44
+ * const insertHelper = writer(endCursor);
45
+ *
46
+ * insertHelper.insert([
47
+ * { id: 1, name: "John" },
48
+ * { id: 2, name: "Jane" },
49
+ * ]);
50
+ * }
51
+ *
52
+ * ```
53
+ */
54
+ declare class CsvSink extends Sink {
55
+ private _stringifier;
56
+ private _config;
57
+ constructor(_stringifier: Stringifier, _config: CsvSinkOptions);
58
+ private write;
59
+ transaction({ cursor, endCursor, finality }: SinkCursorParams, cb: (params: TxnParams) => Promise<void>): Promise<void>;
60
+ invalidate(cursor?: Cursor): Promise<void>;
61
+ private insertToCSV;
62
+ private processCursorColumn;
63
+ }
64
+ declare const csv: (args: CsvArgs & CsvSinkOptions) => CsvSink;
65
+
66
+ export { type CsvArgs, CsvSink, type CsvSinkOptions, csv };
@@ -0,0 +1,66 @@
1
+ import { Cursor } from '@apibara/protocol';
2
+ import { Options, Stringifier } from 'csv-stringify';
3
+ import { b as Sink, a as SinkCursorParams, S as SinkData } from '../shared/indexer.d227f25c.js';
4
+
5
+ type CsvArgs = {
6
+ /**
7
+ * csv-stringy options
8
+ * @reference https://csv.js.org/stringify/options/
9
+ */
10
+ csvOptions?: Options;
11
+ /**
12
+ * filepath for your csv file
13
+ */
14
+ filepath: string;
15
+ };
16
+ type CsvSinkOptions = {
17
+ /**
18
+ * An optional column name used to store the cursor value. If specified,
19
+ * the value of this column must match the `endCursor.orderKey` for each row.
20
+ */
21
+ cursorColumn?: string;
22
+ };
23
+ type TxnParams = {
24
+ writer: {
25
+ insert: (data: SinkData[]) => void;
26
+ };
27
+ };
28
+ /**
29
+ * A sink that writes data to a CSV file.
30
+ *
31
+ * @example
32
+ *
33
+ * ```ts
34
+ * const sink = csv({
35
+ * filepath: "./data.csv",
36
+ * csvOptions: {
37
+ * header: true,
38
+ * },
39
+ * });
40
+ *
41
+ * ...
42
+ * async transform({context, endCursor}){
43
+ * const { writer } = useSink(context);
44
+ * const insertHelper = writer(endCursor);
45
+ *
46
+ * insertHelper.insert([
47
+ * { id: 1, name: "John" },
48
+ * { id: 2, name: "Jane" },
49
+ * ]);
50
+ * }
51
+ *
52
+ * ```
53
+ */
54
+ declare class CsvSink extends Sink {
55
+ private _stringifier;
56
+ private _config;
57
+ constructor(_stringifier: Stringifier, _config: CsvSinkOptions);
58
+ private write;
59
+ transaction({ cursor, endCursor, finality }: SinkCursorParams, cb: (params: TxnParams) => Promise<void>): Promise<void>;
60
+ invalidate(cursor?: Cursor): Promise<void>;
61
+ private insertToCSV;
62
+ private processCursorColumn;
63
+ }
64
+ declare const csv: (args: CsvArgs & CsvSinkOptions) => CsvSink;
65
+
66
+ export { type CsvArgs, CsvSink, type CsvSinkOptions, csv };
@@ -0,0 +1,78 @@
1
+ import fs from 'node:fs';
2
+ import { stringify } from 'csv-stringify';
3
+ import { S as Sink } from '../shared/indexer.93d6b2eb.mjs';
4
+ import 'consola';
5
+
6
+ const transactionHelper = (context) => {
7
+ return {
8
+ insert: (data) => {
9
+ context.buffer.push(...data);
10
+ }
11
+ };
12
+ };
13
+ class CsvSink extends Sink {
14
+ constructor(_stringifier, _config) {
15
+ super();
16
+ this._stringifier = _stringifier;
17
+ this._config = _config;
18
+ }
19
+ async write({
20
+ data,
21
+ endCursor
22
+ }) {
23
+ data = this.processCursorColumn(data, endCursor);
24
+ await this.insertToCSV(data);
25
+ }
26
+ async transaction({ cursor, endCursor, finality }, cb) {
27
+ const context = {
28
+ buffer: []
29
+ };
30
+ const writer = transactionHelper(context);
31
+ await cb({ writer });
32
+ await this.write({ data: context.buffer, endCursor });
33
+ }
34
+ async invalidate(cursor) {
35
+ throw new Error("Not implemented");
36
+ }
37
+ async insertToCSV(data) {
38
+ if (data.length === 0)
39
+ return;
40
+ return await new Promise((resolve, reject) => {
41
+ for (const row of data) {
42
+ this._stringifier.write(row, (err) => {
43
+ if (err)
44
+ throw new Error(err.message);
45
+ if (row === data[data.length - 1]) {
46
+ resolve();
47
+ }
48
+ });
49
+ }
50
+ });
51
+ }
52
+ processCursorColumn(data, endCursor) {
53
+ const { cursorColumn } = this._config;
54
+ if (cursorColumn && data.some(
55
+ (row) => Number(row[cursorColumn]) !== Number(endCursor?.orderKey)
56
+ )) {
57
+ throw new Error(
58
+ `Mismatch of ${cursorColumn} and Cursor ${Number(endCursor?.orderKey)}`
59
+ );
60
+ }
61
+ if (cursorColumn) {
62
+ return data;
63
+ }
64
+ return data.map((row) => ({
65
+ ...row,
66
+ _cursor: Number(endCursor?.orderKey)
67
+ }));
68
+ }
69
+ }
70
+ const csv = (args) => {
71
+ const { csvOptions, filepath, ...sinkOptions } = args;
72
+ const stringifier = stringify({ ...csvOptions });
73
+ const writeStream = fs.createWriteStream(filepath, { flags: "a" });
74
+ stringifier.pipe(writeStream);
75
+ return new CsvSink(stringifier, sinkOptions);
76
+ };
77
+
78
+ export { CsvSink, csv };
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ const sink = require('../shared/indexer.a8b7ab1f.cjs');
4
+ require('consola');
5
+
6
+ var __defProp = Object.defineProperty;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __publicField = (obj, key, value) => {
9
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
10
+ return value;
11
+ };
12
+ const transactionHelper = (context) => {
13
+ return {
14
+ insert: (data) => {
15
+ context.buffer.push(...data);
16
+ }
17
+ };
18
+ };
19
+ class SqliteSink extends sink.Sink {
20
+ constructor(options) {
21
+ super();
22
+ __publicField(this, "_config");
23
+ __publicField(this, "_db");
24
+ const { database, ...config } = options;
25
+ this._config = config;
26
+ this._db = database;
27
+ }
28
+ async write({
29
+ data,
30
+ endCursor
31
+ }) {
32
+ data = this.processCursorColumn(data, endCursor);
33
+ await this.insertJsonArray(data);
34
+ }
35
+ async transaction({ cursor, endCursor, finality }, cb) {
36
+ const context = {
37
+ buffer: []
38
+ };
39
+ const writer = transactionHelper(context);
40
+ await cb({ writer });
41
+ await this.write({ data: context.buffer, endCursor });
42
+ }
43
+ async invalidate(cursor) {
44
+ throw new Error("Not implemented");
45
+ }
46
+ async insertJsonArray(data) {
47
+ if (data.length === 0)
48
+ return;
49
+ const columns = Object.keys(data[0]);
50
+ const columnNames = columns.join(", ");
51
+ const placeholders = columns.map(() => "?").join(", ");
52
+ const conflictClause = this.buildConflictClause();
53
+ const insertSQL = `INSERT INTO ${this._config.tableName} (${columnNames}) VALUES `;
54
+ const valuePlaceholders = data.map(() => `(${placeholders})`).join(", ");
55
+ const statement = insertSQL + valuePlaceholders + conflictClause;
56
+ const values = data.flatMap((row) => columns.map((col) => row[col]));
57
+ this._db.prepare(statement).run(values);
58
+ }
59
+ processCursorColumn(data, endCursor) {
60
+ const { cursorColumn } = this._config;
61
+ if (cursorColumn && data.some(
62
+ (row) => Number(row[cursorColumn]) !== Number(endCursor?.orderKey)
63
+ )) {
64
+ throw new Error(
65
+ `Mismatch of ${cursorColumn} and Cursor ${Number(endCursor?.orderKey)}`
66
+ );
67
+ }
68
+ if (cursorColumn) {
69
+ return data;
70
+ }
71
+ return data.map((row) => ({
72
+ ...row,
73
+ _cursor: Number(endCursor?.orderKey)
74
+ }));
75
+ }
76
+ buildConflictClause() {
77
+ const { on, update } = this._config.onConflict || {};
78
+ if (on && update && update.length > 0) {
79
+ const updateColumns = update.map((col) => `${col}=excluded.${col}`).join(", ");
80
+ return ` ON CONFLICT(${on}) DO UPDATE SET ${updateColumns}`;
81
+ }
82
+ return "";
83
+ }
84
+ }
85
+ const sqlite = (args) => {
86
+ return new SqliteSink(args);
87
+ };
88
+
89
+ exports.SqliteSink = SqliteSink;
90
+ exports.sqlite = sqlite;
@@ -0,0 +1,71 @@
1
+ import { Cursor } from '@apibara/protocol';
2
+ import { Database } from 'better-sqlite3';
3
+ import { b as Sink, a as SinkCursorParams, S as SinkData } from '../shared/indexer.d227f25c.cjs';
4
+
5
+ type SqliteSinkOptions = {
6
+ /**
7
+ * Database instance of better-sqlite3
8
+ */
9
+ database: Database;
10
+ /**
11
+ * The name of the table where data will be inserted.
12
+ */
13
+ tableName: string;
14
+ /**
15
+ * An optional column name used to store the cursor value. If specified,
16
+ * the value of this column must match the `endCursor.orderKey` for each row.
17
+ */
18
+ cursorColumn?: string;
19
+ /**
20
+ * An optional configuration to handle conflicts during data insertion.
21
+ * - `on`: The column name on which conflicts are detected.
22
+ * - `update`: An array of column names to be updated if a conflict occurs.
23
+ */
24
+ onConflict?: {
25
+ on: string;
26
+ update: string[];
27
+ };
28
+ };
29
+ type TxnParams = {
30
+ writer: {
31
+ insert: (data: SinkData[]) => void;
32
+ };
33
+ };
34
+ /**
35
+ * A sink that writes data to a SQLite database.
36
+ *
37
+ * @example
38
+ *
39
+ * ```ts
40
+ * const sink = sqlite({
41
+ * database: db,
42
+ * tableName: "test",
43
+ * });
44
+ *
45
+ * ...
46
+ * async transform({context, endCursor}){
47
+ * const { writer } = useSink(context);
48
+ * const insertHelper = writer(endCursor);
49
+ *
50
+ * insertHelper.insert([
51
+ * { id: 1, name: "John" },
52
+ * { id: 2, name: "Jane" },
53
+ * ]);
54
+ * }
55
+ *
56
+ * ```
57
+ */
58
+ declare class SqliteSink extends Sink {
59
+ private _config;
60
+ private _db;
61
+ constructor(options: SqliteSinkOptions);
62
+ private write;
63
+ transaction({ cursor, endCursor, finality }: SinkCursorParams, cb: (params: TxnParams) => Promise<void>): Promise<void>;
64
+ invalidate(cursor?: Cursor): Promise<void>;
65
+ private insertJsonArray;
66
+ private processCursorColumn;
67
+ private buildConflictClause;
68
+ }
69
+ declare const sqlite: (args: SqliteSinkOptions) => SqliteSink;
70
+
71
+ export { SqliteSink, type SqliteSinkOptions, sqlite };
@@ -0,0 +1,71 @@
1
+ import { Cursor } from '@apibara/protocol';
2
+ import { Database } from 'better-sqlite3';
3
+ import { b as Sink, a as SinkCursorParams, S as SinkData } from '../shared/indexer.d227f25c.mjs';
4
+
5
+ type SqliteSinkOptions = {
6
+ /**
7
+ * Database instance of better-sqlite3
8
+ */
9
+ database: Database;
10
+ /**
11
+ * The name of the table where data will be inserted.
12
+ */
13
+ tableName: string;
14
+ /**
15
+ * An optional column name used to store the cursor value. If specified,
16
+ * the value of this column must match the `endCursor.orderKey` for each row.
17
+ */
18
+ cursorColumn?: string;
19
+ /**
20
+ * An optional configuration to handle conflicts during data insertion.
21
+ * - `on`: The column name on which conflicts are detected.
22
+ * - `update`: An array of column names to be updated if a conflict occurs.
23
+ */
24
+ onConflict?: {
25
+ on: string;
26
+ update: string[];
27
+ };
28
+ };
29
+ type TxnParams = {
30
+ writer: {
31
+ insert: (data: SinkData[]) => void;
32
+ };
33
+ };
34
+ /**
35
+ * A sink that writes data to a SQLite database.
36
+ *
37
+ * @example
38
+ *
39
+ * ```ts
40
+ * const sink = sqlite({
41
+ * database: db,
42
+ * tableName: "test",
43
+ * });
44
+ *
45
+ * ...
46
+ * async transform({context, endCursor}){
47
+ * const { writer } = useSink(context);
48
+ * const insertHelper = writer(endCursor);
49
+ *
50
+ * insertHelper.insert([
51
+ * { id: 1, name: "John" },
52
+ * { id: 2, name: "Jane" },
53
+ * ]);
54
+ * }
55
+ *
56
+ * ```
57
+ */
58
+ declare class SqliteSink extends Sink {
59
+ private _config;
60
+ private _db;
61
+ constructor(options: SqliteSinkOptions);
62
+ private write;
63
+ transaction({ cursor, endCursor, finality }: SinkCursorParams, cb: (params: TxnParams) => Promise<void>): Promise<void>;
64
+ invalidate(cursor?: Cursor): Promise<void>;
65
+ private insertJsonArray;
66
+ private processCursorColumn;
67
+ private buildConflictClause;
68
+ }
69
+ declare const sqlite: (args: SqliteSinkOptions) => SqliteSink;
70
+
71
+ export { SqliteSink, type SqliteSinkOptions, sqlite };
@@ -0,0 +1,71 @@
1
+ import { Cursor } from '@apibara/protocol';
2
+ import { Database } from 'better-sqlite3';
3
+ import { b as Sink, a as SinkCursorParams, S as SinkData } from '../shared/indexer.d227f25c.js';
4
+
5
+ type SqliteSinkOptions = {
6
+ /**
7
+ * Database instance of better-sqlite3
8
+ */
9
+ database: Database;
10
+ /**
11
+ * The name of the table where data will be inserted.
12
+ */
13
+ tableName: string;
14
+ /**
15
+ * An optional column name used to store the cursor value. If specified,
16
+ * the value of this column must match the `endCursor.orderKey` for each row.
17
+ */
18
+ cursorColumn?: string;
19
+ /**
20
+ * An optional configuration to handle conflicts during data insertion.
21
+ * - `on`: The column name on which conflicts are detected.
22
+ * - `update`: An array of column names to be updated if a conflict occurs.
23
+ */
24
+ onConflict?: {
25
+ on: string;
26
+ update: string[];
27
+ };
28
+ };
29
+ type TxnParams = {
30
+ writer: {
31
+ insert: (data: SinkData[]) => void;
32
+ };
33
+ };
34
+ /**
35
+ * A sink that writes data to a SQLite database.
36
+ *
37
+ * @example
38
+ *
39
+ * ```ts
40
+ * const sink = sqlite({
41
+ * database: db,
42
+ * tableName: "test",
43
+ * });
44
+ *
45
+ * ...
46
+ * async transform({context, endCursor}){
47
+ * const { writer } = useSink(context);
48
+ * const insertHelper = writer(endCursor);
49
+ *
50
+ * insertHelper.insert([
51
+ * { id: 1, name: "John" },
52
+ * { id: 2, name: "Jane" },
53
+ * ]);
54
+ * }
55
+ *
56
+ * ```
57
+ */
58
+ declare class SqliteSink extends Sink {
59
+ private _config;
60
+ private _db;
61
+ constructor(options: SqliteSinkOptions);
62
+ private write;
63
+ transaction({ cursor, endCursor, finality }: SinkCursorParams, cb: (params: TxnParams) => Promise<void>): Promise<void>;
64
+ invalidate(cursor?: Cursor): Promise<void>;
65
+ private insertJsonArray;
66
+ private processCursorColumn;
67
+ private buildConflictClause;
68
+ }
69
+ declare const sqlite: (args: SqliteSinkOptions) => SqliteSink;
70
+
71
+ export { SqliteSink, type SqliteSinkOptions, sqlite };
@@ -0,0 +1,87 @@
1
+ import { S as Sink } from '../shared/indexer.93d6b2eb.mjs';
2
+ import 'consola';
3
+
4
+ var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => {
7
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
+ return value;
9
+ };
10
+ const transactionHelper = (context) => {
11
+ return {
12
+ insert: (data) => {
13
+ context.buffer.push(...data);
14
+ }
15
+ };
16
+ };
17
+ class SqliteSink extends Sink {
18
+ constructor(options) {
19
+ super();
20
+ __publicField(this, "_config");
21
+ __publicField(this, "_db");
22
+ const { database, ...config } = options;
23
+ this._config = config;
24
+ this._db = database;
25
+ }
26
+ async write({
27
+ data,
28
+ endCursor
29
+ }) {
30
+ data = this.processCursorColumn(data, endCursor);
31
+ await this.insertJsonArray(data);
32
+ }
33
+ async transaction({ cursor, endCursor, finality }, cb) {
34
+ const context = {
35
+ buffer: []
36
+ };
37
+ const writer = transactionHelper(context);
38
+ await cb({ writer });
39
+ await this.write({ data: context.buffer, endCursor });
40
+ }
41
+ async invalidate(cursor) {
42
+ throw new Error("Not implemented");
43
+ }
44
+ async insertJsonArray(data) {
45
+ if (data.length === 0)
46
+ return;
47
+ const columns = Object.keys(data[0]);
48
+ const columnNames = columns.join(", ");
49
+ const placeholders = columns.map(() => "?").join(", ");
50
+ const conflictClause = this.buildConflictClause();
51
+ const insertSQL = `INSERT INTO ${this._config.tableName} (${columnNames}) VALUES `;
52
+ const valuePlaceholders = data.map(() => `(${placeholders})`).join(", ");
53
+ const statement = insertSQL + valuePlaceholders + conflictClause;
54
+ const values = data.flatMap((row) => columns.map((col) => row[col]));
55
+ this._db.prepare(statement).run(values);
56
+ }
57
+ processCursorColumn(data, endCursor) {
58
+ const { cursorColumn } = this._config;
59
+ if (cursorColumn && data.some(
60
+ (row) => Number(row[cursorColumn]) !== Number(endCursor?.orderKey)
61
+ )) {
62
+ throw new Error(
63
+ `Mismatch of ${cursorColumn} and Cursor ${Number(endCursor?.orderKey)}`
64
+ );
65
+ }
66
+ if (cursorColumn) {
67
+ return data;
68
+ }
69
+ return data.map((row) => ({
70
+ ...row,
71
+ _cursor: Number(endCursor?.orderKey)
72
+ }));
73
+ }
74
+ buildConflictClause() {
75
+ const { on, update } = this._config.onConflict || {};
76
+ if (on && update && update.length > 0) {
77
+ const updateColumns = update.map((col) => `${col}=excluded.${col}`).join(", ");
78
+ return ` ON CONFLICT(${on}) DO UPDATE SET ${updateColumns}`;
79
+ }
80
+ return "";
81
+ }
82
+ }
83
+ const sqlite = (args) => {
84
+ return new SqliteSink(args);
85
+ };
86
+
87
+ export { SqliteSink, sqlite };