@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
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const vcr_index = require('./shared/indexer.541d43eb.cjs');
|
|
4
|
+
const sink = require('./shared/indexer.a8b7ab1f.cjs');
|
|
5
|
+
const helper = require('./shared/indexer.318d3617.cjs');
|
|
6
|
+
const plugins_index = require('./plugins/index.cjs');
|
|
7
|
+
require('node:fs/promises');
|
|
8
|
+
require('node:path');
|
|
9
|
+
require('klona/full');
|
|
10
|
+
require('consola');
|
|
11
|
+
require('hookable');
|
|
12
|
+
require('node:assert');
|
|
13
|
+
require('node:fs');
|
|
14
|
+
require('@apibara/protocol/testing');
|
|
15
|
+
require('node:async_hooks');
|
|
16
|
+
require('unctx');
|
|
17
|
+
require('@opentelemetry/api');
|
|
18
|
+
|
|
19
|
+
function useKVStore() {
|
|
20
|
+
const ctx = helper.useIndexerContext();
|
|
21
|
+
if (!ctx?.kv)
|
|
22
|
+
throw new Error("KV Plugin is not available in context!");
|
|
23
|
+
return ctx.kv;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function useSink({
|
|
27
|
+
context
|
|
28
|
+
}) {
|
|
29
|
+
if (!context.sinkTransaction) {
|
|
30
|
+
throw new Error("Transaction context doesn't exist!");
|
|
31
|
+
}
|
|
32
|
+
return context.sinkTransaction;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
exports.createIndexer = vcr_index.createIndexer;
|
|
36
|
+
exports.defineIndexer = vcr_index.defineIndexer;
|
|
37
|
+
exports.loadCassette = vcr_index.loadCassette;
|
|
38
|
+
exports.record = vcr_index.record;
|
|
39
|
+
exports.replay = vcr_index.replay;
|
|
40
|
+
exports.run = vcr_index.run;
|
|
41
|
+
exports.DefaultSink = sink.DefaultSink;
|
|
42
|
+
exports.Sink = sink.Sink;
|
|
43
|
+
exports.defaultSink = sink.defaultSink;
|
|
44
|
+
exports.deserialize = helper.deserialize;
|
|
45
|
+
exports.isCassetteAvailable = helper.isCassetteAvailable;
|
|
46
|
+
exports.serialize = helper.serialize;
|
|
47
|
+
exports.useIndexerContext = helper.useIndexerContext;
|
|
48
|
+
exports.defineIndexerPlugin = plugins_index.defineIndexerPlugin;
|
|
49
|
+
exports.useKVStore = useKVStore;
|
|
50
|
+
exports.useSink = useSink;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { a as IndexerContext } from './shared/indexer.c7ed6b83.cjs';
|
|
2
|
+
export { f as Indexer, c as IndexerConfig, b as IndexerHooks, I as IndexerPlugin, d as IndexerWithStreamConfig, g as createIndexer, e as defineIndexer, h as defineIndexerPlugin, r as run, u as useIndexerContext } from './shared/indexer.c7ed6b83.cjs';
|
|
3
|
+
export { D as DefaultSink, S as Sink, a as SinkCursorParams, b as SinkData, d as defaultSink } from './shared/indexer.b9c8f0d8.cjs';
|
|
4
|
+
export { C as CassetteOptions, V as VcrConfig, a as VcrReplayResult, l as loadCassette, r as replay } from './shared/indexer.500fd281.cjs';
|
|
5
|
+
export { CassetteDataType, deserialize, isCassetteAvailable, record, serialize } from './vcr/index.cjs';
|
|
6
|
+
import { KVStore } from './plugins/kv.cjs';
|
|
7
|
+
import '@apibara/protocol';
|
|
8
|
+
import 'hookable';
|
|
9
|
+
import 'better-sqlite3';
|
|
10
|
+
|
|
11
|
+
type UseKVStoreResult = InstanceType<typeof KVStore>;
|
|
12
|
+
declare function useKVStore(): UseKVStoreResult;
|
|
13
|
+
|
|
14
|
+
declare function useSink<TTxnParams>({ context, }: {
|
|
15
|
+
context: IndexerContext<TTxnParams>;
|
|
16
|
+
}): NonNullable<TTxnParams>;
|
|
17
|
+
|
|
18
|
+
export { type UseKVStoreResult, useKVStore, useSink };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { a as IndexerContext } from './shared/indexer.e8bd138d.mjs';
|
|
2
|
+
export { f as Indexer, c as IndexerConfig, b as IndexerHooks, I as IndexerPlugin, d as IndexerWithStreamConfig, g as createIndexer, e as defineIndexer, h as defineIndexerPlugin, r as run, u as useIndexerContext } from './shared/indexer.e8bd138d.mjs';
|
|
3
|
+
export { D as DefaultSink, S as Sink, a as SinkCursorParams, b as SinkData, d as defaultSink } from './shared/indexer.b9c8f0d8.mjs';
|
|
4
|
+
export { C as CassetteOptions, V as VcrConfig, a as VcrReplayResult, l as loadCassette, r as replay } from './shared/indexer.e1856641.mjs';
|
|
5
|
+
export { CassetteDataType, deserialize, isCassetteAvailable, record, serialize } from './vcr/index.mjs';
|
|
6
|
+
import { KVStore } from './plugins/kv.mjs';
|
|
7
|
+
import '@apibara/protocol';
|
|
8
|
+
import 'hookable';
|
|
9
|
+
import 'better-sqlite3';
|
|
10
|
+
|
|
11
|
+
type UseKVStoreResult = InstanceType<typeof KVStore>;
|
|
12
|
+
declare function useKVStore(): UseKVStoreResult;
|
|
13
|
+
|
|
14
|
+
declare function useSink<TTxnParams>({ context, }: {
|
|
15
|
+
context: IndexerContext<TTxnParams>;
|
|
16
|
+
}): NonNullable<TTxnParams>;
|
|
17
|
+
|
|
18
|
+
export { type UseKVStoreResult, useKVStore, useSink };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { a as IndexerContext } from './shared/indexer.f761abcd.js';
|
|
2
|
+
export { f as Indexer, c as IndexerConfig, b as IndexerHooks, I as IndexerPlugin, d as IndexerWithStreamConfig, g as createIndexer, e as defineIndexer, h as defineIndexerPlugin, r as run, u as useIndexerContext } from './shared/indexer.f761abcd.js';
|
|
3
|
+
export { D as DefaultSink, S as Sink, a as SinkCursorParams, b as SinkData, d as defaultSink } from './shared/indexer.b9c8f0d8.js';
|
|
4
|
+
export { C as CassetteOptions, V as VcrConfig, a as VcrReplayResult, l as loadCassette, r as replay } from './shared/indexer.e4f2430f.js';
|
|
5
|
+
export { CassetteDataType, deserialize, isCassetteAvailable, record, serialize } from './vcr/index.js';
|
|
6
|
+
import { KVStore } from './plugins/kv.js';
|
|
7
|
+
import '@apibara/protocol';
|
|
8
|
+
import 'hookable';
|
|
9
|
+
import 'better-sqlite3';
|
|
10
|
+
|
|
11
|
+
type UseKVStoreResult = InstanceType<typeof KVStore>;
|
|
12
|
+
declare function useKVStore(): UseKVStoreResult;
|
|
13
|
+
|
|
14
|
+
declare function useSink<TTxnParams>({ context, }: {
|
|
15
|
+
context: IndexerContext<TTxnParams>;
|
|
16
|
+
}): NonNullable<TTxnParams>;
|
|
17
|
+
|
|
18
|
+
export { type UseKVStoreResult, useKVStore, useSink };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export { c as createIndexer, d as defineIndexer, l as loadCassette, a as record, b as replay, r as run } from './shared/indexer.36530330.mjs';
|
|
2
|
+
export { D as DefaultSink, S as Sink, d as defaultSink } from './shared/indexer.93d6b2eb.mjs';
|
|
3
|
+
import { u as useIndexerContext } from './shared/indexer.2c23c9cd.mjs';
|
|
4
|
+
export { d as deserialize, i as isCassetteAvailable, s as serialize } from './shared/indexer.2c23c9cd.mjs';
|
|
5
|
+
export { defineIndexerPlugin } from './plugins/index.mjs';
|
|
6
|
+
import 'node:fs/promises';
|
|
7
|
+
import 'node:path';
|
|
8
|
+
import 'klona/full';
|
|
9
|
+
import 'consola';
|
|
10
|
+
import 'hookable';
|
|
11
|
+
import 'node:assert';
|
|
12
|
+
import 'node:fs';
|
|
13
|
+
import '@apibara/protocol/testing';
|
|
14
|
+
import 'node:async_hooks';
|
|
15
|
+
import 'unctx';
|
|
16
|
+
import '@opentelemetry/api';
|
|
17
|
+
|
|
18
|
+
function useKVStore() {
|
|
19
|
+
const ctx = useIndexerContext();
|
|
20
|
+
if (!ctx?.kv)
|
|
21
|
+
throw new Error("KV Plugin is not available in context!");
|
|
22
|
+
return ctx.kv;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function useSink({
|
|
26
|
+
context
|
|
27
|
+
}) {
|
|
28
|
+
if (!context.sinkTransaction) {
|
|
29
|
+
throw new Error("Transaction context doesn't exist!");
|
|
30
|
+
}
|
|
31
|
+
return context.sinkTransaction;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { useIndexerContext, useKVStore, useSink };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('node:assert');
|
|
4
|
+
const helper = require('../shared/indexer.318d3617.cjs');
|
|
5
|
+
require('node:fs/promises');
|
|
6
|
+
require('node:path');
|
|
7
|
+
require('klona/full');
|
|
8
|
+
require('consola');
|
|
9
|
+
require('hookable');
|
|
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
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
18
|
+
|
|
19
|
+
const assert__default = /*#__PURE__*/_interopDefaultCompat(assert);
|
|
20
|
+
|
|
21
|
+
var __defProp = Object.defineProperty;
|
|
22
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
23
|
+
var __publicField = (obj, key, value) => {
|
|
24
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
25
|
+
return value;
|
|
26
|
+
};
|
|
27
|
+
function kv({
|
|
28
|
+
database
|
|
29
|
+
}) {
|
|
30
|
+
return plugins_index.defineIndexerPlugin((indexer) => {
|
|
31
|
+
indexer.hooks.hook("run:before", () => {
|
|
32
|
+
KVStore.initialize(database);
|
|
33
|
+
});
|
|
34
|
+
indexer.hooks.hook("handler:before", ({ finality, endCursor }) => {
|
|
35
|
+
const ctx = helper.useIndexerContext();
|
|
36
|
+
assert__default(endCursor, new Error("endCursor cannot be undefined"));
|
|
37
|
+
ctx.kv = new KVStore(database, finality, endCursor);
|
|
38
|
+
ctx.kv.beginTransaction();
|
|
39
|
+
});
|
|
40
|
+
indexer.hooks.hook("handler:after", () => {
|
|
41
|
+
const ctx = helper.useIndexerContext();
|
|
42
|
+
ctx.kv.commitTransaction();
|
|
43
|
+
ctx.kv = null;
|
|
44
|
+
});
|
|
45
|
+
indexer.hooks.hook("handler:exception", () => {
|
|
46
|
+
const ctx = helper.useIndexerContext();
|
|
47
|
+
ctx.kv.rollbackTransaction();
|
|
48
|
+
ctx.kv = null;
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
class KVStore {
|
|
53
|
+
constructor(_db, _finality, _endCursor) {
|
|
54
|
+
this._db = _db;
|
|
55
|
+
this._finality = _finality;
|
|
56
|
+
this._endCursor = _endCursor;
|
|
57
|
+
/** Sqlite Queries Prepare Statements */
|
|
58
|
+
__publicField(this, "_beginTxnQuery");
|
|
59
|
+
__publicField(this, "_commitTxnQuery");
|
|
60
|
+
__publicField(this, "_rollbackTxnQuery");
|
|
61
|
+
__publicField(this, "_getQuery");
|
|
62
|
+
__publicField(this, "_updateToBlockQuery");
|
|
63
|
+
__publicField(this, "_insertIntoKvsQuery");
|
|
64
|
+
__publicField(this, "_delQuery");
|
|
65
|
+
this._beginTxnQuery = this._db.prepare(statements.beginTxn);
|
|
66
|
+
this._commitTxnQuery = this._db.prepare(statements.commitTxn);
|
|
67
|
+
this._rollbackTxnQuery = this._db.prepare(statements.rollbackTxn);
|
|
68
|
+
this._getQuery = this._db.prepare(statements.get);
|
|
69
|
+
this._updateToBlockQuery = this._db.prepare(statements.updateToBlock);
|
|
70
|
+
this._insertIntoKvsQuery = this._db.prepare(statements.insertIntoKvs);
|
|
71
|
+
this._delQuery = this._db.prepare(statements.del);
|
|
72
|
+
}
|
|
73
|
+
static initialize(db) {
|
|
74
|
+
db.prepare(statements.createTable).run();
|
|
75
|
+
}
|
|
76
|
+
beginTransaction() {
|
|
77
|
+
this._beginTxnQuery.run();
|
|
78
|
+
}
|
|
79
|
+
commitTransaction() {
|
|
80
|
+
this._commitTxnQuery.run();
|
|
81
|
+
}
|
|
82
|
+
rollbackTransaction() {
|
|
83
|
+
this._rollbackTxnQuery.run();
|
|
84
|
+
}
|
|
85
|
+
get(key) {
|
|
86
|
+
const row = this._getQuery.get(key);
|
|
87
|
+
return row ? helper.deserialize(row.v) : void 0;
|
|
88
|
+
}
|
|
89
|
+
put(key, value) {
|
|
90
|
+
this._updateToBlockQuery.run(Number(this._endCursor.orderKey), key);
|
|
91
|
+
this._insertIntoKvsQuery.run(
|
|
92
|
+
Number(this._endCursor.orderKey),
|
|
93
|
+
key,
|
|
94
|
+
helper.serialize(value)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
del(key) {
|
|
98
|
+
this._delQuery.run(Number(this._endCursor.orderKey), key);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const statements = {
|
|
102
|
+
beginTxn: "BEGIN TRANSACTION",
|
|
103
|
+
commitTxn: "COMMIT TRANSACTION",
|
|
104
|
+
rollbackTxn: "ROLLBACK TRANSACTION",
|
|
105
|
+
createTable: `
|
|
106
|
+
CREATE TABLE IF NOT EXISTS kvs (
|
|
107
|
+
from_block INTEGER NOT NULL,
|
|
108
|
+
to_block INTEGER,
|
|
109
|
+
k TEXT NOT NULL,
|
|
110
|
+
v BLOB NOT NULL,
|
|
111
|
+
PRIMARY KEY (from_block, k)
|
|
112
|
+
);`,
|
|
113
|
+
get: `
|
|
114
|
+
SELECT v
|
|
115
|
+
FROM kvs
|
|
116
|
+
WHERE k = ? AND to_block IS NULL`,
|
|
117
|
+
updateToBlock: `
|
|
118
|
+
UPDATE kvs
|
|
119
|
+
SET to_block = ?
|
|
120
|
+
WHERE k = ? AND to_block IS NULL`,
|
|
121
|
+
insertIntoKvs: `
|
|
122
|
+
INSERT INTO kvs (from_block, to_block, k, v)
|
|
123
|
+
VALUES (?, NULL, ?, ?)`,
|
|
124
|
+
del: `
|
|
125
|
+
UPDATE kvs
|
|
126
|
+
SET to_block = ?
|
|
127
|
+
WHERE k = ? AND to_block IS NULL`
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
exports.KVStore = KVStore;
|
|
131
|
+
exports.kv = kv;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { I as IndexerPlugin } from '../shared/indexer.c7ed6b83.cjs';
|
|
2
|
+
import { DataFinality, Cursor } from '@apibara/protocol';
|
|
3
|
+
import { Database } from 'better-sqlite3';
|
|
4
|
+
import 'hookable';
|
|
5
|
+
import '../shared/indexer.b9c8f0d8.cjs';
|
|
6
|
+
|
|
7
|
+
declare function kv<TFilter, TBlock, TTxnParams>({ database, }: {
|
|
8
|
+
database: Database;
|
|
9
|
+
}): IndexerPlugin<TFilter, TBlock, TTxnParams>;
|
|
10
|
+
declare class KVStore {
|
|
11
|
+
private _db;
|
|
12
|
+
private _finality;
|
|
13
|
+
private _endCursor;
|
|
14
|
+
/** Sqlite Queries Prepare Statements */
|
|
15
|
+
private _beginTxnQuery;
|
|
16
|
+
private _commitTxnQuery;
|
|
17
|
+
private _rollbackTxnQuery;
|
|
18
|
+
private _getQuery;
|
|
19
|
+
private _updateToBlockQuery;
|
|
20
|
+
private _insertIntoKvsQuery;
|
|
21
|
+
private _delQuery;
|
|
22
|
+
constructor(_db: Database, _finality: DataFinality, _endCursor: Cursor);
|
|
23
|
+
static initialize(db: Database): void;
|
|
24
|
+
beginTransaction(): void;
|
|
25
|
+
commitTransaction(): void;
|
|
26
|
+
rollbackTransaction(): void;
|
|
27
|
+
get<T>(key: string): T;
|
|
28
|
+
put<T>(key: string, value: T): void;
|
|
29
|
+
del(key: string): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { KVStore, kv };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { I as IndexerPlugin } from '../shared/indexer.e8bd138d.mjs';
|
|
2
|
+
import { DataFinality, Cursor } from '@apibara/protocol';
|
|
3
|
+
import { Database } from 'better-sqlite3';
|
|
4
|
+
import 'hookable';
|
|
5
|
+
import '../shared/indexer.b9c8f0d8.mjs';
|
|
6
|
+
|
|
7
|
+
declare function kv<TFilter, TBlock, TTxnParams>({ database, }: {
|
|
8
|
+
database: Database;
|
|
9
|
+
}): IndexerPlugin<TFilter, TBlock, TTxnParams>;
|
|
10
|
+
declare class KVStore {
|
|
11
|
+
private _db;
|
|
12
|
+
private _finality;
|
|
13
|
+
private _endCursor;
|
|
14
|
+
/** Sqlite Queries Prepare Statements */
|
|
15
|
+
private _beginTxnQuery;
|
|
16
|
+
private _commitTxnQuery;
|
|
17
|
+
private _rollbackTxnQuery;
|
|
18
|
+
private _getQuery;
|
|
19
|
+
private _updateToBlockQuery;
|
|
20
|
+
private _insertIntoKvsQuery;
|
|
21
|
+
private _delQuery;
|
|
22
|
+
constructor(_db: Database, _finality: DataFinality, _endCursor: Cursor);
|
|
23
|
+
static initialize(db: Database): void;
|
|
24
|
+
beginTransaction(): void;
|
|
25
|
+
commitTransaction(): void;
|
|
26
|
+
rollbackTransaction(): void;
|
|
27
|
+
get<T>(key: string): T;
|
|
28
|
+
put<T>(key: string, value: T): void;
|
|
29
|
+
del(key: string): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { KVStore, kv };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { I as IndexerPlugin } from '../shared/indexer.f761abcd.js';
|
|
2
|
+
import { DataFinality, Cursor } from '@apibara/protocol';
|
|
3
|
+
import { Database } from 'better-sqlite3';
|
|
4
|
+
import 'hookable';
|
|
5
|
+
import '../shared/indexer.b9c8f0d8.js';
|
|
6
|
+
|
|
7
|
+
declare function kv<TFilter, TBlock, TTxnParams>({ database, }: {
|
|
8
|
+
database: Database;
|
|
9
|
+
}): IndexerPlugin<TFilter, TBlock, TTxnParams>;
|
|
10
|
+
declare class KVStore {
|
|
11
|
+
private _db;
|
|
12
|
+
private _finality;
|
|
13
|
+
private _endCursor;
|
|
14
|
+
/** Sqlite Queries Prepare Statements */
|
|
15
|
+
private _beginTxnQuery;
|
|
16
|
+
private _commitTxnQuery;
|
|
17
|
+
private _rollbackTxnQuery;
|
|
18
|
+
private _getQuery;
|
|
19
|
+
private _updateToBlockQuery;
|
|
20
|
+
private _insertIntoKvsQuery;
|
|
21
|
+
private _delQuery;
|
|
22
|
+
constructor(_db: Database, _finality: DataFinality, _endCursor: Cursor);
|
|
23
|
+
static initialize(db: Database): void;
|
|
24
|
+
beginTransaction(): void;
|
|
25
|
+
commitTransaction(): void;
|
|
26
|
+
rollbackTransaction(): void;
|
|
27
|
+
get<T>(key: string): T;
|
|
28
|
+
put<T>(key: string, value: T): void;
|
|
29
|
+
del(key: string): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { KVStore, kv };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { u as useIndexerContext, d as deserialize, s as serialize } from '../shared/indexer.2c23c9cd.mjs';
|
|
3
|
+
import 'node:fs/promises';
|
|
4
|
+
import 'node:path';
|
|
5
|
+
import 'klona/full';
|
|
6
|
+
import 'consola';
|
|
7
|
+
import 'hookable';
|
|
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 kv({
|
|
22
|
+
database
|
|
23
|
+
}) {
|
|
24
|
+
return defineIndexerPlugin((indexer) => {
|
|
25
|
+
indexer.hooks.hook("run:before", () => {
|
|
26
|
+
KVStore.initialize(database);
|
|
27
|
+
});
|
|
28
|
+
indexer.hooks.hook("handler:before", ({ finality, endCursor }) => {
|
|
29
|
+
const ctx = useIndexerContext();
|
|
30
|
+
assert(endCursor, new Error("endCursor cannot be undefined"));
|
|
31
|
+
ctx.kv = new KVStore(database, finality, endCursor);
|
|
32
|
+
ctx.kv.beginTransaction();
|
|
33
|
+
});
|
|
34
|
+
indexer.hooks.hook("handler:after", () => {
|
|
35
|
+
const ctx = useIndexerContext();
|
|
36
|
+
ctx.kv.commitTransaction();
|
|
37
|
+
ctx.kv = null;
|
|
38
|
+
});
|
|
39
|
+
indexer.hooks.hook("handler:exception", () => {
|
|
40
|
+
const ctx = useIndexerContext();
|
|
41
|
+
ctx.kv.rollbackTransaction();
|
|
42
|
+
ctx.kv = null;
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
class KVStore {
|
|
47
|
+
constructor(_db, _finality, _endCursor) {
|
|
48
|
+
this._db = _db;
|
|
49
|
+
this._finality = _finality;
|
|
50
|
+
this._endCursor = _endCursor;
|
|
51
|
+
/** Sqlite Queries Prepare Statements */
|
|
52
|
+
__publicField(this, "_beginTxnQuery");
|
|
53
|
+
__publicField(this, "_commitTxnQuery");
|
|
54
|
+
__publicField(this, "_rollbackTxnQuery");
|
|
55
|
+
__publicField(this, "_getQuery");
|
|
56
|
+
__publicField(this, "_updateToBlockQuery");
|
|
57
|
+
__publicField(this, "_insertIntoKvsQuery");
|
|
58
|
+
__publicField(this, "_delQuery");
|
|
59
|
+
this._beginTxnQuery = this._db.prepare(statements.beginTxn);
|
|
60
|
+
this._commitTxnQuery = this._db.prepare(statements.commitTxn);
|
|
61
|
+
this._rollbackTxnQuery = this._db.prepare(statements.rollbackTxn);
|
|
62
|
+
this._getQuery = this._db.prepare(statements.get);
|
|
63
|
+
this._updateToBlockQuery = this._db.prepare(statements.updateToBlock);
|
|
64
|
+
this._insertIntoKvsQuery = this._db.prepare(statements.insertIntoKvs);
|
|
65
|
+
this._delQuery = this._db.prepare(statements.del);
|
|
66
|
+
}
|
|
67
|
+
static initialize(db) {
|
|
68
|
+
db.prepare(statements.createTable).run();
|
|
69
|
+
}
|
|
70
|
+
beginTransaction() {
|
|
71
|
+
this._beginTxnQuery.run();
|
|
72
|
+
}
|
|
73
|
+
commitTransaction() {
|
|
74
|
+
this._commitTxnQuery.run();
|
|
75
|
+
}
|
|
76
|
+
rollbackTransaction() {
|
|
77
|
+
this._rollbackTxnQuery.run();
|
|
78
|
+
}
|
|
79
|
+
get(key) {
|
|
80
|
+
const row = this._getQuery.get(key);
|
|
81
|
+
return row ? deserialize(row.v) : void 0;
|
|
82
|
+
}
|
|
83
|
+
put(key, value) {
|
|
84
|
+
this._updateToBlockQuery.run(Number(this._endCursor.orderKey), key);
|
|
85
|
+
this._insertIntoKvsQuery.run(
|
|
86
|
+
Number(this._endCursor.orderKey),
|
|
87
|
+
key,
|
|
88
|
+
serialize(value)
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
del(key) {
|
|
92
|
+
this._delQuery.run(Number(this._endCursor.orderKey), key);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const statements = {
|
|
96
|
+
beginTxn: "BEGIN TRANSACTION",
|
|
97
|
+
commitTxn: "COMMIT TRANSACTION",
|
|
98
|
+
rollbackTxn: "ROLLBACK TRANSACTION",
|
|
99
|
+
createTable: `
|
|
100
|
+
CREATE TABLE IF NOT EXISTS kvs (
|
|
101
|
+
from_block INTEGER NOT NULL,
|
|
102
|
+
to_block INTEGER,
|
|
103
|
+
k TEXT NOT NULL,
|
|
104
|
+
v BLOB NOT NULL,
|
|
105
|
+
PRIMARY KEY (from_block, k)
|
|
106
|
+
);`,
|
|
107
|
+
get: `
|
|
108
|
+
SELECT v
|
|
109
|
+
FROM kvs
|
|
110
|
+
WHERE k = ? AND to_block IS NULL`,
|
|
111
|
+
updateToBlock: `
|
|
112
|
+
UPDATE kvs
|
|
113
|
+
SET to_block = ?
|
|
114
|
+
WHERE k = ? AND to_block IS NULL`,
|
|
115
|
+
insertIntoKvs: `
|
|
116
|
+
INSERT INTO kvs (from_block, to_block, k, v)
|
|
117
|
+
VALUES (?, NULL, ?, ?)`,
|
|
118
|
+
del: `
|
|
119
|
+
UPDATE kvs
|
|
120
|
+
SET to_block = ?
|
|
121
|
+
WHERE k = ? AND to_block IS NULL`
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export { KVStore, kv };
|