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

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
@@ -1,35 +1,23 @@
1
1
  'use strict';
2
2
 
3
- const fs$1 = require('node:fs/promises');
3
+ const helper = require('./indexer.318d3617.cjs');
4
+ const fs = require('node:fs/promises');
4
5
  const path = require('node:path');
5
- const consola = require('consola');
6
6
  const full = require('klona/full');
7
- const assert = require('node:assert');
8
- const fs = require('node:fs');
9
- const testing = require('@apibara/protocol/testing');
7
+ const consola = require('consola');
10
8
  const hookable = require('hookable');
11
- const node_async_hooks = require('node:async_hooks');
12
- const unctx = require('unctx');
13
- const api = require('@opentelemetry/api');
9
+ const assert = require('node:assert');
14
10
  const sink = require('./indexer.a8b7ab1f.cjs');
11
+ const fs$1 = require('node:fs');
12
+ const testing = require('@apibara/protocol/testing');
15
13
 
16
14
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
17
15
 
18
- const fs__default$1 = /*#__PURE__*/_interopDefaultCompat(fs$1);
16
+ const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
19
17
  const path__default = /*#__PURE__*/_interopDefaultCompat(path);
20
18
  const consola__default = /*#__PURE__*/_interopDefaultCompat(consola);
21
19
  const assert__default = /*#__PURE__*/_interopDefaultCompat(assert);
22
- const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
23
-
24
- const indexerAsyncContext = unctx.getContext("indexer", {
25
- asyncContext: true,
26
- AsyncLocalStorage: node_async_hooks.AsyncLocalStorage
27
- });
28
- function useIndexerContext() {
29
- return indexerAsyncContext.use();
30
- }
31
-
32
- const tracer = api.trace.getTracer("@apibara/indexer");
20
+ const fs__default$1 = /*#__PURE__*/_interopDefaultCompat(fs$1);
33
21
 
34
22
  function defineIndexer(streamConfig) {
35
23
  return (config) => ({
@@ -56,8 +44,8 @@ function createIndexer({
56
44
  return indexer;
57
45
  }
58
46
  async function run(client, indexer) {
59
- await indexerAsyncContext.callAsync({}, async () => {
60
- const context = useIndexerContext();
47
+ await helper.indexerAsyncContext.callAsync({}, async () => {
48
+ const context = helper.useIndexerContext();
61
49
  const sink$1 = indexer.options.sink ?? sink.defaultSink();
62
50
  context.sink = sink$1;
63
51
  await indexer.hooks.callHook("run:before");
@@ -83,7 +71,7 @@ async function run(client, indexer) {
83
71
  await indexer.hooks.callHook("message", { message });
84
72
  switch (message._tag) {
85
73
  case "data": {
86
- await tracer.startActiveSpan("message data", async (span) => {
74
+ await helper.tracer.startActiveSpan("message data", async (span) => {
87
75
  const blocks = message.data.data;
88
76
  const { cursor, endCursor, finality } = message.data;
89
77
  await sink$1.transaction(
@@ -125,7 +113,7 @@ async function run(client, indexer) {
125
113
  block = blocks[0];
126
114
  }
127
115
  if (block) {
128
- await tracer.startActiveSpan("handler", async (span2) => {
116
+ await helper.tracer.startActiveSpan("handler", async (span2) => {
129
117
  await indexer.hooks.callHook("handler:before", {
130
118
  block,
131
119
  endCursor,
@@ -165,7 +153,7 @@ async function run(client, indexer) {
165
153
  break;
166
154
  }
167
155
  case "invalidate": {
168
- await tracer.startActiveSpan("message invalidate", async (span) => {
156
+ await helper.tracer.startActiveSpan("message invalidate", async (span) => {
169
157
  await sink$1.invalidate(message.invalidate.cursor);
170
158
  });
171
159
  break;
@@ -180,24 +168,6 @@ async function run(client, indexer) {
180
168
  });
181
169
  }
182
170
 
183
- function deserialize(str) {
184
- return JSON.parse(
185
- str,
186
- (_, value) => typeof value === "string" && value.match(/^\d+n$/) ? BigInt(value.slice(0, -1)) : value
187
- );
188
- }
189
- function serialize(obj) {
190
- return JSON.stringify(
191
- obj,
192
- (_, value) => typeof value === "bigint" ? `${value.toString()}n` : value,
193
- " "
194
- );
195
- }
196
- function isCassetteAvailable(vcrConfig, cassetteName) {
197
- const filePath = path__default.join(vcrConfig.cassetteDir, `${cassetteName}.json`);
198
- return fs__default.existsSync(filePath);
199
- }
200
-
201
171
  async function record(vcrConfig, client, indexerArg, cassetteOptions) {
202
172
  const indexer = full.klona(indexerArg);
203
173
  const messages = [];
@@ -218,7 +188,7 @@ async function record(vcrConfig, client, indexerArg, cassetteOptions) {
218
188
  vcrConfig.cassetteDir,
219
189
  `${cassetteOptions.name}.json`
220
190
  );
221
- await fs__default$1.writeFile(filePath, serialize(output), { flag: "w" });
191
+ await fs__default.writeFile(filePath, helper.serialize(output), { flag: "w" });
222
192
  }
223
193
  });
224
194
  await run(client, indexer);
@@ -273,8 +243,8 @@ async function replay(vcrConfig, indexer, cassetteName) {
273
243
  }
274
244
  function loadCassette(vcrConfig, cassetteName) {
275
245
  const filePath = path__default.join(vcrConfig.cassetteDir, `${cassetteName}.json`);
276
- const data = fs__default.readFileSync(filePath, "utf8");
277
- const cassetteData = deserialize(data);
246
+ const data = fs__default$1.readFileSync(filePath, "utf8");
247
+ const cassetteData = helper.deserialize(data);
278
248
  const { filter, messages } = cassetteData;
279
249
  return new testing.MockClient((request, options) => {
280
250
  assert__default.deepStrictEqual(
@@ -289,12 +259,8 @@ function loadCassette(vcrConfig, cassetteName) {
289
259
  exports.VcrSink = VcrSink;
290
260
  exports.createIndexer = createIndexer;
291
261
  exports.defineIndexer = defineIndexer;
292
- exports.deserialize = deserialize;
293
- exports.isCassetteAvailable = isCassetteAvailable;
294
262
  exports.loadCassette = loadCassette;
295
263
  exports.record = record;
296
264
  exports.replay = replay;
297
265
  exports.run = run;
298
- exports.serialize = serialize;
299
- exports.useIndexerContext = useIndexerContext;
300
266
  exports.vcr = vcr;
@@ -16,4 +16,4 @@ declare class DefaultSink extends Sink<unknown> {
16
16
  }
17
17
  declare function defaultSink(): DefaultSink;
18
18
 
19
- export { DefaultSink as D, type SinkData as S, type SinkCursorParams as a, Sink as b, defaultSink as d };
19
+ export { DefaultSink as D, Sink as S, type SinkCursorParams as a, type SinkData as b, defaultSink as d };
@@ -16,4 +16,4 @@ declare class DefaultSink extends Sink<unknown> {
16
16
  }
17
17
  declare function defaultSink(): DefaultSink;
18
18
 
19
- export { DefaultSink as D, type SinkData as S, type SinkCursorParams as a, Sink as b, defaultSink as d };
19
+ export { DefaultSink as D, Sink as S, type SinkCursorParams as a, type SinkData as b, defaultSink as d };
@@ -16,4 +16,4 @@ declare class DefaultSink extends Sink<unknown> {
16
16
  }
17
17
  declare function defaultSink(): DefaultSink;
18
18
 
19
- export { DefaultSink as D, type SinkData as S, type SinkCursorParams as a, Sink as b, defaultSink as d };
19
+ export { DefaultSink as D, Sink as S, type SinkCursorParams as a, type SinkData as b, defaultSink as d };
@@ -1,6 +1,6 @@
1
1
  import { StreamDataRequest, StreamDataOptions, Cursor, DataFinality, StreamDataResponse, StreamConfig, Client } from '@apibara/protocol';
2
2
  import { NestedHooks, Hookable } from 'hookable';
3
- import { b as Sink, S as SinkData } from './indexer.d227f25c.js';
3
+ import { S as Sink } from './indexer.b9c8f0d8.cjs';
4
4
 
5
5
  interface IndexerContext<TTxnParams = any> extends Record<string, any> {
6
6
  sink?: Sink<TTxnParams>;
@@ -79,22 +79,4 @@ interface Indexer<TFilter, TBlock, TTxnParams> {
79
79
  declare function createIndexer<TFilter, TBlock, TTxnParams>({ streamConfig, ...options }: IndexerWithStreamConfig<TFilter, TBlock, TTxnParams>): Indexer<TFilter, TBlock, TTxnParams>;
80
80
  declare function run<TFilter, TBlock, TTxnParams>(client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock, TTxnParams>): Promise<void>;
81
81
 
82
- type VcrConfig = {
83
- cassetteDir: string;
84
- };
85
- type CassetteOptions = {
86
- name: string;
87
- startingCursor: Cursor;
88
- endingCursor: Cursor;
89
- };
90
-
91
- declare function replay<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, indexer: Indexer<TFilter, TBlock, TTxnParams>, cassetteName: string): Promise<VcrReplayResult>;
92
- type VcrReplayResult = {
93
- outputs: Array<{
94
- endCursor?: Cursor;
95
- data: SinkData[];
96
- }>;
97
- };
98
- declare function loadCassette<TFilter, TBlock>(vcrConfig: VcrConfig, cassetteName: string): Client<TFilter, TBlock>;
99
-
100
- export { type CassetteOptions as C, type Indexer as I, type VcrConfig as V, type IndexerContext as a, type IndexerHooks as b, type IndexerConfig as c, type IndexerWithStreamConfig as d, defineIndexer as e, createIndexer as f, type IndexerPlugin as g, defineIndexerPlugin as h, replay as i, type VcrReplayResult as j, loadCassette as l, run as r, useIndexerContext as u };
82
+ export { type IndexerPlugin as I, type IndexerContext as a, type IndexerHooks as b, type IndexerConfig as c, type IndexerWithStreamConfig as d, defineIndexer as e, type Indexer as f, createIndexer as g, defineIndexerPlugin as h, run as r, useIndexerContext as u };
@@ -0,0 +1,23 @@
1
+ import { Cursor, Client } from '@apibara/protocol';
2
+ import { f as Indexer } from './indexer.e8bd138d.mjs';
3
+ import { b as SinkData } from './indexer.b9c8f0d8.mjs';
4
+
5
+ type VcrConfig = {
6
+ cassetteDir: string;
7
+ };
8
+ type CassetteOptions = {
9
+ name: string;
10
+ startingCursor: Cursor;
11
+ endingCursor: Cursor;
12
+ };
13
+
14
+ declare function replay<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, indexer: Indexer<TFilter, TBlock, TTxnParams>, cassetteName: string): Promise<VcrReplayResult>;
15
+ type VcrReplayResult = {
16
+ outputs: Array<{
17
+ endCursor?: Cursor;
18
+ data: SinkData[];
19
+ }>;
20
+ };
21
+ declare function loadCassette<TFilter, TBlock>(vcrConfig: VcrConfig, cassetteName: string): Client<TFilter, TBlock>;
22
+
23
+ export { type CassetteOptions as C, type VcrConfig as V, type VcrReplayResult as a, loadCassette as l, replay as r };
@@ -0,0 +1,23 @@
1
+ import { Cursor, Client } from '@apibara/protocol';
2
+ import { f as Indexer } from './indexer.f761abcd.js';
3
+ import { b as SinkData } from './indexer.b9c8f0d8.js';
4
+
5
+ type VcrConfig = {
6
+ cassetteDir: string;
7
+ };
8
+ type CassetteOptions = {
9
+ name: string;
10
+ startingCursor: Cursor;
11
+ endingCursor: Cursor;
12
+ };
13
+
14
+ declare function replay<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, indexer: Indexer<TFilter, TBlock, TTxnParams>, cassetteName: string): Promise<VcrReplayResult>;
15
+ type VcrReplayResult = {
16
+ outputs: Array<{
17
+ endCursor?: Cursor;
18
+ data: SinkData[];
19
+ }>;
20
+ };
21
+ declare function loadCassette<TFilter, TBlock>(vcrConfig: VcrConfig, cassetteName: string): Client<TFilter, TBlock>;
22
+
23
+ export { type CassetteOptions as C, type VcrConfig as V, type VcrReplayResult as a, loadCassette as l, replay as r };
@@ -1,6 +1,6 @@
1
1
  import { StreamDataRequest, StreamDataOptions, Cursor, DataFinality, StreamDataResponse, StreamConfig, Client } from '@apibara/protocol';
2
2
  import { NestedHooks, Hookable } from 'hookable';
3
- import { b as Sink, S as SinkData } from './indexer.d227f25c.cjs';
3
+ import { S as Sink } from './indexer.b9c8f0d8.mjs';
4
4
 
5
5
  interface IndexerContext<TTxnParams = any> extends Record<string, any> {
6
6
  sink?: Sink<TTxnParams>;
@@ -79,22 +79,4 @@ interface Indexer<TFilter, TBlock, TTxnParams> {
79
79
  declare function createIndexer<TFilter, TBlock, TTxnParams>({ streamConfig, ...options }: IndexerWithStreamConfig<TFilter, TBlock, TTxnParams>): Indexer<TFilter, TBlock, TTxnParams>;
80
80
  declare function run<TFilter, TBlock, TTxnParams>(client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock, TTxnParams>): Promise<void>;
81
81
 
82
- type VcrConfig = {
83
- cassetteDir: string;
84
- };
85
- type CassetteOptions = {
86
- name: string;
87
- startingCursor: Cursor;
88
- endingCursor: Cursor;
89
- };
90
-
91
- declare function replay<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, indexer: Indexer<TFilter, TBlock, TTxnParams>, cassetteName: string): Promise<VcrReplayResult>;
92
- type VcrReplayResult = {
93
- outputs: Array<{
94
- endCursor?: Cursor;
95
- data: SinkData[];
96
- }>;
97
- };
98
- declare function loadCassette<TFilter, TBlock>(vcrConfig: VcrConfig, cassetteName: string): Client<TFilter, TBlock>;
99
-
100
- export { type CassetteOptions as C, type Indexer as I, type VcrConfig as V, type IndexerContext as a, type IndexerHooks as b, type IndexerConfig as c, type IndexerWithStreamConfig as d, defineIndexer as e, createIndexer as f, type IndexerPlugin as g, defineIndexerPlugin as h, replay as i, type VcrReplayResult as j, loadCassette as l, run as r, useIndexerContext as u };
82
+ export { type IndexerPlugin as I, type IndexerContext as a, type IndexerHooks as b, type IndexerConfig as c, type IndexerWithStreamConfig as d, defineIndexer as e, type Indexer as f, createIndexer as g, defineIndexerPlugin as h, run as r, useIndexerContext as u };
@@ -1,6 +1,6 @@
1
1
  import { StreamDataRequest, StreamDataOptions, Cursor, DataFinality, StreamDataResponse, StreamConfig, Client } from '@apibara/protocol';
2
2
  import { NestedHooks, Hookable } from 'hookable';
3
- import { b as Sink, S as SinkData } from './indexer.d227f25c.mjs';
3
+ import { S as Sink } from './indexer.b9c8f0d8.js';
4
4
 
5
5
  interface IndexerContext<TTxnParams = any> extends Record<string, any> {
6
6
  sink?: Sink<TTxnParams>;
@@ -79,22 +79,4 @@ interface Indexer<TFilter, TBlock, TTxnParams> {
79
79
  declare function createIndexer<TFilter, TBlock, TTxnParams>({ streamConfig, ...options }: IndexerWithStreamConfig<TFilter, TBlock, TTxnParams>): Indexer<TFilter, TBlock, TTxnParams>;
80
80
  declare function run<TFilter, TBlock, TTxnParams>(client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock, TTxnParams>): Promise<void>;
81
81
 
82
- type VcrConfig = {
83
- cassetteDir: string;
84
- };
85
- type CassetteOptions = {
86
- name: string;
87
- startingCursor: Cursor;
88
- endingCursor: Cursor;
89
- };
90
-
91
- declare function replay<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, indexer: Indexer<TFilter, TBlock, TTxnParams>, cassetteName: string): Promise<VcrReplayResult>;
92
- type VcrReplayResult = {
93
- outputs: Array<{
94
- endCursor?: Cursor;
95
- data: SinkData[];
96
- }>;
97
- };
98
- declare function loadCassette<TFilter, TBlock>(vcrConfig: VcrConfig, cassetteName: string): Client<TFilter, TBlock>;
99
-
100
- export { type CassetteOptions as C, type Indexer as I, type VcrConfig as V, type IndexerContext as a, type IndexerHooks as b, type IndexerConfig as c, type IndexerWithStreamConfig as d, defineIndexer as e, createIndexer as f, type IndexerPlugin as g, defineIndexerPlugin as h, replay as i, type VcrReplayResult as j, loadCassette as l, run as r, useIndexerContext as u };
82
+ export { type IndexerPlugin as I, type IndexerContext as a, type IndexerHooks as b, type IndexerConfig as c, type IndexerWithStreamConfig as d, defineIndexer as e, type Indexer as f, createIndexer as g, defineIndexerPlugin as h, run as r, useIndexerContext as u };
@@ -1,6 +1,6 @@
1
1
  import { Cursor } from '@apibara/protocol';
2
2
  import { Options, Stringifier } from 'csv-stringify';
3
- import { b as Sink, a as SinkCursorParams, S as SinkData } from '../shared/indexer.d227f25c.cjs';
3
+ import { S as Sink, a as SinkCursorParams, b as SinkData } from '../shared/indexer.b9c8f0d8.cjs';
4
4
 
5
5
  type CsvArgs = {
6
6
  /**
@@ -1,6 +1,6 @@
1
1
  import { Cursor } from '@apibara/protocol';
2
2
  import { Options, Stringifier } from 'csv-stringify';
3
- import { b as Sink, a as SinkCursorParams, S as SinkData } from '../shared/indexer.d227f25c.mjs';
3
+ import { S as Sink, a as SinkCursorParams, b as SinkData } from '../shared/indexer.b9c8f0d8.mjs';
4
4
 
5
5
  type CsvArgs = {
6
6
  /**
@@ -1,6 +1,6 @@
1
1
  import { Cursor } from '@apibara/protocol';
2
2
  import { Options, Stringifier } from 'csv-stringify';
3
- import { b as Sink, a as SinkCursorParams, S as SinkData } from '../shared/indexer.d227f25c.js';
3
+ import { S as Sink, a as SinkCursorParams, b as SinkData } from '../shared/indexer.b9c8f0d8.js';
4
4
 
5
5
  type CsvArgs = {
6
6
  /**
@@ -0,0 +1,210 @@
1
+ 'use strict';
2
+
3
+ const drizzleOrm = require('drizzle-orm');
4
+ const sink = require('../../shared/indexer.a8b7ab1f.cjs');
5
+ const pgCore = require('drizzle-orm/pg-core');
6
+ const range = require('postgres-range');
7
+ require('consola');
8
+
9
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
10
+
11
+ const range__default = /*#__PURE__*/_interopDefaultCompat(range);
12
+
13
+ class DrizzleSinkDelete {
14
+ constructor(db, table, endCursor) {
15
+ this.db = db;
16
+ this.table = table;
17
+ this.endCursor = endCursor;
18
+ }
19
+ //@ts-ignore
20
+ where(where) {
21
+ return this.db.update(this.table).set({
22
+ // @ts-ignore
23
+ _cursor: drizzleOrm.sql`int8range(lower(_cursor), ${Number(this.endCursor?.orderKey)}, '[)')`
24
+ }).where(where);
25
+ }
26
+ }
27
+
28
+ class Int8Range {
29
+ constructor(range) {
30
+ this.range = range;
31
+ }
32
+ get start() {
33
+ return this.range.lower != null ? {
34
+ value: this.range.lower,
35
+ inclusive: this.range.isLowerBoundClosed()
36
+ } : null;
37
+ }
38
+ get end() {
39
+ return this.range.upper != null ? {
40
+ value: this.range.upper,
41
+ inclusive: this.range.isUpperBoundClosed()
42
+ } : null;
43
+ }
44
+ }
45
+ const int8range = pgCore.customType({
46
+ dataType: () => "int8range",
47
+ fromDriver: (value) => {
48
+ if (typeof value !== "string") {
49
+ throw new Error("Expected string");
50
+ }
51
+ const parsed = range.parse(value, (val) => Number.parseInt(val, 10));
52
+ return new Int8Range(parsed);
53
+ },
54
+ toDriver: (value) => range.serialize(value.range)
55
+ });
56
+
57
+ const pgTable = (name, columns, extraConfig) => {
58
+ return pgCore.pgTable(
59
+ name,
60
+ {
61
+ ...columns,
62
+ _cursor: int8range("_cursor").notNull()
63
+ },
64
+ extraConfig
65
+ );
66
+ };
67
+ const getDrizzleCursor = (cursor_range) => {
68
+ const isArray = Array.isArray(cursor_range);
69
+ const [lower, upper] = isArray ? cursor_range : [cursor_range, void 0];
70
+ let isNoUpperBound = false;
71
+ if (!lower) {
72
+ throw new Error("Lower bound cursor is required");
73
+ }
74
+ if (!upper) {
75
+ isNoUpperBound = true;
76
+ }
77
+ return new Int8Range(
78
+ new range__default.Range(
79
+ Number(lower),
80
+ Number(upper),
81
+ range__default.RANGE_LB_INC | (isNoUpperBound ? range__default.RANGE_UB_INF : 0)
82
+ )
83
+ );
84
+ };
85
+
86
+ class DrizzleSinkInsert {
87
+ constructor(db, table, endCursor) {
88
+ this.db = db;
89
+ this.table = table;
90
+ this.endCursor = endCursor;
91
+ }
92
+ values(values) {
93
+ const originalInsert = this.db.insert(this.table);
94
+ const cursoredValues = (Array.isArray(values) ? values : [values]).map(
95
+ (v) => {
96
+ return {
97
+ ...v,
98
+ _cursor: getDrizzleCursor(this.endCursor?.orderKey)
99
+ };
100
+ }
101
+ );
102
+ return originalInsert.values(cursoredValues);
103
+ }
104
+ }
105
+
106
+ class DrizzleSinkSelect {
107
+ constructor(db, fields, endCursor) {
108
+ this.db = db;
109
+ this.fields = fields;
110
+ this.endCursor = endCursor;
111
+ }
112
+ from(source) {
113
+ if (this.fields) {
114
+ const originalFrom = this.db.select(this.fields).from(source);
115
+ return {
116
+ ...originalFrom,
117
+ where: (where) => {
118
+ const combinedWhere = drizzleOrm.sql`${where ? drizzleOrm.sql`${where} AND ` : drizzleOrm.sql``}upper_inf(_cursor)`;
119
+ return originalFrom.where(combinedWhere);
120
+ }
121
+ };
122
+ }
123
+ return this.db.select().from(source).where(drizzleOrm.sql`upper_inf(_cursor)`);
124
+ }
125
+ }
126
+
127
+ class DrizzleSinkUpdate {
128
+ constructor(db, table, endCursor) {
129
+ this.db = db;
130
+ this.table = table;
131
+ this.endCursor = endCursor;
132
+ }
133
+ set(values) {
134
+ const originalUpdate = this.db.update(this.table);
135
+ const originalSet = originalUpdate.set(values);
136
+ return {
137
+ ...originalSet,
138
+ where: async (where) => {
139
+ await this.db.update(this.table).set({
140
+ _cursor: drizzleOrm.sql`int8range(lower(_cursor), ${Number(this.endCursor?.orderKey)}, '[)')`
141
+ }).where(drizzleOrm.sql`${where ? drizzleOrm.sql`${where} AND ` : drizzleOrm.sql``}upper_inf(_cursor)`).execute();
142
+ return originalSet.where(where);
143
+ }
144
+ };
145
+ }
146
+ }
147
+
148
+ class DrizzleSinkTransaction {
149
+ constructor(db, endCursor) {
150
+ this.db = db;
151
+ this.endCursor = endCursor;
152
+ }
153
+ insert(table) {
154
+ return new DrizzleSinkInsert(this.db, table, this.endCursor);
155
+ }
156
+ update(table) {
157
+ return new DrizzleSinkUpdate(this.db, table, this.endCursor);
158
+ }
159
+ delete(table) {
160
+ return new DrizzleSinkDelete(this.db, table, this.endCursor);
161
+ }
162
+ select(fields) {
163
+ return new DrizzleSinkSelect(this.db, fields, this.endCursor);
164
+ }
165
+ }
166
+
167
+ var __defProp = Object.defineProperty;
168
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
169
+ var __publicField = (obj, key, value) => {
170
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
171
+ return value;
172
+ };
173
+ class DrizzleSink extends sink.Sink {
174
+ constructor(options) {
175
+ super();
176
+ __publicField(this, "_db");
177
+ __publicField(this, "_tables");
178
+ const { database, tables } = options;
179
+ this._db = database;
180
+ this._tables = tables;
181
+ }
182
+ async transaction({ cursor, endCursor, finality }, cb) {
183
+ await this._db.transaction(async (db) => {
184
+ await cb({ db: new DrizzleSinkTransaction(db, endCursor) });
185
+ });
186
+ }
187
+ async invalidate(cursor) {
188
+ await this._db.transaction(async (db) => {
189
+ for (const table of this._tables) {
190
+ await db.delete(table).where(drizzleOrm.gt(drizzleOrm.sql`lower(_cursor)`, drizzleOrm.sql`${Number(cursor?.orderKey)}`)).returning();
191
+ await db.update(table).set({
192
+ _cursor: drizzleOrm.sql`int8range(lower(_cursor), NULL, '[)')`
193
+ }).where(drizzleOrm.gt(drizzleOrm.sql`upper(_cursor)`, drizzleOrm.sql`${Number(cursor?.orderKey)}`));
194
+ }
195
+ });
196
+ }
197
+ }
198
+ const drizzle = (args) => {
199
+ return new DrizzleSink(args);
200
+ };
201
+
202
+ exports.DrizzleSink = DrizzleSink;
203
+ exports.DrizzleSinkDelete = DrizzleSinkDelete;
204
+ exports.DrizzleSinkTransaction = DrizzleSinkTransaction;
205
+ exports.DrizzleSinkUpdate = DrizzleSinkUpdate;
206
+ exports.Int8Range = Int8Range;
207
+ exports.drizzle = drizzle;
208
+ exports.getDrizzleCursor = getDrizzleCursor;
209
+ exports.int8range = int8range;
210
+ exports.pgTable = pgTable;
@@ -0,0 +1,111 @@
1
+ import { Cursor } from '@apibara/protocol';
2
+ import { TablesRelationalConfig, ExtractTablesWithRelations, SQL } from 'drizzle-orm';
3
+ import * as drizzle_orm_pg_core from 'drizzle-orm/pg-core';
4
+ import { PgTable, PgQueryResultHKT, PgTransaction, PgUpdateBase, PgInsertValue, PgUpdateSetSource, PgSelectBuilder, SelectedFields, TableConfig, PgTableWithColumns, PgDatabase, AnyPgTable, PgTableFn } from 'drizzle-orm/pg-core';
5
+ import { S as Sink, a as SinkCursorParams } from '../../shared/indexer.b9c8f0d8.cjs';
6
+ import { Range } from 'postgres-range';
7
+
8
+ declare class DrizzleSinkDelete<TTable extends PgTable, TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
9
+ private db;
10
+ private table;
11
+ private endCursor?;
12
+ constructor(db: PgTransaction<TQueryResult, TFullSchema, TSchema>, table: TTable, endCursor?: Cursor | undefined);
13
+ where(where: SQL): Omit<PgUpdateBase<TTable, TQueryResult, undefined, false, "where">, "where">;
14
+ }
15
+
16
+ declare class DrizzleSinkInsert<TTable extends PgTable, TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
17
+ private db;
18
+ private table;
19
+ private endCursor?;
20
+ constructor(db: PgTransaction<TQueryResult, TFullSchema, TSchema>, table: TTable, endCursor?: Cursor | undefined);
21
+ values(values: PgInsertValue<TTable> | PgInsertValue<TTable>[]): drizzle_orm_pg_core.PgInsertBase<TTable, TQueryResult, undefined, false, never>;
22
+ }
23
+
24
+ declare class DrizzleSinkUpdate<TTable extends PgTable, TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
25
+ private db;
26
+ private table;
27
+ private endCursor?;
28
+ constructor(db: PgTransaction<TQueryResult, TFullSchema, TSchema>, table: TTable, endCursor?: Cursor | undefined);
29
+ set(values: PgUpdateSetSource<TTable>): PgUpdateBase<TTable, TQueryResult>;
30
+ }
31
+
32
+ declare class DrizzleSinkTransaction<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
33
+ private db;
34
+ private endCursor?;
35
+ constructor(db: PgTransaction<TQueryResult, TFullSchema, TSchema>, endCursor?: Cursor | undefined);
36
+ insert<TTable extends PgTable>(table: TTable): DrizzleSinkInsert<TTable, TQueryResult, TFullSchema, TSchema>;
37
+ update<TTable extends PgTable>(table: TTable): DrizzleSinkUpdate<TTable, TQueryResult, TFullSchema, TSchema>;
38
+ delete<TTable extends PgTable>(table: TTable): DrizzleSinkDelete<TTable, TQueryResult, TFullSchema, TSchema>;
39
+ select(): PgSelectBuilder<undefined>;
40
+ select<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;
41
+ }
42
+
43
+ type DrizzleSinkTables<TTableConfig extends Record<string, TableConfig>> = {
44
+ [K in keyof TTableConfig]: PgTableWithColumns<TTableConfig[K]>;
45
+ };
46
+ type DrizzleSinkOptions<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> = {
47
+ /**
48
+ * Database instance of drizzle-orm
49
+ */
50
+ database: PgDatabase<TQueryResult, TFullSchema, TSchema>;
51
+ tables: AnyPgTable[];
52
+ };
53
+ /**
54
+ * A sink that writes data to a PostgreSQL database using Drizzle ORM.
55
+ *
56
+ * @example
57
+ *
58
+ * ```ts
59
+ * const sink = drizzle({
60
+ * database: db,
61
+ * });
62
+ *
63
+ * ...
64
+ * async transform({context, endCursor}){
65
+ * const { transaction } = useSink(context);
66
+ * const db = transaction(endCursor);
67
+ *
68
+ * db.insert(users).values([
69
+ * { id: 1, name: "John" },
70
+ * { id: 2, name: "Jane" },
71
+ * ]);
72
+ * }
73
+ *
74
+ * ```
75
+ */
76
+ declare class DrizzleSink<TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> extends Sink {
77
+ private _db;
78
+ private _tables;
79
+ constructor(options: DrizzleSinkOptions<TQueryResult, TFullSchema, TSchema>);
80
+ transaction({ cursor, endCursor, finality }: SinkCursorParams, cb: (params: {
81
+ db: DrizzleSinkTransaction<TQueryResult, TFullSchema, TSchema>;
82
+ }) => Promise<void>): Promise<void>;
83
+ invalidate(cursor?: Cursor): Promise<void>;
84
+ }
85
+ declare const drizzle: <TQueryResult extends PgQueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>>(args: DrizzleSinkOptions<TQueryResult, TFullSchema, TSchema>) => DrizzleSink<TQueryResult, TFullSchema, TSchema>;
86
+
87
+ type Comparable = string | number;
88
+ type RangeBound<T extends Comparable> = T | {
89
+ value: T;
90
+ inclusive: boolean;
91
+ };
92
+ declare class Int8Range {
93
+ readonly range: Range<number>;
94
+ constructor(range: Range<number>);
95
+ get start(): RangeBound<number> | null;
96
+ get end(): RangeBound<number> | null;
97
+ }
98
+ declare const int8range: <TName extends string>(dbName: TName, fieldConfig?: unknown) => drizzle_orm_pg_core.PgCustomColumnBuilder<{
99
+ name: TName;
100
+ dataType: "custom";
101
+ columnType: "PgCustomColumn";
102
+ data: Int8Range;
103
+ driverParam: unknown;
104
+ enumValues: undefined;
105
+ generated: undefined;
106
+ }>;
107
+
108
+ declare const pgTable: PgTableFn;
109
+ declare const getDrizzleCursor: (cursor_range: [bigint | undefined, bigint | undefined] | bigint | undefined) => Int8Range;
110
+
111
+ export { DrizzleSink, DrizzleSinkDelete, type DrizzleSinkOptions, type DrizzleSinkTables, DrizzleSinkTransaction, DrizzleSinkUpdate, Int8Range, drizzle, getDrizzleCursor, int8range, pgTable };