UNPKG

warehouse-multiconnect

Version:
47 lines 1.88 kB
import { BigQuery, BigQueryDate, BigQueryDatetime, BigQueryTimestamp } from "@google-cloud/bigquery"; import chalk from "chalk"; const bigquery = new BigQuery(); export async function query(query, params) { const t0 = Date.now(); if (process.env.VERBOSE) { console.log(); console.log(chalk.gray(query)); if (params && Object.keys(params).length > 0) console.log(chalk.gray(`QUERY PARAMS: ${JSON.stringify(params)}`)); } const [rows] = await bigquery.query({ query, params }); if (process.env.VERBOSE) console.log(chalk.gray(`(${rows.length} rows returned in ${((Date.now() - t0) / 1000).toFixed(3)} seconds)`)); return rows.map(row => formatRow(row)); } export async function insert(table, data) { const [dataset_name, table_name] = table.split("."); await bigquery.dataset(dataset_name).table(table_name).insert(data); } export function safeValue(value) { if (typeof value === "string") return /^[a-z0-9,./_-]*$/i.test(value) && value.length <= 64 ? `'${value}'` : "null"; else if (typeof value === "number") return String(value); else throw `Unsafe value for query: "${value}"`; } export function safeUrl(value) { if (typeof value === "string" && value.length < 500) return `'${new URL(value).href.replaceAll("'", "%60")}'`; else throw `Unsafe value for query: "${value}"`; } function formatRow(obj) { if (obj === null || typeof obj !== "object") return obj; if (Array.isArray(obj)) return obj.map(formatRow); if (obj instanceof BigQueryDate || obj instanceof BigQueryDatetime || obj instanceof BigQueryTimestamp) return new Date(obj.value); const result = {}; for (let key of Object.keys(obj)) result[key] = formatRow(obj[key]); return result; } //# sourceMappingURL=bigquery.js.map