All files / utils/api getSchemaBigQuery.ts

92.85% Statements 26/28
75% Branches 3/4
100% Functions 1/1
92.85% Lines 26/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 291x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 119x 119x 119x 11x 11x 119x 1x 1x 1x     1x  
import { CustomError } from '../error'
 
export default async (
  req: Record<string, any>,
  $bq
): Promise<Record<string, any>> => {
  try {
    const entity = req.params.entity
    const dataset = `data_mart_${entity}`
    const sql = `
      SELECT column_name, table_name
      FROM ${dataset}.INFORMATION_SCHEMA.COLUMNS
    `
    const result = await $bq.runQuery({ sql })
    let schema = {}
    result.forEach((row) => {
      let { column_name, table_name } = row
      table_name = table_name.replace('mvw_', '')
      if (!schema[table_name]) {
        schema[table_name] = []
      }
      schema[table_name].push(column_name)
    })
    return schema
  } catch (error: any) {
    throw new CustomError(`[getBigQuerySchema] ${error.message}`, 400)
  }
}