@secondlayer/subgraphs 0.5.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 +219 -0
- package/dist/src/index.js +956 -0
- package/dist/src/index.js.map +22 -0
- package/dist/src/runtime/block-processor.d.ts +100 -0
- package/dist/src/runtime/block-processor.js +501 -0
- package/dist/src/runtime/block-processor.js.map +16 -0
- package/dist/src/runtime/catchup.d.ts +78 -0
- package/dist/src/runtime/catchup.js +630 -0
- package/dist/src/runtime/catchup.js.map +18 -0
- package/dist/src/runtime/clarity.d.ts +15 -0
- package/dist/src/runtime/clarity.js +41 -0
- package/dist/src/runtime/clarity.js.map +10 -0
- package/dist/src/runtime/context.d.ts +69 -0
- package/dist/src/runtime/context.js +177 -0
- package/dist/src/runtime/context.js.map +10 -0
- package/dist/src/runtime/processor.d.ts +8 -0
- package/dist/src/runtime/processor.js +770 -0
- package/dist/src/runtime/processor.js.map +20 -0
- package/dist/src/runtime/reindex.d.ts +84 -0
- package/dist/src/runtime/reindex.js +738 -0
- package/dist/src/runtime/reindex.js.map +19 -0
- package/dist/src/runtime/reorg.d.ts +78 -0
- package/dist/src/runtime/reorg.js +536 -0
- package/dist/src/runtime/reorg.js.map +17 -0
- package/dist/src/runtime/runner.d.ts +147 -0
- package/dist/src/runtime/runner.js +130 -0
- package/dist/src/runtime/runner.js.map +11 -0
- package/dist/src/runtime/source-matcher.d.ts +53 -0
- package/dist/src/runtime/source-matcher.js +111 -0
- package/dist/src/runtime/source-matcher.js.map +11 -0
- package/dist/src/runtime/stats.d.ts +24 -0
- package/dist/src/runtime/stats.js +75 -0
- package/dist/src/runtime/stats.js.map +10 -0
- package/dist/src/schema/index.d.ts +117 -0
- package/dist/src/schema/index.js +305 -0
- package/dist/src/schema/index.js.map +13 -0
- package/dist/src/service.d.ts +0 -0
- package/dist/src/service.js +780 -0
- package/dist/src/service.js.map +21 -0
- package/dist/src/types.d.ts +80 -0
- package/dist/src/types.js +22 -0
- package/dist/src/types.js.map +10 -0
- package/dist/src/validate.d.ts +84 -0
- package/dist/src/validate.js +59 -0
- package/dist/src/validate.js.map +10 -0
- package/package.json +49 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/** Supported column types for subgraph schemas */
|
|
2
|
+
type ColumnType = "text" | "uint" | "int" | "principal" | "boolean" | "timestamp" | "jsonb";
|
|
3
|
+
/** Column definition in a subgraph table */
|
|
4
|
+
interface SubgraphColumn {
|
|
5
|
+
type: ColumnType;
|
|
6
|
+
nullable?: boolean;
|
|
7
|
+
indexed?: boolean;
|
|
8
|
+
search?: boolean;
|
|
9
|
+
default?: string | number | boolean;
|
|
10
|
+
}
|
|
11
|
+
/** Table definition within a subgraph schema */
|
|
12
|
+
interface SubgraphTable {
|
|
13
|
+
columns: Record<string, SubgraphColumn>;
|
|
14
|
+
/** Composite indexes (each entry is an array of column names) */
|
|
15
|
+
indexes?: string[][];
|
|
16
|
+
/** Unique key constraints (each entry is an array of column names). Required for upsert. */
|
|
17
|
+
uniqueKeys?: string[][];
|
|
18
|
+
}
|
|
19
|
+
/** Subgraph schema — maps table names to table definitions */
|
|
20
|
+
type SubgraphSchema = Record<string, SubgraphTable>;
|
|
21
|
+
/** Source filter for what blockchain data this subgraph processes */
|
|
22
|
+
interface SubgraphSource {
|
|
23
|
+
/** Contract principal (e.g., SP000...::contract-name). Supports * wildcards. */
|
|
24
|
+
contract?: string;
|
|
25
|
+
/** Event name/topic to filter on */
|
|
26
|
+
event?: string;
|
|
27
|
+
/** Function name to filter on */
|
|
28
|
+
function?: string;
|
|
29
|
+
/** Transaction type filter (e.g., "stx_transfer", "contract_call") */
|
|
30
|
+
type?: string;
|
|
31
|
+
/** Minimum amount filter (for stx_transfer sources) */
|
|
32
|
+
minAmount?: bigint;
|
|
33
|
+
}
|
|
34
|
+
/** Context passed to subgraph handlers during event processing */
|
|
35
|
+
interface SubgraphContext {
|
|
36
|
+
block: {
|
|
37
|
+
height: number
|
|
38
|
+
hash: string
|
|
39
|
+
timestamp: number
|
|
40
|
+
burnBlockHeight: number
|
|
41
|
+
};
|
|
42
|
+
tx: {
|
|
43
|
+
txId: string
|
|
44
|
+
sender: string
|
|
45
|
+
type: string
|
|
46
|
+
status: string
|
|
47
|
+
};
|
|
48
|
+
insert(table: string, row: Record<string, unknown>): void;
|
|
49
|
+
update(table: string, where: Record<string, unknown>, set: Record<string, unknown>): void;
|
|
50
|
+
upsert(table: string, key: Record<string, unknown>, row: Record<string, unknown>): void;
|
|
51
|
+
delete(table: string, where: Record<string, unknown>): void;
|
|
52
|
+
findOne(table: string, where: Record<string, unknown>): Promise<Record<string, unknown> | null>;
|
|
53
|
+
findMany(table: string, where: Record<string, unknown>): Promise<Record<string, unknown>[]>;
|
|
54
|
+
}
|
|
55
|
+
/** Handler function that processes events and writes to the subgraph */
|
|
56
|
+
type SubgraphHandler = (event: Record<string, unknown>, ctx: SubgraphContext) => Promise<void> | void;
|
|
57
|
+
/**
|
|
58
|
+
* Derive the source key used to look up handlers.
|
|
59
|
+
* - { contract: "SP123.market", function: "list" } → "SP123.market::list"
|
|
60
|
+
* - { contract: "SP123.market", event: "sale" } → "SP123.market::sale"
|
|
61
|
+
* - { contract: "SP123.market" } → "SP123.market"
|
|
62
|
+
* - { type: "stx_transfer" } → "stx_transfer"
|
|
63
|
+
*/
|
|
64
|
+
declare function sourceKey(source: SubgraphSource): string;
|
|
65
|
+
/** Complete subgraph definition */
|
|
66
|
+
interface SubgraphDefinition {
|
|
67
|
+
/** Unique subgraph name (lowercase, alphanumeric + hyphens) */
|
|
68
|
+
name: string;
|
|
69
|
+
/** Semantic version */
|
|
70
|
+
version?: string;
|
|
71
|
+
/** Human description */
|
|
72
|
+
description?: string;
|
|
73
|
+
/** What blockchain data to process — one or more source filters */
|
|
74
|
+
sources: SubgraphSource[];
|
|
75
|
+
/** Tables in this subgraph */
|
|
76
|
+
schema: SubgraphSchema;
|
|
77
|
+
/** Keyed handler functions — keys match sourceKey() output, "*" is catch-all */
|
|
78
|
+
handlers: Record<string, SubgraphHandler>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Validates a subgraph definition, returning the parsed result or throwing on failure.
|
|
82
|
+
*/
|
|
83
|
+
declare function validateSubgraphDefinition(def: unknown): SubgraphDefinition;
|
|
84
|
+
interface ReindexOptions {
|
|
85
|
+
fromBlock?: number;
|
|
86
|
+
toBlock?: number;
|
|
87
|
+
schemaName?: string;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Reindex a subgraph by dropping its tables, recreating them, and reprocessing
|
|
91
|
+
* all historical blocks through the handler pipeline.
|
|
92
|
+
*/
|
|
93
|
+
declare function reindexSubgraph(def: SubgraphDefinition, opts?: ReindexOptions): Promise<{
|
|
94
|
+
processed: number
|
|
95
|
+
}>;
|
|
96
|
+
/**
|
|
97
|
+
* Identity function that preserves schema literal types for type inference.
|
|
98
|
+
*
|
|
99
|
+
* The generic `S` is narrowed to the exact schema shape so that column type
|
|
100
|
+
* literals (e.g. `"uint"`) are preserved rather than widened to `string`.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* defineSubgraph({
|
|
105
|
+
* name: "my-subgraph",
|
|
106
|
+
* sources: [{ contract: "SP000...::my-contract" }],
|
|
107
|
+
* schema: { transfers: { columns: { amount: { type: "uint" } } } },
|
|
108
|
+
* handlers: { "*": (event, ctx) => { ... } }
|
|
109
|
+
* })
|
|
110
|
+
* // typeof result.schema.transfers.columns.amount.type → "uint" (not string)
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
declare function defineSubgraph<S extends SubgraphSchema>(def: Omit<SubgraphDefinition, "schema"> & {
|
|
114
|
+
schema: S
|
|
115
|
+
}): Omit<SubgraphDefinition, "schema"> & {
|
|
116
|
+
schema: S
|
|
117
|
+
};
|
|
118
|
+
interface GeneratedSQL {
|
|
119
|
+
statements: string[];
|
|
120
|
+
hash: string;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Generates PostgreSQL DDL statements for a subgraph definition.
|
|
124
|
+
* Creates a dedicated schema `subgraph_<name>` with one table per schema entry,
|
|
125
|
+
* each with auto-columns and indexes.
|
|
126
|
+
*/
|
|
127
|
+
declare function generateSubgraphSQL(def: SubgraphDefinition, schemaNameOverride?: string): GeneratedSQL;
|
|
128
|
+
import { pgSchemaName } from "@secondlayer/shared/db/queries/subgraphs";
|
|
129
|
+
import { Kysely } from "kysely";
|
|
130
|
+
import { Database } from "@secondlayer/shared/db";
|
|
131
|
+
type AnyDb = Kysely<Database>;
|
|
132
|
+
interface TableDiff {
|
|
133
|
+
/** Tables added to the schema */
|
|
134
|
+
addedTables: string[];
|
|
135
|
+
/** Tables removed from the schema */
|
|
136
|
+
removedTables: string[];
|
|
137
|
+
/** Per-table column diffs (only for tables present in both) */
|
|
138
|
+
tables: Record<string, ColumnDiff>;
|
|
139
|
+
}
|
|
140
|
+
interface ColumnDiff {
|
|
141
|
+
added: string[];
|
|
142
|
+
removed: string[];
|
|
143
|
+
changed: string[];
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Compare two multi-table subgraph schemas and return differences.
|
|
147
|
+
*/
|
|
148
|
+
declare function diffSchema(existing: SubgraphSchema, incoming: SubgraphSchema): TableDiff;
|
|
149
|
+
/**
|
|
150
|
+
* Deploy a subgraph schema to the database.
|
|
151
|
+
* - New subgraph → CREATE SCHEMA + tables + register
|
|
152
|
+
* - Same hash → no-op
|
|
153
|
+
* - Additive change → ALTER TABLE ADD COLUMN / CREATE TABLE for new tables
|
|
154
|
+
* - Breaking change → throws error
|
|
155
|
+
*/
|
|
156
|
+
declare function deploySchema(db: AnyDb, def: SubgraphDefinition, handlerPath: string, opts?: {
|
|
157
|
+
forceReindex?: boolean
|
|
158
|
+
apiKeyId?: string
|
|
159
|
+
schemaName?: string
|
|
160
|
+
}): Promise<{
|
|
161
|
+
action: "created" | "unchanged" | "updated" | "reindexed"
|
|
162
|
+
subgraphId: string
|
|
163
|
+
}>;
|
|
164
|
+
/** Maps a ColumnType string literal to its TypeScript equivalent */
|
|
165
|
+
type ColumnToTS<T extends string> = T extends "uint" | "int" ? number : T extends "text" | "principal" | "timestamp" ? string : T extends "boolean" ? boolean : T extends "jsonb" ? Record<string, unknown> : unknown;
|
|
166
|
+
/** Infer TS type for a single column definition, respecting nullable */
|
|
167
|
+
type InferColumnType<C extends SubgraphColumn> = C["type"] extends ColumnType ? C["nullable"] extends true ? ColumnToTS<C["type"]> | null : ColumnToTS<C["type"]> : unknown;
|
|
168
|
+
/** Shape of system columns on every returned row (camelCase, underscore-prefixed) */
|
|
169
|
+
interface SystemRow {
|
|
170
|
+
_id: string;
|
|
171
|
+
_blockHeight: number;
|
|
172
|
+
_txId: string;
|
|
173
|
+
_createdAt: string;
|
|
174
|
+
}
|
|
175
|
+
/** Infer the row shape for a single table definition */
|
|
176
|
+
type InferTableRow<T extends SubgraphTable> = SystemRow & { [K in keyof T["columns"]] : InferColumnType<T["columns"][K]> };
|
|
177
|
+
/** Comparison operators for a scalar value */
|
|
178
|
+
type ComparisonFilter<T> = {
|
|
179
|
+
eq?: T
|
|
180
|
+
neq?: T
|
|
181
|
+
gt?: T
|
|
182
|
+
gte?: T
|
|
183
|
+
lt?: T
|
|
184
|
+
lte?: T
|
|
185
|
+
};
|
|
186
|
+
/** Where clause — each column accepts a scalar (eq) or comparison object */
|
|
187
|
+
type WhereInput<TRow> = { [K in keyof TRow]? : TRow[K] | ComparisonFilter<TRow[K]> };
|
|
188
|
+
/**
|
|
189
|
+
* No-prefix aliases for system columns accepted in where/orderBy inputs.
|
|
190
|
+
* Both `_blockHeight` and `blockHeight` are valid — serializer handles mapping.
|
|
191
|
+
*/
|
|
192
|
+
type SystemWhereAliases = {
|
|
193
|
+
blockHeight?: number | ComparisonFilter<number>
|
|
194
|
+
txId?: string | ComparisonFilter<string>
|
|
195
|
+
createdAt?: string | ComparisonFilter<string>
|
|
196
|
+
id?: string | ComparisonFilter<string>
|
|
197
|
+
};
|
|
198
|
+
type SystemOrderByAliases = {
|
|
199
|
+
blockHeight?: "asc" | "desc"
|
|
200
|
+
txId?: "asc" | "desc"
|
|
201
|
+
createdAt?: "asc" | "desc"
|
|
202
|
+
id?: "asc" | "desc"
|
|
203
|
+
};
|
|
204
|
+
interface FindManyOptions<TRow> {
|
|
205
|
+
where?: WhereInput<TRow> & SystemWhereAliases;
|
|
206
|
+
orderBy?: { [K in keyof TRow]? : "asc" | "desc" } & SystemOrderByAliases;
|
|
207
|
+
limit?: number;
|
|
208
|
+
offset?: number;
|
|
209
|
+
fields?: (keyof TRow & string)[];
|
|
210
|
+
}
|
|
211
|
+
interface SubgraphTableClient<TRow> {
|
|
212
|
+
findMany(options?: FindManyOptions<TRow>): Promise<TRow[]>;
|
|
213
|
+
count(where?: WhereInput<TRow> & SystemWhereAliases): Promise<number>;
|
|
214
|
+
}
|
|
215
|
+
/** Infer a typed client object from a subgraph definition shape */
|
|
216
|
+
type InferSubgraphClient<T> = T extends {
|
|
217
|
+
schema: infer S
|
|
218
|
+
} ? { [K in keyof S] : S[K] extends SubgraphTable ? SubgraphTableClient<InferTableRow<S[K]>> : never } : never;
|
|
219
|
+
export { validateSubgraphDefinition, sourceKey, reindexSubgraph, pgSchemaName, generateSubgraphSQL, diffSchema, deploySchema, defineSubgraph, WhereInput, TableDiff, SystemRow, SubgraphTableClient, SubgraphTable, SubgraphSource, SubgraphSchema, SubgraphHandler, SubgraphDefinition, SubgraphContext, SubgraphColumn, ReindexOptions, InferTableRow, InferSubgraphClient, InferColumnType, GeneratedSQL, FindManyOptions, ComparisonFilter, ColumnType, ColumnToTS, ColumnDiff };
|