new Dataset(bigQuery, id[, options])

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

Parameters

Name Type Optional Description

bigQuery

 

 

BigQuery instance.

id

 

 

The ID of the Dataset.

options

 

Yes

Dataset options.

Values in options have the following properties:

Name Type Optional Description

location

 

Yes

The geographic location of the dataset. Defaults to US.

Properties

getModelsStream

List all or some of the {module:bigquery/model} objects in your project as a readable object stream.

Examples

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

dataset.getModelsStream()
  .on('error', console.error)
  .on('data', (model) => {})
  .on('end', () => {
    // All models have been retrieved
  });
<caption>If you anticipate many results, you can end a stream
early to prevent unnecessary processing and API requests.</caption>
dataset.getModelsStream()
  .on('data', function(model) {
    this.end();
  });

Parameter

Name Type Optional Description

options

object

Yes

Configuration object. See Dataset#getModels for a complete list of options.

Returns

stream 

getTablesStream

List all or some of the {module:bigquery/table} objects in your project as a readable object stream.

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

dataset.getTablesStream()
  .on('error', console.error)
  .on('data', (table) => {})
  .on('end', () => {
    // All tables have been retrieved
  });

//-
// If you anticipate many results, you can end a stream early to prevent
// unnecessary processing and API requests.
//-
dataset.getTablesStream()
  .on('data', function(table) {
    this.end();
  });

Parameter

Name Type Optional Description

options

object

Yes

Configuration object. See Dataset#getTables for a complete list of options.

Returns

stream 

Methods

create([callback]) → Promise

Create a dataset.

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');
dataset.create((err, dataset, apiResponse) => {
  if (!err) {
    // The dataset was created successfully.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.create().then((data) => {
  const dataset = data[0];
  const apiResponse = data[1];
});

Parameters

Name Type Optional Description

callback

function()

Yes

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request.

Value can be null.

dataset

Dataset

 

The created dataset.

apiResponse

object

 

The full API response.

Returns

Promise 

createQueryJob(options[, callback]) → Promise

Run a query as a job. No results are immediately returned. Instead, your callback will be executed with a Job object that you must ping for the results. See the Job documentation for explanations of how to check on the status of the job.

See BigQuery#createQueryJob for full documentation of this method.

Parameters

Name Type Optional Description

options

object

 

See BigQuery#createQueryJob for full documentation of this method.

callback

function()

Yes

See BigQuery#createQueryJob for full documentation of this method.

Returns

Promise 

See BigQuery#createQueryJob for full documentation of this method.

createQueryStream(options) → stream

Run a query scoped to your dataset as a readable object stream.

See BigQuery#createQueryStream for full documentation of this method.

Parameter

Name Type Optional Description

options

object

 

See BigQuery#createQueryStream for full documentation of this method.

Returns

stream 

createTable(id[, options][, callback]) → Promise

Create a table given a tableId or configuration object.

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

const tableId = 'institution_data';

const options = {
  // From the data.gov CSV dataset (http://goo.gl/kSE7z6):
  schema: 'UNITID,INSTNM,ADDR,CITY,STABBR,ZIP,FIPS,OBEREG,CHFNM,...'
};

dataset.createTable(tableId, options, (err, table, apiResponse) => {});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.createTable(tableId, options).then((data) => {
  const table = data[0];
  const apiResponse = data[1];
});

Parameters

Name Type Optional Description

id

string

 

Table id.

options

object

Yes

See a Table resource.

Values in options have the following properties:

Name Type Optional Description

schema

(string or object)

Yes

A comma-separated list of name:type pairs. Valid types are "string", "integer", "float", "boolean", and "timestamp". If the type is omitted, it is assumed to be "string". Example: "name:string, age:integer". Schemas can also be specified as a JSON array of fields, which allows for nested and repeated fields. See a Table resource for more detailed information.

callback

function()

Yes

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request

Value can be null.

table

Table

 

The newly created table.

apiResponse

object

 

The full API response.

See also

Tables: insert API Documentation

Returns

Promise 

delete([options][, callback]) → Promise

Delete the dataset.

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

//-
// Delete the dataset, only if it does not have any tables.
//-
dataset.delete((err, apiResponse) => {});

//-
// Delete the dataset and any tables it contains.
//-
dataset.delete({ force: true }, (err, apiResponse) => {});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.delete().then((data) => {
  const apiResponse = data[0];
});

Parameters

Name Type Optional Description

options

object

Yes

The configuration object.

Values in options have the following properties:

Name Type Optional Description

force

boolean

Yes

Force delete dataset and all tables.

Defaults to false.

callback

function()

Yes

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request

Value can be null.

apiResponse

object

 

The full API response.

See also

Datasets: delete API Documentation

Returns

Promise 

exists([callback]) → Promise

Check if the dataset exists.

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');
dataset.exists((err, exists) => {});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.exists().then((data) => {
  const exists = data[0];
});

Parameters

Name Type Optional Description

callback

function()

Yes

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request.

Value can be null.

exists

boolean

 

Whether the dataset exists or not.

Returns

Promise 

get([options][, callback]) → Promise

Get a dataset if it exists.

You may optionally use this to "get or create" an object by providing an object with autoCreate set to true. Any extra configuration that is normally required for the create method must be contained within this object as well.

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');
dataset.get((err, dataset, apiResponse) => {
  if (!err) {
    // `dataset.metadata` has been populated.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.get().then((data) => {
  const dataset = data[0];
  const apiResponse = data[1];
});

Parameters

Name Type Optional Description

options

options

Yes

Configuration object.

Values in options have the following properties:

Name Type Optional Description

autoCreate

boolean

Yes

Automatically create the object if it does not exist.

Defaults to false.

callback

function()

Yes

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request.

Value can be null.

dataset

Dataset

 

The dataset.

Returns

Promise 

getMetadata([callback]) → Promise

Get the metadata for the Dataset.

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');
dataset.getMetadata((err, metadata, apiResponse) => {});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.getMetadata().then((data) => {
  const metadata = data[0];
  const apiResponse = data[1];
});

Parameters

Name Type Optional Description

callback

function()

Yes

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request.

Value can be null.

metadata

object

 

The dataset's metadata.

apiResponse

object

 

The full API response.

See also

Datasets: get API Documentation

Returns

Promise 

getModels([options][, callback]) → Promise

Get a list of models.

Examples

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

dataset.getModels((err, models) => {
  // models is an array of `Model` objects.
});
<caption>To control how many API requests are made and page
through the results manually, set `autoPaginate` to `false`.</caption>
function manualPaginationCallback(err, models, nextQuery, apiResponse) {
  if (nextQuery) {
    // More results exist.
    dataset.getModels(nextQuery, manualPaginationCallback);
  }
}

dataset.getModels({
  autoPaginate: false
}, manualPaginationCallback);
<caption>If the callback is omitted, we'll return a Promise.
</caption>
dataset.getModels().then((data) => {
  const models = data[0];
});

Parameters

Name Type Optional Description

options

object

Yes

Configuration object.

Values in options have the following properties:

Name Type Optional Description

autoPaginate

boolean

Yes

Have pagination handled automatically.

Defaults to true.

maxApiCalls

number

Yes

Maximum number of API calls to make.

maxResults

number

Yes

Maximum number of results to return.

pageToken

string

Yes

Token returned from a previous call, to request the next page of results.

callback

function()

Yes

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request

Value can be null.

models

Array of Model

 

The list of models from your Dataset.

See also

Models: list API Documentation

Returns

Promise 

getTables([options][, callback]) → Promise

Get a list of tables.

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

dataset.getTables((err, tables) => {
  // tables is an array of `Table` objects.
});

//-
// To control how many API requests are made and page through the results
// manually, set `autoPaginate` to `false`.
//-
function manualPaginationCallback(err, tables, nextQuery, apiResponse) {
  if (nextQuery) {
    // More results exist.
    dataset.getTables(nextQuery, manualPaginationCallback);
  }
}

dataset.getTables({
  autoPaginate: false
}, manualPaginationCallback);

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.getTables().then((data) => {
  const tables = data[0];
});

Parameters

Name Type Optional Description

options

object

Yes

Configuration object.

Values in options have the following properties:

Name Type Optional Description

autoPaginate

boolean

Yes

Have pagination handled automatically.

Defaults to true.

maxApiCalls

number

Yes

Maximum number of API calls to make.

maxResults

number

Yes

Maximum number of results to return.

pageToken

string

Yes

Token returned from a previous call, to request the next page of results.

callback

function()

Yes

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request

Value can be null.

tables

Array of Table

 

The list of tables from your Dataset.

See also

Tables: list API Documentation

Returns

Promise 

model(id) → Model

Create a Model object.

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

const model = dataset.model('my-model');

Parameter

Name Type Optional Description

id

string

 

The ID of the model.

Throws

TypeError 

if model ID is missing.

Returns

Model 

setMetadata(metadata[, callback]) → Promise

Sets the metadata of the Dataset object.

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

const metadata = {
  description: 'Info for every institution in the 2013 IPEDS universe'
};

dataset.setMetadata(metadata, (err, apiResponse) => {});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.setMetadata(metadata).then((data) => {
  const apiResponse = data[0];
});

Parameters

Name Type Optional Description

metadata

object

 

Metadata to save on the Dataset.

callback

function()

Yes

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request.

Value can be null.

apiResponse

object

 

The full API response.

See also

Datasets: patch API Documentation

Returns

Promise 

table(id[, options]) → Table

Create a Table object.

Example

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

const institutions = dataset.table('institution_data');

Parameters

Name Type Optional Description

id

string

 

The ID of the table.

options

object

Yes

Table options.

Values in options have the following properties:

Name Type Optional Description

location

string

Yes

The geographic location of the table, by default this value is inherited from the dataset. This can be used to configure the location of all jobs created through a table instance. It cannot be used to set the actual location of the table. This value will be superseded by any API responses containing location data for the table.

Throws

TypeError 

if table ID is missing.

Returns

Table