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

dbt-helpers.mddocs/api/utilities/specialized/

DBT Utilities and Helpers

Utilities for working with DBT (Data Build Tool) projects and configurations. This file documents DBT-related helper functions mentioned in the original utilities file.

Capabilities

This module provides the following functionality:

Project Directory Utilities

/**
 * Gets the project directory from DBT connection configuration
 * @param dbtConnection - Optional DBT project configuration
 * @returns Project directory path or undefined
 */
function getProjectDirectory(
  dbtConnection?: DbtProjectConfig
): string | undefined;

Examples

Basic Project Directory Access

import { getProjectDirectory } from '@lightdash/common';

// Get project directory from DBT connection
const dbtConnection: DbtProjectConfig = {
  type: 'dbt_cloud_ide',
  target: 'dev',
  project_dir: '/home/user/my-dbt-project',
  profiles_dir: '/home/user/.dbt',
};

const projectDir = getProjectDirectory(dbtConnection);
console.log('Project directory:', projectDir);
// Output: '/home/user/my-dbt-project'

// Handle missing configuration
const emptyDir = getProjectDirectory();
console.log('Directory:', emptyDir); // undefined

// Handle connection without project_dir
const minimalConnection: DbtProjectConfig = {
  type: 'dbt',
  target: 'prod',
};

const dir = getProjectDirectory(minimalConnection);
console.log('Directory:', dir); // undefined

DBT Connection Types

import { getProjectDirectory } from '@lightdash/common';

// Different DBT connection types
const cloudIdeConnection = {
  type: 'dbt_cloud_ide',
  project_dir: '/workspace/dbt-project',
  target: 'dev',
};

const localConnection = {
  type: 'dbt',
  project_dir: './dbt',
  target: 'dev',
  profiles_dir: '~/.dbt',
};

const githubConnection = {
  type: 'github',
  repository: 'https://github.com/org/dbt-project',
  branch: 'main',
  project_dir: '/dbt',
  target: 'prod',
};

// Extract project directory from any connection type
const cloudDir = getProjectDirectory(cloudIdeConnection);
const localDir = getProjectDirectory(localConnection);
const githubDir = getProjectDirectory(githubConnection);

Project Configuration Loading

import { getProjectDirectory, loadLightdashProjectConfig } from '@lightdash/common';

async function loadProjectConfig(dbtConnection: DbtProjectConfig) {
  // Get project directory
  const projectDir = getProjectDirectory(dbtConnection);

  if (!projectDir) {
    throw new Error('Project directory not configured in DBT connection');
  }

  // Load Lightdash configuration from project directory
  const config = await loadLightdashProjectConfig(projectDir);

  return config;
}

CLI Integration

import { getProjectDirectory } from '@lightdash/common';
// Node.js built-in (server-side only)
import path from 'path';

async function deployDbtProject(options: {
  connection?: DbtProjectConfig;
  projectDir?: string;
}) {
  // Try to get project directory from connection first
  let projectDir = options.connection
    ? getProjectDirectory(options.connection)
    : undefined;

  // Fall back to explicit project dir or current directory
  projectDir = projectDir || options.projectDir || process.cwd();

  console.log(`Deploying DBT project from: ${projectDir}`);

  // Look for dbt_project.yml
  const dbtProjectPath = path.join(projectDir, 'dbt_project.yml');

  // Compile and deploy
  await compileDbtProject(dbtProjectPath);
}

Project Validation

import { getProjectDirectory } from '@lightdash/common';
// Node.js built-ins (server-side only)
import fs from 'fs/promises';
import path from 'path';

async function validateDbtProject(dbtConnection: DbtProjectConfig): Promise<boolean> {
  const projectDir = getProjectDirectory(dbtConnection);

  if (!projectDir) {
    console.error('No project directory configured');
    return false;
  }

  // Check if dbt_project.yml exists
  const dbtProjectPath = path.join(projectDir, 'dbt_project.yml');

  try {
    await fs.access(dbtProjectPath);
    console.log('Valid DBT project found');
    return true;
  } catch (error) {
    console.error('dbt_project.yml not found in project directory');
    return false;
  }
}

Project Path Resolution

import { getProjectDirectory } from '@lightdash/common';
// Node.js built-in (server-side only)
import path from 'path';

function resolveProjectPaths(dbtConnection: DbtProjectConfig) {
  const projectDir = getProjectDirectory(dbtConnection);

  if (!projectDir) {
    throw new Error('Project directory not configured');
  }

  return {
    projectDir,
    dbtProjectYml: path.join(projectDir, 'dbt_project.yml'),
    modelsDir: path.join(projectDir, 'models'),
    macrosDir: path.join(projectDir, 'macros'),
    seedsDir: path.join(projectDir, 'seeds'),
    testsDir: path.join(projectDir, 'tests'),
    targetDir: path.join(projectDir, 'target'),
  };
}

// Usage
const paths = resolveProjectPaths(dbtConnection);
console.log('Models directory:', paths.modelsDir);

Multi-Project Management

import { getProjectDirectory } from '@lightdash/common';

interface LightdashProject {
  id: string;
  name: string;
  dbtConnection: DbtProjectConfig;
}

function getProjectDirectories(projects: LightdashProject[]): Map<string, string> {
  const directories = new Map<string, string>();

  for (const project of projects) {
    const dir = getProjectDirectory(project.dbtConnection);

    if (dir) {
      directories.set(project.id, dir);
    } else {
      console.warn(`Project ${project.name} has no configured directory`);
    }
  }

  return directories;
}

API Integration

import { getProjectDirectory } from '@lightdash/common';

// API endpoint to get project info
app.get('/api/projects/:id/info', async (req, res) => {
  const project = await getProject(req.params.id);

  const projectDir = getProjectDirectory(project.dbtConnection);

  res.json({
    id: project.id,
    name: project.name,
    projectDirectory: projectDir,
    hasValidDirectory: !!projectDir,
    connectionType: project.dbtConnection?.type,
  });
});

Testing

import { getProjectDirectory } from '@lightdash/common';

describe('DBT utilities', () => {
  describe('getProjectDirectory', () => {
    it('should return project directory from connection', () => {
      const connection = {
        type: 'dbt',
        project_dir: '/path/to/project',
        target: 'dev',
      };

      const dir = getProjectDirectory(connection);
      expect(dir).toBe('/path/to/project');
    });

    it('should return undefined for missing connection', () => {
      const dir = getProjectDirectory();
      expect(dir).toBeUndefined();
    });

    it('should return undefined for connection without project_dir', () => {
      const connection = {
        type: 'dbt',
        target: 'dev',
      };

      const dir = getProjectDirectory(connection);
      expect(dir).toBeUndefined();
    });
  });
});

DBT Connection Types

Lightdash supports multiple DBT connection types:

Local DBT

{
  type: 'dbt',
  project_dir: './dbt',
  profiles_dir: '~/.dbt',
  target: 'dev'
}

DBT Cloud IDE

{
  type: 'dbt_cloud_ide',
  project_dir: '/workspace/dbt-project',
  target: 'dev',
  api_key: 'dbt_cloud_api_key'
}

GitHub

{
  type: 'github',
  repository: 'https://github.com/org/dbt-project',
  branch: 'main',
  project_dir: '/dbt',
  personal_access_token: 'ghp_token',
  target: 'prod'
}

GitLab

{
  type: 'gitlab',
  repository: 'https://gitlab.com/org/dbt-project',
  branch: 'main',
  project_dir: '/dbt',
  personal_access_token: 'gitlab_token',
  target: 'prod'
}

Use Cases

  • Project Loading: Get project directory for loading configurations
  • File Resolution: Resolve paths to DBT project files
  • Validation: Validate project structure and configuration
  • CLI Tools: Access project files from command-line tools
  • API Endpoints: Provide project information through APIs
  • Multi-Project: Manage multiple DBT projects
  • Deployment: Deploy DBT projects to Lightdash

Related Utilities

  • Project Configuration: See project-config.md for loadLightdashProjectConfig()
  • GitHub Integration: See github.md for GitHub token validation
  • Path Utilities: File path resolution and validation

Notes

  • The function returns undefined if no connection is provided or if the connection doesn't include a project directory
  • Different connection types may store the project directory in different formats
  • Always validate that the returned directory exists before using it
  • Project directory paths can be relative or absolute
  • For GitHub/GitLab connections, the project_dir refers to the path within the repository

Future Enhancements

Additional DBT utilities that may be documented here in the future:

  • DBT manifest parsing
  • DBT model compilation
  • DBT profile management
  • DBT target configuration
  • DBT package management
  • DBT test execution
  • DBT documentation generation