Utilities for working with DBT (Data Build Tool) projects and configurations. This file documents DBT-related helper functions mentioned in the original utilities file.
This module provides the following functionality:
/**
* 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;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); // undefinedimport { 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);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;
}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);
}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;
}
}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);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;
}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,
});
});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();
});
});
});Lightdash supports multiple DBT connection types:
{
type: 'dbt',
project_dir: './dbt',
profiles_dir: '~/.dbt',
target: 'dev'
}{
type: 'dbt_cloud_ide',
project_dir: '/workspace/dbt-project',
target: 'dev',
api_key: 'dbt_cloud_api_key'
}{
type: 'github',
repository: 'https://github.com/org/dbt-project',
branch: 'main',
project_dir: '/dbt',
personal_access_token: 'ghp_token',
target: 'prod'
}{
type: 'gitlab',
repository: 'https://gitlab.com/org/dbt-project',
branch: 'main',
project_dir: '/dbt',
personal_access_token: 'gitlab_token',
target: 'prod'
}loadLightdashProjectConfig()undefined if no connection is provided or if the connection doesn't include a project directoryAdditional DBT utilities that may be documented here in the future: