Skip to content

Commit

Permalink
Update BigQuery sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
hokaccha committed Jun 12, 2021
1 parent 9353526 commit 0b8861a
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 408 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -27,7 +27,7 @@
},
"dependencies": {
"@fortawesome/fontawesome-free": "5.12.0",
"@google-cloud/bigquery": "0.7.0",
"@google-cloud/bigquery": "5.6.0",
"aws-sdk": "2.259.1",
"classnames": "2.2.5",
"codemirror": "~5.58.2",
Expand Down
32 changes: 20 additions & 12 deletions src/lib/DataSourceDefinition/BigQuery.ts
@@ -1,4 +1,4 @@
import bigquery from "@google-cloud/bigquery";
import { BigQuery as BigQueryClient } from "@google-cloud/bigquery";
import Base, { ConfigSchemasType } from "./Base";
import { flatten } from "lodash";
import { DataSourceKeys } from "../../renderer/pages/DataSource/DataSourceStore";
Expand All @@ -15,8 +15,8 @@ export default class BigQuery extends Base {
static get configSchema(): ConfigSchemasType {
return [
{
name: "project",
label: "Project",
name: "projectId",
label: "Project Id",
type: "string",
placeholder: "your-project-id"
},
Expand All @@ -29,11 +29,16 @@ export default class BigQuery extends Base {
];
}

get client(): BigQueryClient {
return new BigQueryClient(this.config);
}

execute(query: string): Promise<any> {
this._cancel = null;
return new Promise((resolve, reject) => {
bigquery(this.config).startQuery(query, (err, job) => {
this.client.createQueryJob(query, (err, job) => {
if (err) return reject(err);
if (!job) return reject("Invalid job");

this._cancel = async (): Promise<void> => {
await job.cancel();
Expand All @@ -42,11 +47,13 @@ export default class BigQuery extends Base {

job.getQueryResults((err, rows) => {
if (err) return reject(err);
if (rows.length === 0) return resolve({});
if (!rows || rows.length === 0) return resolve({});

resolve({
fields: Object.keys(rows[0]),
rows: rows.map(Object.values)
rows: rows.map(row => {
return Object.values(row).map((v: any) => (v != null && v.value !== undefined ? v.value : v));
})
});
});
});
Expand All @@ -58,20 +65,21 @@ export default class BigQuery extends Base {
}

async connectionTest(): Promise<void> {
await bigquery(this.config).query("select 1");
await this.client.query("select 1");
}

async fetchTables(): Promise<{ name: string; type: string; schema?: string }[]> {
const [datasets]: [any[]] = await bigquery(this.config).getDatasets();
const promises = datasets.map<Promise<{ name: string; type: string; schema?: string }>>(async dataset => {
const [datasets] = await this.client.getDatasets();
const promises = datasets.map(async dataset => {
const [tables] = await dataset.getTables();
return tables.map(table => ({
schema: dataset.id,
name: table.id,
schema: dataset.id ?? "",
name: table.id ?? "",
type: table.metadata.type.toLowerCase()
}));
});
const results = await Promise.all(promises);
console.log(results);
return flatten(results);
}

Expand All @@ -82,7 +90,7 @@ export default class BigQuery extends Base {
schema: string;
name: string;
}): Promise<{ name: string; defs: { fields: string[]; rows: (string | null)[][] }; schema?: string }> {
const [metadata] = await bigquery(this.config)
const [metadata] = await this.client
.dataset(schema)
.table(name)
.getMetadata();
Expand Down

0 comments on commit 0b8861a

Please sign in to comment.