Skip to content
ENC Protocol

@enc-protocol/dataview — API Reference

@enc-protocol/dataview is the server-side projection of app state for clients. It provides a push receiver and HTTP client interface to query materialized views across enclaves, backed by protocol-layer state snapshots.

Installation

npm install @enc-protocol/dataview

Depends on @enc-protocol/core.

Package Structure

receiver.js — Push Receiver

The push receiver subscribes to app events from enclaves and materializes them into queryable views.

DataViewReceiver

import { DataViewReceiver } from '@enc-protocol/dataview/receiver.js'
 
const receiver = new DataViewReceiver(opts)

Parameters:

ParameterTypeDescription
opts.appIdstringApp identifier
opts.enclavesstring[]Enclave IDs to subscribe to
opts.nodeUrlstringNode URL for subscriptions
opts.identityObjectIdentity for authentication

.subscribe()

await receiver.subscribe() → Promise<void>

Open subscriptions to all configured enclaves.

.on(event, handler)

receiver.on('event', (event) => { /* … */ })
receiver.on('error', (err) => { /* … */ })

Register handlers for 'event' and 'error' events.

.ingest(event)

receiver.ingest(event: Object) → void

Process an event and update materialized views.

.close()

receiver.close() → void

Close all subscriptions.

client.js — HTTP Client

Query dataviews over HTTP.

DataViewClient

import { DataViewClient } from '@enc-protocol/dataview/client.js'
 
const client = new DataViewClient(baseUrl)
ParameterTypeDescription
baseUrlstringDataview HTTP endpoint

.query(viewName, opts?)

client.query(viewName: string, opts?: Object) → Promise<Object[]>

Query a materialized view.

Parameters:

ParameterTypeDescription
viewNamestringName of the view (e.g., 'profiles', 'feed')
opts.limitnumberMax rows to return
opts.reversebooleanReverse order
opts.filterObjectOptional key-value filters

Returns: Array of view rows (each row includes from, _enclave, _event_id, _timestamp).

const profiles = await client.query('profiles', { limit: 100 })
for (const row of profiles) {
  console.log(row.id_pub, row.name, row._enclave)
}

.getStatus()

client.getStatus() → Promise<Object>

Get dataview health and sync status.

Returns:

{
  status: 'healthy' | 'syncing' | 'error',
  synced_enclave_count: number,
  total_enclaves: number,
  last_event_timestamp: number,
}

base-dataview.js — Base Class

Abstract base for implementing custom dataviews.

BaseDataView

import { BaseDataView } from '@enc-protocol/dataview/base-dataview.js'
 
class MyDataView extends BaseDataView {
  async ingest(event) {
    // Process event and update views
  }
 
  async query(viewName, opts) {
    // Return materialized rows
  }
}

Methods to override

async ingest(event)

Called when a new event arrives.

async ingest(event: Object) → void

Update internal materialized views based on the event type and content.

async query(viewName, opts)

Query a view.

async query(viewName: string, opts?: Object) → Object[]

Example: Personal SDK with DataView

import { DataViewClient } from '@enc-protocol/dataview/client.js'
import { createIdentity } from '@enc-protocol/client'
 
// Query profiles materialized from Personal enclaves
const dataview = new DataViewClient('https://dv.example.com')
 
// List all profiles
const profiles = await dataview.query('profiles', { limit: 50 })
for (const p of profiles) {
  console.log(p.name, p.id_pub)
}
 
// Check dataview sync status
const status = await dataview.getStatus()
console.log(`Synced ${status.synced_enclave_count}/${status.total_enclaves} enclaves`)

See also