Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async connectionTest(): Promise {
await bigquery(this.config).query("select 1");
}
return new Promise((resolve, reject) => {
bigquery(this.config).startQuery(query, (err, job) => {
if (err) return reject(err);
this._cancel = async () => {
await job.cancel();
reject(new Error("Query is canceled"));
};
job.getQueryResults((err, rows) => {
if (err) return reject(err);
if (rows.length === 0) return resolve({});
resolve({
fields: Object.keys(rows[0]),
rows: rows.map(Object.values)
});
});
async fetchTableSummary({
schema,
name
}: {
schema: string;
name: string;
}): Promise<{ name: string; defs: { fields: string[]; rows: (string | null)[][] }; schema?: string }> {
const [metadata] = await bigquery(this.config)
.dataset(schema)
.table(name)
.getMetadata();
const schemaFields = metadata.schema.fields;
const defs = {
fields: Object.keys(schemaFields[0]),
rows: schemaFields.map(Object.values)
};
return { schema, name, defs };
}
async fetchTables(): Promise<{ name: string; type: string; schema?: string }[]> {
const [datasets]: [any[]] = await bigquery(this.config).getDatasets();
const promises = datasets.map>(async dataset => {
const [tables] = await dataset.getTables();
return tables.map(table => ({
schema: dataset.id,
name: table.id,
type: table.metadata.type.toLowerCase()
}));
});
const results = await Promise.all(promises);
return flatten(results);
}