@secondlayer/shared 6.17.0 → 6.19.0

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.
@@ -57,6 +57,9 @@ function getEnv() {
57
57
  cachedEnv = { ...result.data, enabledNetworks };
58
58
  return cachedEnv;
59
59
  }
60
+ function isPox4DecoderEnabled() {
61
+ return process.env.POX4_DECODER_ENABLED !== "false";
62
+ }
60
63
  // src/logger.ts
61
64
  var LOG_LEVELS = {
62
65
  debug: 0,
@@ -151,6 +154,232 @@ import { Kysely } from "kysely";
151
154
  import { PostgresJSDialect } from "kysely-postgres-js";
152
155
  import postgres from "postgres";
153
156
  import { sql as sql2 } from "kysely";
157
+
158
+ // src/db/source-read-columns.ts
159
+ var SOURCE_READ_COLUMNS = {
160
+ blocks: [
161
+ "burn_block_hash",
162
+ "burn_block_height",
163
+ "canonical",
164
+ "hash",
165
+ "height",
166
+ "parent_hash",
167
+ "timestamp"
168
+ ],
169
+ decoded_events: [
170
+ "amount",
171
+ "asset_identifier",
172
+ "block_height",
173
+ "canonical",
174
+ "contract_id",
175
+ "cursor",
176
+ "event_index",
177
+ "event_type",
178
+ "memo",
179
+ "payload",
180
+ "recipient",
181
+ "sender",
182
+ "tx_id",
183
+ "tx_index",
184
+ "value"
185
+ ],
186
+ transactions: [
187
+ "block_height",
188
+ "contract_id",
189
+ "function_args",
190
+ "function_name",
191
+ "raw_result",
192
+ "raw_tx",
193
+ "sender",
194
+ "status",
195
+ "tx_id",
196
+ "tx_index",
197
+ "type"
198
+ ],
199
+ mempool_transactions: [
200
+ "contract_id",
201
+ "function_args",
202
+ "function_name",
203
+ "raw_tx",
204
+ "received_at",
205
+ "sender",
206
+ "seq",
207
+ "tx_id",
208
+ "type"
209
+ ],
210
+ pox4_calls: [
211
+ "aggregated_amount_ustx",
212
+ "aggregated_signer_index",
213
+ "amount_ustx",
214
+ "auth_allowed",
215
+ "auth_id",
216
+ "auth_period",
217
+ "auth_topic",
218
+ "block_height",
219
+ "block_time",
220
+ "burn_block_height",
221
+ "caller",
222
+ "canonical",
223
+ "cursor",
224
+ "delegate_to",
225
+ "end_cycle",
226
+ "function_name",
227
+ "lock_period",
228
+ "max_amount",
229
+ "pox_addr_btc",
230
+ "pox_addr_hashbytes",
231
+ "pox_addr_version",
232
+ "result_ok",
233
+ "reward_cycle",
234
+ "signer_key",
235
+ "signer_signature",
236
+ "source_cursor",
237
+ "stacker",
238
+ "start_cycle",
239
+ "tx_id",
240
+ "tx_index"
241
+ ],
242
+ sbtc_events: [
243
+ "amount",
244
+ "bitcoin_txid",
245
+ "block_height",
246
+ "block_time",
247
+ "burn_hash",
248
+ "burn_height",
249
+ "canonical",
250
+ "cursor",
251
+ "event_index",
252
+ "fee",
253
+ "governance_contract_type",
254
+ "governance_new_contract",
255
+ "max_fee",
256
+ "output_index",
257
+ "recipient_btc_hashbytes",
258
+ "recipient_btc_version",
259
+ "request_id",
260
+ "sender",
261
+ "signer_address",
262
+ "signer_aggregate_pubkey",
263
+ "signer_bitmap",
264
+ "signer_keys_count",
265
+ "signer_threshold",
266
+ "sweep_txid",
267
+ "topic",
268
+ "tx_id",
269
+ "tx_index"
270
+ ],
271
+ sbtc_token_events: [
272
+ "amount",
273
+ "block_height",
274
+ "block_time",
275
+ "canonical",
276
+ "cursor",
277
+ "event_index",
278
+ "event_type",
279
+ "memo",
280
+ "recipient",
281
+ "sender",
282
+ "tx_id",
283
+ "tx_index"
284
+ ],
285
+ bns_name_events: [
286
+ "block_height",
287
+ "block_time",
288
+ "bns_id",
289
+ "canonical",
290
+ "cursor",
291
+ "event_index",
292
+ "fqn",
293
+ "hashed_salted_fqn_preorder",
294
+ "imported_at",
295
+ "name",
296
+ "namespace",
297
+ "owner",
298
+ "preordered_by",
299
+ "registered_at",
300
+ "renewal_height",
301
+ "stx_burn",
302
+ "topic",
303
+ "tx_id",
304
+ "tx_index"
305
+ ],
306
+ bns_namespace_events: [
307
+ "block_height",
308
+ "block_time",
309
+ "canonical",
310
+ "cursor",
311
+ "event_index",
312
+ "launched_at",
313
+ "lifetime",
314
+ "manager",
315
+ "manager_frozen",
316
+ "manager_transfers_disabled",
317
+ "namespace",
318
+ "price_frozen",
319
+ "price_function",
320
+ "revealed_at",
321
+ "status",
322
+ "tx_id",
323
+ "tx_index"
324
+ ],
325
+ bns_marketplace_events: [
326
+ "action",
327
+ "block_height",
328
+ "block_time",
329
+ "bns_id",
330
+ "canonical",
331
+ "commission",
332
+ "cursor",
333
+ "event_index",
334
+ "price_ustx",
335
+ "tx_id",
336
+ "tx_index"
337
+ ],
338
+ bns_names: [
339
+ "bns_id",
340
+ "fqn",
341
+ "last_event_at",
342
+ "last_event_cursor",
343
+ "name",
344
+ "namespace",
345
+ "owner",
346
+ "registered_at",
347
+ "renewal_height"
348
+ ],
349
+ bns_namespaces: [
350
+ "last_event_at",
351
+ "last_event_cursor",
352
+ "launched_at",
353
+ "lifetime",
354
+ "manager",
355
+ "manager_frozen",
356
+ "name_count",
357
+ "namespace",
358
+ "price_frozen"
359
+ ],
360
+ burn_block_rewards: [
361
+ "amount_sats",
362
+ "burn_amount",
363
+ "burn_block_hash",
364
+ "burn_block_height",
365
+ "canonical",
366
+ "cursor",
367
+ "recipient_btc",
368
+ "reward_index"
369
+ ],
370
+ burn_block_reward_slots: [
371
+ "burn_block_hash",
372
+ "burn_block_height",
373
+ "canonical",
374
+ "cursor",
375
+ "holder_btc",
376
+ "slot_index"
377
+ ],
378
+ events: ["block_height", "data", "event_index", "tx_id", "type"],
379
+ chain_reorgs: ["detected_at"]
380
+ };
381
+
382
+ // src/db/index.ts
154
383
  var DEFAULT_URL = "postgres://postgres:postgres@localhost:5432/secondlayer_dev";
155
384
  var pools = new Map;
156
385
  var poolSeq = 0;
@@ -183,6 +412,18 @@ function resolveSourceUrl() {
183
412
  function resolveTargetUrl() {
184
413
  return process.env.TARGET_DATABASE_URL || process.env.DATABASE_URL || DEFAULT_URL;
185
414
  }
415
+ function assertDbSplit() {
416
+ const wantsSplit = !!(process.env.SOURCE_DATABASE_URL || process.env.TARGET_DATABASE_URL);
417
+ if (!wantsSplit)
418
+ return;
419
+ if (resolveSourceUrl() === resolveTargetUrl()) {
420
+ const msg = "DB split requested but SOURCE_DATABASE_URL === TARGET_DATABASE_URL (check for a typo or a stray DATABASE_URL fallback)";
421
+ if (false)
422
+ ;
423
+ else
424
+ console.warn(`⚠️ ${msg}`);
425
+ }
426
+ }
186
427
  function getOrCreatePool(url) {
187
428
  const existing = pools.get(url);
188
429
  if (existing) {
@@ -324,6 +565,13 @@ async function readChainReorgsSince(params) {
324
565
  `.execute(db);
325
566
  return result.rows.map(normalizeRow);
326
567
  }
568
+ async function readChainReorgsForHeightRange(params) {
569
+ return readChainReorgsForRange({
570
+ from: { block_height: params.fromHeight, event_index: 0 },
571
+ to: { block_height: params.toHeight, event_index: 2147483647 },
572
+ db: params.db
573
+ });
574
+ }
327
575
  async function readChainReorgsForRange(params) {
328
576
  const db = params.db ?? getSourceDb();
329
577
  const { from, to } = params;
@@ -352,9 +600,10 @@ async function readChainReorgsForRange(params) {
352
600
  export {
353
601
  readChainReorgsSince,
354
602
  readChainReorgsForRange,
603
+ readChainReorgsForHeightRange,
355
604
  insertChainReorg,
356
605
  encodeChainReorgCursor
357
606
  };
358
607
 
359
- //# debugId=DF0935B4C41413A864756E2164756E21
608
+ //# debugId=365CA6CEA262361B64756E2164756E21
360
609
  //# sourceMappingURL=chain-reorgs.js.map
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../src/env.ts", "../src/logger.ts", "../src/db/jsonb.ts", "../src/db/index.ts", "../src/db/queries/chain-reorgs.ts"],
3
+ "sources": ["../src/env.ts", "../src/logger.ts", "../src/db/jsonb.ts", "../src/db/index.ts", "../src/db/source-read-columns.ts", "../src/db/queries/chain-reorgs.ts"],
4
4
  "sourcesContent": [
5
- "import { z } from \"zod/v4\";\n\n// Parse comma-separated networks\nconst networksSchema = z.string().transform((val) => {\n\tconst networks = val\n\t\t.split(\",\")\n\t\t.map((n) => n.trim())\n\t\t.filter(Boolean);\n\tconst valid = [\"mainnet\", \"testnet\"];\n\tfor (const n of networks) {\n\t\tif (!valid.includes(n)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Invalid network: ${n}. Must be one of: ${valid.join(\", \")}`,\n\t\t\t);\n\t\t}\n\t}\n\treturn networks as (\"mainnet\" | \"testnet\")[];\n});\n\ninterface EnvSchemaOutput {\n\tDATABASE_URL?: string;\n\t/**\n\t * Shared indexer DB (blocks/txs/events). Falls back to DATABASE_URL.\n\t * Set this alongside TARGET_DATABASE_URL to enable dual-DB mode.\n\t */\n\tSOURCE_DATABASE_URL?: string;\n\t/**\n\t * Tenant DB (subgraph schemas + subgraphs table). Falls back to DATABASE_URL.\n\t * Set this alongside SOURCE_DATABASE_URL to enable dual-DB mode.\n\t */\n\tTARGET_DATABASE_URL?: string;\n\tNETWORK?: \"mainnet\" | \"testnet\";\n\tNETWORKS?: (\"mainnet\" | \"testnet\")[];\n\tLOG_LEVEL: \"debug\" | \"info\" | \"warn\" | \"error\";\n\tNODE_ENV: \"development\" | \"production\" | \"test\";\n}\n\n// Cast needed: z.preprocess / z.default create different _input vs _output types\n// that z.ZodType<T> can't represent without explicit input type param\nconst envSchema: z.ZodType<EnvSchemaOutput> = z.object({\n\tDATABASE_URL: z.preprocess(\n\t\t(val) => (typeof val === \"string\" && val.length === 0 ? undefined : val),\n\t\tz.string().url().optional(),\n\t),\n\tSOURCE_DATABASE_URL: z.preprocess(\n\t\t(val) => (typeof val === \"string\" && val.length === 0 ? undefined : val),\n\t\tz.string().url().optional(),\n\t),\n\tTARGET_DATABASE_URL: z.preprocess(\n\t\t(val) => (typeof val === \"string\" && val.length === 0 ? undefined : val),\n\t\tz.string().url().optional(),\n\t),\n\tNETWORK: z.enum([\"mainnet\", \"testnet\"]).optional(),\n\tNETWORKS: networksSchema.optional(),\n\tLOG_LEVEL: z.enum([\"debug\", \"info\", \"warn\", \"error\"]).default(\"info\"),\n\tNODE_ENV: z\n\t\t.enum([\"development\", \"production\", \"test\"])\n\t\t.default(\"development\"),\n}) as unknown as z.ZodType<EnvSchemaOutput>;\n\nexport type Env = EnvSchemaOutput & {\n\tenabledNetworks: (\"mainnet\" | \"testnet\")[];\n};\n\nlet cachedEnv: Env | null = null;\n\nexport function getEnv(): Env {\n\tif (cachedEnv) {\n\t\treturn cachedEnv;\n\t}\n\n\tconst result = envSchema.safeParse(process.env);\n\n\tif (!result.success) {\n\t\tconsole.error(\"❌ Invalid environment configuration:\");\n\t\tconsole.error(z.treeifyError(result.error));\n\t\tthrow new Error(\"Invalid environment configuration\");\n\t}\n\n\t// Compute enabled networks from NETWORKS or NETWORK\n\tlet enabledNetworks: (\"mainnet\" | \"testnet\")[];\n\tif (result.data.NETWORKS && result.data.NETWORKS.length > 0) {\n\t\tenabledNetworks = result.data.NETWORKS;\n\t} else if (result.data.NETWORK) {\n\t\tenabledNetworks = [result.data.NETWORK];\n\t} else {\n\t\tenabledNetworks = [\"mainnet\"]; // Default\n\t}\n\n\tcachedEnv = { ...result.data, enabledNetworks };\n\treturn cachedEnv;\n}\n\n// Export for testing\nexport { envSchema };\n",
5
+ "import { z } from \"zod/v4\";\n\n// Parse comma-separated networks\nconst networksSchema = z.string().transform((val) => {\n\tconst networks = val\n\t\t.split(\",\")\n\t\t.map((n) => n.trim())\n\t\t.filter(Boolean);\n\tconst valid = [\"mainnet\", \"testnet\"];\n\tfor (const n of networks) {\n\t\tif (!valid.includes(n)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Invalid network: ${n}. Must be one of: ${valid.join(\", \")}`,\n\t\t\t);\n\t\t}\n\t}\n\treturn networks as (\"mainnet\" | \"testnet\")[];\n});\n\ninterface EnvSchemaOutput {\n\tDATABASE_URL?: string;\n\t/**\n\t * Shared indexer DB (blocks/txs/events). Falls back to DATABASE_URL.\n\t * Set this alongside TARGET_DATABASE_URL to enable dual-DB mode.\n\t */\n\tSOURCE_DATABASE_URL?: string;\n\t/**\n\t * Tenant DB (subgraph schemas + subgraphs table). Falls back to DATABASE_URL.\n\t * Set this alongside SOURCE_DATABASE_URL to enable dual-DB mode.\n\t */\n\tTARGET_DATABASE_URL?: string;\n\tNETWORK?: \"mainnet\" | \"testnet\";\n\tNETWORKS?: (\"mainnet\" | \"testnet\")[];\n\tLOG_LEVEL: \"debug\" | \"info\" | \"warn\" | \"error\";\n\tNODE_ENV: \"development\" | \"production\" | \"test\";\n}\n\n// Cast needed: z.preprocess / z.default create different _input vs _output types\n// that z.ZodType<T> can't represent without explicit input type param\nconst envSchema: z.ZodType<EnvSchemaOutput> = z.object({\n\tDATABASE_URL: z.preprocess(\n\t\t(val) => (typeof val === \"string\" && val.length === 0 ? undefined : val),\n\t\tz.string().url().optional(),\n\t),\n\tSOURCE_DATABASE_URL: z.preprocess(\n\t\t(val) => (typeof val === \"string\" && val.length === 0 ? undefined : val),\n\t\tz.string().url().optional(),\n\t),\n\tTARGET_DATABASE_URL: z.preprocess(\n\t\t(val) => (typeof val === \"string\" && val.length === 0 ? undefined : val),\n\t\tz.string().url().optional(),\n\t),\n\tNETWORK: z.enum([\"mainnet\", \"testnet\"]).optional(),\n\tNETWORKS: networksSchema.optional(),\n\tLOG_LEVEL: z.enum([\"debug\", \"info\", \"warn\", \"error\"]).default(\"info\"),\n\tNODE_ENV: z\n\t\t.enum([\"development\", \"production\", \"test\"])\n\t\t.default(\"development\"),\n}) as unknown as z.ZodType<EnvSchemaOutput>;\n\nexport type Env = EnvSchemaOutput & {\n\tenabledNetworks: (\"mainnet\" | \"testnet\")[];\n};\n\nlet cachedEnv: Env | null = null;\n\nexport function getEnv(): Env {\n\tif (cachedEnv) {\n\t\treturn cachedEnv;\n\t}\n\n\tconst result = envSchema.safeParse(process.env);\n\n\tif (!result.success) {\n\t\tconsole.error(\"❌ Invalid environment configuration:\");\n\t\tconsole.error(z.treeifyError(result.error));\n\t\tthrow new Error(\"Invalid environment configuration\");\n\t}\n\n\t// Compute enabled networks from NETWORKS or NETWORK\n\tlet enabledNetworks: (\"mainnet\" | \"testnet\")[];\n\tif (result.data.NETWORKS && result.data.NETWORKS.length > 0) {\n\t\tenabledNetworks = result.data.NETWORKS;\n\t} else if (result.data.NETWORK) {\n\t\tenabledNetworks = [result.data.NETWORK];\n\t} else {\n\t\tenabledNetworks = [\"mainnet\"]; // Default\n\t}\n\n\tcachedEnv = { ...result.data, enabledNetworks };\n\treturn cachedEnv;\n}\n\n/**\n * PoX-4 stacking decoder is ON by default — `/v1/index/stacking` is part of the\n * public surface, so the decoder that fills `pox4_calls` runs unless explicitly\n * opted out with `POX4_DECODER_ENABLED=false` (mirrors the sBTC decoder policy).\n */\nexport function isPox4DecoderEnabled(): boolean {\n\treturn process.env.POX4_DECODER_ENABLED !== \"false\";\n}\n\n// Export for testing\nexport { envSchema };\n",
6
6
  "import { getEnv } from \"./env.ts\";\n\ntype LogLevel = \"debug\" | \"info\" | \"warn\" | \"error\";\n\nconst LOG_LEVELS: Record<LogLevel, number> = {\n\tdebug: 0,\n\tinfo: 1,\n\twarn: 2,\n\terror: 3,\n};\n\nclass Logger {\n\tprivate _level?: LogLevel;\n\tprivate _isProduction?: boolean;\n\tprivate _initialized = false;\n\n\tprivate init() {\n\t\tif (this._initialized) return;\n\t\tthis._initialized = true;\n\t\ttry {\n\t\t\tconst env = getEnv();\n\t\t\tthis._level = env.LOG_LEVEL;\n\t\t\tthis._isProduction = env.NODE_ENV === \"production\";\n\t\t} catch {\n\t\t\t// Fallback when env is unavailable (e.g. tests without DATABASE_URL)\n\t\t\tthis._level = \"info\";\n\t\t\tthis._isProduction = false;\n\t\t}\n\t}\n\n\tprivate get level(): LogLevel {\n\t\tthis.init();\n\t\t// biome-ignore lint/style/noNonNullAssertion: value is non-null after preceding check or by construction; TS narrowing limitation\n\t\treturn this._level!;\n\t}\n\n\tprivate get isProduction(): boolean {\n\t\tthis.init();\n\t\t// biome-ignore lint/style/noNonNullAssertion: value is non-null after preceding check or by construction; TS narrowing limitation\n\t\treturn this._isProduction!;\n\t}\n\n\tprivate shouldLog(level: LogLevel): boolean {\n\t\treturn LOG_LEVELS[level] >= LOG_LEVELS[this.level];\n\t}\n\n\tprivate formatMessage(\n\t\tlevel: LogLevel,\n\t\tmessage: string,\n\t\t// biome-ignore lint/suspicious/noExplicitAny: interop boundary or dynamic-shape value where typing adds friction without runtime safety\n\t\tmeta?: Record<string, any>,\n\t) {\n\t\tconst timestamp = new Date().toISOString();\n\n\t\tif (this.isProduction) {\n\t\t\t// JSON output for production\n\t\t\treturn JSON.stringify({\n\t\t\t\ttimestamp,\n\t\t\t\tlevel,\n\t\t\t\tmessage,\n\t\t\t\t...meta,\n\t\t\t});\n\t\t}\n\n\t\t// Human-readable output for development\n\t\tconst metaStr = meta ? ` ${JSON.stringify(meta)}` : \"\";\n\t\treturn `[${timestamp}] ${level.toUpperCase()}: ${message}${metaStr}`;\n\t}\n\n\t// biome-ignore lint/suspicious/noExplicitAny: interop boundary or dynamic-shape value where typing adds friction without runtime safety\n\tdebug(message: string, meta?: Record<string, any>): void {\n\t\tif (this.shouldLog(\"debug\")) {\n\t\t\tconsole.debug(this.formatMessage(\"debug\", message, meta));\n\t\t}\n\t}\n\n\t// biome-ignore lint/suspicious/noExplicitAny: interop boundary or dynamic-shape value where typing adds friction without runtime safety\n\tinfo(message: string, meta?: Record<string, any>): void {\n\t\tif (this.shouldLog(\"info\")) {\n\t\t\tconsole.info(this.formatMessage(\"info\", message, meta));\n\t\t}\n\t}\n\n\t// biome-ignore lint/suspicious/noExplicitAny: interop boundary or dynamic-shape value where typing adds friction without runtime safety\n\twarn(message: string, meta?: Record<string, any>): void {\n\t\tif (this.shouldLog(\"warn\")) {\n\t\t\tconsole.warn(this.formatMessage(\"warn\", message, meta));\n\t\t}\n\t}\n\n\t// biome-ignore lint/suspicious/noExplicitAny: interop boundary or dynamic-shape value where typing adds friction without runtime safety\n\terror(message: string, meta?: Record<string, any>): void {\n\t\tif (this.shouldLog(\"error\")) {\n\t\t\tconsole.error(this.formatMessage(\"error\", message, meta));\n\t\t}\n\t}\n}\n\n// Export singleton instance\nexport const logger: Logger = new Logger();\n",
7
7
  "import { type RawBuilder, sql } from \"kysely\";\n\n/**\n * Safely encode a JS value as a JSONB literal for Kysely inserts/updates.\n * Kysely + postgres.js double-encodes JSON when using parameterized queries\n * with ::jsonb casts. This uses sql.raw to inline a properly escaped literal.\n *\n * Generic parameter lets callers set the RawBuilder's output type so they\n * don't need to cast at the insert site. Default is `unknown` — widen at\n * the call site when the column type is narrower, e.g. `jsonb<MyShape>(...)`.\n */\nexport function jsonb<T = unknown>(value: T): RawBuilder<T> {\n\tconst escaped = JSON.stringify(value, (_k, v) =>\n\t\ttypeof v === \"bigint\" ? v.toString() : v,\n\t).replace(/'/g, \"''\");\n\treturn sql`${sql.raw(`'${escaped}'::jsonb`)}`;\n}\n\n/**\n * Safely parse a JSONB value from the database.\n * Handles double-encoded strings where postgres.js returns a JSON string\n * instead of a parsed object.\n */\nexport function parseJsonb<T = unknown>(value: unknown): T {\n\tif (typeof value === \"string\") {\n\t\ttry {\n\t\t\treturn JSON.parse(value) as T;\n\t\t} catch {\n\t\t\treturn value as T;\n\t\t}\n\t}\n\treturn (value ?? {}) as T;\n}\n",
8
- "import { Kysely } from \"kysely\";\nimport { PostgresJSDialect } from \"kysely-postgres-js\";\nimport postgres from \"postgres\";\nimport { logger } from \"../logger.ts\";\nimport type { Database } from \"./types.ts\";\n\nexport type { Database } from \"./types.ts\";\n\nconst DEFAULT_URL =\n\t\"postgres://postgres:postgres@localhost:5432/secondlayer_dev\";\n\ninterface PoolEntry {\n\tdb: Kysely<Database>;\n\trawClient: ReturnType<typeof postgres>;\n\t/** Last access (ms) — drives LRU eviction of BYO pools. */\n\tlastUsed: number;\n\t/** Monotonic counter as a tiebreaker; Date.now() can repeat under load. */\n\tseq: number;\n}\n\n/**\n * Cache of Kysely + raw postgres.js pools keyed by resolved URL.\n * Two getters resolving to the same URL share one entry (single pool) —\n * this is the single-DB backward-compat contract: when only `DATABASE_URL`\n * is set, `getSourceDb() === getTargetDb()` (zero regression vs. pre-dual-DB).\n *\n * The BYO data plane adds one pool per user-owned DB. To stop N user DBs from\n * exhausting connections/FDs, the map is bounded (`DATABASE_MAX_POOLS`, default\n * 25) with LRU eviction — the hot source/target pools are never evicted.\n */\nconst pools = new Map<string, PoolEntry>();\nlet poolSeq = 0;\n\nfunction maxPools(): number {\n\treturn Number.parseInt(process.env.DATABASE_MAX_POOLS ?? \"25\", 10);\n}\n\n/** Close the least-recently-used non-(source/target) pool when over the cap. */\nfunction evictIfNeeded(): void {\n\tif (pools.size <= maxPools()) return;\n\tconst protectedUrls = new Set([resolveSourceUrl(), resolveTargetUrl()]);\n\tlet lruUrl: string | undefined;\n\tlet lruEntry: PoolEntry | undefined;\n\tfor (const [url, entry] of pools) {\n\t\tif (protectedUrls.has(url)) continue;\n\t\tif (\n\t\t\t!lruEntry ||\n\t\t\tentry.lastUsed < lruEntry.lastUsed ||\n\t\t\t(entry.lastUsed === lruEntry.lastUsed && entry.seq < lruEntry.seq)\n\t\t) {\n\t\t\tlruUrl = url;\n\t\t\tlruEntry = entry;\n\t\t}\n\t}\n\tif (!lruUrl || !lruEntry) return;\n\tpools.delete(lruUrl);\n\tconst evicted = lruEntry;\n\t// Close in the background — never block pool creation on teardown.\n\tvoid evicted.db\n\t\t.destroy()\n\t\t.catch(() => {})\n\t\t.then(() => evicted.rawClient.end({ timeout: 5 }))\n\t\t.catch(() => {});\n}\n\nfunction resolveSourceUrl(): string {\n\treturn (\n\t\tprocess.env.SOURCE_DATABASE_URL || process.env.DATABASE_URL || DEFAULT_URL\n\t);\n}\n\nfunction resolveTargetUrl(): string {\n\treturn (\n\t\tprocess.env.TARGET_DATABASE_URL || process.env.DATABASE_URL || DEFAULT_URL\n\t);\n}\n\nfunction getOrCreatePool(url: string): PoolEntry {\n\tconst existing = pools.get(url);\n\tif (existing) {\n\t\texisting.lastUsed = Date.now();\n\t\texisting.seq = poolSeq++;\n\t\treturn existing;\n\t}\n\n\t// \"Local\" = we skip TLS. Any Docker service alias (single-label hostname\n\t// with no dots) is on an internal network and won't serve TLS.\n\tconst host = (() => {\n\t\ttry {\n\t\t\treturn new URL(url).hostname;\n\t\t} catch {\n\t\t\treturn \"\";\n\t\t}\n\t})();\n\tconst isLocal =\n\t\thost === \"localhost\" || host === \"127.0.0.1\" || !host.includes(\".\");\n\tconst poolMax = Number.parseInt(process.env.DATABASE_POOL_MAX ?? \"20\", 10);\n\t// Close idle connections so a fleet of BYO pools doesn't pin connections it\n\t// no longer needs (0 = never; postgres.js default).\n\tconst idleTimeout = Number.parseInt(\n\t\tprocess.env.DATABASE_IDLE_TIMEOUT ?? \"300\",\n\t\t10,\n\t);\n\tconst rawClient = postgres(url, {\n\t\tmax: poolMax,\n\t\tidle_timeout: idleTimeout,\n\t\tssl: isLocal\n\t\t\t? undefined\n\t\t\t: {\n\t\t\t\t\trejectUnauthorized: process.env.NODE_TLS_REJECT_UNAUTHORIZED !== \"0\",\n\t\t\t\t},\n\t});\n\tconst db = new Kysely<Database>({\n\t\tdialect: new PostgresJSDialect({ postgres: rawClient }),\n\t\t// Diagnostic hook: surface the failing SQL whenever postgres rejects\n\t\t// with code 42P10 (ON CONFLICT target doesn't match any unique\n\t\t// constraint). Temporary — remove once we've caught the culprit\n\t\t// query in prod logs and fixed the schema drift.\n\t\tlog: (event) => {\n\t\t\tif (event.level !== \"error\") return;\n\t\t\tconst err = event.error as {\n\t\t\t\tcode?: string;\n\t\t\t\tmessage?: string;\n\t\t\t} | null;\n\t\t\tif (err?.code !== \"42P10\") return;\n\t\t\tlogger.warn(\"db.on_conflict_constraint_missing\", {\n\t\t\t\tcode: err.code,\n\t\t\t\tmessage: err.message,\n\t\t\t\tsql: event.query.sql,\n\t\t\t\tparams: event.query.parameters,\n\t\t\t});\n\t\t},\n\t});\n\tconst entry: PoolEntry = {\n\t\tdb,\n\t\trawClient,\n\t\tlastUsed: Date.now(),\n\t\tseq: poolSeq++,\n\t};\n\tpools.set(url, entry);\n\tevictIfNeeded();\n\treturn entry;\n}\n\n/**\n * Kysely instance for the SOURCE DB (block/tx/event reads from the shared\n * indexer). Resolution: `SOURCE_DATABASE_URL || DATABASE_URL`.\n */\nexport function getSourceDb(): Kysely<Database> {\n\treturn getOrCreatePool(resolveSourceUrl()).db;\n}\n\n/**\n * Kysely instance for the TARGET DB (subgraph schemas, subgraphs table,\n * account-scoped data — tenant-side writes). Resolution:\n * `TARGET_DATABASE_URL || DATABASE_URL`.\n */\nexport function getTargetDb(): Kysely<Database> {\n\treturn getOrCreatePool(resolveTargetUrl()).db;\n}\n\n/**\n * Backward-compat alias for `getTargetDb()`. Accepts an optional\n * `connectionString` override used by seed/test helpers — when supplied,\n * bypasses env resolution and uses the provided URL directly (still cached).\n */\nexport function getDb(connectionString?: string): Kysely<Database> {\n\tif (connectionString) return getOrCreatePool(connectionString).db;\n\treturn getTargetDb();\n}\n\n/**\n * Raw postgres.js client for dynamic schema DDL (CREATE SCHEMA, DROP, etc.).\n * Defaults to the target role (tenant schemas live in the target DB).\n */\nexport function getRawClient(\n\trole: \"source\" | \"target\" = \"target\",\n): ReturnType<typeof postgres> {\n\tconst url = role === \"source\" ? resolveSourceUrl() : resolveTargetUrl();\n\treturn getOrCreatePool(url).rawClient;\n}\n\n/**\n * Raw postgres.js client for an arbitrary connection string (cached by URL).\n * Used by the BYO data plane to run DDL / serving queries against a\n * user-owned Postgres. Distinct from {@link getRawClient}, which only knows the\n * source/target roles resolved from env.\n */\nexport function getRawClientFor(url: string): ReturnType<typeof postgres> {\n\treturn getOrCreatePool(url).rawClient;\n}\n\n/** Close all DB connection pools. Call in CLI commands to allow process exit. */\nexport async function closeDb(): Promise<void> {\n\tfor (const entry of pools.values()) {\n\t\tawait entry.db.destroy();\n\t\tawait entry.rawClient.end();\n\t}\n\tpools.clear();\n}\n\nimport { sql } from \"kysely\";\nexport { sql };\nexport * from \"./types.ts\";\nexport { jsonb, parseJsonb } from \"./jsonb.ts\";\n",
9
- "import { getSourceDb, sql } from \"../index.ts\";\nimport type { Database } from \"../types.ts\";\nimport type { Kysely, Transaction } from \"kysely\";\n\nexport type ChainReorgCursor = {\n\tblock_height: number;\n\tevent_index: number;\n};\n\nexport type ChainReorgRecord = {\n\tid: string;\n\tdetected_at: string;\n\tfork_point_height: number;\n\told_index_block_hash: string | null;\n\tnew_index_block_hash: string | null;\n\torphaned_range: { from: string; to: string };\n\tnew_canonical_tip: string;\n};\n\nexport type InsertChainReorgParams = {\n\tforkPointHeight: number;\n\toldIndexBlockHash?: string | null;\n\tnewIndexBlockHash?: string | null;\n\torphanedFrom: ChainReorgCursor;\n\torphanedTo: ChainReorgCursor;\n\tnewCanonicalTip: ChainReorgCursor;\n\tdb?: Kysely<Database> | Transaction<Database>;\n};\n\nexport type ReadChainReorgsSinceParams = {\n\tsince: Date | ChainReorgCursor;\n\tlimit: number;\n\tdb?: Kysely<Database>;\n};\n\nexport type ReadChainReorgsForRangeParams = {\n\tfrom: ChainReorgCursor;\n\tto: ChainReorgCursor;\n\tdb?: Kysely<Database>;\n};\n\ntype ChainReorgRow = {\n\tid: string;\n\tdetected_at: Date | string;\n\tfork_point_height: string | number;\n\told_index_block_hash: string | null;\n\tnew_index_block_hash: string | null;\n\torphaned_from_height: string | number;\n\torphaned_from_event_index: string | number;\n\torphaned_to_height: string | number;\n\torphaned_to_event_index: string | number;\n\tnew_canonical_height: string | number;\n\tnew_canonical_event_index: string | number;\n};\n\nexport function encodeChainReorgCursor(cursor: ChainReorgCursor): string {\n\treturn `${cursor.block_height}:${cursor.event_index}`;\n}\n\nfunction normalizeRow(row: ChainReorgRow): ChainReorgRecord {\n\tconst detectedAt =\n\t\trow.detected_at instanceof Date\n\t\t\t? row.detected_at.toISOString()\n\t\t\t: new Date(row.detected_at).toISOString();\n\n\treturn {\n\t\tid: row.id,\n\t\tdetected_at: detectedAt,\n\t\tfork_point_height: Number(row.fork_point_height),\n\t\told_index_block_hash: row.old_index_block_hash,\n\t\tnew_index_block_hash: row.new_index_block_hash,\n\t\torphaned_range: {\n\t\t\tfrom: encodeChainReorgCursor({\n\t\t\t\tblock_height: Number(row.orphaned_from_height),\n\t\t\t\tevent_index: Number(row.orphaned_from_event_index),\n\t\t\t}),\n\t\t\tto: encodeChainReorgCursor({\n\t\t\t\tblock_height: Number(row.orphaned_to_height),\n\t\t\t\tevent_index: Number(row.orphaned_to_event_index),\n\t\t\t}),\n\t\t},\n\t\tnew_canonical_tip: encodeChainReorgCursor({\n\t\t\tblock_height: Number(row.new_canonical_height),\n\t\t\tevent_index: Number(row.new_canonical_event_index),\n\t\t}),\n\t};\n}\n\nexport async function insertChainReorg(\n\tparams: InsertChainReorgParams,\n): Promise<ChainReorgRecord> {\n\tconst db = params.db ?? getSourceDb();\n\tconst row = await db\n\t\t.insertInto(\"chain_reorgs\")\n\t\t.values({\n\t\t\tfork_point_height: params.forkPointHeight,\n\t\t\told_index_block_hash: params.oldIndexBlockHash ?? null,\n\t\t\tnew_index_block_hash: params.newIndexBlockHash ?? null,\n\t\t\torphaned_from_height: params.orphanedFrom.block_height,\n\t\t\torphaned_from_event_index: params.orphanedFrom.event_index,\n\t\t\torphaned_to_height: params.orphanedTo.block_height,\n\t\t\torphaned_to_event_index: params.orphanedTo.event_index,\n\t\t\tnew_canonical_height: params.newCanonicalTip.block_height,\n\t\t\tnew_canonical_event_index: params.newCanonicalTip.event_index,\n\t\t})\n\t\t.returningAll()\n\t\t.executeTakeFirstOrThrow();\n\n\treturn normalizeRow(row);\n}\n\nexport async function readChainReorgsSince(\n\tparams: ReadChainReorgsSinceParams,\n): Promise<ChainReorgRecord[]> {\n\tconst db = params.db ?? getSourceDb();\n\tconst limit = Math.min(1000, Math.max(1, params.limit));\n\n\tconst result =\n\t\tparams.since instanceof Date\n\t\t\t? await sql<ChainReorgRow>`\n\t\t\t\t\tSELECT *\n\t\t\t\t\tFROM chain_reorgs\n\t\t\t\t\tWHERE detected_at > ${params.since}\n\t\t\t\t\tORDER BY detected_at ASC, id ASC\n\t\t\t\t\tLIMIT ${limit}\n\t\t\t\t`.execute(db)\n\t\t\t: await sql<ChainReorgRow>`\n\t\t\t\t\tSELECT *\n\t\t\t\t\tFROM chain_reorgs\n\t\t\t\t\tWHERE\n\t\t\t\t\t\torphaned_to_height > ${params.since.block_height}\n\t\t\t\t\t\tOR (\n\t\t\t\t\t\t\torphaned_to_height = ${params.since.block_height}\n\t\t\t\t\t\t\tAND orphaned_to_event_index >= ${params.since.event_index}\n\t\t\t\t\t\t)\n\t\t\t\t\tORDER BY detected_at ASC, id ASC\n\t\t\t\t\tLIMIT ${limit}\n\t\t\t\t`.execute(db);\n\n\treturn result.rows.map(normalizeRow);\n}\n\nexport async function readChainReorgsForRange(\n\tparams: ReadChainReorgsForRangeParams,\n): Promise<ChainReorgRecord[]> {\n\tconst db = params.db ?? getSourceDb();\n\tconst { from, to } = params;\n\tconst { rows } = await sql<ChainReorgRow>`\n\t\tSELECT *\n\t\tFROM chain_reorgs\n\t\tWHERE\n\t\t\t(\n\t\t\t\torphaned_from_height < ${to.block_height}\n\t\t\t\tOR (\n\t\t\t\t\torphaned_from_height = ${to.block_height}\n\t\t\t\t\tAND orphaned_from_event_index <= ${to.event_index}\n\t\t\t\t)\n\t\t\t)\n\t\t\tAND (\n\t\t\t\torphaned_to_height > ${from.block_height}\n\t\t\t\tOR (\n\t\t\t\t\torphaned_to_height = ${from.block_height}\n\t\t\t\t\tAND orphaned_to_event_index >= ${from.event_index}\n\t\t\t\t)\n\t\t\t)\n\t\tORDER BY detected_at ASC, id ASC\n\t`.execute(db);\n\n\treturn rows.map(normalizeRow);\n}\n"
8
+ "import { Kysely } from \"kysely\";\nimport { PostgresJSDialect } from \"kysely-postgres-js\";\nimport postgres from \"postgres\";\nimport { logger } from \"../logger.ts\";\nimport type { Database } from \"./types.ts\";\n\nexport type { Database } from \"./types.ts\";\n\nconst DEFAULT_URL =\n\t\"postgres://postgres:postgres@localhost:5432/secondlayer_dev\";\n\ninterface PoolEntry {\n\tdb: Kysely<Database>;\n\trawClient: ReturnType<typeof postgres>;\n\t/** Last access (ms) — drives LRU eviction of BYO pools. */\n\tlastUsed: number;\n\t/** Monotonic counter as a tiebreaker; Date.now() can repeat under load. */\n\tseq: number;\n}\n\n/**\n * Cache of Kysely + raw postgres.js pools keyed by resolved URL.\n * Two getters resolving to the same URL share one entry (single pool) —\n * this is the single-DB backward-compat contract: when only `DATABASE_URL`\n * is set, `getSourceDb() === getTargetDb()` (zero regression vs. pre-dual-DB).\n *\n * The BYO data plane adds one pool per user-owned DB. To stop N user DBs from\n * exhausting connections/FDs, the map is bounded (`DATABASE_MAX_POOLS`, default\n * 25) with LRU eviction — the hot source/target pools are never evicted.\n */\nconst pools = new Map<string, PoolEntry>();\nlet poolSeq = 0;\n\nfunction maxPools(): number {\n\treturn Number.parseInt(process.env.DATABASE_MAX_POOLS ?? \"25\", 10);\n}\n\n/** Close the least-recently-used non-(source/target) pool when over the cap. */\nfunction evictIfNeeded(): void {\n\tif (pools.size <= maxPools()) return;\n\tconst protectedUrls = new Set([resolveSourceUrl(), resolveTargetUrl()]);\n\tlet lruUrl: string | undefined;\n\tlet lruEntry: PoolEntry | undefined;\n\tfor (const [url, entry] of pools) {\n\t\tif (protectedUrls.has(url)) continue;\n\t\tif (\n\t\t\t!lruEntry ||\n\t\t\tentry.lastUsed < lruEntry.lastUsed ||\n\t\t\t(entry.lastUsed === lruEntry.lastUsed && entry.seq < lruEntry.seq)\n\t\t) {\n\t\t\tlruUrl = url;\n\t\t\tlruEntry = entry;\n\t\t}\n\t}\n\tif (!lruUrl || !lruEntry) return;\n\tpools.delete(lruUrl);\n\tconst evicted = lruEntry;\n\t// Close in the background — never block pool creation on teardown.\n\tvoid evicted.db\n\t\t.destroy()\n\t\t.catch(() => {})\n\t\t.then(() => evicted.rawClient.end({ timeout: 5 }))\n\t\t.catch(() => {});\n}\n\nfunction resolveSourceUrl(): string {\n\treturn (\n\t\tprocess.env.SOURCE_DATABASE_URL || process.env.DATABASE_URL || DEFAULT_URL\n\t);\n}\n\nfunction resolveTargetUrl(): string {\n\treturn (\n\t\tprocess.env.TARGET_DATABASE_URL || process.env.DATABASE_URL || DEFAULT_URL\n\t);\n}\n\n/**\n * Boot guard: if the chain/control split is requested (`SOURCE_DATABASE_URL` or\n * `TARGET_DATABASE_URL` set) but both still resolve to the same DB — a typo, or\n * a stray `DATABASE_URL` masking the intent — the split silently collapses and\n * chain + control plane share one instance. Surface it loudly. Fail-soft (no\n * throw) so a misconfig doesn't brick startup; logs make it obvious.\n */\nexport function assertDbSplit(): void {\n\tconst wantsSplit = !!(\n\t\tprocess.env.SOURCE_DATABASE_URL || process.env.TARGET_DATABASE_URL\n\t);\n\tif (!wantsSplit) return;\n\tif (resolveSourceUrl() === resolveTargetUrl()) {\n\t\tconst msg =\n\t\t\t\"DB split requested but SOURCE_DATABASE_URL === TARGET_DATABASE_URL (check for a typo or a stray DATABASE_URL fallback)\";\n\t\tif (process.env.NODE_ENV === \"production\") console.error(`❌ ${msg}`);\n\t\telse console.warn(`⚠️ ${msg}`);\n\t}\n}\n\nfunction getOrCreatePool(url: string): PoolEntry {\n\tconst existing = pools.get(url);\n\tif (existing) {\n\t\texisting.lastUsed = Date.now();\n\t\texisting.seq = poolSeq++;\n\t\treturn existing;\n\t}\n\n\t// \"Local\" = we skip TLS. Any Docker service alias (single-label hostname\n\t// with no dots) is on an internal network and won't serve TLS.\n\tconst host = (() => {\n\t\ttry {\n\t\t\treturn new URL(url).hostname;\n\t\t} catch {\n\t\t\treturn \"\";\n\t\t}\n\t})();\n\tconst isLocal =\n\t\thost === \"localhost\" || host === \"127.0.0.1\" || !host.includes(\".\");\n\tconst poolMax = Number.parseInt(process.env.DATABASE_POOL_MAX ?? \"20\", 10);\n\t// Close idle connections so a fleet of BYO pools doesn't pin connections it\n\t// no longer needs (0 = never; postgres.js default).\n\tconst idleTimeout = Number.parseInt(\n\t\tprocess.env.DATABASE_IDLE_TIMEOUT ?? \"300\",\n\t\t10,\n\t);\n\tconst rawClient = postgres(url, {\n\t\tmax: poolMax,\n\t\tidle_timeout: idleTimeout,\n\t\tssl: isLocal\n\t\t\t? undefined\n\t\t\t: {\n\t\t\t\t\trejectUnauthorized: process.env.NODE_TLS_REJECT_UNAUTHORIZED !== \"0\",\n\t\t\t\t},\n\t});\n\tconst db = new Kysely<Database>({\n\t\tdialect: new PostgresJSDialect({ postgres: rawClient }),\n\t\t// Diagnostic hook: surface the failing SQL whenever postgres rejects\n\t\t// with code 42P10 (ON CONFLICT target doesn't match any unique\n\t\t// constraint). Temporary — remove once we've caught the culprit\n\t\t// query in prod logs and fixed the schema drift.\n\t\tlog: (event) => {\n\t\t\tif (event.level !== \"error\") return;\n\t\t\tconst err = event.error as {\n\t\t\t\tcode?: string;\n\t\t\t\tmessage?: string;\n\t\t\t} | null;\n\t\t\tif (err?.code !== \"42P10\") return;\n\t\t\tlogger.warn(\"db.on_conflict_constraint_missing\", {\n\t\t\t\tcode: err.code,\n\t\t\t\tmessage: err.message,\n\t\t\t\tsql: event.query.sql,\n\t\t\t\tparams: event.query.parameters,\n\t\t\t});\n\t\t},\n\t});\n\tconst entry: PoolEntry = {\n\t\tdb,\n\t\trawClient,\n\t\tlastUsed: Date.now(),\n\t\tseq: poolSeq++,\n\t};\n\tpools.set(url, entry);\n\tevictIfNeeded();\n\treturn entry;\n}\n\n/**\n * Kysely instance for the SOURCE DB (block/tx/event reads from the shared\n * indexer). Resolution: `SOURCE_DATABASE_URL || DATABASE_URL`.\n */\nexport function getSourceDb(): Kysely<Database> {\n\treturn getOrCreatePool(resolveSourceUrl()).db;\n}\n\n/**\n * Kysely instance for the TARGET DB (subgraph schemas, subgraphs table,\n * account-scoped data — tenant-side writes). Resolution:\n * `TARGET_DATABASE_URL || DATABASE_URL`.\n */\nexport function getTargetDb(): Kysely<Database> {\n\treturn getOrCreatePool(resolveTargetUrl()).db;\n}\n\n/**\n * Backward-compat alias for `getTargetDb()`. Accepts an optional\n * `connectionString` override used by seed/test helpers — when supplied,\n * bypasses env resolution and uses the provided URL directly (still cached).\n */\nexport function getDb(connectionString?: string): Kysely<Database> {\n\tif (connectionString) return getOrCreatePool(connectionString).db;\n\treturn getTargetDb();\n}\n\n/**\n * Raw postgres.js client for dynamic schema DDL (CREATE SCHEMA, DROP, etc.).\n * Defaults to the target role (tenant schemas live in the target DB).\n */\nexport function getRawClient(\n\trole: \"source\" | \"target\" = \"target\",\n): ReturnType<typeof postgres> {\n\tconst url = role === \"source\" ? resolveSourceUrl() : resolveTargetUrl();\n\treturn getOrCreatePool(url).rawClient;\n}\n\n/**\n * Raw postgres.js client for an arbitrary connection string (cached by URL).\n * Used by the BYO data plane to run DDL / serving queries against a\n * user-owned Postgres. Distinct from {@link getRawClient}, which only knows the\n * source/target roles resolved from env.\n */\nexport function getRawClientFor(url: string): ReturnType<typeof postgres> {\n\treturn getOrCreatePool(url).rawClient;\n}\n\n/** Close all DB connection pools. Call in CLI commands to allow process exit. */\nexport async function closeDb(): Promise<void> {\n\tfor (const entry of pools.values()) {\n\t\tawait entry.db.destroy();\n\t\tawait entry.rawClient.end();\n\t}\n\tpools.clear();\n}\n\nimport { sql } from \"kysely\";\nexport { sql };\nexport * from \"./types.ts\";\nexport type { DbReadRow, NumericAsText } from \"./read-row.ts\";\nexport { SOURCE_READ_COLUMNS } from \"./source-read-columns.ts\";\nexport { jsonb, parseJsonb } from \"./jsonb.ts\";\n",
9
+ "import type { Database } from \"./types.ts\";\n\n// The physical source-table columns the public read API (packages/api) actually\n// reads via getSourceDb(). This is the read contract between the indexer (which\n// owns the write schema) and the API (which reads it raw). A column rename or\n// removal on the producer side that isn't reflected here is caught by the\n// snapshot drift test; the `satisfies` clause guarantees every entry is a real\n// table + column today.\n//\n// Computed/aliased read expressions are intentionally excluded — they are not\n// part of the producer contract: `cursor`/`block_time` where derived in SQL\n// rather than stored, window ranks (stream_event_index), and JSONB extractions\n// from `events.data`. Generated bookkeeping columns (created_at/updated_at) are\n// excluded unless a read path selects them.\nexport const SOURCE_READ_COLUMNS = {\n\tblocks: [\n\t\t\"burn_block_hash\",\n\t\t\"burn_block_height\",\n\t\t\"canonical\",\n\t\t\"hash\",\n\t\t\"height\",\n\t\t\"parent_hash\",\n\t\t\"timestamp\",\n\t],\n\tdecoded_events: [\n\t\t\"amount\",\n\t\t\"asset_identifier\",\n\t\t\"block_height\",\n\t\t\"canonical\",\n\t\t\"contract_id\",\n\t\t\"cursor\",\n\t\t\"event_index\",\n\t\t\"event_type\",\n\t\t\"memo\",\n\t\t\"payload\",\n\t\t\"recipient\",\n\t\t\"sender\",\n\t\t\"tx_id\",\n\t\t\"tx_index\",\n\t\t\"value\",\n\t],\n\ttransactions: [\n\t\t\"block_height\",\n\t\t\"contract_id\",\n\t\t\"function_args\",\n\t\t\"function_name\",\n\t\t\"raw_result\",\n\t\t\"raw_tx\",\n\t\t\"sender\",\n\t\t\"status\",\n\t\t\"tx_id\",\n\t\t\"tx_index\",\n\t\t\"type\",\n\t],\n\tmempool_transactions: [\n\t\t\"contract_id\",\n\t\t\"function_args\",\n\t\t\"function_name\",\n\t\t\"raw_tx\",\n\t\t\"received_at\",\n\t\t\"sender\",\n\t\t\"seq\",\n\t\t\"tx_id\",\n\t\t\"type\",\n\t],\n\tpox4_calls: [\n\t\t\"aggregated_amount_ustx\",\n\t\t\"aggregated_signer_index\",\n\t\t\"amount_ustx\",\n\t\t\"auth_allowed\",\n\t\t\"auth_id\",\n\t\t\"auth_period\",\n\t\t\"auth_topic\",\n\t\t\"block_height\",\n\t\t\"block_time\",\n\t\t\"burn_block_height\",\n\t\t\"caller\",\n\t\t\"canonical\",\n\t\t\"cursor\",\n\t\t\"delegate_to\",\n\t\t\"end_cycle\",\n\t\t\"function_name\",\n\t\t\"lock_period\",\n\t\t\"max_amount\",\n\t\t\"pox_addr_btc\",\n\t\t\"pox_addr_hashbytes\",\n\t\t\"pox_addr_version\",\n\t\t\"result_ok\",\n\t\t\"reward_cycle\",\n\t\t\"signer_key\",\n\t\t\"signer_signature\",\n\t\t\"source_cursor\",\n\t\t\"stacker\",\n\t\t\"start_cycle\",\n\t\t\"tx_id\",\n\t\t\"tx_index\",\n\t],\n\tsbtc_events: [\n\t\t\"amount\",\n\t\t\"bitcoin_txid\",\n\t\t\"block_height\",\n\t\t\"block_time\",\n\t\t\"burn_hash\",\n\t\t\"burn_height\",\n\t\t\"canonical\",\n\t\t\"cursor\",\n\t\t\"event_index\",\n\t\t\"fee\",\n\t\t\"governance_contract_type\",\n\t\t\"governance_new_contract\",\n\t\t\"max_fee\",\n\t\t\"output_index\",\n\t\t\"recipient_btc_hashbytes\",\n\t\t\"recipient_btc_version\",\n\t\t\"request_id\",\n\t\t\"sender\",\n\t\t\"signer_address\",\n\t\t\"signer_aggregate_pubkey\",\n\t\t\"signer_bitmap\",\n\t\t\"signer_keys_count\",\n\t\t\"signer_threshold\",\n\t\t\"sweep_txid\",\n\t\t\"topic\",\n\t\t\"tx_id\",\n\t\t\"tx_index\",\n\t],\n\tsbtc_token_events: [\n\t\t\"amount\",\n\t\t\"block_height\",\n\t\t\"block_time\",\n\t\t\"canonical\",\n\t\t\"cursor\",\n\t\t\"event_index\",\n\t\t\"event_type\",\n\t\t\"memo\",\n\t\t\"recipient\",\n\t\t\"sender\",\n\t\t\"tx_id\",\n\t\t\"tx_index\",\n\t],\n\tbns_name_events: [\n\t\t\"block_height\",\n\t\t\"block_time\",\n\t\t\"bns_id\",\n\t\t\"canonical\",\n\t\t\"cursor\",\n\t\t\"event_index\",\n\t\t\"fqn\",\n\t\t\"hashed_salted_fqn_preorder\",\n\t\t\"imported_at\",\n\t\t\"name\",\n\t\t\"namespace\",\n\t\t\"owner\",\n\t\t\"preordered_by\",\n\t\t\"registered_at\",\n\t\t\"renewal_height\",\n\t\t\"stx_burn\",\n\t\t\"topic\",\n\t\t\"tx_id\",\n\t\t\"tx_index\",\n\t],\n\tbns_namespace_events: [\n\t\t\"block_height\",\n\t\t\"block_time\",\n\t\t\"canonical\",\n\t\t\"cursor\",\n\t\t\"event_index\",\n\t\t\"launched_at\",\n\t\t\"lifetime\",\n\t\t\"manager\",\n\t\t\"manager_frozen\",\n\t\t\"manager_transfers_disabled\",\n\t\t\"namespace\",\n\t\t\"price_frozen\",\n\t\t\"price_function\",\n\t\t\"revealed_at\",\n\t\t\"status\",\n\t\t\"tx_id\",\n\t\t\"tx_index\",\n\t],\n\tbns_marketplace_events: [\n\t\t\"action\",\n\t\t\"block_height\",\n\t\t\"block_time\",\n\t\t\"bns_id\",\n\t\t\"canonical\",\n\t\t\"commission\",\n\t\t\"cursor\",\n\t\t\"event_index\",\n\t\t\"price_ustx\",\n\t\t\"tx_id\",\n\t\t\"tx_index\",\n\t],\n\tbns_names: [\n\t\t\"bns_id\",\n\t\t\"fqn\",\n\t\t\"last_event_at\",\n\t\t\"last_event_cursor\",\n\t\t\"name\",\n\t\t\"namespace\",\n\t\t\"owner\",\n\t\t\"registered_at\",\n\t\t\"renewal_height\",\n\t],\n\tbns_namespaces: [\n\t\t\"last_event_at\",\n\t\t\"last_event_cursor\",\n\t\t\"launched_at\",\n\t\t\"lifetime\",\n\t\t\"manager\",\n\t\t\"manager_frozen\",\n\t\t\"name_count\",\n\t\t\"namespace\",\n\t\t\"price_frozen\",\n\t],\n\tburn_block_rewards: [\n\t\t\"amount_sats\",\n\t\t\"burn_amount\",\n\t\t\"burn_block_hash\",\n\t\t\"burn_block_height\",\n\t\t\"canonical\",\n\t\t\"cursor\",\n\t\t\"recipient_btc\",\n\t\t\"reward_index\",\n\t],\n\tburn_block_reward_slots: [\n\t\t\"burn_block_hash\",\n\t\t\"burn_block_height\",\n\t\t\"canonical\",\n\t\t\"cursor\",\n\t\t\"holder_btc\",\n\t\t\"slot_index\",\n\t],\n\tevents: [\"block_height\", \"data\", \"event_index\", \"tx_id\", \"type\"],\n\tchain_reorgs: [\"detected_at\"],\n} as const satisfies {\n\treadonly [T in keyof Database]?: readonly (keyof Database[T])[];\n};\n",
10
+ "import type { Kysely, Transaction } from \"kysely\";\nimport { getSourceDb, sql } from \"../index.ts\";\nimport type { Database } from \"../types.ts\";\n\nexport type ChainReorgCursor = {\n\tblock_height: number;\n\tevent_index: number;\n};\n\nexport type ChainReorgRecord = {\n\tid: string;\n\tdetected_at: string;\n\tfork_point_height: number;\n\told_index_block_hash: string | null;\n\tnew_index_block_hash: string | null;\n\torphaned_range: { from: string; to: string };\n\tnew_canonical_tip: string;\n};\n\nexport type InsertChainReorgParams = {\n\tforkPointHeight: number;\n\toldIndexBlockHash?: string | null;\n\tnewIndexBlockHash?: string | null;\n\torphanedFrom: ChainReorgCursor;\n\torphanedTo: ChainReorgCursor;\n\tnewCanonicalTip: ChainReorgCursor;\n\tdb?: Kysely<Database> | Transaction<Database>;\n};\n\nexport type ReadChainReorgsSinceParams = {\n\tsince: Date | ChainReorgCursor;\n\tlimit: number;\n\tdb?: Kysely<Database>;\n};\n\nexport type ReadChainReorgsForRangeParams = {\n\tfrom: ChainReorgCursor;\n\tto: ChainReorgCursor;\n\tdb?: Kysely<Database>;\n};\n\nexport type ReadChainReorgsForHeightRangeParams = {\n\tfromHeight: number;\n\ttoHeight: number;\n\tdb?: Kysely<Database>;\n};\n\ntype ChainReorgRow = {\n\tid: string;\n\tdetected_at: Date | string;\n\tfork_point_height: string | number;\n\told_index_block_hash: string | null;\n\tnew_index_block_hash: string | null;\n\torphaned_from_height: string | number;\n\torphaned_from_event_index: string | number;\n\torphaned_to_height: string | number;\n\torphaned_to_event_index: string | number;\n\tnew_canonical_height: string | number;\n\tnew_canonical_event_index: string | number;\n};\n\nexport function encodeChainReorgCursor(cursor: ChainReorgCursor): string {\n\treturn `${cursor.block_height}:${cursor.event_index}`;\n}\n\nfunction normalizeRow(row: ChainReorgRow): ChainReorgRecord {\n\tconst detectedAt =\n\t\trow.detected_at instanceof Date\n\t\t\t? row.detected_at.toISOString()\n\t\t\t: new Date(row.detected_at).toISOString();\n\n\treturn {\n\t\tid: row.id,\n\t\tdetected_at: detectedAt,\n\t\tfork_point_height: Number(row.fork_point_height),\n\t\told_index_block_hash: row.old_index_block_hash,\n\t\tnew_index_block_hash: row.new_index_block_hash,\n\t\torphaned_range: {\n\t\t\tfrom: encodeChainReorgCursor({\n\t\t\t\tblock_height: Number(row.orphaned_from_height),\n\t\t\t\tevent_index: Number(row.orphaned_from_event_index),\n\t\t\t}),\n\t\t\tto: encodeChainReorgCursor({\n\t\t\t\tblock_height: Number(row.orphaned_to_height),\n\t\t\t\tevent_index: Number(row.orphaned_to_event_index),\n\t\t\t}),\n\t\t},\n\t\tnew_canonical_tip: encodeChainReorgCursor({\n\t\t\tblock_height: Number(row.new_canonical_height),\n\t\t\tevent_index: Number(row.new_canonical_event_index),\n\t\t}),\n\t};\n}\n\nexport async function insertChainReorg(\n\tparams: InsertChainReorgParams,\n): Promise<ChainReorgRecord> {\n\tconst db = params.db ?? getSourceDb();\n\tconst row = await db\n\t\t.insertInto(\"chain_reorgs\")\n\t\t.values({\n\t\t\tfork_point_height: params.forkPointHeight,\n\t\t\told_index_block_hash: params.oldIndexBlockHash ?? null,\n\t\t\tnew_index_block_hash: params.newIndexBlockHash ?? null,\n\t\t\torphaned_from_height: params.orphanedFrom.block_height,\n\t\t\torphaned_from_event_index: params.orphanedFrom.event_index,\n\t\t\torphaned_to_height: params.orphanedTo.block_height,\n\t\t\torphaned_to_event_index: params.orphanedTo.event_index,\n\t\t\tnew_canonical_height: params.newCanonicalTip.block_height,\n\t\t\tnew_canonical_event_index: params.newCanonicalTip.event_index,\n\t\t})\n\t\t.returningAll()\n\t\t.executeTakeFirstOrThrow();\n\n\treturn normalizeRow(row);\n}\n\nexport async function readChainReorgsSince(\n\tparams: ReadChainReorgsSinceParams,\n): Promise<ChainReorgRecord[]> {\n\tconst db = params.db ?? getSourceDb();\n\tconst limit = Math.min(1000, Math.max(1, params.limit));\n\n\tconst result =\n\t\tparams.since instanceof Date\n\t\t\t? await sql<ChainReorgRow>`\n\t\t\t\t\tSELECT *\n\t\t\t\t\tFROM chain_reorgs\n\t\t\t\t\tWHERE detected_at > ${params.since}\n\t\t\t\t\tORDER BY detected_at ASC, id ASC\n\t\t\t\t\tLIMIT ${limit}\n\t\t\t\t`.execute(db)\n\t\t\t: await sql<ChainReorgRow>`\n\t\t\t\t\tSELECT *\n\t\t\t\t\tFROM chain_reorgs\n\t\t\t\t\tWHERE\n\t\t\t\t\t\torphaned_to_height > ${params.since.block_height}\n\t\t\t\t\t\tOR (\n\t\t\t\t\t\t\torphaned_to_height = ${params.since.block_height}\n\t\t\t\t\t\t\tAND orphaned_to_event_index >= ${params.since.event_index}\n\t\t\t\t\t\t)\n\t\t\t\t\tORDER BY detected_at ASC, id ASC\n\t\t\t\t\tLIMIT ${limit}\n\t\t\t\t`.execute(db);\n\n\treturn result.rows.map(normalizeRow);\n}\n\n/**\n * Reorgs overlapping the block-height window [fromHeight, toHeight], ignoring\n * event_index. For cursor keyspaces that are NOT event-indexed (the\n * transactions / contract-calls endpoints key on block_height:tx_index): a\n * height-granular overlap. Over-inclusive — a partially-orphaned height in range\n * surfaces even if the page's specific rows survived — but never under-reports.\n */\nexport async function readChainReorgsForHeightRange(\n\tparams: ReadChainReorgsForHeightRangeParams,\n): Promise<ChainReorgRecord[]> {\n\treturn readChainReorgsForRange({\n\t\tfrom: { block_height: params.fromHeight, event_index: 0 },\n\t\t// int4 max — event_index is a Postgres `integer`, so a larger sentinel\n\t\t// (e.g. Number.MAX_SAFE_INTEGER) overflows when bound for comparison.\n\t\tto: { block_height: params.toHeight, event_index: 2_147_483_647 },\n\t\tdb: params.db,\n\t});\n}\n\nexport async function readChainReorgsForRange(\n\tparams: ReadChainReorgsForRangeParams,\n): Promise<ChainReorgRecord[]> {\n\tconst db = params.db ?? getSourceDb();\n\tconst { from, to } = params;\n\tconst { rows } = await sql<ChainReorgRow>`\n\t\tSELECT *\n\t\tFROM chain_reorgs\n\t\tWHERE\n\t\t\t(\n\t\t\t\torphaned_from_height < ${to.block_height}\n\t\t\t\tOR (\n\t\t\t\t\torphaned_from_height = ${to.block_height}\n\t\t\t\t\tAND orphaned_from_event_index <= ${to.event_index}\n\t\t\t\t)\n\t\t\t)\n\t\t\tAND (\n\t\t\t\torphaned_to_height > ${from.block_height}\n\t\t\t\tOR (\n\t\t\t\t\torphaned_to_height = ${from.block_height}\n\t\t\t\t\tAND orphaned_to_event_index >= ${from.event_index}\n\t\t\t\t)\n\t\t\t)\n\t\tORDER BY detected_at ASC, id ASC\n\t`.execute(db);\n\n\treturn rows.map(normalizeRow);\n}\n"
10
11
  ],
11
- "mappings": ";;;;;;;;;;;;;;;;;AAAA;AAGA,IAAM,iBAAiB,EAAE,OAAO,EAAE,UAAU,CAAC,QAAQ;AAAA,EACpD,MAAM,WAAW,IACf,MAAM,GAAG,EACT,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,EACnB,OAAO,OAAO;AAAA,EAChB,MAAM,QAAQ,CAAC,WAAW,SAAS;AAAA,EACnC,WAAW,KAAK,UAAU;AAAA,IACzB,IAAI,CAAC,MAAM,SAAS,CAAC,GAAG;AAAA,MACvB,MAAM,IAAI,MACT,oBAAoB,sBAAsB,MAAM,KAAK,IAAI,GAC1D;AAAA,IACD;AAAA,EACD;AAAA,EACA,OAAO;AAAA,CACP;AAsBD,IAAM,YAAwC,EAAE,OAAO;AAAA,EACtD,cAAc,EAAE,WACf,CAAC,QAAS,OAAO,QAAQ,YAAY,IAAI,WAAW,IAAI,YAAY,KACpE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,CAC3B;AAAA,EACA,qBAAqB,EAAE,WACtB,CAAC,QAAS,OAAO,QAAQ,YAAY,IAAI,WAAW,IAAI,YAAY,KACpE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,CAC3B;AAAA,EACA,qBAAqB,EAAE,WACtB,CAAC,QAAS,OAAO,QAAQ,YAAY,IAAI,WAAW,IAAI,YAAY,KACpE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,CAC3B;AAAA,EACA,SAAS,EAAE,KAAK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AAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAAA,EACA,mBAAmB;AAAA,IAClB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAAA,EACA,iBAAiB;AAAA,IAChB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAAA,EACA,sBAAsB;AAAA,IACrB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAAA,EACA,wBAAwB;AAAA,IACvB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAAA,EACA,WAAW;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAAA,EACA,gBAAgB;AAAA,IACf;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAAA,EACA,oBAAoB;AAAA,IACnB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;A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EAC9B,MAAM,KAAK,OAAO,MAAM,YAAY;AAAA,EACpC,MAAM,QAAQ,KAAK,IAAI,MAAM,KAAK,IAAI,GAAG,OAAO,KAAK,CAAC;AAAA,EAEtD,MAAM,SACL,OAAO,iBAAiB,OACrB,MAAM;AAAA;AAAA;AAAA,2BAGgB,OAAO;AAAA;AAAA,aAErB;AAAA,MACP,QAAQ,EAAE,IACX,MAAM;AAAA;AAAA;AAAA;AAAA,6BAIkB,OAAO,MAAM;AAAA;AAAA,8BAEZ,OAAO,MAAM;AAAA,wCACH,OAAO,MAAM;AAAA;AAAA;AAAA,aAGxC;AAAA,MACP,QAAQ,EAAE;AAAA,EAEf,OAAO,OAAO,KAAK,IAAI,YAAY;AAAA;AAUpC,eAAsB,6BAA6B,CAClD,QAC8B;AAAA,EAC9B,OAAO,wBAAwB;AAAA,IAC9B,MAAM,EAAE,cAAc,OAAO,YAAY,aAAa,EAAE;AAAA,IAGxD,IAAI,EAAE,cAAc,OAAO,UAAU,aAAa,WAAc;AAAA,IAChE,IAAI,OAAO;AAAA,EACZ,CAAC;AAAA;AAGF,eAAsB,uBAAuB,CAC5C,QAC8B;AAAA,EAC9B,MAAM,KAAK,OAAO,MAAM,YAAY;AAAA,EACpC,QAAQ,MAAM,OAAO;AAAA,EACrB,QAAQ,SAAS,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA,6BAKK,GAAG;AAAA;AAAA,8BAEF,GAAG;AAAA,wCACO,GAAG;AAAA;AAAA;AAAA;AAAA,2BAIhB,KAAK;AAAA;AAAA,4BAEJ,KAAK;AAAA,sCACK,KAAK;AAAA;AAAA;AAAA;AAAA,GAIxC,QAAQ,EAAE;AAAA,EAEZ,OAAO,KAAK,IAAI,YAAY;AAAA;",
13
+ "debugId": "365CA6CEA262361B64756E2164756E21",
13
14
  "names": []
14
15
  }
@@ -847,7 +847,9 @@ declare function waitForSubgraphOperationsClear(db: Kysely<Database>, subgraphId
847
847
  declare function claimSubgraphOperation(db: Kysely<Database>, lockedBy: string): Promise<SubgraphOperation | null>;
848
848
  declare function heartbeatSubgraphOperation(db: Kysely<Database>, operationId: string, lockedBy: string): Promise<void>;
849
849
  declare function getSubgraphOperation(db: Kysely<Database>, operationId: string): Promise<SubgraphOperation | null>;
850
+ /** Recent operations for a subgraph, newest first (for the status read API). */
851
+ declare function listSubgraphOperations(db: Kysely<Database>, subgraphId: string, limit?: number): Promise<SubgraphOperation[]>;
850
852
  declare function completeSubgraphOperation(db: Kysely<Database>, operationId: string, lockedBy: string, processedBlocks: number): Promise<void>;
851
853
  declare function cancelSubgraphOperation(db: Kysely<Database>, operationId: string, lockedBy: string, processedBlocks: number): Promise<void>;
852
854
  declare function failSubgraphOperation(db: Kysely<Database>, operationId: string, lockedBy: string, error: string, processedBlocks?: number): Promise<void>;
853
- export { waitForSubgraphOperationsClear, requestSubgraphOperationsCancelForDelete, requestSubgraphOperationCancel, isActiveSubgraphOperationConflict, heartbeatSubgraphOperation, getSubgraphOperation, findActiveSubgraphOperation, failSubgraphOperation, createSubgraphOperation, completeSubgraphOperation, claimSubgraphOperation, cancelSubgraphOperation };
855
+ export { waitForSubgraphOperationsClear, requestSubgraphOperationsCancelForDelete, requestSubgraphOperationCancel, listSubgraphOperations, isActiveSubgraphOperationConflict, heartbeatSubgraphOperation, getSubgraphOperation, findActiveSubgraphOperation, failSubgraphOperation, createSubgraphOperation, completeSubgraphOperation, claimSubgraphOperation, cancelSubgraphOperation };
@@ -98,6 +98,9 @@ async function heartbeatSubgraphOperation(db, operationId, lockedBy) {
98
98
  async function getSubgraphOperation(db, operationId) {
99
99
  return await db.selectFrom("subgraph_operations").selectAll().where("id", "=", operationId).executeTakeFirst() ?? null;
100
100
  }
101
+ async function listSubgraphOperations(db, subgraphId, limit = 20) {
102
+ return db.selectFrom("subgraph_operations").selectAll().where("subgraph_id", "=", subgraphId).orderBy("created_at", "desc").limit(limit).execute();
103
+ }
101
104
  async function completeSubgraphOperation(db, operationId, lockedBy, processedBlocks) {
102
105
  await db.updateTable("subgraph_operations").set({
103
106
  status: "completed",
@@ -133,6 +136,7 @@ export {
133
136
  waitForSubgraphOperationsClear,
134
137
  requestSubgraphOperationsCancelForDelete,
135
138
  requestSubgraphOperationCancel,
139
+ listSubgraphOperations,
136
140
  isActiveSubgraphOperationConflict,
137
141
  heartbeatSubgraphOperation,
138
142
  getSubgraphOperation,
@@ -144,5 +148,5 @@ export {
144
148
  cancelSubgraphOperation
145
149
  };
146
150
 
147
- //# debugId=E70EFC240746415E64756E2164756E21
151
+ //# debugId=7F3BE21002C861D864756E2164756E21
148
152
  //# sourceMappingURL=subgraph-operations.js.map
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../src/db/queries/subgraph-operations.ts"],
4
4
  "sourcesContent": [
5
- "import { type Kysely, sql } from \"kysely\";\nimport type {\n\tDatabase,\n\tSubgraphOperation,\n\tSubgraphOperationKind,\n\tSubgraphOperationStatus,\n} from \"../types.ts\";\n\nconst ACTIVE_STATUSES: SubgraphOperationStatus[] = [\"queued\", \"running\"];\n\nexport function isActiveSubgraphOperationConflict(err: unknown): boolean {\n\tif (!(err instanceof Error)) return false;\n\tconst candidate = err as Error & {\n\t\tcode?: string;\n\t\tconstraint?: string;\n\t\tconstraint_name?: string;\n\t};\n\treturn (\n\t\tcandidate.code === \"23505\" &&\n\t\t(candidate.constraint === \"subgraph_operations_active_unique\" ||\n\t\t\tcandidate.constraint_name === \"subgraph_operations_active_unique\")\n\t);\n}\n\nexport async function createSubgraphOperation(\n\tdb: Kysely<Database>,\n\tdata: {\n\t\tsubgraphId: string;\n\t\tsubgraphName: string;\n\t\taccountId?: string | null;\n\t\tkind: SubgraphOperationKind;\n\t\tfromBlock?: number;\n\t\ttoBlock?: number;\n\t},\n): Promise<SubgraphOperation> {\n\treturn await db\n\t\t.insertInto(\"subgraph_operations\")\n\t\t.values({\n\t\t\tsubgraph_id: data.subgraphId,\n\t\t\tsubgraph_name: data.subgraphName,\n\t\t\taccount_id: data.accountId ?? null,\n\t\t\tkind: data.kind,\n\t\t\tfrom_block: data.fromBlock ?? null,\n\t\t\tto_block: data.toBlock ?? null,\n\t\t})\n\t\t.returningAll()\n\t\t.executeTakeFirstOrThrow();\n}\n\nexport async function findActiveSubgraphOperation(\n\tdb: Kysely<Database>,\n\tsubgraphId: string,\n): Promise<SubgraphOperation | null> {\n\treturn (\n\t\t(await db\n\t\t\t.selectFrom(\"subgraph_operations\")\n\t\t\t.selectAll()\n\t\t\t.where(\"subgraph_id\", \"=\", subgraphId)\n\t\t\t.where(\"status\", \"in\", ACTIVE_STATUSES)\n\t\t\t.orderBy(\"created_at\", \"asc\")\n\t\t\t.executeTakeFirst()) ?? null\n\t);\n}\n\nexport async function requestSubgraphOperationCancel(\n\tdb: Kysely<Database>,\n\tsubgraphId: string,\n): Promise<SubgraphOperation | null> {\n\treturn (\n\t\t(await db\n\t\t\t.updateTable(\"subgraph_operations\")\n\t\t\t.set({ cancel_requested: true, updated_at: new Date() })\n\t\t\t.where(\"subgraph_id\", \"=\", subgraphId)\n\t\t\t.where(\"status\", \"in\", ACTIVE_STATUSES)\n\t\t\t.returningAll()\n\t\t\t.executeTakeFirst()) ?? null\n\t);\n}\n\nexport async function requestSubgraphOperationsCancelForDelete(\n\tdb: Kysely<Database>,\n\tsubgraphId: string,\n): Promise<SubgraphOperation[]> {\n\treturn await db\n\t\t.updateTable(\"subgraph_operations\")\n\t\t.set({ cancel_requested: true, updated_at: new Date() })\n\t\t.where(\"subgraph_id\", \"=\", subgraphId)\n\t\t.where(\"status\", \"in\", ACTIVE_STATUSES)\n\t\t.returningAll()\n\t\t.execute();\n}\n\n/**\n * Poll until no active subgraph operations remain for the subgraph or until\n * `timeoutMs` elapses. Returns true if all active operations cleared, false\n * if we timed out. Callers should use this before `DROP SCHEMA` so the active\n * processor has a chance to observe `cancel_requested` and release its row /\n * advisory locks. Without this, the DROP blocks behind the live transaction\n * and the API socket times out before the lock releases.\n */\nexport async function waitForSubgraphOperationsClear(\n\tdb: Kysely<Database>,\n\tsubgraphId: string,\n\topts?: { timeoutMs?: number; pollMs?: number },\n): Promise<boolean> {\n\tconst timeoutMs = opts?.timeoutMs ?? 30_000;\n\tconst pollMs = opts?.pollMs ?? 500;\n\tconst deadline = Date.now() + timeoutMs;\n\twhile (Date.now() < deadline) {\n\t\tconst active = await db\n\t\t\t.selectFrom(\"subgraph_operations\")\n\t\t\t.select(\"id\")\n\t\t\t.where(\"subgraph_id\", \"=\", subgraphId)\n\t\t\t.where(\"status\", \"in\", ACTIVE_STATUSES)\n\t\t\t.limit(1)\n\t\t\t.executeTakeFirst();\n\t\tif (!active) return true;\n\t\tawait new Promise((r) => setTimeout(r, pollMs));\n\t}\n\treturn false;\n}\n\nexport async function claimSubgraphOperation(\n\tdb: Kysely<Database>,\n\tlockedBy: string,\n): Promise<SubgraphOperation | null> {\n\t// Fair queue: accounts with fewer currently-running operations are served first,\n\t// breaking ties by creation time. Prevents one user's long reindex from\n\t// starving other accounts when SUBGRAPH_OPERATION_CONCURRENCY > 1.\n\tconst result = await sql<SubgraphOperation>`\n\t\tUPDATE subgraph_operations\n\t\tSET\n\t\t\tstatus = 'running',\n\t\t\tlocked_by = ${lockedBy},\n\t\t\tlocked_until = now() + interval '60 seconds',\n\t\t\tstarted_at = COALESCE(started_at, now()),\n\t\t\tupdated_at = now()\n\t\tWHERE id = (\n\t\t\tSELECT so.id\n\t\t\tFROM subgraph_operations so\n\t\t\tLEFT JOIN (\n\t\t\t\tSELECT account_id, COUNT(*) AS cnt\n\t\t\t\tFROM subgraph_operations\n\t\t\t\tWHERE status = 'running'\n\t\t\t\tGROUP BY account_id\n\t\t\t) rc ON so.account_id = rc.account_id\n\t\t\tWHERE\n\t\t\t\tso.status = 'queued'\n\t\t\t\tOR (\n\t\t\t\t\tso.status = 'running'\n\t\t\t\t\tAND (so.locked_until IS NULL OR so.locked_until < now())\n\t\t\t\t)\n\t\t\tORDER BY\n\t\t\t\tCOALESCE(rc.cnt, 0) ASC,\n\t\t\t\tCASE WHEN so.status = 'queued' THEN 0 ELSE 1 END,\n\t\t\t\tso.created_at ASC\n\t\t\tFOR UPDATE OF so SKIP LOCKED\n\t\t\tLIMIT 1\n\t\t)\n\t\tRETURNING *\n\t`.execute(db);\n\treturn result.rows[0] ?? null;\n}\n\nexport async function heartbeatSubgraphOperation(\n\tdb: Kysely<Database>,\n\toperationId: string,\n\tlockedBy: string,\n): Promise<void> {\n\tawait db\n\t\t.updateTable(\"subgraph_operations\")\n\t\t.set({\n\t\t\tlocked_until: sql<Date>`now() + interval '60 seconds'`,\n\t\t\tupdated_at: new Date(),\n\t\t})\n\t\t.where(\"id\", \"=\", operationId)\n\t\t.where(\"status\", \"=\", \"running\")\n\t\t.where(\"locked_by\", \"=\", lockedBy)\n\t\t.execute();\n}\n\nexport async function getSubgraphOperation(\n\tdb: Kysely<Database>,\n\toperationId: string,\n): Promise<SubgraphOperation | null> {\n\treturn (\n\t\t(await db\n\t\t\t.selectFrom(\"subgraph_operations\")\n\t\t\t.selectAll()\n\t\t\t.where(\"id\", \"=\", operationId)\n\t\t\t.executeTakeFirst()) ?? null\n\t);\n}\n\nexport async function completeSubgraphOperation(\n\tdb: Kysely<Database>,\n\toperationId: string,\n\tlockedBy: string,\n\tprocessedBlocks: number,\n): Promise<void> {\n\tawait db\n\t\t.updateTable(\"subgraph_operations\")\n\t\t.set({\n\t\t\tstatus: \"completed\",\n\t\t\tfinished_at: new Date(),\n\t\t\tprocessed_blocks: processedBlocks,\n\t\t\tlocked_by: null,\n\t\t\tlocked_until: null,\n\t\t\tupdated_at: new Date(),\n\t\t})\n\t\t.where(\"id\", \"=\", operationId)\n\t\t.where(\"locked_by\", \"=\", lockedBy)\n\t\t.execute();\n}\n\nexport async function cancelSubgraphOperation(\n\tdb: Kysely<Database>,\n\toperationId: string,\n\tlockedBy: string,\n\tprocessedBlocks: number,\n): Promise<void> {\n\tawait db\n\t\t.updateTable(\"subgraph_operations\")\n\t\t.set({\n\t\t\tstatus: \"cancelled\",\n\t\t\tfinished_at: new Date(),\n\t\t\tprocessed_blocks: processedBlocks,\n\t\t\tlocked_by: null,\n\t\t\tlocked_until: null,\n\t\t\tupdated_at: new Date(),\n\t\t})\n\t\t.where(\"id\", \"=\", operationId)\n\t\t.where(\"locked_by\", \"=\", lockedBy)\n\t\t.execute();\n}\n\nexport async function failSubgraphOperation(\n\tdb: Kysely<Database>,\n\toperationId: string,\n\tlockedBy: string,\n\terror: string,\n\tprocessedBlocks?: number,\n): Promise<void> {\n\tawait db\n\t\t.updateTable(\"subgraph_operations\")\n\t\t.set({\n\t\t\tstatus: \"failed\",\n\t\t\tfinished_at: new Date(),\n\t\t\tprocessed_blocks: processedBlocks ?? null,\n\t\t\terror,\n\t\t\tlocked_by: null,\n\t\t\tlocked_until: null,\n\t\t\tupdated_at: new Date(),\n\t\t})\n\t\t.where(\"id\", \"=\", operationId)\n\t\t.where(\"locked_by\", \"=\", lockedBy)\n\t\t.execute();\n}\n"
5
+ "import { type Kysely, sql } from \"kysely\";\nimport type {\n\tDatabase,\n\tSubgraphOperation,\n\tSubgraphOperationKind,\n\tSubgraphOperationStatus,\n} from \"../types.ts\";\n\nconst ACTIVE_STATUSES: SubgraphOperationStatus[] = [\"queued\", \"running\"];\n\nexport function isActiveSubgraphOperationConflict(err: unknown): boolean {\n\tif (!(err instanceof Error)) return false;\n\tconst candidate = err as Error & {\n\t\tcode?: string;\n\t\tconstraint?: string;\n\t\tconstraint_name?: string;\n\t};\n\treturn (\n\t\tcandidate.code === \"23505\" &&\n\t\t(candidate.constraint === \"subgraph_operations_active_unique\" ||\n\t\t\tcandidate.constraint_name === \"subgraph_operations_active_unique\")\n\t);\n}\n\nexport async function createSubgraphOperation(\n\tdb: Kysely<Database>,\n\tdata: {\n\t\tsubgraphId: string;\n\t\tsubgraphName: string;\n\t\taccountId?: string | null;\n\t\tkind: SubgraphOperationKind;\n\t\tfromBlock?: number;\n\t\ttoBlock?: number;\n\t},\n): Promise<SubgraphOperation> {\n\treturn await db\n\t\t.insertInto(\"subgraph_operations\")\n\t\t.values({\n\t\t\tsubgraph_id: data.subgraphId,\n\t\t\tsubgraph_name: data.subgraphName,\n\t\t\taccount_id: data.accountId ?? null,\n\t\t\tkind: data.kind,\n\t\t\tfrom_block: data.fromBlock ?? null,\n\t\t\tto_block: data.toBlock ?? null,\n\t\t})\n\t\t.returningAll()\n\t\t.executeTakeFirstOrThrow();\n}\n\nexport async function findActiveSubgraphOperation(\n\tdb: Kysely<Database>,\n\tsubgraphId: string,\n): Promise<SubgraphOperation | null> {\n\treturn (\n\t\t(await db\n\t\t\t.selectFrom(\"subgraph_operations\")\n\t\t\t.selectAll()\n\t\t\t.where(\"subgraph_id\", \"=\", subgraphId)\n\t\t\t.where(\"status\", \"in\", ACTIVE_STATUSES)\n\t\t\t.orderBy(\"created_at\", \"asc\")\n\t\t\t.executeTakeFirst()) ?? null\n\t);\n}\n\nexport async function requestSubgraphOperationCancel(\n\tdb: Kysely<Database>,\n\tsubgraphId: string,\n): Promise<SubgraphOperation | null> {\n\treturn (\n\t\t(await db\n\t\t\t.updateTable(\"subgraph_operations\")\n\t\t\t.set({ cancel_requested: true, updated_at: new Date() })\n\t\t\t.where(\"subgraph_id\", \"=\", subgraphId)\n\t\t\t.where(\"status\", \"in\", ACTIVE_STATUSES)\n\t\t\t.returningAll()\n\t\t\t.executeTakeFirst()) ?? null\n\t);\n}\n\nexport async function requestSubgraphOperationsCancelForDelete(\n\tdb: Kysely<Database>,\n\tsubgraphId: string,\n): Promise<SubgraphOperation[]> {\n\treturn await db\n\t\t.updateTable(\"subgraph_operations\")\n\t\t.set({ cancel_requested: true, updated_at: new Date() })\n\t\t.where(\"subgraph_id\", \"=\", subgraphId)\n\t\t.where(\"status\", \"in\", ACTIVE_STATUSES)\n\t\t.returningAll()\n\t\t.execute();\n}\n\n/**\n * Poll until no active subgraph operations remain for the subgraph or until\n * `timeoutMs` elapses. Returns true if all active operations cleared, false\n * if we timed out. Callers should use this before `DROP SCHEMA` so the active\n * processor has a chance to observe `cancel_requested` and release its row /\n * advisory locks. Without this, the DROP blocks behind the live transaction\n * and the API socket times out before the lock releases.\n */\nexport async function waitForSubgraphOperationsClear(\n\tdb: Kysely<Database>,\n\tsubgraphId: string,\n\topts?: { timeoutMs?: number; pollMs?: number },\n): Promise<boolean> {\n\tconst timeoutMs = opts?.timeoutMs ?? 30_000;\n\tconst pollMs = opts?.pollMs ?? 500;\n\tconst deadline = Date.now() + timeoutMs;\n\twhile (Date.now() < deadline) {\n\t\tconst active = await db\n\t\t\t.selectFrom(\"subgraph_operations\")\n\t\t\t.select(\"id\")\n\t\t\t.where(\"subgraph_id\", \"=\", subgraphId)\n\t\t\t.where(\"status\", \"in\", ACTIVE_STATUSES)\n\t\t\t.limit(1)\n\t\t\t.executeTakeFirst();\n\t\tif (!active) return true;\n\t\tawait new Promise((r) => setTimeout(r, pollMs));\n\t}\n\treturn false;\n}\n\nexport async function claimSubgraphOperation(\n\tdb: Kysely<Database>,\n\tlockedBy: string,\n): Promise<SubgraphOperation | null> {\n\t// Fair queue: accounts with fewer currently-running operations are served first,\n\t// breaking ties by creation time. Prevents one user's long reindex from\n\t// starving other accounts when SUBGRAPH_OPERATION_CONCURRENCY > 1.\n\tconst result = await sql<SubgraphOperation>`\n\t\tUPDATE subgraph_operations\n\t\tSET\n\t\t\tstatus = 'running',\n\t\t\tlocked_by = ${lockedBy},\n\t\t\tlocked_until = now() + interval '60 seconds',\n\t\t\tstarted_at = COALESCE(started_at, now()),\n\t\t\tupdated_at = now()\n\t\tWHERE id = (\n\t\t\tSELECT so.id\n\t\t\tFROM subgraph_operations so\n\t\t\tLEFT JOIN (\n\t\t\t\tSELECT account_id, COUNT(*) AS cnt\n\t\t\t\tFROM subgraph_operations\n\t\t\t\tWHERE status = 'running'\n\t\t\t\tGROUP BY account_id\n\t\t\t) rc ON so.account_id = rc.account_id\n\t\t\tWHERE\n\t\t\t\tso.status = 'queued'\n\t\t\t\tOR (\n\t\t\t\t\tso.status = 'running'\n\t\t\t\t\tAND (so.locked_until IS NULL OR so.locked_until < now())\n\t\t\t\t)\n\t\t\tORDER BY\n\t\t\t\tCOALESCE(rc.cnt, 0) ASC,\n\t\t\t\tCASE WHEN so.status = 'queued' THEN 0 ELSE 1 END,\n\t\t\t\tso.created_at ASC\n\t\t\tFOR UPDATE OF so SKIP LOCKED\n\t\t\tLIMIT 1\n\t\t)\n\t\tRETURNING *\n\t`.execute(db);\n\treturn result.rows[0] ?? null;\n}\n\nexport async function heartbeatSubgraphOperation(\n\tdb: Kysely<Database>,\n\toperationId: string,\n\tlockedBy: string,\n): Promise<void> {\n\tawait db\n\t\t.updateTable(\"subgraph_operations\")\n\t\t.set({\n\t\t\tlocked_until: sql<Date>`now() + interval '60 seconds'`,\n\t\t\tupdated_at: new Date(),\n\t\t})\n\t\t.where(\"id\", \"=\", operationId)\n\t\t.where(\"status\", \"=\", \"running\")\n\t\t.where(\"locked_by\", \"=\", lockedBy)\n\t\t.execute();\n}\n\nexport async function getSubgraphOperation(\n\tdb: Kysely<Database>,\n\toperationId: string,\n): Promise<SubgraphOperation | null> {\n\treturn (\n\t\t(await db\n\t\t\t.selectFrom(\"subgraph_operations\")\n\t\t\t.selectAll()\n\t\t\t.where(\"id\", \"=\", operationId)\n\t\t\t.executeTakeFirst()) ?? null\n\t);\n}\n\n/** Recent operations for a subgraph, newest first (for the status read API). */\nexport async function listSubgraphOperations(\n\tdb: Kysely<Database>,\n\tsubgraphId: string,\n\tlimit = 20,\n): Promise<SubgraphOperation[]> {\n\treturn db\n\t\t.selectFrom(\"subgraph_operations\")\n\t\t.selectAll()\n\t\t.where(\"subgraph_id\", \"=\", subgraphId)\n\t\t.orderBy(\"created_at\", \"desc\")\n\t\t.limit(limit)\n\t\t.execute();\n}\n\nexport async function completeSubgraphOperation(\n\tdb: Kysely<Database>,\n\toperationId: string,\n\tlockedBy: string,\n\tprocessedBlocks: number,\n): Promise<void> {\n\tawait db\n\t\t.updateTable(\"subgraph_operations\")\n\t\t.set({\n\t\t\tstatus: \"completed\",\n\t\t\tfinished_at: new Date(),\n\t\t\tprocessed_blocks: processedBlocks,\n\t\t\tlocked_by: null,\n\t\t\tlocked_until: null,\n\t\t\tupdated_at: new Date(),\n\t\t})\n\t\t.where(\"id\", \"=\", operationId)\n\t\t.where(\"locked_by\", \"=\", lockedBy)\n\t\t.execute();\n}\n\nexport async function cancelSubgraphOperation(\n\tdb: Kysely<Database>,\n\toperationId: string,\n\tlockedBy: string,\n\tprocessedBlocks: number,\n): Promise<void> {\n\tawait db\n\t\t.updateTable(\"subgraph_operations\")\n\t\t.set({\n\t\t\tstatus: \"cancelled\",\n\t\t\tfinished_at: new Date(),\n\t\t\tprocessed_blocks: processedBlocks,\n\t\t\tlocked_by: null,\n\t\t\tlocked_until: null,\n\t\t\tupdated_at: new Date(),\n\t\t})\n\t\t.where(\"id\", \"=\", operationId)\n\t\t.where(\"locked_by\", \"=\", lockedBy)\n\t\t.execute();\n}\n\nexport async function failSubgraphOperation(\n\tdb: Kysely<Database>,\n\toperationId: string,\n\tlockedBy: string,\n\terror: string,\n\tprocessedBlocks?: number,\n): Promise<void> {\n\tawait db\n\t\t.updateTable(\"subgraph_operations\")\n\t\t.set({\n\t\t\tstatus: \"failed\",\n\t\t\tfinished_at: new Date(),\n\t\t\tprocessed_blocks: processedBlocks ?? null,\n\t\t\terror,\n\t\t\tlocked_by: null,\n\t\t\tlocked_until: null,\n\t\t\tupdated_at: new Date(),\n\t\t})\n\t\t.where(\"id\", \"=\", operationId)\n\t\t.where(\"locked_by\", \"=\", lockedBy)\n\t\t.execute();\n}\n"
6
6
  ],
7
- "mappings": ";;;;;;;;;;;;;;;;;AAAA;AAQA,IAAM,kBAA6C,CAAC,UAAU,SAAS;AAEhE,SAAS,iCAAiC,CAAC,KAAuB;AAAA,EACxE,IAAI,EAAE,eAAe;AAAA,IAAQ,OAAO;AAAA,EACpC,MAAM,YAAY;AAAA,EAKlB,OACC,UAAU,SAAS,YAClB,UAAU,eAA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