# Unigraph SQL Examples

export const bashSQLConnectSnippet = `psql postgresql://user:password@host:5432/ensdb_devnet`;

export const sqlDiscoverSnippet = `SELECT DISTINCT ens_indexer_schema_name
FROM ensnode.metadata;`;

export const sqlDiscoverResultJson = [
  { ens_indexer_schema_name: "ensindexer_0" },
  { ens_indexer_schema_name: "ensindexer_1" },
];

export const tsConnectCodeSnippet = `// Connect by providing a connection string and the ENSIndexer Schema Name to query
const ensDbReader = new EnsDbReader(ensDbConnectionString, ensIndexerSchemaName);
const { ensDb, ensIndexerSchema, ensNodeSchema } = ensDbReader;`;

export const tsDiscoverCodeSnippet = `const availableEnsDbWriterSchemas = await ensDb
  .selectDistinct({
    ensIndexerSchemaName: ensNodeSchema.metadata.ensIndexerSchemaName,
  })
  .from(ensNodeSchema.metadata);

console.log(availableEnsDbWriterSchemas);`;

export const tsDiscoverResultJson = [
  { ensIndexerSchemaName: "ensindexer_0" },
  { ensIndexerSchemaName: "ensindexer_1" },
];

export const bashSdkInstallSnippet = `npm install @ensnode/ensdb-sdk`;

These examples show the same queries two ways: in **raw SQL** (any PostgreSQL client, any language) and with the typed **[`@ensnode/ensdb-sdk`](https://www.npmjs.com/package/@ensnode/ensdb-sdk)** for TypeScript projects.

<UnigraphIntro />

## Connect

The ENS Unigraph lives in [ENSDb](/docs/services/ensdb), a PostgreSQL database. Each [ENSDb Writer](/docs/services/ensdb/concepts/glossary#ensdb-writer) instance writes to its own **ENSDb Writer Schema** (e.g. `ensindexer_0`). Each instance of [ENSDb Metadata Writer](/docs/services/ensdb/concepts/glossary#ensdb-metadata-writer) writes to a shared operational [metadata table](/docs/services/ensdb/concepts/glossary#ensnode-metadata-table) in the [`ensnode` schema](/docs/services/ensdb/concepts/glossary#ensnode-schema).

<UnigraphExampleWrapper>
  <UnigraphExampleSQLTab>
    <p class="mx-5 -mt-3">Connect to your ENSDb instance:</p>

    <StaticExampleCodeSection lang="bash" code={bashSQLConnectSnippet} />

    <p class="p-5 pb-0">Discover the available ENSDb Writer Schemas:</p>

    <StaticExampleCodeSection badge="SQL" lang="sql" code={sqlDiscoverSnippet} />

    <div class="px-5 mt-4">
      <Pill>Output</Pill>
    </div>

    <SqlResultTable rows={sqlDiscoverResultJson} />

  </UnigraphExampleSQLTab>

  <UnigraphExampleEnsDbSdkTab>
    <p class="mx-5 -mt-3">Install ENSDb SDK from NPM registry:</p>

    <StaticExampleCodeSection lang="bash" code={bashSdkInstallSnippet} />

    <div class="px-5 pt-5">
**ENSDb Reader:** The `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](/docs/services/ensdb/concepts/glossary#ensindexer-schema) in a type-safe way.
    </div>

    <p class="mx-5 mt-3">Connect to your ENSDb instance:</p>

    <StaticExampleCodeSection badge="TypeScript" lang="ts" code={tsConnectCodeSnippet} />

    <p class="p-5 pb-0">Discover the available ENSDb Writer Schemas:</p>

    <StaticExampleCodeSection badge="TypeScript" lang="ts" code={tsDiscoverCodeSnippet} />

    <div class="px-5 mt-4">
      <Pill>Output</Pill>
    </div>
    <SqlResultTable rows={tsDiscoverResultJson} />

  </UnigraphExampleEnsDbSdkTab>
</UnigraphExampleWrapper>

## 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.

[Domain by Name](/docs/integrate/unigraph/examples/domain-by-name)
  [Subdomains](/docs/integrate/unigraph/examples/subdomains-by-parent-name)
  [Expiring Registrations](/docs/integrate/unigraph/examples/expiring-registrations)
  [Indexing Status](/docs/integrate/unigraph/examples/indexing-status)