overleaf/services/web/scripts/analytics/helpers/GoogleBigQueryHelper.mjs
Liangjun Song 902ae750dc Merge pull request #21202 from overleaf/ls-scripts-to-esm-2
Migrate scripts folder to esm 2/x

GitOrigin-RevId: 1698bc4f13e026fa281d37a4914a2f997849c761
2024-10-23 08:04:53 +00:00

30 lines
628 B
JavaScript

import { BigQuery as GoogleBigQuery } from '@google-cloud/bigquery'
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
}
export default {
query,
}