@secondlayer/subgraphs 4.4.1 → 5.0.1

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 (65) hide show
  1. package/README.md +13 -21
  2. package/dist/{src/index.d.ts → index.d.ts} +59 -189
  3. package/dist/index.js +2151 -0
  4. package/dist/index.js.map +24 -0
  5. package/dist/{src/runtime → runtime}/emitter.d.ts +17 -11
  6. package/dist/{src/runtime → runtime}/emitter.js +58 -18
  7. package/dist/runtime/emitter.js.map +19 -0
  8. package/dist/{src/runtime → runtime}/replay.d.ts +15 -1
  9. package/dist/{src/runtime → runtime}/replay.js +418 -723
  10. package/dist/{src/runtime → runtime}/replay.js.map +6 -6
  11. package/dist/{src/schema → schema}/index.d.ts +10 -69
  12. package/dist/{src/schema → schema}/index.js +188 -281
  13. package/dist/schema/index.js.map +13 -0
  14. package/dist/{src/testing → testing}/index.d.ts +65 -115
  15. package/dist/{src/testing → testing}/index.js +554 -968
  16. package/dist/testing/index.js.map +21 -0
  17. package/dist/{src/types.d.ts → types.d.ts} +10 -66
  18. package/dist/{src/validate.d.ts → validate.d.ts} +11 -69
  19. package/dist/{src/validate.js → validate.js} +18 -98
  20. package/dist/validate.js.map +10 -0
  21. package/package.json +18 -26
  22. package/dist/src/index.js +0 -6081
  23. package/dist/src/index.js.map +0 -44
  24. package/dist/src/runtime/block-processor.d.ts +0 -559
  25. package/dist/src/runtime/block-processor.js +0 -3810
  26. package/dist/src/runtime/block-processor.js.map +0 -31
  27. package/dist/src/runtime/catchup.d.ts +0 -473
  28. package/dist/src/runtime/catchup.js +0 -4228
  29. package/dist/src/runtime/catchup.js.map +0 -35
  30. package/dist/src/runtime/clarity.d.ts +0 -15
  31. package/dist/src/runtime/clarity.js +0 -41
  32. package/dist/src/runtime/clarity.js.map +0 -10
  33. package/dist/src/runtime/context.d.ts +0 -325
  34. package/dist/src/runtime/context.js +0 -936
  35. package/dist/src/runtime/context.js.map +0 -13
  36. package/dist/src/runtime/emitter.js.map +0 -19
  37. package/dist/src/runtime/processor.d.ts +0 -11
  38. package/dist/src/runtime/processor.js +0 -5372
  39. package/dist/src/runtime/processor.js.map +0 -40
  40. package/dist/src/runtime/reindex.d.ts +0 -539
  41. package/dist/src/runtime/reindex.js +0 -4802
  42. package/dist/src/runtime/reindex.js.map +0 -37
  43. package/dist/src/runtime/reorg.d.ts +0 -432
  44. package/dist/src/runtime/reorg.js +0 -4355
  45. package/dist/src/runtime/reorg.js.map +0 -36
  46. package/dist/src/runtime/runner.d.ts +0 -719
  47. package/dist/src/runtime/runner.js +0 -1332
  48. package/dist/src/runtime/runner.js.map +0 -16
  49. package/dist/src/runtime/source-matcher.d.ts +0 -304
  50. package/dist/src/runtime/source-matcher.js +0 -335
  51. package/dist/src/runtime/source-matcher.js.map +0 -10
  52. package/dist/src/runtime/stats.d.ts +0 -23
  53. package/dist/src/runtime/stats.js +0 -71
  54. package/dist/src/runtime/stats.js.map +0 -10
  55. package/dist/src/schema/index.js.map +0 -13
  56. package/dist/src/service.d.ts +0 -0
  57. package/dist/src/service.js +0 -5398
  58. package/dist/src/service.js.map +0 -41
  59. package/dist/src/testing/index.js.map +0 -22
  60. package/dist/src/triggers/index.d.ts +0 -381
  61. package/dist/src/triggers/index.js +0 -31
  62. package/dist/src/triggers/index.js.map +0 -10
  63. package/dist/src/validate.js.map +0 -10
  64. /package/dist/{src/types.js → types.js} +0 -0
  65. /package/dist/{src/types.js.map → types.js.map} +0 -0
@@ -1,936 +0,0 @@
1
- import { createRequire } from "node:module";
2
- var __require = /* @__PURE__ */ createRequire(import.meta.url);
3
-
4
- // src/runtime/context.ts
5
- import { logger as logger2 } from "@secondlayer/shared/logger";
6
- import { formatUnits } from "@secondlayer/stacks/utils";
7
- import { sql } from "kysely";
8
-
9
- // src/schema/generator.ts
10
- import { createHash } from "node:crypto";
11
-
12
- // src/schema/utils.ts
13
- import { pgSchemaName } from "@secondlayer/shared/db/queries/subgraphs";
14
- function quotePgIdent(name) {
15
- if (/^[a-z_][a-z0-9_]*$/i.test(name))
16
- return name;
17
- return `"${name.replace(/"/g, '""')}"`;
18
- }
19
- function snakeToCamel(name) {
20
- return name.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
21
- }
22
- function pascalCase(name) {
23
- const camel = snakeToCamel(name);
24
- return camel.charAt(0).toUpperCase() + camel.slice(1);
25
- }
26
-
27
- // src/schema/generator.ts
28
- var TYPE_MAP = {
29
- text: "TEXT",
30
- uint: "NUMERIC",
31
- int: "NUMERIC",
32
- principal: "TEXT",
33
- boolean: "BOOLEAN",
34
- timestamp: "TIMESTAMPTZ",
35
- jsonb: "JSONB"
36
- };
37
- var MAX_IDENT_BYTES = 63;
38
- function safeIndexName(name) {
39
- if (name.length <= MAX_IDENT_BYTES)
40
- return name;
41
- const hash = createHash("sha256").update(name).digest("hex").slice(0, 8);
42
- return `${name.slice(0, MAX_IDENT_BYTES - hash.length - 1)}_${hash}`;
43
- }
44
- function emitIndexedColumnIndexDDL(schemaName, tableName, colName, opts) {
45
- const concurrently = opts?.concurrently ? " CONCURRENTLY" : "";
46
- const qualified = `${quotePgIdent(schemaName)}.${quotePgIdent(tableName)}`;
47
- const plain = `idx_${schemaName}_${tableName}_${colName}`;
48
- const composite = safeIndexName(`idx_${schemaName}_${tableName}_${colName}_id`);
49
- const col = quotePgIdent(colName);
50
- const id = quotePgIdent("_id");
51
- return {
52
- create: [
53
- `CREATE INDEX${concurrently} IF NOT EXISTS ${quotePgIdent(plain)} ON ${qualified} (${col})`,
54
- `CREATE INDEX${concurrently} IF NOT EXISTS ${quotePgIdent(composite)} ON ${qualified} (${col}, ${id})`
55
- ],
56
- drop: [
57
- `DROP INDEX${concurrently} IF EXISTS ${quotePgIdent(schemaName)}.${quotePgIdent(plain)}`,
58
- `DROP INDEX${concurrently} IF EXISTS ${quotePgIdent(schemaName)}.${quotePgIdent(composite)}`
59
- ]
60
- };
61
- }
62
- function escapeLiteralDefault(value) {
63
- if (value === null || value === undefined)
64
- return "NULL";
65
- if (typeof value === "number" || typeof value === "bigint")
66
- return String(value);
67
- if (typeof value === "boolean")
68
- return value ? "TRUE" : "FALSE";
69
- return `'${String(value).replace(/'/g, "''")}'`;
70
- }
71
- function tableNeedsTrgm(tableDef) {
72
- return Object.values(tableDef.columns).some((col) => col.search);
73
- }
74
- function emitTableDDL(schemaName, tableName, tableDef) {
75
- const qualifiedName = `${quotePgIdent(schemaName)}.${quotePgIdent(tableName)}`;
76
- const statements = [];
77
- const columnDefs = [
78
- "_id BIGSERIAL PRIMARY KEY",
79
- "_block_height BIGINT NOT NULL",
80
- "_tx_id TEXT NOT NULL",
81
- "_created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()"
82
- ];
83
- for (const [colName, col] of Object.entries(tableDef.columns)) {
84
- const sqlType = TYPE_MAP[col.type];
85
- const nullable = col.nullable ? "" : " NOT NULL";
86
- let colDef = `${quotePgIdent(colName)} ${sqlType}${nullable}`;
87
- if (col.default !== undefined) {
88
- colDef += ` DEFAULT ${escapeLiteralDefault(col.default)}`;
89
- }
90
- if (col.type === "uint") {
91
- colDef += ` CHECK (${quotePgIdent(colName)} >= 0)`;
92
- }
93
- columnDefs.push(colDef);
94
- }
95
- statements.push(`CREATE TABLE IF NOT EXISTS ${qualifiedName} (
96
- ${columnDefs.join(`,
97
- `)}
98
- )`);
99
- statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_block_height`)} ON ${qualifiedName} (_block_height)`);
100
- statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_tx_id`)} ON ${qualifiedName} (_tx_id)`);
101
- for (const [colName, col] of Object.entries(tableDef.columns)) {
102
- if (col.indexed) {
103
- statements.push(...emitIndexedColumnIndexDDL(schemaName, tableName, colName).create);
104
- }
105
- }
106
- for (const [colName, col] of Object.entries(tableDef.columns)) {
107
- if (col.search) {
108
- statements.push(`CREATE INDEX IF NOT EXISTS ${quotePgIdent(`idx_${schemaName}_${tableName}_${colName}_trgm`)} ON ${qualifiedName} USING gin (${quotePgIdent(colName)} gin_trgm_ops)`);
109
- }
110
- }
111
- if (tableDef.indexes) {
112
- for (let i = 0;i < tableDef.indexes.length; i++) {
113
- const cols = tableDef.indexes[i];
114
- const idxName = quotePgIdent(`idx_${schemaName}_${tableName}_composite_${i}`);
115
- statements.push(`CREATE INDEX IF NOT EXISTS ${idxName} ON ${qualifiedName} (${cols.map(quotePgIdent).join(", ")})`);
116
- }
117
- }
118
- if (tableDef.uniqueKeys) {
119
- for (let i = 0;i < tableDef.uniqueKeys.length; i++) {
120
- const cols = tableDef.uniqueKeys[i];
121
- const constraintName = quotePgIdent(`uq_${schemaName}_${tableName}_${cols.join("_")}`);
122
- statements.push(`ALTER TABLE ${qualifiedName} ADD CONSTRAINT ${constraintName} UNIQUE (${cols.map(quotePgIdent).join(", ")})`);
123
- }
124
- }
125
- return statements;
126
- }
127
- function emitJournalDDL(schemaName) {
128
- return [
129
- `CREATE TABLE IF NOT EXISTS ${schemaName}._journal (
130
- _jid BIGSERIAL PRIMARY KEY,
131
- block_height BIGINT NOT NULL,
132
- table_name TEXT NOT NULL,
133
- row_key JSONB NOT NULL,
134
- prev_row JSONB,
135
- _created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
136
- )`,
137
- `CREATE INDEX IF NOT EXISTS idx_${schemaName}_journal_height ON ${schemaName}._journal (block_height)`
138
- ];
139
- }
140
- function emitFactoryDDL(schemaName) {
141
- return [
142
- `CREATE TABLE IF NOT EXISTS ${schemaName}._factory_addresses (
143
- source_name TEXT NOT NULL,
144
- address TEXT NOT NULL,
145
- block_height BIGINT NOT NULL,
146
- _created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
147
- PRIMARY KEY (source_name, address)
148
- )`,
149
- `CREATE INDEX IF NOT EXISTS idx_${schemaName}_factory_height ON ${schemaName}._factory_addresses (block_height)`
150
- ];
151
- }
152
- function emitForeignKeyDDL(schemaName, tableName, tableDef) {
153
- return (tableDef.relations ?? []).map((rel) => {
154
- const constraintName = quotePgIdent(`fk_${schemaName}_${tableName}_${rel.name}`);
155
- return `ALTER TABLE ${quotePgIdent(schemaName)}.${quotePgIdent(tableName)} ADD CONSTRAINT ${constraintName} ` + `FOREIGN KEY (${rel.fields.map(quotePgIdent).join(", ")}) ` + `REFERENCES ${quotePgIdent(schemaName)}.${quotePgIdent(rel.references)} (${rel.referencedColumns.map(quotePgIdent).join(", ")})`;
156
- });
157
- }
158
- function generateSubgraphSQL(def, schemaNameOverride) {
159
- const schemaName = schemaNameOverride ?? pgSchemaName(def.name);
160
- const statements = [];
161
- const needsTrgm = Object.values(def.schema).some((table) => Object.values(table.columns).some((col) => col.search));
162
- if (needsTrgm) {
163
- statements.push("CREATE EXTENSION IF NOT EXISTS pg_trgm");
164
- }
165
- statements.push(`CREATE SCHEMA IF NOT EXISTS ${schemaName}`);
166
- for (const [tableName, tableDef] of Object.entries(def.schema)) {
167
- statements.push(...emitTableDDL(schemaName, tableName, tableDef));
168
- }
169
- statements.push(...emitJournalDDL(schemaName));
170
- if (Object.values(def.sources ?? {}).some((src) => src.factory !== undefined)) {
171
- statements.push(...emitFactoryDDL(schemaName));
172
- }
173
- for (const [tableName, tableDef] of Object.entries(def.schema)) {
174
- statements.push(...emitForeignKeyDDL(schemaName, tableName, tableDef));
175
- }
176
- const hashInput = JSON.stringify({
177
- name: def.name,
178
- schema: def.schema,
179
- sources: def.sources
180
- }, (_key, value) => typeof value === "bigint" ? value.toString() : value);
181
- const hash = createHash("sha256").update(hashInput).digest("hex");
182
- return { statements, hash };
183
- }
184
-
185
- // src/runtime/chain-read.ts
186
- import { getSourceDb } from "@secondlayer/shared/db";
187
- import { logger } from "@secondlayer/shared/logger";
188
- import { http, createPublicClient } from "@secondlayer/stacks";
189
- import {
190
- ContractResponseError,
191
- buildFunctionArgs,
192
- isResponseOutput,
193
- readContract
194
- } from "@secondlayer/stacks/actions";
195
- import {
196
- clarityValueToJSUntyped,
197
- deserializeCV,
198
- normalizeAbi,
199
- serializeCV,
200
- toCamelCase
201
- } from "@secondlayer/stacks/clarity";
202
- function readContractAt(ctx, contractId, abi, options) {
203
- return ctx.client.contract(contractId, abi, options);
204
- }
205
-
206
- class ChainReadError extends Error {
207
- name = "ChainReadError";
208
- }
209
- function nodeClient(rpcUrl) {
210
- return createPublicClient({
211
- transport: http(rpcUrl, { retryCount: 0, timeout: 1e4 })
212
- });
213
- }
214
-
215
- class ConcurrencyGate {
216
- limit;
217
- active = 0;
218
- queue = [];
219
- constructor(limit) {
220
- this.limit = limit;
221
- }
222
- async run(fn) {
223
- if (this.active >= this.limit) {
224
- await new Promise((resolve) => this.queue.push(resolve));
225
- }
226
- this.active++;
227
- try {
228
- return await fn();
229
- } finally {
230
- this.active--;
231
- this.queue.shift()?.();
232
- }
233
- }
234
- }
235
- var DEFAULT_MAX_CONCURRENT_READS = 4;
236
- function maxConcurrentReads() {
237
- const raw = process.env.SUBGRAPH_CHAIN_READ_CONCURRENCY;
238
- const parsed = raw ? Number.parseInt(raw, 10) : Number.NaN;
239
- return Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_MAX_CONCURRENT_READS;
240
- }
241
- var gate = new ConcurrencyGate(maxConcurrentReads());
242
- function hashArgs(args) {
243
- const serialized = args.map((arg) => serializeCV(arg)).join(",");
244
- return Bun.hash(serialized).toString(16);
245
- }
246
- function findFunction(abi, name) {
247
- const fn = abi.functions.find((f) => f.name === name);
248
- if (!fn) {
249
- throw new ChainReadError(`No function "${name}" in the supplied ABI.`);
250
- }
251
- if (fn.access !== "read-only") {
252
- throw new ChainReadError(`"${name}" is ${fn.access}, not read-only — a handler cannot call it.`);
253
- }
254
- return fn;
255
- }
256
- function createChainReadClient(ctx) {
257
- const readValue = async (contractId, abi, functionName, args, options) => {
258
- const rpcUrl = ctx.rpcUrl ?? process.env.STACKS_NODE_RPC_URL;
259
- if (!rpcUrl) {
260
- throw new ChainReadError("ctx.client needs a Stacks node: set STACKS_NODE_RPC_URL on the subgraph processor.");
261
- }
262
- const constant = options.cache === "contract-constant";
263
- if (!constant && !ctx.indexBlockHash) {
264
- throw new ChainReadError(`Block ${ctx.blockHeight} has no index_block_hash, so a read cannot be pinned to it. Reading at the node's tip would make this handler non-deterministic (the same reindex would produce different rows). Re-ingest the block, or pass cache: "contract-constant" if the value genuinely cannot change.`);
265
- }
266
- const fn = findFunction(abi, functionName);
267
- const clarityArgs = buildFunctionArgs(fn, args);
268
- const db = ctx.db ?? getSourceDb();
269
- const argsHash = hashArgs(clarityArgs);
270
- const pinnedTo = constant ? null : ctx.indexBlockHash;
271
- const cached = await db.selectFrom("chain_read_cache").select("result_hex").where("contract_id", "=", contractId).where("function_name", "=", fn.name).where("args_hash", "=", argsHash).where((eb) => pinnedTo === null ? eb("index_block_hash", "is", null) : eb("index_block_hash", "=", pinnedTo)).executeTakeFirst();
272
- const resultHex = cached?.result_hex ?? await gate.run(async () => {
273
- if (!ctx.indexBlockHash) {
274
- logger.warn("Chain read is not pinned to a block (no index_block_hash)", {
275
- contractId,
276
- functionName: fn.name,
277
- blockHeight: ctx.blockHeight,
278
- note: 'cache: "contract-constant" asserts this value cannot change; the node answered at its own tip'
279
- });
280
- }
281
- const value2 = await readContract(nodeClient(rpcUrl), {
282
- contract: contractId,
283
- functionName: fn.name,
284
- args: clarityArgs,
285
- tip: ctx.indexBlockHash ?? undefined
286
- });
287
- return serializeCV(value2);
288
- });
289
- if (!cached) {
290
- await db.insertInto("chain_read_cache").values({
291
- contract_id: contractId,
292
- function_name: fn.name,
293
- args_hash: argsHash,
294
- index_block_hash: pinnedTo,
295
- block_height: constant ? null : ctx.blockHeight,
296
- result_hex: resultHex
297
- }).onConflict((oc) => oc.doNothing()).execute();
298
- }
299
- const value = deserializeCV(resultHex);
300
- const js = clarityValueToJSUntyped(fn.outputs, value);
301
- if (isResponseOutput(fn.outputs)) {
302
- const response = js;
303
- if ("ok" in response)
304
- return response.ok;
305
- throw new ContractResponseError(`${contractId}::${fn.name} returned an err response.`, response.err);
306
- }
307
- return js;
308
- };
309
- return {
310
- contract(contractId, rawAbi, options = {}) {
311
- const abi = normalizeAbi(rawAbi);
312
- const camelToKebab = new Map;
313
- for (const fn of abi.functions) {
314
- camelToKebab.set(toCamelCase(fn.name), fn.name);
315
- }
316
- const read = new Proxy({}, {
317
- get(_target, prop) {
318
- const fnName = camelToKebab.get(prop) ?? prop;
319
- return (args = {}) => readValue(contractId, abi, fnName, args, options);
320
- }
321
- });
322
- return { read };
323
- }
324
- };
325
- }
326
-
327
- // src/runtime/context.ts
328
- var JOURNAL_RETENTION_BLOCKS = 300;
329
- var journalEnsured = new Set;
330
- function validateColumnName(name) {
331
- if (!/^[a-z_][a-z0-9_]*$/i.test(name)) {
332
- throw new Error(`Invalid column name: ${name}`);
333
- }
334
- }
335
-
336
- class SubgraphContext {
337
- block;
338
- _tx;
339
- db;
340
- pgSchemaName;
341
- subgraphSchema;
342
- ops = [];
343
- journal;
344
- _client;
345
- constructor(db, pgSchemaName2, subgraphSchema, block, tx, journal = false) {
346
- this.db = db;
347
- this.pgSchemaName = pgSchemaName2;
348
- this.subgraphSchema = subgraphSchema;
349
- this.block = block;
350
- this._tx = tx;
351
- this.journal = journal;
352
- }
353
- get client() {
354
- this._client ??= createChainReadClient({
355
- blockHeight: this.block.height,
356
- indexBlockHash: this.block.indexBlockHash
357
- });
358
- return this._client;
359
- }
360
- get tx() {
361
- return this._tx;
362
- }
363
- setTx(tx) {
364
- this._tx = tx;
365
- }
366
- insert(table, row) {
367
- this.validateTable(table);
368
- this.ops.push({
369
- kind: "insert",
370
- table,
371
- data: { ...row, _block_height: this.block.height, _tx_id: this._tx.txId }
372
- });
373
- }
374
- update(table, where, set) {
375
- this.validateTable(table);
376
- this.ops.push({ kind: "update", table, data: where, set });
377
- }
378
- upsert(table, key, row) {
379
- this.validateTable(table);
380
- const tableDef = this.subgraphSchema[table];
381
- if (!tableDef)
382
- return;
383
- const keyColumns = Object.keys(key);
384
- const hasUniqueConstraint = tableDef.uniqueKeys?.some((uk) => uk.length === keyColumns.length && uk.every((c) => keyColumns.includes(c)));
385
- const meta = { _block_height: this.block.height, _tx_id: this._tx.txId };
386
- if (hasUniqueConstraint) {
387
- this.ops.push({
388
- kind: "insert",
389
- table,
390
- data: { ...key, ...row, ...meta, _upsert_keys: keyColumns }
391
- });
392
- } else {
393
- logger2.warn("upsert called without matching uniqueKeys constraint, using fallback", {
394
- table,
395
- keys: keyColumns
396
- });
397
- this.ops.push({
398
- kind: "insert",
399
- table,
400
- data: {
401
- ...key,
402
- ...row,
403
- ...meta,
404
- _upsert_fallback_keys: keyColumns,
405
- _upsert_fallback_set: row
406
- }
407
- });
408
- }
409
- }
410
- delete(table, where) {
411
- this.validateTable(table);
412
- this.ops.push({ kind: "delete", table, data: where });
413
- }
414
- increment(table, key, deltas) {
415
- this.validateTable(table);
416
- const tableDef = this.subgraphSchema[table];
417
- const keyColumns = Object.keys(key);
418
- const hasUniqueConstraint = tableDef?.uniqueKeys?.some((uk) => uk.length === keyColumns.length && uk.every((c) => keyColumns.includes(c)));
419
- if (!hasUniqueConstraint) {
420
- throw new Error(`increment("${table}") requires a uniqueKeys constraint on [${keyColumns.join(", ")}]`);
421
- }
422
- for (const [col, v] of Object.entries(deltas)) {
423
- validateColumnName(col);
424
- if (keyColumns.includes(col)) {
425
- throw new Error(`increment("${table}"): "${col}" is a key column`);
426
- }
427
- if (typeof v !== "bigint" && typeof v !== "number") {
428
- throw new Error(`increment("${table}"): delta for "${col}" must be bigint or number`);
429
- }
430
- }
431
- this.ops.push({
432
- kind: "increment",
433
- table,
434
- data: {
435
- ...key,
436
- _block_height: this.block.height,
437
- _tx_id: this._tx.txId,
438
- _upsert_keys: keyColumns
439
- },
440
- set: { ...deltas }
441
- });
442
- }
443
- opsCheckpoint() {
444
- return this.ops.length;
445
- }
446
- rollbackTo(checkpoint) {
447
- if (checkpoint < 0 || checkpoint > this.ops.length)
448
- return;
449
- this.ops.length = checkpoint;
450
- }
451
- patch(table, where, set) {
452
- this.update(table, where, set);
453
- }
454
- async patchOrInsert(table, key, row) {
455
- const existing = await this.findOne(table, key);
456
- const resolved = {};
457
- for (const [k, v] of Object.entries(row)) {
458
- resolved[k] = typeof v === "function" ? v(existing) : v;
459
- }
460
- this.upsert(table, key, resolved);
461
- }
462
- formatUnits(value, decimals) {
463
- return formatUnits(value, decimals);
464
- }
465
- async readRows(table, where, limit) {
466
- const qualifiedTable = `"${this.pgSchemaName}"."${table}"`;
467
- const { clause } = buildWhereClause(where);
468
- const query = `SELECT * FROM ${qualifiedTable} WHERE ${clause}${limit !== undefined ? ` LIMIT ${limit}` : ""}`;
469
- const { rows } = await sql.raw(query).execute(this.db);
470
- return rows.map((r) => this.coerceRow(table, r));
471
- }
472
- async findOne(table, where) {
473
- this.validateTable(table);
474
- const row = (await this.readRows(table, where, 1))[0] ?? null;
475
- return this.overlayOne(table, where, row);
476
- }
477
- async findMany(table, where) {
478
- this.validateTable(table);
479
- return this.overlayMany(table, where, await this.readRows(table, where));
480
- }
481
- overlayOne(table, where, dbRow) {
482
- if (this.ops.length === 0)
483
- return dbRow;
484
- let row = dbRow;
485
- for (const op of this.ops) {
486
- if (op.table !== table)
487
- continue;
488
- row = this.applyOpToRow(op, row, where);
489
- }
490
- return row;
491
- }
492
- overlayMany(table, where, dbRows) {
493
- if (this.ops.length === 0)
494
- return [...dbRows];
495
- let result = [...dbRows];
496
- for (const op of this.ops) {
497
- if (op.table !== table)
498
- continue;
499
- if (op.kind === "update") {
500
- for (let i = 0;i < result.length; i++) {
501
- if (rowMatches(result[i], op.data))
502
- result[i] = { ...result[i], ...op.set ?? {} };
503
- }
504
- } else if (op.kind === "delete") {
505
- result = result.filter((r) => !rowMatches(r, op.data));
506
- } else {
507
- const upsertKeys = op.data._upsert_keys;
508
- const clean = stripControlKeys(op.data);
509
- const idx = upsertKeys ? result.findIndex((r) => upsertKeys.every((k) => valEq(r[k], clean[k]))) : -1;
510
- if (idx >= 0) {
511
- const existing = result[idx];
512
- result[idx] = this.applyOpToRow(op, existing, where) ?? existing;
513
- } else {
514
- const created = this.applyOpToRow(op, null, where);
515
- if (created)
516
- result.push(created);
517
- }
518
- }
519
- }
520
- return result;
521
- }
522
- applyOpToRow(op, row, where) {
523
- const upsertKeys = op.data._upsert_keys;
524
- const clean = stripControlKeys(op.data);
525
- switch (op.kind) {
526
- case "insert": {
527
- if (row) {
528
- if (upsertKeys?.every((k) => valEq(row[k], clean[k]))) {
529
- const merged = { ...row };
530
- for (const [k, v] of Object.entries(clean)) {
531
- if (!upsertKeys.includes(k) && !k.startsWith("_"))
532
- merged[k] = v;
533
- }
534
- return merged;
535
- }
536
- return row;
537
- }
538
- return rowMatches(clean, where) ? { ...clean } : null;
539
- }
540
- case "increment": {
541
- const deltas = op.set ?? {};
542
- if (row) {
543
- if (upsertKeys.every((k) => valEq(row[k], clean[k]))) {
544
- const merged = { ...row };
545
- for (const [col, d] of Object.entries(deltas)) {
546
- merged[col] = toBigIntOr0(merged[col]) + toBigIntOr0(d);
547
- }
548
- return merged;
549
- }
550
- return row;
551
- }
552
- if (!rowMatches(clean, where))
553
- return null;
554
- const created = { ...clean };
555
- for (const [col, d] of Object.entries(deltas)) {
556
- created[col] = toBigIntOr0(d);
557
- }
558
- return created;
559
- }
560
- case "update":
561
- return row && rowMatches(row, op.data) ? { ...row, ...op.set ?? {} } : row;
562
- case "delete":
563
- return row && rowMatches(row, op.data) ? null : row;
564
- }
565
- }
566
- async count(table, where) {
567
- this.validateTable(table);
568
- const qualifiedTable = `"${this.pgSchemaName}"."${table}"`;
569
- const whereClause = where ? `WHERE ${buildWhereClause(where).clause}` : "";
570
- const { rows } = await sql.raw(`SELECT COUNT(*)::int AS count FROM ${qualifiedTable} ${whereClause}`).execute(this.db);
571
- return Number(rows[0]?.count ?? 0);
572
- }
573
- async sum(table, column, where) {
574
- this.validateTable(table);
575
- validateColumnName(column);
576
- const qualifiedTable = `"${this.pgSchemaName}"."${table}"`;
577
- const whereClause = where ? `WHERE ${buildWhereClause(where).clause}` : "";
578
- const { rows } = await sql.raw(`SELECT COALESCE(SUM("${column}"), 0) AS total FROM ${qualifiedTable} ${whereClause}`).execute(this.db);
579
- return BigInt(rows[0]?.total?.toString() ?? "0");
580
- }
581
- async min(table, column, where) {
582
- this.validateTable(table);
583
- validateColumnName(column);
584
- const qualifiedTable = `"${this.pgSchemaName}"."${table}"`;
585
- const whereClause = where ? `WHERE ${buildWhereClause(where).clause}` : "";
586
- const { rows } = await sql.raw(`SELECT MIN("${column}") AS val FROM ${qualifiedTable} ${whereClause}`).execute(this.db);
587
- const val = rows[0]?.val;
588
- return val != null ? BigInt(val.toString()) : null;
589
- }
590
- async max(table, column, where) {
591
- this.validateTable(table);
592
- validateColumnName(column);
593
- const qualifiedTable = `"${this.pgSchemaName}"."${table}"`;
594
- const whereClause = where ? `WHERE ${buildWhereClause(where).clause}` : "";
595
- const { rows } = await sql.raw(`SELECT MAX("${column}") AS val FROM ${qualifiedTable} ${whereClause}`).execute(this.db);
596
- const val = rows[0]?.val;
597
- return val != null ? BigInt(val.toString()) : null;
598
- }
599
- async countDistinct(table, column, where) {
600
- this.validateTable(table);
601
- validateColumnName(column);
602
- const qualifiedTable = `"${this.pgSchemaName}"."${table}"`;
603
- const whereClause = where ? `WHERE ${buildWhereClause(where).clause}` : "";
604
- const { rows } = await sql.raw(`SELECT COUNT(DISTINCT "${column}")::int AS count FROM ${qualifiedTable} ${whereClause}`).execute(this.db);
605
- return Number(rows[0]?.count ?? 0);
606
- }
607
- coerceRow(table, row) {
608
- const tableDef = this.subgraphSchema[table];
609
- if (!tableDef)
610
- return row;
611
- const result = { ...row };
612
- for (const [col, def] of Object.entries(tableDef.columns)) {
613
- if ((def.type === "uint" || def.type === "int") && typeof result[col] === "string") {
614
- result[col] = BigInt(result[col]);
615
- }
616
- }
617
- return result;
618
- }
619
- get pendingOps() {
620
- return this.ops.length;
621
- }
622
- async flush() {
623
- if (this.ops.length === 0)
624
- return { count: 0, writes: [] };
625
- await this.ensureJournalTable();
626
- const opsToFlush = [...this.ops];
627
- this.ops.length = 0;
628
- const statements = this.buildStatements(opsToFlush);
629
- if ("isTransaction" in this.db) {
630
- await this.execStatements(this.db, statements);
631
- } else {
632
- await this.db.transaction().execute(async (tx) => {
633
- await this.execStatements(tx, statements);
634
- });
635
- }
636
- const writes = opsToFlush.map((op, rowIndex) => {
637
- const blockHeight = op.data._block_height ?? this.block.height;
638
- const txId = op.data._tx_id ?? this._tx.txId;
639
- const baseRow = op.kind === "update" || op.kind === "increment" ? { ...op.data, ...op.set ?? {} } : { ...op.data };
640
- baseRow._upsert_keys = undefined;
641
- baseRow._upsert_fallback_keys = undefined;
642
- baseRow._upsert_fallback_set = undefined;
643
- return {
644
- op: op.kind === "increment" ? "update" : op.kind,
645
- table: op.table,
646
- row: jsonSafe(baseRow),
647
- pk: { blockHeight, txId, rowIndex }
648
- };
649
- });
650
- return { count: opsToFlush.length, writes };
651
- }
652
- async execStatements(executor, statements) {
653
- const affected = (r) => r.numAffectedRows ?? 0n;
654
- for (const stmt of statements) {
655
- if (typeof stmt === "string") {
656
- await sql.raw(stmt).execute(executor);
657
- continue;
658
- }
659
- const updated = await sql.raw(stmt.update).execute(executor);
660
- if (affected(updated) > 0n)
661
- continue;
662
- const inserted = await sql.raw(stmt.insert).execute(executor);
663
- if (affected(inserted) > 0n)
664
- continue;
665
- const retried = await sql.raw(stmt.update).execute(executor);
666
- if (affected(retried) === 0n) {
667
- throw new Error(`increment could not be applied to ${this.pgSchemaName}.${stmt.table} where ${stmt.keyPred}: row neither updated nor inserted`);
668
- }
669
- }
670
- }
671
- prepareInsert(op) {
672
- const upsertKeys = op.data._upsert_keys;
673
- const data = { ...op.data };
674
- delete data._upsert_keys;
675
- delete data._upsert_fallback_keys;
676
- delete data._upsert_fallback_set;
677
- if (!data._block_height)
678
- data._block_height = this.block.height;
679
- if (!data._tx_id)
680
- data._tx_id = this._tx.txId;
681
- data._created_at = "NOW()";
682
- const cols = Object.keys(data);
683
- cols.forEach(validateColumnName);
684
- const vals = cols.map((c) => data[c] === "NOW()" ? "NOW()" : escapeLiteral(data[c]));
685
- const batchKey = `${op.table}:${[...cols].sort().join(",")}:${upsertKeys ? [...upsertKeys].sort().join(",") : ""}`;
686
- return { data, cols, vals, upsertKeys, batchKey };
687
- }
688
- async ensureJournalTable() {
689
- if (!this.journal || journalEnsured.has(this.pgSchemaName))
690
- return;
691
- const { rows } = await sql.raw(`SELECT to_regclass('"${this.pgSchemaName}"."_journal"') AS r`).execute(this.db);
692
- if (rows[0]?.r) {
693
- journalEnsured.add(this.pgSchemaName);
694
- return;
695
- }
696
- for (const stmt of emitJournalDDL(this.pgSchemaName)) {
697
- await sql.raw(stmt).execute(this.db);
698
- }
699
- }
700
- columnSqlType(table, col) {
701
- const def = this.subgraphSchema[table]?.columns?.[col];
702
- return def ? TYPE_MAP[def.type] : undefined;
703
- }
704
- journalCaptureSQL(table, keyCols, keyLiteralRows) {
705
- const cast = (col, expr) => {
706
- const t = this.columnSqlType(table, col);
707
- return t ? `CAST(${expr} AS ${t})` : expr;
708
- };
709
- const keyObj = keyCols.map((k) => `'${k}', ${cast(k, `v."${k}"`)}`).join(", ");
710
- const joinCond = keyCols.map((k) => `t."${k}" = ${cast(k, `v."${k}"`)}`).join(" AND ");
711
- const valuesList = keyLiteralRows.map((r) => `(${r.join(", ")})`).join(", ");
712
- return `INSERT INTO "${this.pgSchemaName}"."_journal" ("block_height", "table_name", "row_key", "prev_row") ` + `SELECT ${this.block.height}, '${table}', jsonb_build_object(${keyObj}), to_jsonb(t.*) ` + `FROM (VALUES ${valuesList}) AS v(${keyCols.map((k) => `"${k}"`).join(", ")}) ` + `LEFT JOIN "${this.pgSchemaName}"."${table}" t ON ${joinCond}`;
713
- }
714
- journalCaptureByWhereSQL(table, clause) {
715
- return `INSERT INTO "${this.pgSchemaName}"."_journal" ("block_height", "table_name", "row_key", "prev_row") ` + `SELECT ${this.block.height}, '${table}', jsonb_build_object('_id', t."_id"), to_jsonb(t.*) ` + `FROM "${this.pgSchemaName}"."${table}" t WHERE ${clause}`;
716
- }
717
- buildStatements(ops) {
718
- const statements = [];
719
- let currentBatch = null;
720
- let currentBatchKey = "";
721
- let incBatch = null;
722
- let incBatchKey = "";
723
- const flushIncrementBatch = () => {
724
- if (!incBatch)
725
- return;
726
- const batch = incBatch;
727
- const qualifiedTable = `"${this.pgSchemaName}"."${batch.table}"`;
728
- const cols = [
729
- ...batch.keyCols,
730
- ...batch.deltaCols,
731
- "_block_height",
732
- "_tx_id",
733
- "_created_at"
734
- ];
735
- if (this.journal) {
736
- statements.push(this.journalCaptureSQL(batch.table, batch.keyCols, Array.from(batch.rows.values()).map((r) => batch.keyCols.map((k) => escapeLiteral(r.keys[k])))));
737
- }
738
- for (const r of batch.rows.values()) {
739
- const keyPred = batch.keyCols.map((k) => `"${k}" = ${escapeLiteral(r.keys[k])}`).join(" AND ");
740
- const updSet = batch.deltaCols.map((c) => `"${c}" = COALESCE("${c}", 0) + (${String(r.deltas[c] ?? 0n)})`).join(", ");
741
- const insertVals = [
742
- ...batch.keyCols.map((k) => escapeLiteral(r.keys[k])),
743
- ...batch.deltaCols.map((c) => String(r.deltas[c] ?? 0n)),
744
- escapeLiteral(r.meta.blockHeight),
745
- escapeLiteral(r.meta.txId),
746
- "NOW()"
747
- ];
748
- statements.push({
749
- kind: "increment",
750
- table: batch.table,
751
- keyPred,
752
- update: `UPDATE ${qualifiedTable} SET ${updSet} WHERE ${keyPred}`,
753
- insert: `INSERT INTO ${qualifiedTable} (${cols.map((c) => `"${c}"`).join(", ")}) ` + `SELECT ${insertVals.join(", ")} ` + `WHERE NOT EXISTS (SELECT 1 FROM ${qualifiedTable} WHERE ${keyPred})`
754
- });
755
- }
756
- incBatch = null;
757
- incBatchKey = "";
758
- };
759
- const flushInsertBatch = () => {
760
- if (!currentBatch)
761
- return;
762
- const batch = currentBatch;
763
- const qualifiedTable = `"${this.pgSchemaName}"."${batch.table}"`;
764
- const colList = batch.cols.map((c) => `"${c}"`).join(", ");
765
- let rows = batch.rows;
766
- if (batch.upsertKeys && batch.upsertKeys.length > 0) {
767
- const uKeys = batch.upsertKeys;
768
- const keyIndices = uKeys.map((k) => batch.cols.indexOf(k));
769
- const seen = new Map;
770
- for (let i = 0;i < rows.length; i++) {
771
- const key = keyIndices.map((ki) => rows[i][ki]).join("\x00");
772
- seen.set(key, i);
773
- }
774
- if (seen.size < rows.length) {
775
- rows = Array.from(seen.values()).map((i) => rows[i]);
776
- }
777
- }
778
- const valuesList = rows.map((r) => `(${r.join(", ")})`).join(", ");
779
- let stmt = `INSERT INTO ${qualifiedTable} (${colList}) VALUES ${valuesList}`;
780
- if (this.journal && batch.upsertKeys && batch.upsertKeys.length > 0) {
781
- const uKeys = batch.upsertKeys;
782
- const keyIndices = uKeys.map((k) => batch.cols.indexOf(k));
783
- statements.push(this.journalCaptureSQL(batch.table, uKeys, rows.map((r) => keyIndices.map((ki) => r[ki]))));
784
- }
785
- if (batch.upsertKeys && batch.upsertKeys.length > 0) {
786
- const batchKeys = batch.upsertKeys;
787
- const updateCols = batch.cols.filter((c) => !batchKeys.includes(c) && !c.startsWith("_"));
788
- if (updateCols.length > 0) {
789
- const setClauses = updateCols.map((c) => `"${c}" = EXCLUDED."${c}"`);
790
- stmt += ` ON CONFLICT (${batchKeys.map((k) => `"${k}"`).join(", ")}) DO UPDATE SET ${setClauses.join(", ")}`;
791
- } else {
792
- stmt += ` ON CONFLICT (${batchKeys.map((k) => `"${k}"`).join(", ")}) DO NOTHING`;
793
- }
794
- }
795
- statements.push(stmt);
796
- currentBatch = null;
797
- currentBatchKey = "";
798
- };
799
- for (const op of ops) {
800
- const qualifiedTable = `"${this.pgSchemaName}"."${op.table}"`;
801
- if (op.kind === "insert") {
802
- flushIncrementBatch();
803
- const { cols, vals, upsertKeys, batchKey } = this.prepareInsert(op);
804
- if (batchKey === currentBatchKey && currentBatch) {
805
- currentBatch.rows.push(vals);
806
- } else {
807
- flushInsertBatch();
808
- currentBatch = { table: op.table, cols, rows: [vals], upsertKeys };
809
- currentBatchKey = batchKey;
810
- }
811
- } else if (op.kind === "increment") {
812
- flushInsertBatch();
813
- const keyCols = [...op.data._upsert_keys].sort();
814
- const deltaCols = Object.keys(op.set ?? {}).sort();
815
- const batchKey = `inc:${op.table}:${keyCols.join(",")}:${deltaCols.join(",")}`;
816
- if (batchKey !== incBatchKey || !incBatch) {
817
- flushIncrementBatch();
818
- incBatch = { table: op.table, keyCols, deltaCols, rows: new Map };
819
- incBatchKey = batchKey;
820
- }
821
- const clean = stripControlKeys(op.data);
822
- const keySig = keyCols.map((k) => escapeLiteral(clean[k])).join("\x00");
823
- const existing = incBatch.rows.get(keySig);
824
- if (existing) {
825
- for (const c of deltaCols) {
826
- existing.deltas[c] = (existing.deltas[c] ?? 0n) + toBigIntOr0(op.set?.[c]);
827
- }
828
- } else {
829
- const deltas = {};
830
- for (const c of deltaCols)
831
- deltas[c] = toBigIntOr0(op.set?.[c]);
832
- incBatch.rows.set(keySig, {
833
- keys: clean,
834
- deltas,
835
- meta: {
836
- blockHeight: op.data._block_height ?? this.block.height,
837
- txId: op.data._tx_id ?? this._tx.txId
838
- }
839
- });
840
- }
841
- } else {
842
- flushInsertBatch();
843
- flushIncrementBatch();
844
- if (op.kind === "update") {
845
- const setEntries = Object.entries(op.set ?? {});
846
- for (const [k] of setEntries)
847
- validateColumnName(k);
848
- const setClauses = setEntries.map(([k, v]) => `"${k}" = ${escapeLiteral(v)}`);
849
- const { clause } = buildWhereClause(op.data);
850
- if (this.journal) {
851
- statements.push(this.journalCaptureByWhereSQL(op.table, clause));
852
- }
853
- statements.push(`UPDATE ${qualifiedTable} SET ${setClauses.join(", ")} WHERE ${clause}`);
854
- } else if (op.kind === "delete") {
855
- const { clause } = buildWhereClause(op.data);
856
- if (this.journal) {
857
- statements.push(this.journalCaptureByWhereSQL(op.table, clause));
858
- }
859
- statements.push(`DELETE FROM ${qualifiedTable} WHERE ${clause}`);
860
- }
861
- }
862
- }
863
- flushInsertBatch();
864
- flushIncrementBatch();
865
- return statements;
866
- }
867
- validateTable(table) {
868
- if (!this.subgraphSchema[table]) {
869
- throw new Error(`Table "${table}" not found in subgraph schema. Available: [${Object.keys(this.subgraphSchema).join(", ")}]`);
870
- }
871
- }
872
- }
873
- function stripControlKeys(data) {
874
- const {
875
- _upsert_keys: _a,
876
- _upsert_fallback_keys: _b,
877
- _upsert_fallback_set: _c,
878
- ...clean
879
- } = data;
880
- return clean;
881
- }
882
- function valEq(a, b) {
883
- if (a === b)
884
- return true;
885
- if (a == null || b == null)
886
- return false;
887
- return String(a) === String(b);
888
- }
889
- function rowMatches(row, where) {
890
- return Object.entries(where).every(([k, v]) => valEq(row[k], v));
891
- }
892
- function toBigIntOr0(v) {
893
- if (typeof v === "bigint")
894
- return v;
895
- if (v == null)
896
- return 0n;
897
- try {
898
- return BigInt(String(v));
899
- } catch {
900
- return 0n;
901
- }
902
- }
903
- function jsonSafe(row) {
904
- const out = {};
905
- for (const [k, v] of Object.entries(row)) {
906
- out[k] = typeof v === "bigint" ? v.toString() : v;
907
- }
908
- return out;
909
- }
910
- function escapeLiteral(value) {
911
- if (value === null || value === undefined)
912
- return "NULL";
913
- if (typeof value === "number" || typeof value === "bigint")
914
- return String(value);
915
- if (typeof value === "boolean")
916
- return value ? "TRUE" : "FALSE";
917
- if (typeof value === "object")
918
- return `'${JSON.stringify(value, (_k, v) => typeof v === "bigint" ? v.toString() : v).replace(/'/g, "''")}'::jsonb`;
919
- return `'${String(value).replace(/'/g, "''")}'`;
920
- }
921
- function buildWhereClause(where) {
922
- const entries = Object.entries(where);
923
- if (entries.length === 0)
924
- return { clause: "TRUE", values: [] };
925
- for (const [k] of entries)
926
- validateColumnName(k);
927
- const parts = entries.map(([k, v]) => `"${k}" = ${escapeLiteral(v)}`);
928
- return { clause: parts.join(" AND "), values: [] };
929
- }
930
- export {
931
- SubgraphContext,
932
- JOURNAL_RETENTION_BLOCKS
933
- };
934
-
935
- //# debugId=92CDE25F6A98E6BA64756E2164756E21
936
- //# sourceMappingURL=context.js.map