.do
Patterns

View-Based Patterns

CLI patterns for transforming and viewing entities in different representations

View-Based CLI Patterns

View-based patterns are for viewing and transforming entities into different representations. Use these for format conversion, data projection, filtering, and read-only transformations.

Pattern Structure

Domain: {subject}.as/{view}

Semantic Triple: $.{Subject}.viewAs.{View}

CLI Command: do {subject}.view {View} [options]

Core Concept

View-based patterns create read-only transformations of data. They don't modify the underlying data, but present it in different formats or projections.

Key Rule: Object (view/format) MUST be explicit

  • ❌ BAD: functions.as (as WHAT?)
  • ✅ GOOD: functions.as/api (as API)
flowchart TB Source[Source Data] --> Select[Select View Type] Select --> JSON[JSON View] Select --> GraphQL[GraphQL View] Select --> REST[REST View] Select --> CSV[CSV View] JSON --> Transform1[Transform to JSON] GraphQL --> Transform2[Transform to GraphQL Schema] REST --> Transform3[Transform to REST API] CSV --> Transform4[Transform to CSV] Transform1 --> Filter[Apply Filters] Transform2 --> Filter Transform3 --> Filter Transform4 --> Filter Filter --> Project[Project Fields] Project --> Format[Format Output] Format --> Cache{Cache?} Cache -->|Yes| Materialize[Materialize View] Cache -->|No| Output[Return Output] Materialize --> Output

CLI Commands

Create View

# Create view with configuration
do {subject}.view create {View} [options]

# Example: Create JSON view of database
do database.view create JSON --collections users,orders --format compact

Query Through View

# Query data through a view
do {subject}.view query {View} [filters]

# Example: Query through JSON view
do database.view query JSON users --where '{"status": "active"}'

List Views

# List all views
do view list

# List views for specific subject
do view list database

# List by type
do view list --type json

Delete View

# Delete view
do view delete {view-id}

# Force delete
do view delete {view-id} --force

Data Format Views

JSON View

# Create JSON view
do database.view create JSON '{
  "database": "production",
  "collections": ["users", "orders", "products"],
  "filter": {"status": "active"},
  "format": "compact"
}'

# Query through JSON view
do database.view query JSON users --limit 100

# Stream JSON
do database.view stream JSON orders --where '{"createdAt": {">": "2025-01-01"}}'

# Export to file
do database.view export JSON users --output users.json

GraphQL View

# Create GraphQL view from schema
do api.view create GraphQL '{
  "schema": "
    type User {
      id: ID!
      email: String!
      name: String
      orders: [Order!]!
    }
    type Order {
      id: ID!
      total: Float!
      status: String!
    }
    type Query {
      users: [User!]!
      orders: [Order!]!
    }
  "
}'

# Query via GraphQL
do api.view query GraphQL '{
  users {
    id
    email
    orders {
      id
      total
    }
  }
}'

# Introspection
do api.view introspect GraphQL

REST API View

# Create REST API view
do api.view create REST '{
  "openapi": "3.0.0",
  "paths": {
    "/users": {
      "get": {"operationId": "listUsers"},
      "post": {"operationId": "createUser"}
    }
  }
}'

# Generate OpenAPI spec
do api.view export REST --format openapi --output api-spec.yaml

# Test endpoints
do api.view test REST GET /users

CSV View

# Create CSV view
do database.view create CSV '{
  "collection": "orders",
  "fields": ["id", "customerEmail", "total", "status", "createdAt"],
  "delimiter": ",",
  "header": true
}'

# Export to CSV
do database.view export CSV orders --output orders.csv

# Import from CSV
do database.import CSV --file users.csv --collection users

XML View

# Create XML view
do data.view create XML '{
  "collection": "products",
  "rootElement": "products",
  "itemElement": "product",
  "namespace": "http://example.com/products"
}'

# Export to XML
do data.view export XML products --output products.xml

Schema Views

Database Schema View

# View database as schema
do database.view create Schema '{
  "database": "production",
  "includeIndexes": true,
  "includeRelationships": true
}'

# Export schema
do database.view export Schema --format graphdl --output schema.gdl

# Visualize schema
do database.view visualize Schema --output schema.svg

# Compare schemas
do database.view compare Schema schema-v1 schema-v2

GraphDL View

# View resources as GraphDL
do resources.view create GraphDL '{
  "collections": ["users", "businesses", "products"],
  "includeRelationships": true
}'

# Export GraphDL
do resources.view export GraphDL --output entities.gdl

# Validate GraphDL
do resources.view validate GraphDL

OpenAPI View

# View API as OpenAPI
do api.view create OpenAPI '{
  "version": "3.1.0",
  "title": "My API",
  "servers": [{"url": "https://api.example.com"}]
}'

# Generate client SDKs
do api.view generate OpenAPI --language typescript --output ./sdk

# Generate documentation
do api.view docs OpenAPI --output ./docs

Transformation Views

Functions as API

# View functions as API endpoints
do functions.view create API '{
  "functions": ["fn_validateEmail", "fn_processPayment", "fn_sendEmail"],
  "basePath": "/api/v1",
  "authentication": "bearer"
}'

# Generate API docs
do functions.view docs API --output api-docs.html

# Test function via API view
do functions.view test API POST /processPayment '{"amount": 100}'

Database as GraphQL

# Auto-generate GraphQL from database
do database.view create GraphQL '{
  "collections": ["users", "orders", "products"],
  "relationships": true,
  "mutations": true,
  "subscriptions": false
}'

# Query database via GraphQL
do database.view query GraphQL '{
  users(where: {role: "admin"}) {
    id
    email
    orders {
      total
    }
  }
}'

Resources as REST

# Convert resources to REST API
do resources.view create REST '{
  "resources": ["User", "Business", "Product"],
  "methods": ["GET", "POST", "PUT", "DELETE"],
  "versioning": "path",
  "pagination": "cursor"
}'

# Test REST endpoints
do resources.view test REST GET /users?limit=10

Projection Views

Field Projection

# Create projection view (subset of fields)
do database.view create Projection '{
  "collection": "users",
  "fields": ["id", "email", "name"],
  "exclude": ["password", "apiKey"]
}'

# Query with projection
do database.view query Projection users

Aggregation View

# Create aggregation view
do analytics.view create Aggregation '{
  "collection": "orders",
  "groupBy": ["status", "region"],
  "metrics": [
    {"field": "total", "operation": "sum", "alias": "revenue"},
    {"field": "id", "operation": "count", "alias": "orderCount"}
  ]
}'

# Query aggregated data
do analytics.view query Aggregation orders

Join View

# Create join view
do database.view create Join '{
  "left": {"collection": "orders", "alias": "o"},
  "right": {"collection": "users", "alias": "u"},
  "on": "o.userId = u.id",
  "type": "left",
  "fields": [
    "o.id",
    "o.total",
    "u.email",
    "u.name"
  ]
}'

# Query joined data
do database.view query Join orders_with_users

Real-Time Views

Streaming View

# Create streaming view
do data.view create Stream '{
  "source": "events",
  "filter": {"type": "user.action"},
  "transform": "json",
  "bufferSize": 100
}'

# Subscribe to stream
do data.view subscribe Stream events --callback 'https://api.example.com/webhook'

# Read from stream
do data.view read Stream events --tail -f

Live Query View

# Create live query view (updates in real-time)
do database.view create LiveQuery '{
  "collection": "orders",
  "query": {"status": "pending"},
  "pollInterval": 5000
}'

# Watch live query
do database.view watch LiveQuery pending_orders

Caching Views

# Create cached view
do database.view create Cached '{
  "collection": "products",
  "ttl": 3600,
  "strategy": "write-through"
}'

# Invalidate cache
do database.view invalidate Cached products

# Warm cache
do database.view warm Cached products

Examples by Use Case

API Documentation Generation

# Create documentation view
do api.view create Documentation '{
  "source": "openapi-spec.yaml",
  "format": "html",
  "theme": "default",
  "includeExamples": true
}'

# Generate docs
do api.view generate Documentation --output ./docs

# Serve interactive docs
do api.view serve Documentation --port 3000

Data Export & ETL

# Create export view
do database.view create Export '{
  "collections": ["users", "orders"],
  "format": "parquet",
  "compression": "snappy",
  "partitionBy": ["year", "month"]
}'

# Run export
do database.view run Export --destination s3://data-lake/exports/

# Schedule regular exports
do database.view schedule Export --cron "0 2 * * *"

Analytics Dashboard

# Create dashboard view
do analytics.view create Dashboard '{
  "queries": [
    {
      "id": "revenue",
      "query": "SELECT SUM(total) FROM orders WHERE status=\"completed\"",
      "refresh": 60
    },
    {
      "id": "activeUsers",
      "query": "SELECT COUNT(*) FROM users WHERE lastActive > NOW() - INTERVAL 24 HOUR",
      "refresh": 300
    }
  ]
}'

# Render dashboard
do analytics.view render Dashboard --output dashboard.html

# Serve dashboard
do analytics.view serve Dashboard --port 8080

Data Validation

# Create validation view
do data.view create Validation '{
  "collection": "users",
  "rules": [
    {"field": "email", "type": "email"},
    {"field": "age", "type": "number", "min": 18},
    {"field": "status", "type": "enum", "values": ["active", "inactive"]}
  ]
}'

# Validate data
do data.view validate Validation users

# Export validation report
do data.view report Validation --output validation-report.json

API Client Generation

# Create client view
do api.view create Client '{
  "source": "openapi-spec.yaml",
  "language": "typescript",
  "framework": "axios",
  "includeTypes": true
}'

# Generate client
do api.view generate Client --output ./sdk/

# Test generated client
do api.view test Client

View Composition

Combine multiple views:

# Create composite view
do views.compose '{
  "name": "UserOrdersAnalytics",
  "views": [
    {"type": "Join", "config": {...}},
    {"type": "Aggregation", "config": {...}},
    {"type": "JSON", "config": {...}}
  ]
}'

# Query composite view
do views.query UserOrdersAnalytics

View Materialization

# Materialize view (cache results)
do database.view materialize JSON users --ttl 3600

# Refresh materialized view
do database.view refresh JSON users

# Schedule refresh
do database.view schedule-refresh JSON users --cron "0 * * * *"

SDK Equivalents

Every CLI command maps directly to SDK calls:

# CLI
do database.view create JSON --collections users
// SDK
const jsonView = await $.Database.viewAs.JSON({
  collections: ['users'],
})
# CLI
do api.view create GraphQL --schema schema.graphql
// SDK
const graphqlView = await $.API.viewAs.GraphQL({
  schema: schemaString,
})

Use Cases

✅ Use View-Based Patterns When:

  • Read-only data transformation
  • Format conversion (JSON, XML, CSV, GraphQL)
  • Projection/filtering
  • View composition
  • API documentation generation
  • Schema visualization
  • Data export/import

❌ Don't Use When:

  • Data modification needed → Use noun-based or verb-based patterns
  • Executing business logic → Use verb-based patterns
  • Building persistent systems → Use noun-based patterns
  • Deploying autonomous agents → Use agent-based patterns

Output Formats

# JSON output
do database.view query JSON users --json

# Pretty print
do database.view query JSON users --pretty

# Stream output
do database.view stream JSON orders

# File output
do database.view export JSON users --output users.json

# Multiple formats
do database.view export JSON users --formats json,csv,xml

Performance Optimization

# Create indexed view
do database.view create Indexed '{
  "collection": "users",
  "indexes": ["email", "createdAt"]
}'

# Parallel processing
do database.view export JSON orders --parallel 10

# Compression
do database.view export JSON users --compress gzip

Access Control

# Create view with permissions
do database.view create JSON '{
  "collection": "users",
  "fields": ["id", "email", "name"],
  "permissions": {
    "read": ["role:admin", "role:analyst"],
    "export": ["role:admin"]
  }
}'

# Check view permissions
do database.view permissions JSON users user-123

References