or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

api

features

charts

charts.mdconditional-formatting.mdvisualizations.md
authorization.mdchangesets.mdcharts-as-code.mdcompiler.mddashboards.mddbt.mdee-features.mdformatting.mdparameters.mdpivot.mdprojects-spaces.mdsql-runner.mdtemplating.mdwarehouse.md
index.md
tile.json

tessl/npm-lightdash--common

Shared TypeScript library for the Lightdash platform containing common types, utilities, and business logic for analytics workflows

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@lightdash/common@0.2231.x

To install, run

npx @tessl/cli install tessl/npm-lightdash--common@0.2231.5

index.mddocs/

Lightdash Common

Overview

Lightdash Common (@lightdash/common) is a comprehensive TypeScript library providing shared types, utilities, and business logic for the Lightdash analytics platform. It enables building dbt-based BI applications with extensive support for data modeling, query execution, authorization, visualization, and warehouse integration across 7 data warehouse types.

Package Information

  • Package Name: @lightdash/common
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @lightdash/common

Quick Start

Essential Imports

import {
  // Core types
  type Explore,
  type CompiledField,
  type MetricQuery,
  type Field,
  type Filters,
  type Dashboard,
  type SavedChart,

  // Query building
  FilterOperator,
  UnitOfTime,

  // Utilities
  getFields,
  formatItemValue,
  getVisibleFields,
  validateEmail,

  // Compiler
  ExploreCompiler,

  // Authorization
  defineUserAbility,
  getUserAbilityBuilder,
} from "@lightdash/common";

CommonJS Usage

For CommonJS environments (Node.js with require):

const {
  // Note: Types not available in CommonJS (use JSDoc for type hints)
  // Compiler
  ExploreCompiler,

  // Utilities
  getFields,
  getDimensions,
  getMetrics,
  formatItemValue,
  getVisibleFields,
  validateEmail,

  // Authorization
  defineUserAbility,
  getUserAbilityBuilder,

  // Enums
  FilterOperator,
  WarehouseTypes,
  ChartKind,
} = require("@lightdash/common");

Note: TypeScript type imports are not available in CommonJS. Use JSDoc comments for type hints if needed.

Basic Usage

Complete workflow showing compilation, field access, and querying:

import {
  type Explore,
  type MetricQuery,
  type Field,
  ExploreCompiler,
  FilterOperator,
  UnitOfTime,
  getFields,
  getDimensions,
  getMetrics,
  formatItemValue,
} from "@lightdash/common";

// 1. Compile a dbt model into a Lightdash explore
const compiler = new ExploreCompiler({
  warehouseClient,
  targetWarehouse: "snowflake",
});

const compiledExplore = await compiler.compileExplore({
  dbtManifest,
  modelName: "customers",
  // ... additional options
});

// 2. Access fields from the explore (data model)
const fields = getFields(compiledExplore);
const dimensions = getDimensions(compiledExplore);
const metrics = getMetrics(compiledExplore);

console.log(`Available: ${dimensions.length} dimensions, ${metrics.length} metrics`);

// 3. Format values for display
const formatted = formatItemValue(field, rawValue, true); // Convert to UTC

// 4. Build metric queries
const query: MetricQuery = {
  exploreName: "customers",
  dimensions: ["customer_id", "customer_name"],
  metrics: ["total_revenue"],
  filters: {
    dimensions: {
      id: "root",
      and: [
        {
          target: { fieldId: "customer_created_at" },
          operator: FilterOperator.IN_THE_PAST,
          values: [6],
          settings: { completed: true, unitOfTime: UnitOfTime.months },
        },
      ],
    },
  },
  sorts: [{ fieldId: "total_revenue", descending: true }],
  limit: 100,
  tableCalculations: [],
};

Example 1: Query an Explore

import { type MetricQuery, FilterOperator, UnitOfTime } from "@lightdash/common";

const query: MetricQuery = {
  exploreName: "orders",
  dimensions: ["orders_customer_id", "orders_status"],
  metrics: ["orders_count", "orders_total_revenue"],
  filters: {
    dimensions: {
      id: "root",
      and: [{
        id: "date_filter",
        target: { fieldId: "orders_created_at" },
        operator: FilterOperator.IN_THE_PAST,
        values: [30],
        settings: { unitOfTime: UnitOfTime.days },
      }],
    },
  },
  sorts: [{ fieldId: "orders_total_revenue", descending: true }],
  limit: 100,
  tableCalculations: [],
};

Example 2: Format Query Results

import { formatItemValue, getItemMap, type Explore, type MetricQuery } from "@lightdash/common";

// Get item map for formatting
const itemsMap = getItemMap(explore, query.additionalMetrics, query.tableCalculations, query.customDimensions);

// Format individual values
results.rows.forEach(row => {
  Object.entries(row).forEach(([fieldId, value]) => {
    const item = itemsMap[fieldId];
    const formatted = formatItemValue(item, value, false); // Use false or true for UTC conversion
    console.log(`${fieldId}: ${formatted}`);
  });
});

Example 3: Check User Permissions

import { defineUserAbility, type LightdashUser, type ProjectMemberProfile } from "@lightdash/common";

const ability = defineUserAbility(user, projectProfiles);

if (ability.can("view", "Dashboard")) {
  // User can view dashboards
}

if (ability.can("update", "SavedChart")) {
  // User can edit charts
}

I Want To...

Quick navigation to common tasks:

TaskSee
Build a queryBuilding Queries Guide
Filter dataFilter System
Parse filter expressionsFilter Parsing & DSL
Compare time periodsPeriod-over-Period Comparison
Format valuesFormatting Guide
Create chartsVisualization Guide
Check permissionsAuthorization Guide
Embed dashboardsJWT Authentication
Compile dbt modelsCompilation Guide
Work with dashboardsDashboards API
Access warehouseWarehouse Integration
Import common APIsImport Patterns
Look up an APICommon APIs

Documentation Structure

Quick Reference

Fast lookups for common needs:

Guides

Task-oriented tutorials:

API Reference

Comprehensive API documentation:

Types

Features

Query Operations:

Charts and Visualization:

Core Features:

Utilities

Core Utilities:

  • Validation - Type guards, email/password validation, JSON schemas
  • Data - Array/object manipulation, item utilities
  • Formatting - String, color, time, result formatting
  • Query - Result transformation, chart utilities, metrics explorer
  • API - URL templating, filters, search params
  • Warehouse - SQL generation, project config, DBT integration
  • Helpers - String operations, array operations, object utilities

Specialized Utilities:

Reference

Additional reference materials:

Core Concepts

Data Modeling with Explores

An Explore is a compiled data model containing tables, dimensions, metrics, and joins. Explores are created by compiling dbt models using the ExploreCompiler.

// Key explore functions
import { getFields, getDimensions, getMetrics, type Explore } from "@lightdash/common";

const fields = getFields(explore);        // All fields (dimensions + metrics)
const dimensions = getDimensions(explore); // Only dimensions
const metrics = getMetrics(explore);      // Only metrics

Query Building with MetricQuery

A MetricQuery declaratively defines what data to fetch without writing SQL:

interface MetricQuery {
  exploreName: string;           // Data model to query
  dimensions: string[];          // Fields to group by
  metrics: string[];            // Values to aggregate
  filters: Filters;             // Dimension/metric filters
  sorts: SortField[];          // Sort order
  limit: number;               // Max rows
  tableCalculations: TableCalculation[];  // Post-query calculations
  additionalMetrics?: AdditionalMetric[]; // Ad-hoc metrics
  customDimensions?: CustomDimension[];   // Runtime dimensions
}

Filtering with 19 Operators

The filter system supports complex conditions with AND/OR logic:

enum FilterOperator {
  EQUALS = 'equals',
  NOT_EQUALS = 'notEquals',
  LESS_THAN = 'lessThan',
  GREATER_THAN = 'greaterThan',
  IN_THE_PAST = 'inThePast',
  IN_THE_NEXT = 'inTheNext',
  IN_BETWEEN = 'inBetween',
  // ... 12 more operators
}

Formatting with Type-Aware Functions

Format values based on field type and custom format options:

import { formatItemValue, type Field } from "@lightdash/common";

// Auto-detects field type and applies formatting
const formatted = formatItemValue(field, rawValue, {
  timeZone: 'UTC',
  parameters: {},
});

Authorization with CASL

Role-based permissions using CASL library:

import { defineUserAbility } from "@lightdash/common";

const ability = defineUserAbility(user, projectProfiles);

// Check permissions
if (ability.can('view', 'Dashboard')) { /* ... */ }
if (ability.can('update', { type: 'SavedChart', uuid: chartId })) { /* ... */ }

Supported Environments

Warehouses

  • BigQuery (Google Cloud)
  • Snowflake
  • Redshift (Amazon)
  • Databricks
  • PostgreSQL
  • Trino
  • ClickHouse

Runtime

  • Node.js 14+ (18+ recommended)
  • TypeScript 4.5+ (5.0+ recommended)
  • Works in both ESM and CommonJS environments

Common Patterns

Pattern 1: Field Access with Maps

import { getFieldMap, getItemMap } from "@lightdash/common";

// Create maps once for O(1) lookups
const fieldMap = getFieldMap(explore);
const itemsMap = getItemMap(explore, additionalMetrics, tableCalculations);

// Fast lookups
const field = fieldMap["orders_customer_id"];
const item = itemsMap["orders_total_revenue"];

Pattern 2: Type Guards for Safety

import { isDimension, isMetric, isTableCalculation } from "@lightdash/common";

function processField(field: Field) {
  if (isDimension(field)) {
    // TypeScript knows this is a Dimension
    console.log("Dimension type:", field.type);
  } else if (isMetric(field)) {
    // TypeScript knows this is a Metric
    console.log("Metric type:", field.type);
  }
}

Pattern 3: Error Handling

import { ExploreCompiler, isExploreError } from "@lightdash/common";

try {
  const explore = compiler.compileExplore(uncompiledExplore);

  if (isExploreError(explore)) {
    console.error("Compilation errors:", explore.errors);
  } else {
    // Use compiled explore
  }
} catch (error) {
  console.error("Compilation failed:", error.message);
}

Key Design Principles

  1. Immutable Types: All transformations return new objects for predictable state management
  2. Warehouse Abstraction: Write queries once, run on any of 7 warehouse types
  3. Type Safety: Extensive TypeScript types with runtime type guards
  4. Declarative Queries: Define what data you want without writing SQL
  5. Authorization First: All operations check permissions before execution
  6. Progressive Enhancement: Start simple, add complexity as needed

Performance Tips

  1. Cache Compiled Explores: Compilation is expensive, cache the results
  2. Use Field Maps: Create getFieldMap() once, reuse for O(1) lookups
  3. Batch Operations: Group related queries when possible
  4. Stream Large Results: Use warehouse pagination for large datasets
  5. Cache User Abilities: Permissions rarely change during a session

Next Steps

  1. New to Lightdash? Start with Building Queries Guide
  2. Looking for an API? Check Common APIs
  3. Need to understand architecture? Read Architecture Reference
  4. Want comprehensive docs? See Complete Getting Started