@secondlayer/subgraphs 3.7.3 → 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.
- package/dist/src/index.d.ts +30 -1
- package/dist/src/index.js +407 -83
- package/dist/src/index.js.map +9 -7
- package/dist/src/runtime/block-processor.js +33 -2
- package/dist/src/runtime/block-processor.js.map +3 -3
- package/dist/src/runtime/catchup.js +33 -2
- package/dist/src/runtime/catchup.js.map +3 -3
- package/dist/src/runtime/emitter.d.ts +18 -0
- package/dist/src/runtime/emitter.js +773 -0
- package/dist/src/runtime/emitter.js.map +19 -0
- package/dist/src/runtime/processor.js +129 -943
- package/dist/src/runtime/processor.js.map +7 -20
- package/dist/src/runtime/reindex.js +94 -52
- package/dist/src/runtime/reindex.js.map +4 -4
- package/dist/src/runtime/reorg.js +33 -2
- package/dist/src/runtime/reorg.js.map +3 -3
- package/dist/src/runtime/replay.js +33 -2
- package/dist/src/runtime/replay.js.map +4 -4
- package/dist/src/schema/index.d.ts +2 -1
- package/dist/src/schema/index.js +75 -82
- package/dist/src/schema/index.js.map +5 -5
- package/dist/src/service.js +134 -948
- package/dist/src/service.js.map +6 -19
- package/dist/src/validate.d.ts +2 -1
- package/dist/src/validate.js +2 -1
- package/dist/src/validate.js.map +3 -3
- package/package.json +6 -2
|
@@ -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", {
|
|
@@ -1737,67 +1768,78 @@ function escapeLiteralDefault(value) {
|
|
|
1737
1768
|
return value ? "TRUE" : "FALSE";
|
|
1738
1769
|
return `'${String(value).replace(/'/g, "''")}'`;
|
|
1739
1770
|
}
|
|
1740
|
-
function
|
|
1741
|
-
|
|
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}`;
|
|
1742
1776
|
const statements = [];
|
|
1743
|
-
const
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
const
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
];
|
|
1756
|
-
for (const [colName, col] of Object.entries(tableDef.columns)) {
|
|
1757
|
-
const sqlType = TYPE_MAP[col.type];
|
|
1758
|
-
const nullable = col.nullable ? "" : " NOT NULL";
|
|
1759
|
-
let colDef = `${colName} ${sqlType}${nullable}`;
|
|
1760
|
-
if (col.default !== undefined) {
|
|
1761
|
-
colDef += ` DEFAULT ${escapeLiteralDefault(col.default)}`;
|
|
1762
|
-
}
|
|
1763
|
-
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)}`;
|
|
1764
1789
|
}
|
|
1765
|
-
|
|
1790
|
+
columnDefs.push(colDef);
|
|
1791
|
+
}
|
|
1792
|
+
statements.push(`CREATE TABLE IF NOT EXISTS ${qualifiedName} (
|
|
1766
1793
|
${columnDefs.join(`,
|
|
1767
1794
|
`)}
|
|
1768
1795
|
)`);
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
}
|
|
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})`);
|
|
1775
1801
|
}
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
}
|
|
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)`);
|
|
1780
1806
|
}
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
}
|
|
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(", ")})`);
|
|
1787
1813
|
}
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
}
|
|
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(", ")})`);
|
|
1794
1820
|
}
|
|
1795
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}`);
|
|
1838
|
+
for (const [tableName, tableDef] of Object.entries(def.schema)) {
|
|
1839
|
+
statements.push(...emitTableDDL(schemaName, tableName, tableDef));
|
|
1840
|
+
}
|
|
1796
1841
|
for (const [tableName, tableDef] of Object.entries(def.schema)) {
|
|
1797
|
-
|
|
1798
|
-
const constraintName = `fk_${schemaName}_${tableName}_${rel.name}`;
|
|
1799
|
-
statements.push(`ALTER TABLE ${schemaName}.${tableName} ADD CONSTRAINT ${constraintName} ` + `FOREIGN KEY (${rel.fields.join(", ")}) ` + `REFERENCES ${schemaName}.${rel.references} (${rel.referencedColumns.join(", ")})`);
|
|
1800
|
-
}
|
|
1842
|
+
statements.push(...emitForeignKeyDDL(schemaName, tableName, tableDef));
|
|
1801
1843
|
}
|
|
1802
1844
|
const hashInput = JSON.stringify({
|
|
1803
1845
|
name: def.name,
|
|
@@ -2219,5 +2261,5 @@ export {
|
|
|
2219
2261
|
backfillSubgraph
|
|
2220
2262
|
};
|
|
2221
2263
|
|
|
2222
|
-
//# debugId=
|
|
2264
|
+
//# debugId=DCE205425496103A64756E2164756E21
|
|
2223
2265
|
//# sourceMappingURL=reindex.js.map
|