2024-10-22 04:21:39 -04:00
|
|
|
import { BigQuery as GoogleBigQuery } from '@google-cloud/bigquery'
|
2022-12-20 11:09:28 -05:00
|
|
|
|
|
|
|
let dataset = null
|
|
|
|
|
|
|
|
function getDataset() {
|
|
|
|
if (!dataset) {
|
|
|
|
console.log(
|
|
|
|
'Connecting to BigQuery dataset: ',
|
|
|
|
process.env.BQ_PROJECT_ID,
|
|
|
|
process.env.BQ_DATASET
|
|
|
|
)
|
|
|
|
|
|
|
|
dataset = new GoogleBigQuery({
|
|
|
|
projectId: process.env.BQ_PROJECT_ID,
|
|
|
|
keyFilename: process.env.GCS_KEY_FILE,
|
|
|
|
}).dataset(process.env.BQ_DATASET)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataset
|
|
|
|
}
|
|
|
|
|
|
|
|
async function query(query) {
|
|
|
|
const [job] = await getDataset().createQueryJob({ query })
|
|
|
|
const [rows] = await job.getQueryResults()
|
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
2024-10-22 04:21:39 -04:00
|
|
|
export default {
|
2022-12-20 11:09:28 -05:00
|
|
|
query,
|
|
|
|
}
|