We value your privacy

This site uses cookies to improve your browsing experience, analyze site traffic, and show personalized content. See our Privacy Policy.

  1. MooseStack
  2. Moose OLAP
  3. Modeling Views

Modeling Views

Overview

Views are read-time projections in ClickHouse. A static SELECT defines the view over one or more base tables or other views. Moose wraps ClickHouse VIEW with a simple View class in TypeScript or Python. You provide the view name, the SELECT, and the list of source tables/views so Moose can order DDL correctly during migrations.

When to use a View

Use View when you want a virtual read-time projection and don't need write-time transformation or a separate storage table. For write-time pipelines and backfills, use a Materialized View instead.

Basic Usage

BasicUsage.ts
import { View, sql } from "@514labs/moose-lib";import { users } from "./Users";import { events } from "./Events"; export const activeUserEvents = new View(  "active_user_events",  sql.statement`    SELECT      ${events.columns.id}    AS event_id,      ${users.columns.id}     AS user_id,      ${users.columns.name}   AS user_name,      ${events.columns.ts}    AS ts    FROM ${events}    JOIN ${users} ON ${events.columns.user_id} = ${users.columns.id}    WHERE ${users.columns.active} = 1  `,  [events, users],);

Multi-Database Views

Pass a database argument to create the view in a specific ClickHouse database:

MultiDatabase.ts
import { View, sql } from "@514labs/moose-lib";import { events } from "./Events"; export const rawEvents = new View(  "raw_events",  sql.statement`SELECT * FROM ${events}`,  [events],  { database: "analytics" },);

When database is set, Moose creates the view as `database`.`view_name` in ClickHouse. Source table references that also carry a database config are automatically qualified as `database`.`table` in source_tables.

Quick Reference

Signature.ts
// new View(name, selectStatement, baseTables, config?, metadata?)new View(  "view_name",  sql.statement`SELECT ... FROM ${someTable}`,  [someTable, /* other tables or views */],  { database: "optional_db" }, // optional ViewConfig);
Static SQL recommended

The SELECT should be static (no runtime parameters). In TypeScript, prefer Moose's sql.statement / sql.fragment templates for safe table/column interpolation; in Python, use string templates with {table.columns.col}.

On this page

OverviewBasic UsageMulti-Database ViewsQuick Reference
Edit this page
FiveonefourFiveonefour
Fiveonefour Docs
MooseStackHostingTemplatesGuides
Release Notes
Source544
  • Overview
Build a New App
  • 5 Minute Quickstart
  • Browse Templates
  • Existing ClickHouse
Add to Existing App
  • Next.js
  • Fastify
Fundamentals
  • Moose Runtime
  • MooseDev MCP
  • Language Server
  • Data Modeling
Moose Modules
  • Moose OLAP
    • Data Modeling
    • Tables
    • Views
    • Materialized Views
    • Materialized Columns
    • Alias Columns
    • External Data & Introspection
    • External Tables
    • Introspecting Tables
    • Data Access
    • Inserting Data
    • Reading Data
    • Performance & Optimization
    • Schema Optimization
    • Secondary & Data-skipping Indexes
    • Projections
    • TTL (Time-to-Live)
    • Schema Versioning
  • Moose Streaming
  • Moose Workflows
  • Moose APIs & Web Apps
Deployment & Lifecycle
  • Moose Dev
  • Moose Migrate
  • Moose Deploy
Reference
  • API Reference
  • Query Layer
  • Data Types
  • Table Engines
  • CLI
  • Configuration
  • Observability Metrics
  • Help
  • Release Notes
Contribution
  • Documentation
  • Framework
BasicUsage.ts
import { View, sql } from "@514labs/moose-lib";import { users } from "./Users";import { events } from "./Events"; export const activeUserEvents = new View(  "active_user_events",  sql.statement`    SELECT      ${events.columns.id}    AS event_id,      ${users.columns.id}     AS user_id,      ${users.columns.name}   AS user_name,      ${events.columns.ts}    AS ts    FROM ${events}    JOIN ${users} ON ${events.columns.user_id} = ${users.columns.id}    WHERE ${users.columns.active} = 1  `,  [events, users],);
MultiDatabase.ts
import { View, sql } from "@514labs/moose-lib";import { events } from "./Events"; export const rawEvents = new View(  "raw_events",  sql.statement`SELECT * FROM ${events}`,  [events],  { database: "analytics" },);
Signature.ts
// new View(name, selectStatement, baseTables, config?, metadata?)new View(  "view_name",  sql.statement`SELECT ... FROM ${someTable}`,  [someTable, /* other tables or views */],  { database: "optional_db" }, // optional ViewConfig);