Moose

Reference

TypeScript API Reference

API Reference

Viewing typescript

switch to python

This is a comprehensive reference for @514labs/moose-lib , detailing all exported components, types, and utilities.

Core Types

Key<T extends string | number | Date>

A type for marking fields as primary keys in data models.

// Example
interface MyModel {
  id: Key<string>; // Marks 'id' as a primary key of type string
}

JWT<T extends object>

A type for working with JSON Web Tokens.

// Example
type UserJWT = JWT<{ userId: string, role: string }>;

ConsumptionUtil

Interface providing utilities for consumption APIs.

interface ConsumptionUtil {
  client: MooseClient;  // Client for interacting with the database
  sql: typeof sql;      // SQL template tag function
  jwt: JWTPayload | undefined;  // Current JWT if available
}

IngestionFormat

Enum defining supported data ingestion formats.

enum IngestionFormat {
  JSON = "JSON",         // Single JSON object
  JSON_ARRAY = "JSON_ARRAY"  // Array of JSON objects
}

Infrastructure Components

OlapTable<T>

Creates a ClickHouse table with the schema of type T.

// Basic usage
const myTable = new OlapTable<UserProfile>("user_profiles");
 
// With configuration
const myTable = new OlapTable<UserProfile>("user_profiles", {
  orderByFields: ["id", "timestamp"],
  deduplicate: true
});

Stream<T>

Creates a Redpanda topic with the schema of type T.

// Basic usage
const myStream = new Stream<UserEvent>("user_events");
 
// With configuration
const myStream = new Stream<UserEvent>("user_events", {
  parallelism: 3,
  retentionPeriod: 86400 // 1 day in seconds
});
 
// Adding transformations
myStream.addTransform(
  destinationStream,
  (record) => transformFunction(record)
);

IngestApi<T>

Creates an HTTP endpoint for ingesting data of type T.

// Basic usage with destination stream
const myIngestApi = new IngestApi<UserEvent>("user_events", {
  destination: myUserEventStream
});

ConsumptionApi<T, R>

Creates an HTTP endpoint for querying data with request type T and response type R.

// Basic usage
const myApi = new ConsumptionApi<UserQuery, UserProfile[]>(
  "getUserProfiles",
  async (params, { client, sql }) => {
    const result = await client.query.execute(
      sql`SELECT * FROM user_profiles WHERE age > ${params.minAge} LIMIT 10`
    );
    return result;
  }
);

IngestPipeline<T>

Combines ingest API, stream, and table creation in a single component.

// Basic usage
const pipeline = new IngestPipeline<UserEvent>("user_pipeline", {
  ingest: true,
  stream: true,
  table: true
});
 
// With advanced configuration
const pipeline = new IngestPipeline<UserEvent>("user_pipeline", {
  ingest: true,
  stream: { parallelism: 3 },
  table: { 
    orderByFields: ["id", "timestamp"],
    deduplicate: true
  }
});

MaterializedView<T>

Creates a materialized view in ClickHouse.

// Basic usage
const view = new MaterializedView<UserStatistics>({
  selectStatement: "SELECT user_id, COUNT(*) as event_count FROM user_events GROUP BY user_id",
  tableName: "user_events",
  materializedViewName: "user_statistics",
  orderByFields: ["user_id"]
});

SQL Utilities

sql Template Tag

Template tag for creating type-safe SQL queries with parameters.

// Basic usage
const query = sql`SELECT * FROM users WHERE id = ${userId}`;
 
// With multiple parameters
const query = sql`
  SELECT * FROM users 
  WHERE age > ${minAge} 
  AND country = ${country}
  LIMIT ${limit}
`;

MooseClient

Client for interacting with ClickHouse and Temporal.

class MooseClient {
  query: QueryClient;      // For database queries
  workflow: WorkflowClient; // For workflow operations
}

ClickHouse Utilities

ClickHouseEngines

Enum for supported ClickHouse table engines.

enum ClickHouseEngines {
  MergeTree = "MergeTree",
  ReplacingMergeTree = "ReplacingMergeTree",
  SummingMergeTree = "SummingMergeTree",
  AggregatingMergeTree = "AggregatingMergeTree",
  CollapsingMergeTree = "CollapsingMergeTree",
  VersionedCollapsingMergeTree = "VersionedCollapsingMergeTree",
  GraphiteMergeTree = "GraphiteMergeTree"
}

Task Management

TaskFunction

Type for defining workflow task functions.

interface TaskFunction {
  (input?: any): Promise<{ task: string; data: any }>;
}

TaskConfig

Configuration options for tasks.

interface TaskConfig {
  retries: number;
}

TaskDefinition

Complete definition of a workflow task.

interface TaskDefinition {
  task: TaskFunction;
  config?: TaskConfig;
}