Skip to content
This repository was archived by the owner on Feb 7, 2026. It is now read-only.

Commit 70d2899

Browse files
steffnayyoshi-automation
authored andcommitted
refactor(samples): add main() function wrappers to samples
refactor(samples): add main() function wrappers to samples - [x] Tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) #401 automerged by dpebot
1 parent 1459787 commit 70d2899

27 files changed

Lines changed: 703 additions & 638 deletions

samples/browseRows.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616

1717
'use strict';
1818

19-
async function browseRows(datasetId, tableId) {
20-
// Displays rows from "my_table" in "my_dataset".
21-
19+
function main(datasetId, tableId) {
2220
// [START bigquery_browse_table]
21+
2322
// Import the Google Cloud client library
2423
const {BigQuery} = require('@google-cloud/bigquery');
2524

@@ -29,18 +28,24 @@ async function browseRows(datasetId, tableId) {
2928
// const datasetId = "my_dataset";
3029
// const tableId = "my_table";
3130

32-
// Create a client
33-
const bigquery = new BigQuery();
31+
async function browseRows() {
32+
// Displays rows from "my_table" in "my_dataset".
33+
34+
// Create a client
35+
const bigqueryClient = new BigQuery();
36+
37+
// List rows in the table
38+
const [rows] = await bigqueryClient
39+
.dataset(datasetId)
40+
.table(tableId)
41+
.getRows();
3442

35-
// List rows in the table
36-
const [rows] = await bigquery
37-
.dataset(datasetId)
38-
.table(tableId)
39-
.getRows();
43+
console.log('Rows:');
44+
rows.forEach(row => console.log(row));
45+
}
4046

41-
console.log('Rows:');
42-
rows.forEach(row => console.log(row));
47+
browseRows();
4348
// [END bigquery_browse_table]
4449
}
4550

46-
browseRows(...process.argv.slice(2)).catch(console.error);
51+
main(...process.argv.slice(2));

samples/copyTable.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
'use strict';
1818

19-
async function copyTable(srcDatasetId, srcTableId, destDatasetId, destTableId) {
20-
// Copies src_dataset:src_table to dest_dataset:dest_table.
21-
19+
function main(srcDatasetId, srcTableId, destDatasetId, destTableId) {
2220
// [START bigquery_copy_table]
2321
// Import the Google Cloud client library
2422
const {BigQuery} = require('@google-cloud/bigquery');
@@ -32,22 +30,27 @@ async function copyTable(srcDatasetId, srcTableId, destDatasetId, destTableId) {
3230
// const destTableId = "my_dest_table";
3331

3432
// Create a client
35-
const bigquery = new BigQuery();
33+
const bigqueryClient = new BigQuery();
34+
35+
async function copyTable() {
36+
// Copies src_dataset:src_table to dest_dataset:dest_table.
3637

37-
// Copy the table contents into another table
38-
const [job] = await bigquery
39-
.dataset(srcDatasetId)
40-
.table(srcTableId)
41-
.copy(bigquery.dataset(destDatasetId).table(destTableId));
38+
// Copy the table contents into another table
39+
const [job] = await bigqueryClient
40+
.dataset(srcDatasetId)
41+
.table(srcTableId)
42+
.copy(bigqueryClient.dataset(destDatasetId).table(destTableId));
4243

43-
console.log(`Job ${job.id} completed.`);
44+
console.log(`Job ${job.id} completed.`);
4445

45-
// Check the job's status for errors
46-
const errors = job.status.errors;
47-
if (errors && errors.length > 0) {
48-
throw errors;
46+
// Check the job's status for errors
47+
const errors = job.status.errors;
48+
if (errors && errors.length > 0) {
49+
throw errors;
50+
}
4951
}
52+
53+
copyTable();
5054
// [END bigquery_copy_table]
5155
}
52-
53-
copyTable(...process.argv.slice(2)).catch(console.error);
56+
main(...process.argv.slice(2));

samples/createDataset.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,34 @@
1616

1717
'use strict';
1818

19-
async function createDataset(datasetId) {
20-
// Creates a new dataset named "my_dataset".
21-
19+
function main(datasetId) {
2220
// [START bigquery_create_dataset]
2321
// Import the Google Cloud client library
2422
const {BigQuery} = require('@google-cloud/bigquery');
2523

26-
/**
27-
* TODO(developer): Uncomment the following lines before running the sample.
28-
*/
29-
// const datasetId = "my_new_dataset";
24+
async function createDataset() {
25+
// Creates a new dataset named "my_dataset".
26+
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// const datasetId = "my_new_dataset";
31+
32+
// Create a client
33+
const bigqueryClient = new BigQuery();
3034

31-
// Create a client
32-
const bigquery = new BigQuery();
35+
// Specify the geographic location where the dataset should reside
36+
const options = {
37+
location: 'US',
38+
};
3339

34-
// Specify the geographic location where the dataset should reside
35-
const options = {
36-
location: 'US',
37-
};
40+
// Create a new dataset
41+
const [dataset] = await bigqueryClient.createDataset(datasetId, options);
42+
console.log(`Dataset ${dataset.id} created.`);
43+
}
3844

39-
// Create a new dataset
40-
const [dataset] = await bigquery.createDataset(datasetId, options);
41-
console.log(`Dataset ${dataset.id} created.`);
45+
createDataset();
4246
// [END bigquery_create_dataset]
4347
}
4448

45-
createDataset(...process.argv.slice(2)).catch(console.error);
49+
main(...process.argv.slice(2));

samples/createTable.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,38 @@
1616

1717
'use strict';
1818

19-
async function createTable(datasetId, tableId, schema) {
20-
// Creates a new table named "my_table" in "my_dataset".
21-
19+
function main(datasetId, tableId, schema) {
2220
// [START bigquery_create_table]
2321
// Import the Google Cloud client library
2422
const {BigQuery} = require('@google-cloud/bigquery');
2523

26-
/**
27-
* TODO(developer): Uncomment the following lines before running the sample
28-
*/
29-
// const datasetId = "my_new_dataset";
30-
// const tableId = "my_new_table";
31-
// const schema = "Name:string, Age:integer, Weight:float, IsMagic:boolean";
32-
33-
// Create a client
34-
const bigquery = new BigQuery();
35-
36-
// For all options, see https://cloud.google.com/bigquery/docs/reference/v2/tables#resource
37-
const options = {
38-
schema: schema,
39-
location: 'US',
40-
};
41-
42-
// Create a new table in the dataset
43-
const [table] = await bigquery
44-
.dataset(datasetId)
45-
.createTable(tableId, options);
46-
47-
console.log(`Table ${table.id} created.`);
24+
async function createTable() {
25+
// Creates a new table named "my_table" in "my_dataset".
26+
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample
29+
*/
30+
// const datasetId = "my_new_dataset";
31+
// const tableId = "my_new_table";
32+
// const schema = "Name:string, Age:integer, Weight:float, IsMagic:boolean";
33+
34+
// Create a client
35+
const bigqueryClient = new BigQuery();
36+
37+
// For all options, see https://cloud.google.com/bigquery/docs/reference/v2/tables#resource
38+
const options = {
39+
schema: schema,
40+
location: 'US',
41+
};
42+
43+
// Create a new table in the dataset
44+
const [table] = await bigqueryClient
45+
.dataset(datasetId)
46+
.createTable(tableId, options);
47+
48+
console.log(`Table ${table.id} created.`);
49+
}
50+
createTable();
4851
// [END bigquery_create_table]
4952
}
50-
51-
createTable(...process.argv.slice(2)).catch(console.error);
53+
main(...process.argv.slice(2));

samples/deleteDataset.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,31 @@
1616

1717
'use strict';
1818

19-
async function deleteDataset(datasetId) {
20-
// Deletes a dataset named "my_dataset".
21-
19+
function main(datasetId) {
2220
// [START bigquery_delete_dataset]
2321
// Import the Google Cloud client library
2422
const {BigQuery} = require('@google-cloud/bigquery');
2523

26-
/**
27-
* TODO(developer): Uncomment the following lines before running the sample.
28-
*/
29-
// const datasetId = "my_new_dataset";
24+
async function deleteDataset() {
25+
// Deletes a dataset named "my_dataset".
26+
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// const datasetId = "my_new_dataset";
3031

31-
// Create a client
32-
const bigquery = new BigQuery();
32+
// Create a client
33+
const bigqueryClient = new BigQuery();
3334

34-
// Create a reference to the existing dataset
35-
const dataset = bigquery.dataset(datasetId);
35+
// Create a reference to the existing dataset
36+
const dataset = bigqueryClient.dataset(datasetId);
3637

37-
// Delete the dataset and its contents
38-
await dataset.delete({force: true});
39-
console.log(`Dataset ${dataset.id} deleted.`);
38+
// Delete the dataset and its contents
39+
await dataset.delete({force: true});
40+
console.log(`Dataset ${dataset.id} deleted.`);
41+
}
42+
deleteDataset();
4043
// [END bigquery_delete_dataset]
4144
}
4245

43-
deleteDataset(...process.argv.slice(2)).catch(console.error);
46+
main(...process.argv.slice(2));

samples/deleteTable.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,33 @@
1616

1717
'use strict';
1818

19-
async function deleteTable(datasetId, tableId) {
20-
// Deletes "my_table" from "my_dataset".
21-
19+
function main(datasetId, tableId) {
2220
// [START bigquery_delete_table]
2321
// Import the Google Cloud client library
2422
const {BigQuery} = require('@google-cloud/bigquery');
2523

26-
/**
27-
* TODO(developer): Uncomment the following lines before running the sample.
28-
*/
29-
// const datasetId = "my_dataset";
30-
// const tableId = "my_table";
24+
async function deleteTable() {
25+
// Deletes "my_table" from "my_dataset".
26+
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// const datasetId = "my_dataset";
31+
// const tableId = "my_table";
3132

32-
// Create a client
33-
const bigquery = new BigQuery();
33+
// Create a client
34+
const bigqueryClient = new BigQuery();
3435

35-
// Delete the table
36-
await bigquery
37-
.dataset(datasetId)
38-
.table(tableId)
39-
.delete();
36+
// Delete the table
37+
await bigqueryClient
38+
.dataset(datasetId)
39+
.table(tableId)
40+
.delete();
4041

41-
console.log(`Table ${tableId} deleted.`);
42+
console.log(`Table ${tableId} deleted.`);
43+
}
44+
deleteTable();
4245
// [END bigquery_delete_table]
4346
}
4447

45-
deleteTable(...process.argv.slice(2)).catch(console.error);
48+
main(...process.argv.slice(2));

samples/extractTableToGCS.js

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,47 @@
1616

1717
'use strict';
1818

19-
async function extractTableToGCS(datasetId, tableId, bucketName, filename) {
20-
// Exports my_dataset:my_table to gcs://my-bucket/my-file as raw CSV.
21-
19+
function main(datasetId, tableId, bucketName, filename) {
2220
// [START bigquery_extract_table]
2321
// Import the Google Cloud client libraries
2422
const {BigQuery} = require('@google-cloud/bigquery');
2523
const {Storage} = require('@google-cloud/storage');
2624

27-
/**
28-
* TODO(developer): Uncomment the following lines before running the sample.
29-
*/
30-
// const datasetId = "my_dataset";
31-
// const tableId = "my_table";
32-
// const bucketName = "my-bucket";
33-
// const filename = "file.csv";
34-
35-
// Instantiate clients
36-
const bigquery = new BigQuery();
37-
const storage = new Storage();
38-
39-
// Location must match that of the source table.
40-
const options = {
41-
location: 'US',
42-
};
43-
44-
// Export data from the table into a Google Cloud Storage file
45-
const [job] = await bigquery
46-
.dataset(datasetId)
47-
.table(tableId)
48-
.extract(storage.bucket(bucketName).file(filename), options);
49-
// load() waits for the job to finish
50-
console.log(`Job ${job.id} completed.`);
51-
52-
// Check the job's status for errors
53-
const errors = job.status.errors;
54-
if (errors && errors.length > 0) {
55-
throw errors;
25+
async function extractTableToGCS() {
26+
// Exports my_dataset:my_table to gcs://my-bucket/my-file as raw CSV.
27+
28+
/**
29+
* TODO(developer): Uncomment the following lines before running the sample.
30+
*/
31+
// const datasetId = "my_dataset";
32+
// const tableId = "my_table";
33+
// const bucketName = "my-bucket";
34+
// const filename = "file.csv";
35+
36+
// Instantiate clients
37+
const bigqueryClient = new BigQuery();
38+
const storageClient = new Storage();
39+
40+
// Location must match that of the source table.
41+
const options = {
42+
location: 'US',
43+
};
44+
45+
// Export data from the table into a Google Cloud Storage file
46+
const [job] = await bigqueryClient
47+
.dataset(datasetId)
48+
.table(tableId)
49+
.extract(storageClient.bucket(bucketName).file(filename), options);
50+
// load() waits for the job to finish
51+
console.log(`Job ${job.id} completed.`);
52+
53+
// Check the job's status for errors
54+
const errors = job.status.errors;
55+
if (errors && errors.length > 0) {
56+
throw errors;
57+
}
5658
}
59+
extractTableToGCS();
5760
// [END bigquery_extract_table]
5861
}
59-
60-
extractTableToGCS(...process.argv.slice(2)).catch(console.error);
62+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)