@apibara/indexer 2.0.0-beta.6 → 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.
Files changed (56) hide show
  1. package/dist/index.cjs +17 -19
  2. package/dist/index.d.cts +9 -38
  3. package/dist/index.d.mts +9 -38
  4. package/dist/index.d.ts +9 -38
  5. package/dist/index.mjs +7 -9
  6. package/dist/plugins/index.cjs +7 -0
  7. package/dist/plugins/index.d.cts +4 -0
  8. package/dist/plugins/index.d.mts +4 -0
  9. package/dist/plugins/index.d.ts +4 -0
  10. package/dist/plugins/index.mjs +5 -0
  11. package/dist/plugins/kv.cjs +131 -0
  12. package/dist/plugins/kv.d.cts +32 -0
  13. package/dist/plugins/kv.d.mts +32 -0
  14. package/dist/plugins/kv.d.ts +32 -0
  15. package/dist/plugins/kv.mjs +124 -0
  16. package/dist/plugins/persistence.cjs +182 -0
  17. package/dist/plugins/persistence.d.cts +50 -0
  18. package/dist/plugins/persistence.d.mts +50 -0
  19. package/dist/plugins/persistence.d.ts +50 -0
  20. package/dist/plugins/persistence.mjs +179 -0
  21. package/dist/shared/indexer.2c23c9cd.mjs +35 -0
  22. package/dist/shared/indexer.318d3617.cjs +47 -0
  23. package/dist/shared/{indexer.28bd9576.mjs → indexer.36530330.mjs} +9 -39
  24. package/dist/shared/indexer.500fd281.d.cts +23 -0
  25. package/dist/shared/{indexer.99ec3128.cjs → indexer.541d43eb.cjs} +16 -50
  26. package/dist/shared/{indexer.d227f25c.d.cts → indexer.b9c8f0d8.d.cts} +1 -1
  27. package/dist/shared/{indexer.d227f25c.d.mts → indexer.b9c8f0d8.d.mts} +1 -1
  28. package/dist/shared/{indexer.d227f25c.d.ts → indexer.b9c8f0d8.d.ts} +1 -1
  29. package/dist/shared/{indexer.26fbe988.d.ts → indexer.c7ed6b83.d.cts} +2 -20
  30. package/dist/shared/indexer.e1856641.d.mts +23 -0
  31. package/dist/shared/indexer.e4f2430f.d.ts +23 -0
  32. package/dist/shared/{indexer.ff960804.d.cts → indexer.e8bd138d.d.mts} +2 -20
  33. package/dist/shared/{indexer.9aa22867.d.mts → indexer.f761abcd.d.ts} +2 -20
  34. package/dist/sinks/csv.d.cts +1 -1
  35. package/dist/sinks/csv.d.mts +1 -1
  36. package/dist/sinks/csv.d.ts +1 -1
  37. package/dist/sinks/drizzle/index.cjs +210 -0
  38. package/dist/sinks/drizzle/index.d.cts +111 -0
  39. package/dist/sinks/drizzle/index.d.mts +111 -0
  40. package/dist/sinks/drizzle/index.d.ts +111 -0
  41. package/dist/sinks/drizzle/index.mjs +196 -0
  42. package/dist/sinks/sqlite.d.cts +1 -1
  43. package/dist/sinks/sqlite.d.mts +1 -1
  44. package/dist/sinks/sqlite.d.ts +1 -1
  45. package/dist/testing/index.cjs +16 -15
  46. package/dist/testing/index.d.cts +3 -2
  47. package/dist/testing/index.d.mts +3 -2
  48. package/dist/testing/index.d.ts +3 -2
  49. package/dist/testing/index.mjs +11 -10
  50. package/dist/vcr/index.cjs +25 -0
  51. package/dist/vcr/index.d.cts +18 -0
  52. package/dist/vcr/index.d.mts +18 -0
  53. package/dist/vcr/index.d.ts +18 -0
  54. package/dist/vcr/index.mjs +14 -0
  55. package/package.json +33 -11
  56. package/src/vcr/record.ts +1 -1
package/dist/index.cjs CHANGED
@@ -1,25 +1,23 @@
1
1
  'use strict';
2
2
 
3
- const replay = require('./shared/indexer.99ec3128.cjs');
3
+ const vcr_index = require('./shared/indexer.541d43eb.cjs');
4
4
  const sink = require('./shared/indexer.a8b7ab1f.cjs');
5
+ const helper = require('./shared/indexer.318d3617.cjs');
6
+ const plugins_index = require('./plugins/index.cjs');
5
7
  require('node:fs/promises');
6
8
  require('node:path');
7
- require('consola');
8
9
  require('klona/full');
10
+ require('consola');
11
+ require('hookable');
9
12
  require('node:assert');
10
13
  require('node:fs');
11
14
  require('@apibara/protocol/testing');
12
- require('hookable');
13
15
  require('node:async_hooks');
14
16
  require('unctx');
15
17
  require('@opentelemetry/api');
16
18
 
17
- function defineIndexerPlugin(def) {
18
- return def;
19
- }
20
-
21
19
  function useKVStore() {
22
- const ctx = replay.useIndexerContext();
20
+ const ctx = helper.useIndexerContext();
23
21
  if (!ctx?.kv)
24
22
  throw new Error("KV Plugin is not available in context!");
25
23
  return ctx.kv;
@@ -34,19 +32,19 @@ function useSink({
34
32
  return context.sinkTransaction;
35
33
  }
36
34
 
37
- exports.createIndexer = replay.createIndexer;
38
- exports.defineIndexer = replay.defineIndexer;
39
- exports.deserialize = replay.deserialize;
40
- exports.isCassetteAvailable = replay.isCassetteAvailable;
41
- exports.loadCassette = replay.loadCassette;
42
- exports.record = replay.record;
43
- exports.replay = replay.replay;
44
- exports.run = replay.run;
45
- exports.serialize = replay.serialize;
46
- exports.useIndexerContext = replay.useIndexerContext;
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;
47
41
  exports.DefaultSink = sink.DefaultSink;
48
42
  exports.Sink = sink.Sink;
49
43
  exports.defaultSink = sink.defaultSink;
50
- exports.defineIndexerPlugin = defineIndexerPlugin;
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;
51
49
  exports.useKVStore = useKVStore;
52
50
  exports.useSink = useSink;
package/dist/index.d.cts CHANGED
@@ -1,41 +1,12 @@
1
- import { V as VcrConfig, I as Indexer, C as CassetteOptions, a as IndexerContext } from './shared/indexer.ff960804.cjs';
2
- export { c as IndexerConfig, b as IndexerHooks, g as IndexerPlugin, d as IndexerWithStreamConfig, j as VcrReplayResult, f as createIndexer, e as defineIndexer, h as defineIndexerPlugin, l as loadCassette, i as replay, r as run, u as useIndexerContext } from './shared/indexer.ff960804.cjs';
3
- export { D as DefaultSink, b as Sink, a as SinkCursorParams, S as SinkData, d as defaultSink } from './shared/indexer.d227f25c.cjs';
4
- import { StreamDataResponse, Client, DataFinality, Cursor } from '@apibara/protocol';
5
- import { Database } from 'better-sqlite3';
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';
6
8
  import 'hookable';
7
-
8
- declare function deserialize(str: string): any;
9
- declare function serialize(obj: Record<string, unknown>): string;
10
- declare function isCassetteAvailable(vcrConfig: VcrConfig, cassetteName: string): boolean;
11
-
12
- type CassetteDataType<TFilter, TBlock> = {
13
- filter: TFilter;
14
- messages: StreamDataResponse<TBlock>[];
15
- };
16
- declare function record<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, client: Client<TFilter, TBlock>, indexerArg: Indexer<TFilter, TBlock, TTxnParams>, cassetteOptions: CassetteOptions): Promise<void>;
17
-
18
- declare class KVStore {
19
- private _db;
20
- private _finality;
21
- private _endCursor;
22
- /** Sqlite Queries Prepare Statements */
23
- private _beginTxnQuery;
24
- private _commitTxnQuery;
25
- private _rollbackTxnQuery;
26
- private _getQuery;
27
- private _updateToBlockQuery;
28
- private _insertIntoKvsQuery;
29
- private _delQuery;
30
- constructor(_db: Database, _finality: DataFinality, _endCursor: Cursor);
31
- static initialize(db: Database): void;
32
- beginTransaction(): void;
33
- commitTransaction(): void;
34
- rollbackTransaction(): void;
35
- get<T>(key: string): T;
36
- put<T>(key: string, value: T): void;
37
- del(key: string): void;
38
- }
9
+ import 'better-sqlite3';
39
10
 
40
11
  type UseKVStoreResult = InstanceType<typeof KVStore>;
41
12
  declare function useKVStore(): UseKVStoreResult;
@@ -44,4 +15,4 @@ declare function useSink<TTxnParams>({ context, }: {
44
15
  context: IndexerContext<TTxnParams>;
45
16
  }): NonNullable<TTxnParams>;
46
17
 
47
- export { type CassetteDataType, CassetteOptions, Indexer, type UseKVStoreResult, VcrConfig, deserialize, isCassetteAvailable, record, serialize, useKVStore, useSink };
18
+ export { type UseKVStoreResult, useKVStore, useSink };
package/dist/index.d.mts CHANGED
@@ -1,41 +1,12 @@
1
- import { V as VcrConfig, I as Indexer, C as CassetteOptions, a as IndexerContext } from './shared/indexer.9aa22867.mjs';
2
- export { c as IndexerConfig, b as IndexerHooks, g as IndexerPlugin, d as IndexerWithStreamConfig, j as VcrReplayResult, f as createIndexer, e as defineIndexer, h as defineIndexerPlugin, l as loadCassette, i as replay, r as run, u as useIndexerContext } from './shared/indexer.9aa22867.mjs';
3
- export { D as DefaultSink, b as Sink, a as SinkCursorParams, S as SinkData, d as defaultSink } from './shared/indexer.d227f25c.mjs';
4
- import { StreamDataResponse, Client, DataFinality, Cursor } from '@apibara/protocol';
5
- import { Database } from 'better-sqlite3';
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';
6
8
  import 'hookable';
7
-
8
- declare function deserialize(str: string): any;
9
- declare function serialize(obj: Record<string, unknown>): string;
10
- declare function isCassetteAvailable(vcrConfig: VcrConfig, cassetteName: string): boolean;
11
-
12
- type CassetteDataType<TFilter, TBlock> = {
13
- filter: TFilter;
14
- messages: StreamDataResponse<TBlock>[];
15
- };
16
- declare function record<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, client: Client<TFilter, TBlock>, indexerArg: Indexer<TFilter, TBlock, TTxnParams>, cassetteOptions: CassetteOptions): Promise<void>;
17
-
18
- declare class KVStore {
19
- private _db;
20
- private _finality;
21
- private _endCursor;
22
- /** Sqlite Queries Prepare Statements */
23
- private _beginTxnQuery;
24
- private _commitTxnQuery;
25
- private _rollbackTxnQuery;
26
- private _getQuery;
27
- private _updateToBlockQuery;
28
- private _insertIntoKvsQuery;
29
- private _delQuery;
30
- constructor(_db: Database, _finality: DataFinality, _endCursor: Cursor);
31
- static initialize(db: Database): void;
32
- beginTransaction(): void;
33
- commitTransaction(): void;
34
- rollbackTransaction(): void;
35
- get<T>(key: string): T;
36
- put<T>(key: string, value: T): void;
37
- del(key: string): void;
38
- }
9
+ import 'better-sqlite3';
39
10
 
40
11
  type UseKVStoreResult = InstanceType<typeof KVStore>;
41
12
  declare function useKVStore(): UseKVStoreResult;
@@ -44,4 +15,4 @@ declare function useSink<TTxnParams>({ context, }: {
44
15
  context: IndexerContext<TTxnParams>;
45
16
  }): NonNullable<TTxnParams>;
46
17
 
47
- export { type CassetteDataType, CassetteOptions, Indexer, type UseKVStoreResult, VcrConfig, deserialize, isCassetteAvailable, record, serialize, useKVStore, useSink };
18
+ export { type UseKVStoreResult, useKVStore, useSink };
package/dist/index.d.ts CHANGED
@@ -1,41 +1,12 @@
1
- import { V as VcrConfig, I as Indexer, C as CassetteOptions, a as IndexerContext } from './shared/indexer.26fbe988.js';
2
- export { c as IndexerConfig, b as IndexerHooks, g as IndexerPlugin, d as IndexerWithStreamConfig, j as VcrReplayResult, f as createIndexer, e as defineIndexer, h as defineIndexerPlugin, l as loadCassette, i as replay, r as run, u as useIndexerContext } from './shared/indexer.26fbe988.js';
3
- export { D as DefaultSink, b as Sink, a as SinkCursorParams, S as SinkData, d as defaultSink } from './shared/indexer.d227f25c.js';
4
- import { StreamDataResponse, Client, DataFinality, Cursor } from '@apibara/protocol';
5
- import { Database } from 'better-sqlite3';
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';
6
8
  import 'hookable';
7
-
8
- declare function deserialize(str: string): any;
9
- declare function serialize(obj: Record<string, unknown>): string;
10
- declare function isCassetteAvailable(vcrConfig: VcrConfig, cassetteName: string): boolean;
11
-
12
- type CassetteDataType<TFilter, TBlock> = {
13
- filter: TFilter;
14
- messages: StreamDataResponse<TBlock>[];
15
- };
16
- declare function record<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, client: Client<TFilter, TBlock>, indexerArg: Indexer<TFilter, TBlock, TTxnParams>, cassetteOptions: CassetteOptions): Promise<void>;
17
-
18
- declare class KVStore {
19
- private _db;
20
- private _finality;
21
- private _endCursor;
22
- /** Sqlite Queries Prepare Statements */
23
- private _beginTxnQuery;
24
- private _commitTxnQuery;
25
- private _rollbackTxnQuery;
26
- private _getQuery;
27
- private _updateToBlockQuery;
28
- private _insertIntoKvsQuery;
29
- private _delQuery;
30
- constructor(_db: Database, _finality: DataFinality, _endCursor: Cursor);
31
- static initialize(db: Database): void;
32
- beginTransaction(): void;
33
- commitTransaction(): void;
34
- rollbackTransaction(): void;
35
- get<T>(key: string): T;
36
- put<T>(key: string, value: T): void;
37
- del(key: string): void;
38
- }
9
+ import 'better-sqlite3';
39
10
 
40
11
  type UseKVStoreResult = InstanceType<typeof KVStore>;
41
12
  declare function useKVStore(): UseKVStoreResult;
@@ -44,4 +15,4 @@ declare function useSink<TTxnParams>({ context, }: {
44
15
  context: IndexerContext<TTxnParams>;
45
16
  }): NonNullable<TTxnParams>;
46
17
 
47
- export { type CassetteDataType, CassetteOptions, Indexer, type UseKVStoreResult, VcrConfig, deserialize, isCassetteAvailable, record, serialize, useKVStore, useSink };
18
+ export { type UseKVStoreResult, useKVStore, useSink };
package/dist/index.mjs CHANGED
@@ -1,22 +1,20 @@
1
- import { u as useIndexerContext } from './shared/indexer.28bd9576.mjs';
2
- export { c as createIndexer, d as defineIndexer, a as deserialize, i as isCassetteAvailable, l as loadCassette, b as record, e as replay, r as run, s as serialize } from './shared/indexer.28bd9576.mjs';
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';
3
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';
4
6
  import 'node:fs/promises';
5
7
  import 'node:path';
6
- import 'consola';
7
8
  import 'klona/full';
9
+ import 'consola';
10
+ import 'hookable';
8
11
  import 'node:assert';
9
12
  import 'node:fs';
10
13
  import '@apibara/protocol/testing';
11
- import 'hookable';
12
14
  import 'node:async_hooks';
13
15
  import 'unctx';
14
16
  import '@opentelemetry/api';
15
17
 
16
- function defineIndexerPlugin(def) {
17
- return def;
18
- }
19
-
20
18
  function useKVStore() {
21
19
  const ctx = useIndexerContext();
22
20
  if (!ctx?.kv)
@@ -33,4 +31,4 @@ function useSink({
33
31
  return context.sinkTransaction;
34
32
  }
35
33
 
36
- export { defineIndexerPlugin, useIndexerContext, useKVStore, useSink };
34
+ export { useIndexerContext, useKVStore, useSink };
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ function defineIndexerPlugin(def) {
4
+ return def;
5
+ }
6
+
7
+ exports.defineIndexerPlugin = defineIndexerPlugin;
@@ -0,0 +1,4 @@
1
+ export { I as IndexerPlugin, h as defineIndexerPlugin } from '../shared/indexer.c7ed6b83.cjs';
2
+ import '@apibara/protocol';
3
+ import 'hookable';
4
+ import '../shared/indexer.b9c8f0d8.cjs';
@@ -0,0 +1,4 @@
1
+ export { I as IndexerPlugin, h as defineIndexerPlugin } from '../shared/indexer.e8bd138d.mjs';
2
+ import '@apibara/protocol';
3
+ import 'hookable';
4
+ import '../shared/indexer.b9c8f0d8.mjs';
@@ -0,0 +1,4 @@
1
+ export { I as IndexerPlugin, h as defineIndexerPlugin } from '../shared/indexer.f761abcd.js';
2
+ import '@apibara/protocol';
3
+ import 'hookable';
4
+ import '../shared/indexer.b9c8f0d8.js';
@@ -0,0 +1,5 @@
1
+ function defineIndexerPlugin(def) {
2
+ return def;
3
+ }
4
+
5
+ export { defineIndexerPlugin };
@@ -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 };