@secondlayer/subgraphs 3.7.4 → 3.8.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.
@@ -1217,6 +1217,37 @@ class PublicApiBlockSource {
1217
1217
  return map;
1218
1218
  }
1219
1219
  }
1220
+
1221
+ class FallbackBlockSource {
1222
+ primary;
1223
+ fallback;
1224
+ constructor(primary, fallback) {
1225
+ this.primary = primary;
1226
+ this.fallback = fallback;
1227
+ }
1228
+ async getTip() {
1229
+ try {
1230
+ return await this.primary.getTip();
1231
+ } catch (err) {
1232
+ logger3.warn("block source primary getTip failed — using DB tap", {
1233
+ error: err instanceof Error ? err.message : String(err)
1234
+ });
1235
+ return this.fallback.getTip();
1236
+ }
1237
+ }
1238
+ async loadBlockRange(fromHeight, toHeight) {
1239
+ try {
1240
+ return await this.primary.loadBlockRange(fromHeight, toHeight);
1241
+ } catch (err) {
1242
+ logger3.warn("block source primary loadBlockRange failed — using DB tap", {
1243
+ from: fromHeight,
1244
+ to: toHeight,
1245
+ error: err instanceof Error ? err.message : String(err)
1246
+ });
1247
+ return this.fallback.loadBlockRange(fromHeight, toHeight);
1248
+ }
1249
+ }
1250
+ }
1220
1251
  var postgresBlockSource = new PostgresBlockSource;
1221
1252
  function buildHttpClient() {
1222
1253
  const baseUrl = process.env.SUBGRAPH_INDEX_API_URL ?? process.env.STREAMS_API_URL ?? "http://api:3800";
@@ -1228,7 +1259,7 @@ function buildHttpClient() {
1228
1259
  }
1229
1260
  function resolveBlockSource(subgraph) {
1230
1261
  if (process.env.SUBGRAPH_SOURCE === "streams-index" && subgraph && isStreamsIndexEligible(subgraph)) {
1231
- return new PublicApiBlockSource(buildHttpClient(), referencedIndexEventTypes(subgraph));
1262
+ return new FallbackBlockSource(new PublicApiBlockSource(buildHttpClient(), referencedIndexEventTypes(subgraph)), postgresBlockSource);
1232
1263
  }
1233
1264
  if (process.env.SUBGRAPH_SOURCE === "streams-index" && subgraph) {
1234
1265
  logger3.debug("Subgraph not streams-index eligible, using DB tap", {
@@ -1463,6 +1494,13 @@ class SubscriptionMatcher {
1463
1494
 
1464
1495
  // src/runtime/subscription-state.ts
1465
1496
  var matcher = new SubscriptionMatcher;
1497
+ async function refreshMatcher(db) {
1498
+ const rows = await sql2`
1499
+ SELECT * FROM subscriptions WHERE status = 'active'
1500
+ `.execute(db);
1501
+ matcher.setAll(rows.rows);
1502
+ return matcher.size();
1503
+ }
1466
1504
 
1467
1505
  // src/runtime/block-processor.ts
1468
1506
  var routeCache = new Map;
@@ -1730,67 +1768,78 @@ function escapeLiteralDefault(value) {
1730
1768
  return value ? "TRUE" : "FALSE";
1731
1769
  return `'${String(value).replace(/'/g, "''")}'`;
1732
1770
  }
1733
- function generateSubgraphSQL(def, schemaNameOverride) {
1734
- const schemaName = schemaNameOverride ?? pgSchemaName(def.name);
1771
+ function tableNeedsTrgm(tableDef) {
1772
+ return Object.values(tableDef.columns).some((col) => col.search);
1773
+ }
1774
+ function emitTableDDL(schemaName, tableName, tableDef) {
1775
+ const qualifiedName = `${schemaName}.${tableName}`;
1735
1776
  const statements = [];
1736
- const needsTrgm = Object.values(def.schema).some((table) => Object.values(table.columns).some((col) => col.search));
1737
- if (needsTrgm) {
1738
- statements.push("CREATE EXTENSION IF NOT EXISTS pg_trgm");
1739
- }
1740
- statements.push(`CREATE SCHEMA IF NOT EXISTS ${schemaName}`);
1741
- for (const [tableName, tableDef] of Object.entries(def.schema)) {
1742
- const qualifiedName = `${schemaName}.${tableName}`;
1743
- const columnDefs = [
1744
- "_id BIGSERIAL PRIMARY KEY",
1745
- "_block_height BIGINT NOT NULL",
1746
- "_tx_id TEXT NOT NULL",
1747
- "_created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()"
1748
- ];
1749
- for (const [colName, col] of Object.entries(tableDef.columns)) {
1750
- const sqlType = TYPE_MAP[col.type];
1751
- const nullable = col.nullable ? "" : " NOT NULL";
1752
- let colDef = `${colName} ${sqlType}${nullable}`;
1753
- if (col.default !== undefined) {
1754
- colDef += ` DEFAULT ${escapeLiteralDefault(col.default)}`;
1755
- }
1756
- columnDefs.push(colDef);
1777
+ const columnDefs = [
1778
+ "_id BIGSERIAL PRIMARY KEY",
1779
+ "_block_height BIGINT NOT NULL",
1780
+ "_tx_id TEXT NOT NULL",
1781
+ "_created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()"
1782
+ ];
1783
+ for (const [colName, col] of Object.entries(tableDef.columns)) {
1784
+ const sqlType = TYPE_MAP[col.type];
1785
+ const nullable = col.nullable ? "" : " NOT NULL";
1786
+ let colDef = `${colName} ${sqlType}${nullable}`;
1787
+ if (col.default !== undefined) {
1788
+ colDef += ` DEFAULT ${escapeLiteralDefault(col.default)}`;
1757
1789
  }
1758
- statements.push(`CREATE TABLE IF NOT EXISTS ${qualifiedName} (
1790
+ columnDefs.push(colDef);
1791
+ }
1792
+ statements.push(`CREATE TABLE IF NOT EXISTS ${qualifiedName} (
1759
1793
  ${columnDefs.join(`,
1760
1794
  `)}
1761
1795
  )`);
1762
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_block_height ON ${qualifiedName} (_block_height)`);
1763
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_tx_id ON ${qualifiedName} (_tx_id)`);
1764
- for (const [colName, col] of Object.entries(tableDef.columns)) {
1765
- if (col.indexed) {
1766
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName} ON ${qualifiedName} (${colName})`);
1767
- }
1796
+ statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_block_height ON ${qualifiedName} (_block_height)`);
1797
+ statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_tx_id ON ${qualifiedName} (_tx_id)`);
1798
+ for (const [colName, col] of Object.entries(tableDef.columns)) {
1799
+ if (col.indexed) {
1800
+ statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName} ON ${qualifiedName} (${colName})`);
1768
1801
  }
1769
- for (const [colName, col] of Object.entries(tableDef.columns)) {
1770
- if (col.search) {
1771
- statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName}_trgm ON ${qualifiedName} USING gin (${colName} gin_trgm_ops)`);
1772
- }
1802
+ }
1803
+ for (const [colName, col] of Object.entries(tableDef.columns)) {
1804
+ if (col.search) {
1805
+ statements.push(`CREATE INDEX IF NOT EXISTS idx_${schemaName}_${tableName}_${colName}_trgm ON ${qualifiedName} USING gin (${colName} gin_trgm_ops)`);
1773
1806
  }
1774
- if (tableDef.indexes) {
1775
- for (let i = 0;i < tableDef.indexes.length; i++) {
1776
- const cols = tableDef.indexes[i];
1777
- const idxName = `idx_${schemaName}_${tableName}_composite_${i}`;
1778
- statements.push(`CREATE INDEX IF NOT EXISTS ${idxName} ON ${qualifiedName} (${cols.join(", ")})`);
1779
- }
1807
+ }
1808
+ if (tableDef.indexes) {
1809
+ for (let i = 0;i < tableDef.indexes.length; i++) {
1810
+ const cols = tableDef.indexes[i];
1811
+ const idxName = `idx_${schemaName}_${tableName}_composite_${i}`;
1812
+ statements.push(`CREATE INDEX IF NOT EXISTS ${idxName} ON ${qualifiedName} (${cols.join(", ")})`);
1780
1813
  }
1781
- if (tableDef.uniqueKeys) {
1782
- for (let i = 0;i < tableDef.uniqueKeys.length; i++) {
1783
- const cols = tableDef.uniqueKeys[i];
1784
- const constraintName = `uq_${schemaName}_${tableName}_${cols.join("_")}`;
1785
- statements.push(`ALTER TABLE ${qualifiedName} ADD CONSTRAINT ${constraintName} UNIQUE (${cols.join(", ")})`);
1786
- }
1814
+ }
1815
+ if (tableDef.uniqueKeys) {
1816
+ for (let i = 0;i < tableDef.uniqueKeys.length; i++) {
1817
+ const cols = tableDef.uniqueKeys[i];
1818
+ const constraintName = `uq_${schemaName}_${tableName}_${cols.join("_")}`;
1819
+ statements.push(`ALTER TABLE ${qualifiedName} ADD CONSTRAINT ${constraintName} UNIQUE (${cols.join(", ")})`);
1787
1820
  }
1788
1821
  }
1822
+ return statements;
1823
+ }
1824
+ function emitForeignKeyDDL(schemaName, tableName, tableDef) {
1825
+ return (tableDef.relations ?? []).map((rel) => {
1826
+ const constraintName = `fk_${schemaName}_${tableName}_${rel.name}`;
1827
+ return `ALTER TABLE ${schemaName}.${tableName} ADD CONSTRAINT ${constraintName} ` + `FOREIGN KEY (${rel.fields.join(", ")}) ` + `REFERENCES ${schemaName}.${rel.references} (${rel.referencedColumns.join(", ")})`;
1828
+ });
1829
+ }
1830
+ function generateSubgraphSQL(def, schemaNameOverride) {
1831
+ const schemaName = schemaNameOverride ?? pgSchemaName(def.name);
1832
+ const statements = [];
1833
+ const needsTrgm = Object.values(def.schema).some((table) => Object.values(table.columns).some((col) => col.search));
1834
+ if (needsTrgm) {
1835
+ statements.push("CREATE EXTENSION IF NOT EXISTS pg_trgm");
1836
+ }
1837
+ statements.push(`CREATE SCHEMA IF NOT EXISTS ${schemaName}`);
1789
1838
  for (const [tableName, tableDef] of Object.entries(def.schema)) {
1790
- for (const rel of tableDef.relations ?? []) {
1791
- const constraintName = `fk_${schemaName}_${tableName}_${rel.name}`;
1792
- statements.push(`ALTER TABLE ${schemaName}.${tableName} ADD CONSTRAINT ${constraintName} ` + `FOREIGN KEY (${rel.fields.join(", ")}) ` + `REFERENCES ${schemaName}.${rel.references} (${rel.referencedColumns.join(", ")})`);
1793
- }
1839
+ statements.push(...emitTableDDL(schemaName, tableName, tableDef));
1840
+ }
1841
+ for (const [tableName, tableDef] of Object.entries(def.schema)) {
1842
+ statements.push(...emitForeignKeyDDL(schemaName, tableName, tableDef));
1794
1843
  }
1795
1844
  const hashInput = JSON.stringify({
1796
1845
  name: def.name,
@@ -2212,5 +2261,5 @@ export {
2212
2261
  backfillSubgraph
2213
2262
  };
2214
2263
 
2215
- //# debugId=A2E914F461AC000564756E2164756E21
2264
+ //# debugId=DCE205425496103A64756E2164756E21
2216
2265
  //# sourceMappingURL=reindex.js.map