@apibara/indexer 2.0.0-beta.5 → 2.0.0-beta.7
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.
- package/dist/index.cjs +50 -0
- package/dist/index.d.cts +18 -0
- package/dist/index.d.mts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.mjs +34 -0
- package/dist/plugins/index.cjs +7 -0
- package/dist/plugins/index.d.cts +4 -0
- package/dist/plugins/index.d.mts +4 -0
- package/dist/plugins/index.d.ts +4 -0
- package/dist/plugins/index.mjs +5 -0
- package/dist/plugins/kv.cjs +131 -0
- package/dist/plugins/kv.d.cts +32 -0
- package/dist/plugins/kv.d.mts +32 -0
- package/dist/plugins/kv.d.ts +32 -0
- package/dist/plugins/kv.mjs +124 -0
- package/dist/plugins/persistence.cjs +182 -0
- package/dist/plugins/persistence.d.cts +50 -0
- package/dist/plugins/persistence.d.mts +50 -0
- package/dist/plugins/persistence.d.ts +50 -0
- package/dist/plugins/persistence.mjs +179 -0
- package/dist/shared/indexer.2c23c9cd.mjs +35 -0
- package/dist/shared/indexer.318d3617.cjs +47 -0
- package/dist/shared/indexer.36530330.mjs +249 -0
- package/dist/shared/indexer.500fd281.d.cts +23 -0
- package/dist/shared/indexer.541d43eb.cjs +266 -0
- package/dist/shared/indexer.93d6b2eb.mjs +17 -0
- package/dist/shared/indexer.a8b7ab1f.cjs +25 -0
- package/dist/shared/indexer.b9c8f0d8.d.cts +19 -0
- package/dist/shared/indexer.b9c8f0d8.d.mts +19 -0
- package/dist/shared/indexer.b9c8f0d8.d.ts +19 -0
- package/dist/shared/indexer.c7ed6b83.d.cts +82 -0
- package/dist/shared/indexer.e1856641.d.mts +23 -0
- package/dist/shared/indexer.e4f2430f.d.ts +23 -0
- package/dist/shared/indexer.e8bd138d.d.mts +82 -0
- package/dist/shared/indexer.f761abcd.d.ts +82 -0
- package/dist/sinks/csv.cjs +85 -0
- package/dist/sinks/csv.d.cts +66 -0
- package/dist/sinks/csv.d.mts +66 -0
- package/dist/sinks/csv.d.ts +66 -0
- package/dist/sinks/csv.mjs +78 -0
- package/dist/sinks/drizzle/index.cjs +210 -0
- package/dist/sinks/drizzle/index.d.cts +111 -0
- package/dist/sinks/drizzle/index.d.mts +111 -0
- package/dist/sinks/drizzle/index.d.ts +111 -0
- package/dist/sinks/drizzle/index.mjs +196 -0
- package/dist/sinks/sqlite.cjs +90 -0
- package/dist/sinks/sqlite.d.cts +71 -0
- package/dist/sinks/sqlite.d.mts +71 -0
- package/dist/sinks/sqlite.d.ts +71 -0
- package/dist/sinks/sqlite.mjs +87 -0
- package/dist/testing/index.cjs +64 -0
- package/dist/testing/index.d.cts +40 -0
- package/dist/testing/index.d.mts +40 -0
- package/dist/testing/index.d.ts +40 -0
- package/dist/testing/index.mjs +60 -0
- package/dist/vcr/index.cjs +25 -0
- package/dist/vcr/index.d.cts +18 -0
- package/dist/vcr/index.d.mts +18 -0
- package/dist/vcr/index.d.ts +18 -0
- package/dist/vcr/index.mjs +14 -0
- package/package.json +33 -11
- package/src/vcr/record.ts +1 -1
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const helper = require('../shared/indexer.318d3617.cjs');
|
|
4
|
+
require('node:fs/promises');
|
|
5
|
+
require('node:path');
|
|
6
|
+
require('klona/full');
|
|
7
|
+
require('consola');
|
|
8
|
+
require('hookable');
|
|
9
|
+
require('node:assert');
|
|
10
|
+
require('node:fs');
|
|
11
|
+
require('@apibara/protocol/testing');
|
|
12
|
+
const plugins_index = require('./index.cjs');
|
|
13
|
+
require('node:async_hooks');
|
|
14
|
+
require('unctx');
|
|
15
|
+
require('@opentelemetry/api');
|
|
16
|
+
|
|
17
|
+
var __defProp = Object.defineProperty;
|
|
18
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
19
|
+
var __publicField = (obj, key, value) => {
|
|
20
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
function sqlitePersistence({
|
|
24
|
+
database
|
|
25
|
+
}) {
|
|
26
|
+
return plugins_index.defineIndexerPlugin((indexer) => {
|
|
27
|
+
let store;
|
|
28
|
+
indexer.hooks.hook("run:before", () => {
|
|
29
|
+
SqlitePersistence.initialize(database);
|
|
30
|
+
store = new SqlitePersistence(database);
|
|
31
|
+
});
|
|
32
|
+
indexer.hooks.hook("connect:before", ({ request }) => {
|
|
33
|
+
const { cursor, filter } = store.get();
|
|
34
|
+
if (cursor) {
|
|
35
|
+
request.startingCursor = cursor;
|
|
36
|
+
}
|
|
37
|
+
if (filter) {
|
|
38
|
+
request.filter[1] = filter;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
indexer.hooks.hook("transaction:commit", ({ endCursor }) => {
|
|
42
|
+
if (endCursor) {
|
|
43
|
+
store.put({ cursor: endCursor });
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
indexer.hooks.hook("connect:factory", ({ request, endCursor }) => {
|
|
47
|
+
if (request.filter[1]) {
|
|
48
|
+
store.put({ cursor: endCursor, filter: request.filter[1] });
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
class SqlitePersistence {
|
|
54
|
+
constructor(_db) {
|
|
55
|
+
this._db = _db;
|
|
56
|
+
/** Sqlite Queries Prepare Statements */
|
|
57
|
+
__publicField(this, "_getCheckpointQuery");
|
|
58
|
+
__publicField(this, "_putCheckpointQuery");
|
|
59
|
+
__publicField(this, "_delCheckpointQuery");
|
|
60
|
+
__publicField(this, "_getFilterQuery");
|
|
61
|
+
__publicField(this, "_updateFilterToBlockQuery");
|
|
62
|
+
__publicField(this, "_insertFilterQuery");
|
|
63
|
+
__publicField(this, "_delFilterQuery");
|
|
64
|
+
this._getCheckpointQuery = this._db.prepare(statements.getCheckpoint);
|
|
65
|
+
this._putCheckpointQuery = this._db.prepare(statements.putCheckpoint);
|
|
66
|
+
this._delCheckpointQuery = this._db.prepare(statements.delCheckpoint);
|
|
67
|
+
this._getFilterQuery = this._db.prepare(statements.getFilter);
|
|
68
|
+
this._updateFilterToBlockQuery = this._db.prepare(
|
|
69
|
+
statements.updateFilterToBlock
|
|
70
|
+
);
|
|
71
|
+
this._insertFilterQuery = this._db.prepare(statements.insertFilter);
|
|
72
|
+
this._delFilterQuery = this._db.prepare(statements.delFilter);
|
|
73
|
+
}
|
|
74
|
+
static initialize(db) {
|
|
75
|
+
db.prepare(statements.createCheckpointsTable).run();
|
|
76
|
+
db.prepare(statements.createFiltersTable).run();
|
|
77
|
+
}
|
|
78
|
+
get() {
|
|
79
|
+
const cursor = this._getCheckpoint();
|
|
80
|
+
const filter = this._getFilter();
|
|
81
|
+
return { cursor, filter };
|
|
82
|
+
}
|
|
83
|
+
put({ cursor, filter }) {
|
|
84
|
+
if (cursor) {
|
|
85
|
+
this._putCheckpoint(cursor);
|
|
86
|
+
if (filter) {
|
|
87
|
+
this._putFilter(filter, cursor);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
del() {
|
|
92
|
+
this._delCheckpoint();
|
|
93
|
+
this._delFilter();
|
|
94
|
+
}
|
|
95
|
+
// --- CHECKPOINTS TABLE METHODS ---
|
|
96
|
+
_getCheckpoint() {
|
|
97
|
+
const row = this._getCheckpointQuery.get("default");
|
|
98
|
+
if (!row)
|
|
99
|
+
return void 0;
|
|
100
|
+
return { orderKey: BigInt(row.order_key), uniqueKey: row.unique_key };
|
|
101
|
+
}
|
|
102
|
+
_putCheckpoint(cursor) {
|
|
103
|
+
this._putCheckpointQuery.run(
|
|
104
|
+
"default",
|
|
105
|
+
Number(cursor.orderKey),
|
|
106
|
+
cursor.uniqueKey
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
_delCheckpoint() {
|
|
110
|
+
this._delCheckpointQuery.run("default");
|
|
111
|
+
}
|
|
112
|
+
// --- FILTERS TABLE METHODS ---
|
|
113
|
+
_getFilter() {
|
|
114
|
+
const row = this._getFilterQuery.get("default");
|
|
115
|
+
if (!row)
|
|
116
|
+
return void 0;
|
|
117
|
+
return helper.deserialize(row.filter);
|
|
118
|
+
}
|
|
119
|
+
_putFilter(filter, endCursor) {
|
|
120
|
+
this._updateFilterToBlockQuery.run(Number(endCursor.orderKey), "default");
|
|
121
|
+
this._insertFilterQuery.run(
|
|
122
|
+
"default",
|
|
123
|
+
helper.serialize(filter),
|
|
124
|
+
Number(endCursor.orderKey)
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
_delFilter() {
|
|
128
|
+
this._delFilterQuery.run("default");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const statements = {
|
|
132
|
+
beginTxn: "BEGIN TRANSACTION",
|
|
133
|
+
commitTxn: "COMMIT TRANSACTION",
|
|
134
|
+
rollbackTxn: "ROLLBACK TRANSACTION",
|
|
135
|
+
createCheckpointsTable: `
|
|
136
|
+
CREATE TABLE IF NOT EXISTS checkpoints (
|
|
137
|
+
id TEXT NOT NULL PRIMARY KEY,
|
|
138
|
+
order_key INTEGER NOT NULL,
|
|
139
|
+
unique_key TEXT
|
|
140
|
+
);`,
|
|
141
|
+
createFiltersTable: `
|
|
142
|
+
CREATE TABLE IF NOT EXISTS filters (
|
|
143
|
+
id TEXT NOT NULL,
|
|
144
|
+
filter BLOB NOT NULL,
|
|
145
|
+
from_block INTEGER NOT NULL,
|
|
146
|
+
to_block INTEGER,
|
|
147
|
+
PRIMARY KEY (id, from_block)
|
|
148
|
+
);`,
|
|
149
|
+
getCheckpoint: `
|
|
150
|
+
SELECT *
|
|
151
|
+
FROM checkpoints
|
|
152
|
+
WHERE id = ?`,
|
|
153
|
+
putCheckpoint: `
|
|
154
|
+
INSERT INTO checkpoints (id, order_key, unique_key)
|
|
155
|
+
VALUES (?, ?, ?)
|
|
156
|
+
ON CONFLICT(id) DO UPDATE SET
|
|
157
|
+
order_key = excluded.order_key,
|
|
158
|
+
unique_key = excluded.unique_key`,
|
|
159
|
+
delCheckpoint: `
|
|
160
|
+
DELETE FROM checkpoints
|
|
161
|
+
WHERE id = ?`,
|
|
162
|
+
getFilter: `
|
|
163
|
+
SELECT *
|
|
164
|
+
FROM filters
|
|
165
|
+
WHERE id = ? AND to_block IS NULL`,
|
|
166
|
+
updateFilterToBlock: `
|
|
167
|
+
UPDATE filters
|
|
168
|
+
SET to_block = ?
|
|
169
|
+
WHERE id = ? AND to_block IS NULL`,
|
|
170
|
+
insertFilter: `
|
|
171
|
+
INSERT INTO filters (id, filter, from_block)
|
|
172
|
+
VALUES (?, ?, ?)
|
|
173
|
+
ON CONFLICT(id, from_block) DO UPDATE SET
|
|
174
|
+
filter = excluded.filter,
|
|
175
|
+
from_block = excluded.from_block`,
|
|
176
|
+
delFilter: `
|
|
177
|
+
DELETE FROM filters
|
|
178
|
+
WHERE id = ?`
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
exports.SqlitePersistence = SqlitePersistence;
|
|
182
|
+
exports.sqlitePersistence = sqlitePersistence;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { I as IndexerPlugin } from '../shared/indexer.c7ed6b83.cjs';
|
|
2
|
+
import { Cursor } from '@apibara/protocol';
|
|
3
|
+
import { Database } from 'better-sqlite3';
|
|
4
|
+
import 'hookable';
|
|
5
|
+
import '../shared/indexer.b9c8f0d8.cjs';
|
|
6
|
+
|
|
7
|
+
declare function sqlitePersistence<TFilter, TBlock, TTxnParams>({ database, }: {
|
|
8
|
+
database: Database;
|
|
9
|
+
}): IndexerPlugin<TFilter, TBlock, TTxnParams>;
|
|
10
|
+
declare class SqlitePersistence<TFilter> {
|
|
11
|
+
private _db;
|
|
12
|
+
/** Sqlite Queries Prepare Statements */
|
|
13
|
+
private _getCheckpointQuery;
|
|
14
|
+
private _putCheckpointQuery;
|
|
15
|
+
private _delCheckpointQuery;
|
|
16
|
+
private _getFilterQuery;
|
|
17
|
+
private _updateFilterToBlockQuery;
|
|
18
|
+
private _insertFilterQuery;
|
|
19
|
+
private _delFilterQuery;
|
|
20
|
+
constructor(_db: Database);
|
|
21
|
+
static initialize(db: Database): void;
|
|
22
|
+
get(): {
|
|
23
|
+
cursor?: Cursor;
|
|
24
|
+
filter?: TFilter;
|
|
25
|
+
};
|
|
26
|
+
put({ cursor, filter }: {
|
|
27
|
+
cursor?: Cursor;
|
|
28
|
+
filter?: TFilter;
|
|
29
|
+
}): void;
|
|
30
|
+
del(): void;
|
|
31
|
+
private _getCheckpoint;
|
|
32
|
+
private _putCheckpoint;
|
|
33
|
+
private _delCheckpoint;
|
|
34
|
+
private _getFilter;
|
|
35
|
+
private _putFilter;
|
|
36
|
+
private _delFilter;
|
|
37
|
+
}
|
|
38
|
+
type CheckpointRow = {
|
|
39
|
+
id: string;
|
|
40
|
+
order_key: number;
|
|
41
|
+
unique_key?: `0x${string}`;
|
|
42
|
+
};
|
|
43
|
+
type FilterRow = {
|
|
44
|
+
id: string;
|
|
45
|
+
filter: string;
|
|
46
|
+
from_block: number;
|
|
47
|
+
to_block?: number;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export { type CheckpointRow, type FilterRow, SqlitePersistence, sqlitePersistence };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { I as IndexerPlugin } from '../shared/indexer.e8bd138d.mjs';
|
|
2
|
+
import { Cursor } from '@apibara/protocol';
|
|
3
|
+
import { Database } from 'better-sqlite3';
|
|
4
|
+
import 'hookable';
|
|
5
|
+
import '../shared/indexer.b9c8f0d8.mjs';
|
|
6
|
+
|
|
7
|
+
declare function sqlitePersistence<TFilter, TBlock, TTxnParams>({ database, }: {
|
|
8
|
+
database: Database;
|
|
9
|
+
}): IndexerPlugin<TFilter, TBlock, TTxnParams>;
|
|
10
|
+
declare class SqlitePersistence<TFilter> {
|
|
11
|
+
private _db;
|
|
12
|
+
/** Sqlite Queries Prepare Statements */
|
|
13
|
+
private _getCheckpointQuery;
|
|
14
|
+
private _putCheckpointQuery;
|
|
15
|
+
private _delCheckpointQuery;
|
|
16
|
+
private _getFilterQuery;
|
|
17
|
+
private _updateFilterToBlockQuery;
|
|
18
|
+
private _insertFilterQuery;
|
|
19
|
+
private _delFilterQuery;
|
|
20
|
+
constructor(_db: Database);
|
|
21
|
+
static initialize(db: Database): void;
|
|
22
|
+
get(): {
|
|
23
|
+
cursor?: Cursor;
|
|
24
|
+
filter?: TFilter;
|
|
25
|
+
};
|
|
26
|
+
put({ cursor, filter }: {
|
|
27
|
+
cursor?: Cursor;
|
|
28
|
+
filter?: TFilter;
|
|
29
|
+
}): void;
|
|
30
|
+
del(): void;
|
|
31
|
+
private _getCheckpoint;
|
|
32
|
+
private _putCheckpoint;
|
|
33
|
+
private _delCheckpoint;
|
|
34
|
+
private _getFilter;
|
|
35
|
+
private _putFilter;
|
|
36
|
+
private _delFilter;
|
|
37
|
+
}
|
|
38
|
+
type CheckpointRow = {
|
|
39
|
+
id: string;
|
|
40
|
+
order_key: number;
|
|
41
|
+
unique_key?: `0x${string}`;
|
|
42
|
+
};
|
|
43
|
+
type FilterRow = {
|
|
44
|
+
id: string;
|
|
45
|
+
filter: string;
|
|
46
|
+
from_block: number;
|
|
47
|
+
to_block?: number;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export { type CheckpointRow, type FilterRow, SqlitePersistence, sqlitePersistence };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { I as IndexerPlugin } from '../shared/indexer.f761abcd.js';
|
|
2
|
+
import { Cursor } from '@apibara/protocol';
|
|
3
|
+
import { Database } from 'better-sqlite3';
|
|
4
|
+
import 'hookable';
|
|
5
|
+
import '../shared/indexer.b9c8f0d8.js';
|
|
6
|
+
|
|
7
|
+
declare function sqlitePersistence<TFilter, TBlock, TTxnParams>({ database, }: {
|
|
8
|
+
database: Database;
|
|
9
|
+
}): IndexerPlugin<TFilter, TBlock, TTxnParams>;
|
|
10
|
+
declare class SqlitePersistence<TFilter> {
|
|
11
|
+
private _db;
|
|
12
|
+
/** Sqlite Queries Prepare Statements */
|
|
13
|
+
private _getCheckpointQuery;
|
|
14
|
+
private _putCheckpointQuery;
|
|
15
|
+
private _delCheckpointQuery;
|
|
16
|
+
private _getFilterQuery;
|
|
17
|
+
private _updateFilterToBlockQuery;
|
|
18
|
+
private _insertFilterQuery;
|
|
19
|
+
private _delFilterQuery;
|
|
20
|
+
constructor(_db: Database);
|
|
21
|
+
static initialize(db: Database): void;
|
|
22
|
+
get(): {
|
|
23
|
+
cursor?: Cursor;
|
|
24
|
+
filter?: TFilter;
|
|
25
|
+
};
|
|
26
|
+
put({ cursor, filter }: {
|
|
27
|
+
cursor?: Cursor;
|
|
28
|
+
filter?: TFilter;
|
|
29
|
+
}): void;
|
|
30
|
+
del(): void;
|
|
31
|
+
private _getCheckpoint;
|
|
32
|
+
private _putCheckpoint;
|
|
33
|
+
private _delCheckpoint;
|
|
34
|
+
private _getFilter;
|
|
35
|
+
private _putFilter;
|
|
36
|
+
private _delFilter;
|
|
37
|
+
}
|
|
38
|
+
type CheckpointRow = {
|
|
39
|
+
id: string;
|
|
40
|
+
order_key: number;
|
|
41
|
+
unique_key?: `0x${string}`;
|
|
42
|
+
};
|
|
43
|
+
type FilterRow = {
|
|
44
|
+
id: string;
|
|
45
|
+
filter: string;
|
|
46
|
+
from_block: number;
|
|
47
|
+
to_block?: number;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export { type CheckpointRow, type FilterRow, SqlitePersistence, sqlitePersistence };
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { d as deserialize, s as serialize } from '../shared/indexer.2c23c9cd.mjs';
|
|
2
|
+
import 'node:fs/promises';
|
|
3
|
+
import 'node:path';
|
|
4
|
+
import 'klona/full';
|
|
5
|
+
import 'consola';
|
|
6
|
+
import 'hookable';
|
|
7
|
+
import 'node:assert';
|
|
8
|
+
import 'node:fs';
|
|
9
|
+
import '@apibara/protocol/testing';
|
|
10
|
+
import { defineIndexerPlugin } from './index.mjs';
|
|
11
|
+
import 'node:async_hooks';
|
|
12
|
+
import 'unctx';
|
|
13
|
+
import '@opentelemetry/api';
|
|
14
|
+
|
|
15
|
+
var __defProp = Object.defineProperty;
|
|
16
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
17
|
+
var __publicField = (obj, key, value) => {
|
|
18
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
19
|
+
return value;
|
|
20
|
+
};
|
|
21
|
+
function sqlitePersistence({
|
|
22
|
+
database
|
|
23
|
+
}) {
|
|
24
|
+
return defineIndexerPlugin((indexer) => {
|
|
25
|
+
let store;
|
|
26
|
+
indexer.hooks.hook("run:before", () => {
|
|
27
|
+
SqlitePersistence.initialize(database);
|
|
28
|
+
store = new SqlitePersistence(database);
|
|
29
|
+
});
|
|
30
|
+
indexer.hooks.hook("connect:before", ({ request }) => {
|
|
31
|
+
const { cursor, filter } = store.get();
|
|
32
|
+
if (cursor) {
|
|
33
|
+
request.startingCursor = cursor;
|
|
34
|
+
}
|
|
35
|
+
if (filter) {
|
|
36
|
+
request.filter[1] = filter;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
indexer.hooks.hook("transaction:commit", ({ endCursor }) => {
|
|
40
|
+
if (endCursor) {
|
|
41
|
+
store.put({ cursor: endCursor });
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
indexer.hooks.hook("connect:factory", ({ request, endCursor }) => {
|
|
45
|
+
if (request.filter[1]) {
|
|
46
|
+
store.put({ cursor: endCursor, filter: request.filter[1] });
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
class SqlitePersistence {
|
|
52
|
+
constructor(_db) {
|
|
53
|
+
this._db = _db;
|
|
54
|
+
/** Sqlite Queries Prepare Statements */
|
|
55
|
+
__publicField(this, "_getCheckpointQuery");
|
|
56
|
+
__publicField(this, "_putCheckpointQuery");
|
|
57
|
+
__publicField(this, "_delCheckpointQuery");
|
|
58
|
+
__publicField(this, "_getFilterQuery");
|
|
59
|
+
__publicField(this, "_updateFilterToBlockQuery");
|
|
60
|
+
__publicField(this, "_insertFilterQuery");
|
|
61
|
+
__publicField(this, "_delFilterQuery");
|
|
62
|
+
this._getCheckpointQuery = this._db.prepare(statements.getCheckpoint);
|
|
63
|
+
this._putCheckpointQuery = this._db.prepare(statements.putCheckpoint);
|
|
64
|
+
this._delCheckpointQuery = this._db.prepare(statements.delCheckpoint);
|
|
65
|
+
this._getFilterQuery = this._db.prepare(statements.getFilter);
|
|
66
|
+
this._updateFilterToBlockQuery = this._db.prepare(
|
|
67
|
+
statements.updateFilterToBlock
|
|
68
|
+
);
|
|
69
|
+
this._insertFilterQuery = this._db.prepare(statements.insertFilter);
|
|
70
|
+
this._delFilterQuery = this._db.prepare(statements.delFilter);
|
|
71
|
+
}
|
|
72
|
+
static initialize(db) {
|
|
73
|
+
db.prepare(statements.createCheckpointsTable).run();
|
|
74
|
+
db.prepare(statements.createFiltersTable).run();
|
|
75
|
+
}
|
|
76
|
+
get() {
|
|
77
|
+
const cursor = this._getCheckpoint();
|
|
78
|
+
const filter = this._getFilter();
|
|
79
|
+
return { cursor, filter };
|
|
80
|
+
}
|
|
81
|
+
put({ cursor, filter }) {
|
|
82
|
+
if (cursor) {
|
|
83
|
+
this._putCheckpoint(cursor);
|
|
84
|
+
if (filter) {
|
|
85
|
+
this._putFilter(filter, cursor);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
del() {
|
|
90
|
+
this._delCheckpoint();
|
|
91
|
+
this._delFilter();
|
|
92
|
+
}
|
|
93
|
+
// --- CHECKPOINTS TABLE METHODS ---
|
|
94
|
+
_getCheckpoint() {
|
|
95
|
+
const row = this._getCheckpointQuery.get("default");
|
|
96
|
+
if (!row)
|
|
97
|
+
return void 0;
|
|
98
|
+
return { orderKey: BigInt(row.order_key), uniqueKey: row.unique_key };
|
|
99
|
+
}
|
|
100
|
+
_putCheckpoint(cursor) {
|
|
101
|
+
this._putCheckpointQuery.run(
|
|
102
|
+
"default",
|
|
103
|
+
Number(cursor.orderKey),
|
|
104
|
+
cursor.uniqueKey
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
_delCheckpoint() {
|
|
108
|
+
this._delCheckpointQuery.run("default");
|
|
109
|
+
}
|
|
110
|
+
// --- FILTERS TABLE METHODS ---
|
|
111
|
+
_getFilter() {
|
|
112
|
+
const row = this._getFilterQuery.get("default");
|
|
113
|
+
if (!row)
|
|
114
|
+
return void 0;
|
|
115
|
+
return deserialize(row.filter);
|
|
116
|
+
}
|
|
117
|
+
_putFilter(filter, endCursor) {
|
|
118
|
+
this._updateFilterToBlockQuery.run(Number(endCursor.orderKey), "default");
|
|
119
|
+
this._insertFilterQuery.run(
|
|
120
|
+
"default",
|
|
121
|
+
serialize(filter),
|
|
122
|
+
Number(endCursor.orderKey)
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
_delFilter() {
|
|
126
|
+
this._delFilterQuery.run("default");
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const statements = {
|
|
130
|
+
beginTxn: "BEGIN TRANSACTION",
|
|
131
|
+
commitTxn: "COMMIT TRANSACTION",
|
|
132
|
+
rollbackTxn: "ROLLBACK TRANSACTION",
|
|
133
|
+
createCheckpointsTable: `
|
|
134
|
+
CREATE TABLE IF NOT EXISTS checkpoints (
|
|
135
|
+
id TEXT NOT NULL PRIMARY KEY,
|
|
136
|
+
order_key INTEGER NOT NULL,
|
|
137
|
+
unique_key TEXT
|
|
138
|
+
);`,
|
|
139
|
+
createFiltersTable: `
|
|
140
|
+
CREATE TABLE IF NOT EXISTS filters (
|
|
141
|
+
id TEXT NOT NULL,
|
|
142
|
+
filter BLOB NOT NULL,
|
|
143
|
+
from_block INTEGER NOT NULL,
|
|
144
|
+
to_block INTEGER,
|
|
145
|
+
PRIMARY KEY (id, from_block)
|
|
146
|
+
);`,
|
|
147
|
+
getCheckpoint: `
|
|
148
|
+
SELECT *
|
|
149
|
+
FROM checkpoints
|
|
150
|
+
WHERE id = ?`,
|
|
151
|
+
putCheckpoint: `
|
|
152
|
+
INSERT INTO checkpoints (id, order_key, unique_key)
|
|
153
|
+
VALUES (?, ?, ?)
|
|
154
|
+
ON CONFLICT(id) DO UPDATE SET
|
|
155
|
+
order_key = excluded.order_key,
|
|
156
|
+
unique_key = excluded.unique_key`,
|
|
157
|
+
delCheckpoint: `
|
|
158
|
+
DELETE FROM checkpoints
|
|
159
|
+
WHERE id = ?`,
|
|
160
|
+
getFilter: `
|
|
161
|
+
SELECT *
|
|
162
|
+
FROM filters
|
|
163
|
+
WHERE id = ? AND to_block IS NULL`,
|
|
164
|
+
updateFilterToBlock: `
|
|
165
|
+
UPDATE filters
|
|
166
|
+
SET to_block = ?
|
|
167
|
+
WHERE id = ? AND to_block IS NULL`,
|
|
168
|
+
insertFilter: `
|
|
169
|
+
INSERT INTO filters (id, filter, from_block)
|
|
170
|
+
VALUES (?, ?, ?)
|
|
171
|
+
ON CONFLICT(id, from_block) DO UPDATE SET
|
|
172
|
+
filter = excluded.filter,
|
|
173
|
+
from_block = excluded.from_block`,
|
|
174
|
+
delFilter: `
|
|
175
|
+
DELETE FROM filters
|
|
176
|
+
WHERE id = ?`
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export { SqlitePersistence, sqlitePersistence };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { getContext } from 'unctx';
|
|
3
|
+
import { trace } from '@opentelemetry/api';
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
const indexerAsyncContext = getContext("indexer", {
|
|
8
|
+
asyncContext: true,
|
|
9
|
+
AsyncLocalStorage
|
|
10
|
+
});
|
|
11
|
+
function useIndexerContext() {
|
|
12
|
+
return indexerAsyncContext.use();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const tracer = trace.getTracer("@apibara/indexer");
|
|
16
|
+
|
|
17
|
+
function deserialize(str) {
|
|
18
|
+
return JSON.parse(
|
|
19
|
+
str,
|
|
20
|
+
(_, value) => typeof value === "string" && value.match(/^\d+n$/) ? BigInt(value.slice(0, -1)) : value
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
function serialize(obj) {
|
|
24
|
+
return JSON.stringify(
|
|
25
|
+
obj,
|
|
26
|
+
(_, value) => typeof value === "bigint" ? `${value.toString()}n` : value,
|
|
27
|
+
" "
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
function isCassetteAvailable(vcrConfig, cassetteName) {
|
|
31
|
+
const filePath = path.join(vcrConfig.cassetteDir, `${cassetteName}.json`);
|
|
32
|
+
return fs.existsSync(filePath);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { indexerAsyncContext as a, deserialize as d, isCassetteAvailable as i, serialize as s, tracer as t, useIndexerContext as u };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const node_async_hooks = require('node:async_hooks');
|
|
4
|
+
const unctx = require('unctx');
|
|
5
|
+
const api = require('@opentelemetry/api');
|
|
6
|
+
const fs = require('node:fs');
|
|
7
|
+
const path = require('node:path');
|
|
8
|
+
|
|
9
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
10
|
+
|
|
11
|
+
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
12
|
+
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
13
|
+
|
|
14
|
+
const indexerAsyncContext = unctx.getContext("indexer", {
|
|
15
|
+
asyncContext: true,
|
|
16
|
+
AsyncLocalStorage: node_async_hooks.AsyncLocalStorage
|
|
17
|
+
});
|
|
18
|
+
function useIndexerContext() {
|
|
19
|
+
return indexerAsyncContext.use();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const tracer = api.trace.getTracer("@apibara/indexer");
|
|
23
|
+
|
|
24
|
+
function deserialize(str) {
|
|
25
|
+
return JSON.parse(
|
|
26
|
+
str,
|
|
27
|
+
(_, value) => typeof value === "string" && value.match(/^\d+n$/) ? BigInt(value.slice(0, -1)) : value
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
function serialize(obj) {
|
|
31
|
+
return JSON.stringify(
|
|
32
|
+
obj,
|
|
33
|
+
(_, value) => typeof value === "bigint" ? `${value.toString()}n` : value,
|
|
34
|
+
" "
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
function isCassetteAvailable(vcrConfig, cassetteName) {
|
|
38
|
+
const filePath = path__default.join(vcrConfig.cassetteDir, `${cassetteName}.json`);
|
|
39
|
+
return fs__default.existsSync(filePath);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
exports.deserialize = deserialize;
|
|
43
|
+
exports.indexerAsyncContext = indexerAsyncContext;
|
|
44
|
+
exports.isCassetteAvailable = isCassetteAvailable;
|
|
45
|
+
exports.serialize = serialize;
|
|
46
|
+
exports.tracer = tracer;
|
|
47
|
+
exports.useIndexerContext = useIndexerContext;
|