0

Run Query

by
Published Jul 17, 2024

Runs a query in BigQuery. [See the documentation](https://cloud.google.com/bigquery/docs/running-queries#node.js) for more information.

Script gcloud Verified

The script

Submitted by hugo697 Bun
Verified 692 days ago
1
import { BigQuery } from "@google-cloud/bigquery";
2

3
type Gcloud = {
4
  type: string;
5
  project_id: string;
6
  private_key_id: string;
7
  private_key: string;
8
  client_email: string;
9
  client_id: string;
10
  auth_uri: string;
11
  token_uri: string;
12
  auth_provider_x509_cert_url: string;
13
  client_x509_cert_url: string;
14
  universe_domain: string;
15
};
16

17
export async function main(resource: Gcloud, query: string) {
18
  const bigQueryClient = new BigQuery({
19
    credentials: resource,
20
    projectId: resource.project_id,
21
  });
22

23
  const [job] = await bigQueryClient.createQueryJob({
24
    query,
25
  });
26

27
  const [rows] = await job.getQueryResults();
28

29
  return rows;
30
}
31