Unigraph SQL Examples
These examples show the same queries two ways: in raw SQL (any PostgreSQL client, any language) and with the typed @ensnode/ensdb-sdk for TypeScript projects.
Connect
Section titled “Connect”The ENS Unigraph lives in ENSDb, a PostgreSQL database. Each ENSDb Writer instance writes to its own ENSDb Writer Schema (e.g. ensindexer_0). Each instance of ENSDb Metadata Writer writes to a shared operational metadata table in the ensnode schema.
Connect to your ENSDb instance:
psql postgresql://user:password@host:5432/ensdb_devnetDiscover the available ENSDb Writer Schemas:
SELECT DISTINCT ens_indexer_schema_nameFROM ensnode.metadata;| # | ens_indexer_schema_name |
|---|---|
| 1 | ensindexer_0 |
| 2 | ensindexer_1 |
Install ENSDb SDK from NPM registry:
npm install @ensnode/ensdb-sdkThe EnsDbReader object enables you to build custom queries against the Unigraph data model in ENSDb. Use the ensDb field to build queries with the Drizzle ORM, and the ensIndexerSchema field to reference database objects (i.e. the Unigraph tables) within the ENSIndexer Schema in a type-safe way.
Connect to your ENSDb instance:
import { EnsDbReader } from "@ensnode/ensdb-sdk";
// Connect by providing a connection string and the ENSIndexer Schema Name to queryconst ensDbReader = new EnsDbReader(ensDbConnectionString, ensIndexerSchemaName);const { ensDb, ensIndexerSchema, ensNodeSchema } = ensDbReader;Discover the available ENSDb Writer Schemas:
const availableEnsDbWriterSchemas = await ensDb .selectDistinct({ ensIndexerSchemaName: ensNodeSchema.metadata.ensIndexerSchemaName, }) .from(ensNodeSchema.metadata);
console.log(availableEnsDbWriterSchemas);| # | ensIndexerSchemaName |
|---|---|
| 1 | ensindexer_0 |
| 2 | ensindexer_1 |
Examples
Section titled “Examples”Here are some example queries to get you started. Each example is available in raw SQL and with the ensdb-sdk for TypeScript projects.
See each example’s page for details on the query and how to run it against your ENSDb instance.