@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,196 @@
|
|
|
1
|
+
import { sql, gt } from 'drizzle-orm';
|
|
2
|
+
import { S as Sink } from '../../shared/indexer.93d6b2eb.mjs';
|
|
3
|
+
import { customType, pgTable as pgTable$1 } from 'drizzle-orm/pg-core';
|
|
4
|
+
import range, { parse, serialize } from 'postgres-range';
|
|
5
|
+
import 'consola';
|
|
6
|
+
|
|
7
|
+
class DrizzleSinkDelete {
|
|
8
|
+
constructor(db, table, endCursor) {
|
|
9
|
+
this.db = db;
|
|
10
|
+
this.table = table;
|
|
11
|
+
this.endCursor = endCursor;
|
|
12
|
+
}
|
|
13
|
+
//@ts-ignore
|
|
14
|
+
where(where) {
|
|
15
|
+
return this.db.update(this.table).set({
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
_cursor: sql`int8range(lower(_cursor), ${Number(this.endCursor?.orderKey)}, '[)')`
|
|
18
|
+
}).where(where);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class Int8Range {
|
|
23
|
+
constructor(range) {
|
|
24
|
+
this.range = range;
|
|
25
|
+
}
|
|
26
|
+
get start() {
|
|
27
|
+
return this.range.lower != null ? {
|
|
28
|
+
value: this.range.lower,
|
|
29
|
+
inclusive: this.range.isLowerBoundClosed()
|
|
30
|
+
} : null;
|
|
31
|
+
}
|
|
32
|
+
get end() {
|
|
33
|
+
return this.range.upper != null ? {
|
|
34
|
+
value: this.range.upper,
|
|
35
|
+
inclusive: this.range.isUpperBoundClosed()
|
|
36
|
+
} : null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const int8range = customType({
|
|
40
|
+
dataType: () => "int8range",
|
|
41
|
+
fromDriver: (value) => {
|
|
42
|
+
if (typeof value !== "string") {
|
|
43
|
+
throw new Error("Expected string");
|
|
44
|
+
}
|
|
45
|
+
const parsed = parse(value, (val) => Number.parseInt(val, 10));
|
|
46
|
+
return new Int8Range(parsed);
|
|
47
|
+
},
|
|
48
|
+
toDriver: (value) => serialize(value.range)
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const pgTable = (name, columns, extraConfig) => {
|
|
52
|
+
return pgTable$1(
|
|
53
|
+
name,
|
|
54
|
+
{
|
|
55
|
+
...columns,
|
|
56
|
+
_cursor: int8range("_cursor").notNull()
|
|
57
|
+
},
|
|
58
|
+
extraConfig
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
const getDrizzleCursor = (cursor_range) => {
|
|
62
|
+
const isArray = Array.isArray(cursor_range);
|
|
63
|
+
const [lower, upper] = isArray ? cursor_range : [cursor_range, void 0];
|
|
64
|
+
let isNoUpperBound = false;
|
|
65
|
+
if (!lower) {
|
|
66
|
+
throw new Error("Lower bound cursor is required");
|
|
67
|
+
}
|
|
68
|
+
if (!upper) {
|
|
69
|
+
isNoUpperBound = true;
|
|
70
|
+
}
|
|
71
|
+
return new Int8Range(
|
|
72
|
+
new range.Range(
|
|
73
|
+
Number(lower),
|
|
74
|
+
Number(upper),
|
|
75
|
+
range.RANGE_LB_INC | (isNoUpperBound ? range.RANGE_UB_INF : 0)
|
|
76
|
+
)
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
class DrizzleSinkInsert {
|
|
81
|
+
constructor(db, table, endCursor) {
|
|
82
|
+
this.db = db;
|
|
83
|
+
this.table = table;
|
|
84
|
+
this.endCursor = endCursor;
|
|
85
|
+
}
|
|
86
|
+
values(values) {
|
|
87
|
+
const originalInsert = this.db.insert(this.table);
|
|
88
|
+
const cursoredValues = (Array.isArray(values) ? values : [values]).map(
|
|
89
|
+
(v) => {
|
|
90
|
+
return {
|
|
91
|
+
...v,
|
|
92
|
+
_cursor: getDrizzleCursor(this.endCursor?.orderKey)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
);
|
|
96
|
+
return originalInsert.values(cursoredValues);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
class DrizzleSinkSelect {
|
|
101
|
+
constructor(db, fields, endCursor) {
|
|
102
|
+
this.db = db;
|
|
103
|
+
this.fields = fields;
|
|
104
|
+
this.endCursor = endCursor;
|
|
105
|
+
}
|
|
106
|
+
from(source) {
|
|
107
|
+
if (this.fields) {
|
|
108
|
+
const originalFrom = this.db.select(this.fields).from(source);
|
|
109
|
+
return {
|
|
110
|
+
...originalFrom,
|
|
111
|
+
where: (where) => {
|
|
112
|
+
const combinedWhere = sql`${where ? sql`${where} AND ` : sql``}upper_inf(_cursor)`;
|
|
113
|
+
return originalFrom.where(combinedWhere);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return this.db.select().from(source).where(sql`upper_inf(_cursor)`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
class DrizzleSinkUpdate {
|
|
122
|
+
constructor(db, table, endCursor) {
|
|
123
|
+
this.db = db;
|
|
124
|
+
this.table = table;
|
|
125
|
+
this.endCursor = endCursor;
|
|
126
|
+
}
|
|
127
|
+
set(values) {
|
|
128
|
+
const originalUpdate = this.db.update(this.table);
|
|
129
|
+
const originalSet = originalUpdate.set(values);
|
|
130
|
+
return {
|
|
131
|
+
...originalSet,
|
|
132
|
+
where: async (where) => {
|
|
133
|
+
await this.db.update(this.table).set({
|
|
134
|
+
_cursor: sql`int8range(lower(_cursor), ${Number(this.endCursor?.orderKey)}, '[)')`
|
|
135
|
+
}).where(sql`${where ? sql`${where} AND ` : sql``}upper_inf(_cursor)`).execute();
|
|
136
|
+
return originalSet.where(where);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
class DrizzleSinkTransaction {
|
|
143
|
+
constructor(db, endCursor) {
|
|
144
|
+
this.db = db;
|
|
145
|
+
this.endCursor = endCursor;
|
|
146
|
+
}
|
|
147
|
+
insert(table) {
|
|
148
|
+
return new DrizzleSinkInsert(this.db, table, this.endCursor);
|
|
149
|
+
}
|
|
150
|
+
update(table) {
|
|
151
|
+
return new DrizzleSinkUpdate(this.db, table, this.endCursor);
|
|
152
|
+
}
|
|
153
|
+
delete(table) {
|
|
154
|
+
return new DrizzleSinkDelete(this.db, table, this.endCursor);
|
|
155
|
+
}
|
|
156
|
+
select(fields) {
|
|
157
|
+
return new DrizzleSinkSelect(this.db, fields, this.endCursor);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
var __defProp = Object.defineProperty;
|
|
162
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
163
|
+
var __publicField = (obj, key, value) => {
|
|
164
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
165
|
+
return value;
|
|
166
|
+
};
|
|
167
|
+
class DrizzleSink extends Sink {
|
|
168
|
+
constructor(options) {
|
|
169
|
+
super();
|
|
170
|
+
__publicField(this, "_db");
|
|
171
|
+
__publicField(this, "_tables");
|
|
172
|
+
const { database, tables } = options;
|
|
173
|
+
this._db = database;
|
|
174
|
+
this._tables = tables;
|
|
175
|
+
}
|
|
176
|
+
async transaction({ cursor, endCursor, finality }, cb) {
|
|
177
|
+
await this._db.transaction(async (db) => {
|
|
178
|
+
await cb({ db: new DrizzleSinkTransaction(db, endCursor) });
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
async invalidate(cursor) {
|
|
182
|
+
await this._db.transaction(async (db) => {
|
|
183
|
+
for (const table of this._tables) {
|
|
184
|
+
await db.delete(table).where(gt(sql`lower(_cursor)`, sql`${Number(cursor?.orderKey)}`)).returning();
|
|
185
|
+
await db.update(table).set({
|
|
186
|
+
_cursor: sql`int8range(lower(_cursor), NULL, '[)')`
|
|
187
|
+
}).where(gt(sql`upper(_cursor)`, sql`${Number(cursor?.orderKey)}`));
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
const drizzle = (args) => {
|
|
193
|
+
return new DrizzleSink(args);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
export { DrizzleSink, DrizzleSinkDelete, DrizzleSinkTransaction, DrizzleSinkUpdate, Int8Range, drizzle, getDrizzleCursor, int8range, pgTable };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const sink = require('../shared/indexer.a8b7ab1f.cjs');
|
|
4
|
+
require('consola');
|
|
5
|
+
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __publicField = (obj, key, value) => {
|
|
9
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
10
|
+
return value;
|
|
11
|
+
};
|
|
12
|
+
const transactionHelper = (context) => {
|
|
13
|
+
return {
|
|
14
|
+
insert: (data) => {
|
|
15
|
+
context.buffer.push(...data);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
class SqliteSink extends sink.Sink {
|
|
20
|
+
constructor(options) {
|
|
21
|
+
super();
|
|
22
|
+
__publicField(this, "_config");
|
|
23
|
+
__publicField(this, "_db");
|
|
24
|
+
const { database, ...config } = options;
|
|
25
|
+
this._config = config;
|
|
26
|
+
this._db = database;
|
|
27
|
+
}
|
|
28
|
+
async write({
|
|
29
|
+
data,
|
|
30
|
+
endCursor
|
|
31
|
+
}) {
|
|
32
|
+
data = this.processCursorColumn(data, endCursor);
|
|
33
|
+
await this.insertJsonArray(data);
|
|
34
|
+
}
|
|
35
|
+
async transaction({ cursor, endCursor, finality }, cb) {
|
|
36
|
+
const context = {
|
|
37
|
+
buffer: []
|
|
38
|
+
};
|
|
39
|
+
const writer = transactionHelper(context);
|
|
40
|
+
await cb({ writer });
|
|
41
|
+
await this.write({ data: context.buffer, endCursor });
|
|
42
|
+
}
|
|
43
|
+
async invalidate(cursor) {
|
|
44
|
+
throw new Error("Not implemented");
|
|
45
|
+
}
|
|
46
|
+
async insertJsonArray(data) {
|
|
47
|
+
if (data.length === 0)
|
|
48
|
+
return;
|
|
49
|
+
const columns = Object.keys(data[0]);
|
|
50
|
+
const columnNames = columns.join(", ");
|
|
51
|
+
const placeholders = columns.map(() => "?").join(", ");
|
|
52
|
+
const conflictClause = this.buildConflictClause();
|
|
53
|
+
const insertSQL = `INSERT INTO ${this._config.tableName} (${columnNames}) VALUES `;
|
|
54
|
+
const valuePlaceholders = data.map(() => `(${placeholders})`).join(", ");
|
|
55
|
+
const statement = insertSQL + valuePlaceholders + conflictClause;
|
|
56
|
+
const values = data.flatMap((row) => columns.map((col) => row[col]));
|
|
57
|
+
this._db.prepare(statement).run(values);
|
|
58
|
+
}
|
|
59
|
+
processCursorColumn(data, endCursor) {
|
|
60
|
+
const { cursorColumn } = this._config;
|
|
61
|
+
if (cursorColumn && data.some(
|
|
62
|
+
(row) => Number(row[cursorColumn]) !== Number(endCursor?.orderKey)
|
|
63
|
+
)) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Mismatch of ${cursorColumn} and Cursor ${Number(endCursor?.orderKey)}`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
if (cursorColumn) {
|
|
69
|
+
return data;
|
|
70
|
+
}
|
|
71
|
+
return data.map((row) => ({
|
|
72
|
+
...row,
|
|
73
|
+
_cursor: Number(endCursor?.orderKey)
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
buildConflictClause() {
|
|
77
|
+
const { on, update } = this._config.onConflict || {};
|
|
78
|
+
if (on && update && update.length > 0) {
|
|
79
|
+
const updateColumns = update.map((col) => `${col}=excluded.${col}`).join(", ");
|
|
80
|
+
return ` ON CONFLICT(${on}) DO UPDATE SET ${updateColumns}`;
|
|
81
|
+
}
|
|
82
|
+
return "";
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const sqlite = (args) => {
|
|
86
|
+
return new SqliteSink(args);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
exports.SqliteSink = SqliteSink;
|
|
90
|
+
exports.sqlite = sqlite;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Cursor } from '@apibara/protocol';
|
|
2
|
+
import { Database } from 'better-sqlite3';
|
|
3
|
+
import { S as Sink, a as SinkCursorParams, b as SinkData } from '../shared/indexer.b9c8f0d8.cjs';
|
|
4
|
+
|
|
5
|
+
type SqliteSinkOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Database instance of better-sqlite3
|
|
8
|
+
*/
|
|
9
|
+
database: Database;
|
|
10
|
+
/**
|
|
11
|
+
* The name of the table where data will be inserted.
|
|
12
|
+
*/
|
|
13
|
+
tableName: string;
|
|
14
|
+
/**
|
|
15
|
+
* An optional column name used to store the cursor value. If specified,
|
|
16
|
+
* the value of this column must match the `endCursor.orderKey` for each row.
|
|
17
|
+
*/
|
|
18
|
+
cursorColumn?: string;
|
|
19
|
+
/**
|
|
20
|
+
* An optional configuration to handle conflicts during data insertion.
|
|
21
|
+
* - `on`: The column name on which conflicts are detected.
|
|
22
|
+
* - `update`: An array of column names to be updated if a conflict occurs.
|
|
23
|
+
*/
|
|
24
|
+
onConflict?: {
|
|
25
|
+
on: string;
|
|
26
|
+
update: string[];
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
type TxnParams = {
|
|
30
|
+
writer: {
|
|
31
|
+
insert: (data: SinkData[]) => void;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* A sink that writes data to a SQLite database.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* const sink = sqlite({
|
|
41
|
+
* database: db,
|
|
42
|
+
* tableName: "test",
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* ...
|
|
46
|
+
* async transform({context, endCursor}){
|
|
47
|
+
* const { writer } = useSink(context);
|
|
48
|
+
* const insertHelper = writer(endCursor);
|
|
49
|
+
*
|
|
50
|
+
* insertHelper.insert([
|
|
51
|
+
* { id: 1, name: "John" },
|
|
52
|
+
* { id: 2, name: "Jane" },
|
|
53
|
+
* ]);
|
|
54
|
+
* }
|
|
55
|
+
*
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare class SqliteSink extends Sink {
|
|
59
|
+
private _config;
|
|
60
|
+
private _db;
|
|
61
|
+
constructor(options: SqliteSinkOptions);
|
|
62
|
+
private write;
|
|
63
|
+
transaction({ cursor, endCursor, finality }: SinkCursorParams, cb: (params: TxnParams) => Promise<void>): Promise<void>;
|
|
64
|
+
invalidate(cursor?: Cursor): Promise<void>;
|
|
65
|
+
private insertJsonArray;
|
|
66
|
+
private processCursorColumn;
|
|
67
|
+
private buildConflictClause;
|
|
68
|
+
}
|
|
69
|
+
declare const sqlite: (args: SqliteSinkOptions) => SqliteSink;
|
|
70
|
+
|
|
71
|
+
export { SqliteSink, type SqliteSinkOptions, sqlite };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Cursor } from '@apibara/protocol';
|
|
2
|
+
import { Database } from 'better-sqlite3';
|
|
3
|
+
import { S as Sink, a as SinkCursorParams, b as SinkData } from '../shared/indexer.b9c8f0d8.mjs';
|
|
4
|
+
|
|
5
|
+
type SqliteSinkOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Database instance of better-sqlite3
|
|
8
|
+
*/
|
|
9
|
+
database: Database;
|
|
10
|
+
/**
|
|
11
|
+
* The name of the table where data will be inserted.
|
|
12
|
+
*/
|
|
13
|
+
tableName: string;
|
|
14
|
+
/**
|
|
15
|
+
* An optional column name used to store the cursor value. If specified,
|
|
16
|
+
* the value of this column must match the `endCursor.orderKey` for each row.
|
|
17
|
+
*/
|
|
18
|
+
cursorColumn?: string;
|
|
19
|
+
/**
|
|
20
|
+
* An optional configuration to handle conflicts during data insertion.
|
|
21
|
+
* - `on`: The column name on which conflicts are detected.
|
|
22
|
+
* - `update`: An array of column names to be updated if a conflict occurs.
|
|
23
|
+
*/
|
|
24
|
+
onConflict?: {
|
|
25
|
+
on: string;
|
|
26
|
+
update: string[];
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
type TxnParams = {
|
|
30
|
+
writer: {
|
|
31
|
+
insert: (data: SinkData[]) => void;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* A sink that writes data to a SQLite database.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* const sink = sqlite({
|
|
41
|
+
* database: db,
|
|
42
|
+
* tableName: "test",
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* ...
|
|
46
|
+
* async transform({context, endCursor}){
|
|
47
|
+
* const { writer } = useSink(context);
|
|
48
|
+
* const insertHelper = writer(endCursor);
|
|
49
|
+
*
|
|
50
|
+
* insertHelper.insert([
|
|
51
|
+
* { id: 1, name: "John" },
|
|
52
|
+
* { id: 2, name: "Jane" },
|
|
53
|
+
* ]);
|
|
54
|
+
* }
|
|
55
|
+
*
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare class SqliteSink extends Sink {
|
|
59
|
+
private _config;
|
|
60
|
+
private _db;
|
|
61
|
+
constructor(options: SqliteSinkOptions);
|
|
62
|
+
private write;
|
|
63
|
+
transaction({ cursor, endCursor, finality }: SinkCursorParams, cb: (params: TxnParams) => Promise<void>): Promise<void>;
|
|
64
|
+
invalidate(cursor?: Cursor): Promise<void>;
|
|
65
|
+
private insertJsonArray;
|
|
66
|
+
private processCursorColumn;
|
|
67
|
+
private buildConflictClause;
|
|
68
|
+
}
|
|
69
|
+
declare const sqlite: (args: SqliteSinkOptions) => SqliteSink;
|
|
70
|
+
|
|
71
|
+
export { SqliteSink, type SqliteSinkOptions, sqlite };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Cursor } from '@apibara/protocol';
|
|
2
|
+
import { Database } from 'better-sqlite3';
|
|
3
|
+
import { S as Sink, a as SinkCursorParams, b as SinkData } from '../shared/indexer.b9c8f0d8.js';
|
|
4
|
+
|
|
5
|
+
type SqliteSinkOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* Database instance of better-sqlite3
|
|
8
|
+
*/
|
|
9
|
+
database: Database;
|
|
10
|
+
/**
|
|
11
|
+
* The name of the table where data will be inserted.
|
|
12
|
+
*/
|
|
13
|
+
tableName: string;
|
|
14
|
+
/**
|
|
15
|
+
* An optional column name used to store the cursor value. If specified,
|
|
16
|
+
* the value of this column must match the `endCursor.orderKey` for each row.
|
|
17
|
+
*/
|
|
18
|
+
cursorColumn?: string;
|
|
19
|
+
/**
|
|
20
|
+
* An optional configuration to handle conflicts during data insertion.
|
|
21
|
+
* - `on`: The column name on which conflicts are detected.
|
|
22
|
+
* - `update`: An array of column names to be updated if a conflict occurs.
|
|
23
|
+
*/
|
|
24
|
+
onConflict?: {
|
|
25
|
+
on: string;
|
|
26
|
+
update: string[];
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
type TxnParams = {
|
|
30
|
+
writer: {
|
|
31
|
+
insert: (data: SinkData[]) => void;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* A sink that writes data to a SQLite database.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* const sink = sqlite({
|
|
41
|
+
* database: db,
|
|
42
|
+
* tableName: "test",
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* ...
|
|
46
|
+
* async transform({context, endCursor}){
|
|
47
|
+
* const { writer } = useSink(context);
|
|
48
|
+
* const insertHelper = writer(endCursor);
|
|
49
|
+
*
|
|
50
|
+
* insertHelper.insert([
|
|
51
|
+
* { id: 1, name: "John" },
|
|
52
|
+
* { id: 2, name: "Jane" },
|
|
53
|
+
* ]);
|
|
54
|
+
* }
|
|
55
|
+
*
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare class SqliteSink extends Sink {
|
|
59
|
+
private _config;
|
|
60
|
+
private _db;
|
|
61
|
+
constructor(options: SqliteSinkOptions);
|
|
62
|
+
private write;
|
|
63
|
+
transaction({ cursor, endCursor, finality }: SinkCursorParams, cb: (params: TxnParams) => Promise<void>): Promise<void>;
|
|
64
|
+
invalidate(cursor?: Cursor): Promise<void>;
|
|
65
|
+
private insertJsonArray;
|
|
66
|
+
private processCursorColumn;
|
|
67
|
+
private buildConflictClause;
|
|
68
|
+
}
|
|
69
|
+
declare const sqlite: (args: SqliteSinkOptions) => SqliteSink;
|
|
70
|
+
|
|
71
|
+
export { SqliteSink, type SqliteSinkOptions, sqlite };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { S as Sink } from '../shared/indexer.93d6b2eb.mjs';
|
|
2
|
+
import 'consola';
|
|
3
|
+
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __publicField = (obj, key, value) => {
|
|
7
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
|
+
return value;
|
|
9
|
+
};
|
|
10
|
+
const transactionHelper = (context) => {
|
|
11
|
+
return {
|
|
12
|
+
insert: (data) => {
|
|
13
|
+
context.buffer.push(...data);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
class SqliteSink extends Sink {
|
|
18
|
+
constructor(options) {
|
|
19
|
+
super();
|
|
20
|
+
__publicField(this, "_config");
|
|
21
|
+
__publicField(this, "_db");
|
|
22
|
+
const { database, ...config } = options;
|
|
23
|
+
this._config = config;
|
|
24
|
+
this._db = database;
|
|
25
|
+
}
|
|
26
|
+
async write({
|
|
27
|
+
data,
|
|
28
|
+
endCursor
|
|
29
|
+
}) {
|
|
30
|
+
data = this.processCursorColumn(data, endCursor);
|
|
31
|
+
await this.insertJsonArray(data);
|
|
32
|
+
}
|
|
33
|
+
async transaction({ cursor, endCursor, finality }, cb) {
|
|
34
|
+
const context = {
|
|
35
|
+
buffer: []
|
|
36
|
+
};
|
|
37
|
+
const writer = transactionHelper(context);
|
|
38
|
+
await cb({ writer });
|
|
39
|
+
await this.write({ data: context.buffer, endCursor });
|
|
40
|
+
}
|
|
41
|
+
async invalidate(cursor) {
|
|
42
|
+
throw new Error("Not implemented");
|
|
43
|
+
}
|
|
44
|
+
async insertJsonArray(data) {
|
|
45
|
+
if (data.length === 0)
|
|
46
|
+
return;
|
|
47
|
+
const columns = Object.keys(data[0]);
|
|
48
|
+
const columnNames = columns.join(", ");
|
|
49
|
+
const placeholders = columns.map(() => "?").join(", ");
|
|
50
|
+
const conflictClause = this.buildConflictClause();
|
|
51
|
+
const insertSQL = `INSERT INTO ${this._config.tableName} (${columnNames}) VALUES `;
|
|
52
|
+
const valuePlaceholders = data.map(() => `(${placeholders})`).join(", ");
|
|
53
|
+
const statement = insertSQL + valuePlaceholders + conflictClause;
|
|
54
|
+
const values = data.flatMap((row) => columns.map((col) => row[col]));
|
|
55
|
+
this._db.prepare(statement).run(values);
|
|
56
|
+
}
|
|
57
|
+
processCursorColumn(data, endCursor) {
|
|
58
|
+
const { cursorColumn } = this._config;
|
|
59
|
+
if (cursorColumn && data.some(
|
|
60
|
+
(row) => Number(row[cursorColumn]) !== Number(endCursor?.orderKey)
|
|
61
|
+
)) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`Mismatch of ${cursorColumn} and Cursor ${Number(endCursor?.orderKey)}`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
if (cursorColumn) {
|
|
67
|
+
return data;
|
|
68
|
+
}
|
|
69
|
+
return data.map((row) => ({
|
|
70
|
+
...row,
|
|
71
|
+
_cursor: Number(endCursor?.orderKey)
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
buildConflictClause() {
|
|
75
|
+
const { on, update } = this._config.onConflict || {};
|
|
76
|
+
if (on && update && update.length > 0) {
|
|
77
|
+
const updateColumns = update.map((col) => `${col}=excluded.${col}`).join(", ");
|
|
78
|
+
return ` ON CONFLICT(${on}) DO UPDATE SET ${updateColumns}`;
|
|
79
|
+
}
|
|
80
|
+
return "";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const sqlite = (args) => {
|
|
84
|
+
return new SqliteSink(args);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export { SqliteSink, sqlite };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const vitest = require('vitest');
|
|
4
|
+
const helper = require('../shared/indexer.318d3617.cjs');
|
|
5
|
+
const vcr_index = require('../shared/indexer.541d43eb.cjs');
|
|
6
|
+
require('node:async_hooks');
|
|
7
|
+
require('unctx');
|
|
8
|
+
require('@opentelemetry/api');
|
|
9
|
+
require('node:fs');
|
|
10
|
+
require('node:path');
|
|
11
|
+
require('node:fs/promises');
|
|
12
|
+
require('klona/full');
|
|
13
|
+
require('consola');
|
|
14
|
+
require('hookable');
|
|
15
|
+
require('node:assert');
|
|
16
|
+
require('../shared/indexer.a8b7ab1f.cjs');
|
|
17
|
+
require('@apibara/protocol/testing');
|
|
18
|
+
|
|
19
|
+
const test = vitest.test.extend({
|
|
20
|
+
vcr: {
|
|
21
|
+
withClient
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
async function withClient(cassetteName, range, callback) {
|
|
25
|
+
const vcrConfig = {
|
|
26
|
+
cassetteDir: "cassettes"
|
|
27
|
+
};
|
|
28
|
+
const cassetteOptions = {
|
|
29
|
+
name: cassetteName,
|
|
30
|
+
startingCursor: {
|
|
31
|
+
orderKey: range.fromBlock
|
|
32
|
+
},
|
|
33
|
+
endingCursor: {
|
|
34
|
+
orderKey: range.toBlock
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const context = {
|
|
38
|
+
async run(indexer) {
|
|
39
|
+
const client = vcr_index.loadCassette(vcrConfig, cassetteName);
|
|
40
|
+
if (!helper.isCassetteAvailable(vcrConfig, cassetteName)) {
|
|
41
|
+
await vcr_index.record(vcrConfig, client, indexer, cassetteOptions);
|
|
42
|
+
}
|
|
43
|
+
return await vcr_index.replay(vcrConfig, indexer, cassetteName);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
await callback(context);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function generateMockMessages(count = 10) {
|
|
50
|
+
return [...Array(count)].map((_, i) => ({
|
|
51
|
+
_tag: "data",
|
|
52
|
+
data: {
|
|
53
|
+
cursor: { orderKey: BigInt(5e6 - 1) },
|
|
54
|
+
finality: "accepted",
|
|
55
|
+
data: [{ data: `${5e6 + i}` }],
|
|
56
|
+
endCursor: { orderKey: BigInt(5e6 + i) }
|
|
57
|
+
}
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
exports.VcrSink = vcr_index.VcrSink;
|
|
62
|
+
exports.vcr = vcr_index.vcr;
|
|
63
|
+
exports.generateMockMessages = generateMockMessages;
|
|
64
|
+
exports.test = test;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as vitest from 'vitest';
|
|
2
|
+
import { f as Indexer } from '../shared/indexer.c7ed6b83.cjs';
|
|
3
|
+
import { a as VcrReplayResult } from '../shared/indexer.500fd281.cjs';
|
|
4
|
+
import { Cursor } from '@apibara/protocol';
|
|
5
|
+
import { S as Sink, b as SinkData, a as SinkCursorParams } from '../shared/indexer.b9c8f0d8.cjs';
|
|
6
|
+
import { MockStreamResponse } from '@apibara/protocol/testing';
|
|
7
|
+
import 'hookable';
|
|
8
|
+
|
|
9
|
+
declare const test: vitest.TestAPI<{
|
|
10
|
+
vcr: {
|
|
11
|
+
withClient: typeof withClient;
|
|
12
|
+
};
|
|
13
|
+
}>;
|
|
14
|
+
type WithClientContext<TFilter, TBlock, TTxnParams> = {
|
|
15
|
+
run: (indexerArgs: Indexer<TFilter, TBlock, TTxnParams>) => Promise<VcrReplayResult>;
|
|
16
|
+
};
|
|
17
|
+
declare function withClient<TFilter, TBlock, TTxnParams>(cassetteName: string, range: {
|
|
18
|
+
fromBlock: bigint;
|
|
19
|
+
toBlock: bigint;
|
|
20
|
+
}, callback: (context: WithClientContext<TFilter, TBlock, TTxnParams>) => Promise<void>): Promise<void>;
|
|
21
|
+
|
|
22
|
+
type TxnParams = {
|
|
23
|
+
writer: {
|
|
24
|
+
insert: (data: SinkData[]) => void;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
declare class VcrSink extends Sink {
|
|
28
|
+
result: VcrReplayResult["outputs"];
|
|
29
|
+
write({ data, endCursor }: {
|
|
30
|
+
data: SinkData[];
|
|
31
|
+
endCursor?: Cursor;
|
|
32
|
+
}): void;
|
|
33
|
+
transaction({ cursor, endCursor, finality }: SinkCursorParams, cb: (params: TxnParams) => Promise<void>): Promise<void>;
|
|
34
|
+
invalidate(cursor?: Cursor): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
declare function vcr(): VcrSink;
|
|
37
|
+
|
|
38
|
+
declare function generateMockMessages(count?: number): MockStreamResponse[];
|
|
39
|
+
|
|
40
|
+
export { VcrSink, generateMockMessages, test, vcr };
|