lightdash ProjectAdapter 接口定义

主要介绍接口定义,实际的实现后续说明
ProjectAdapter 是一个比较重要的东西,定义了project 以及dbt client ,WarehouseClient

接口定义

 
export interface ProjectAdapter {
    compileAllExplores(): Promise<(Explore | ExploreError)[]>;
    getDbtPackages(): Promise<DbtPackages | undefined>;
    runQuery(sql: string): Promise<Record<string, any>[]>;
    test(): Promise<void>;
    destroy(): Promise<void>;
}
 
export interface DbtClient {
    installDeps(): Promise<void>;
    getDbtManifest(): Promise<DbtRpcGetManifestResults>;
    getDbtCatalog(): Promise<DbtRpcDocsGenerateResults>;
    getDbtPackages?(): Promise<DbtPackages | undefined>;
    test(): Promise<void>;
}
 
export type WarehouseTableSchema = {
    [column: string]: DimensionType;
};
 
export type WarehouseCatalog = {
    [database: string]: {
        [schema: string]: {
            [table: string]: WarehouseTableSchema;
        };
    };
};
 
export interface WarehouseClient {
    getCatalog: (
        config: {
            database: string;
            schema: string;
            table: string;
            columns: string[];
        }[],
    ) => Promise<WarehouseCatalog>;
    runQuery(sql: string): Promise<Record<string, any>[]>;
    test(): Promise<void>;
}

相关实现

packages/backend/src/projectAdapters/projectAdapter.ts
定义实现的入口,从此处也可以看出系统内部的实现

 
export const projectAdapterFromConfig = async (
    config: DbtProjectConfig,
    warehouseCredentials: CreateWarehouseCredentials,
): Promise<ProjectAdapter> => {
    const warehouseClient =
        warehouseClientFromCredentials(warehouseCredentials);
    const configType = config.type;
    Logger.debug(`Initialize project adaptor of type ${configType}`);
    switch (config.type) {
        case ProjectType.DBT:
            return new DbtLocalCredentialsProjectAdapter({
                warehouseClient,
                projectDir: config.project_dir || '/usr/app/dbt',
                warehouseCredentials,
            });
        case ProjectType.DBT_CLOUD_IDE:
            return new DbtCloudIdeProjectAdapter({
                warehouseClient,
                accountId: `${config.account_id}`,
                environmentId: `${config.environment_id}`,
                projectId: `${config.project_id}`,
                apiKey: config.api_key,
            });
        case ProjectType.GITHUB:
            return new DbtGithubProjectAdapter({
                warehouseClient,
                githubPersonalAccessToken: config.personal_access_token,
                githubRepository: config.repository,
                githubBranch: config.branch,
                projectDirectorySubPath: config.project_sub_path,
                hostDomain: config.host_domain,
                warehouseCredentials,
            });
        case ProjectType.GITLAB:
            return new DbtGitlabProjectAdapter({
                warehouseClient,
                gitlabPersonalAccessToken: config.personal_access_token,
                gitlabRepository: config.repository,
                gitlabBranch: config.branch,
                projectDirectorySubPath: config.project_sub_path,
                hostDomain: config.host_domain,
                warehouseCredentials,
            });
        case ProjectType.BITBUCKET:
            return new DbtBitBucketProjectAdapter({
                warehouseClient,
                username: config.username,
                personalAccessToken: config.personal_access_token,
                repository: config.repository,
                branch: config.branch,
                projectDirectorySubPath: config.project_sub_path,
                hostDomain: config.host_domain,
                warehouseCredentials,
            });
        case ProjectType.AZURE_DEVOPS:
            return new DbtAzureDevOpsProjectAdapter({
                warehouseClient,
                personalAccessToken: config.personal_access_token,
                organization: config.organization,
                project: config.project,
                repository: config.repository,
                branch: config.branch,
                projectDirectorySubPath: config.project_sub_path,
                warehouseCredentials,
            });
        default:
            const never: never = config;
            throw new Error(`Adapter not implemented for type: ${configType}`);
    }
};

说明

ProjectAdapter 是比较重要的东西,当然lightdash内部还包含了其他东西,比如维度以及度量部分,有些是需要进行编译处理的,后续会介绍下

posted on 2022-03-20 22:06  荣锋亮  阅读(60)  评论(0编辑  收藏  举报

导航