Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
c92f9c6
implement Service and ServiceObject in BigQuery
stephenplusplus Sep 29, 2015
edb8f7e
overhaul Compute Engine
stephenplusplus Oct 1, 2015
b5c7d40
use new inheritance style
stephenplusplus Oct 22, 2015
2b786d1
remove getorcreate
stephenplusplus Oct 23, 2015
55c63fd
add exists()
stephenplusplus Oct 23, 2015
4700d80
dns
stephenplusplus Oct 23, 2015
cb00b8a
pubsub
stephenplusplus Oct 24, 2015
b7d2e61
catching up on docs
stephenplusplus Oct 26, 2015
d34ae7a
implement resource
stephenplusplus Oct 26, 2015
e116084
search
stephenplusplus Oct 26, 2015
bf6924e
storage
stephenplusplus Oct 26, 2015
337c70f
fix storage test
stephenplusplus Oct 28, 2015
8f2d169
test dataset
stephenplusplus Oct 29, 2015
faf158f
test job
stephenplusplus Oct 29, 2015
f028d39
bigquery.table
stephenplusplus Oct 29, 2015
b8167a8
bigquery.index
stephenplusplus Oct 29, 2015
1851e46
compute.address
stephenplusplus Oct 30, 2015
b7846f6
compute.disk
stephenplusplus Oct 30, 2015
345548f
compute.firewall
stephenplusplus Oct 30, 2015
1567c6e
compute.network
stephenplusplus Oct 30, 2015
63a51df
compute.operation
stephenplusplus Oct 30, 2015
cf4e0b0
compute.region
stephenplusplus Oct 30, 2015
90b6915
compute.snapshot
stephenplusplus Oct 30, 2015
47840aa
compute.vm
stephenplusplus Oct 30, 2015
d3c9e1b
compute.zone
stephenplusplus Oct 30, 2015
9d60b92
compute.index
stephenplusplus Oct 30, 2015
38ca0c6
add missing instanceof tests
stephenplusplus Oct 30, 2015
8a2ebd9
fix test for not working diskAttach
stephenplusplus Oct 30, 2015
801b7b3
dns.change
stephenplusplus Oct 30, 2015
34d570f
dns.index
stephenplusplus Oct 30, 2015
eb8e76d
dns.zone
stephenplusplus Oct 30, 2015
f2f532b
pubsub.iam
stephenplusplus Oct 30, 2015
f324f91
pubsub.subscription
stephenplusplus Oct 30, 2015
40504ed
pubsub.topic
stephenplusplus Nov 2, 2015
4f285ff
pubsub.index
stephenplusplus Nov 2, 2015
f05bfc4
resource.index
stephenplusplus Nov 2, 2015
856248b
resource.project
stephenplusplus Nov 2, 2015
72d6c39
search.document
stephenplusplus Nov 2, 2015
f821717
search.index-class
stephenplusplus Nov 2, 2015
92c7c8f
search.index
stephenplusplus Nov 2, 2015
f96590a
storage.acl
stephenplusplus Nov 2, 2015
3d8bdb6
storage.bucket
stephenplusplus Nov 2, 2015
1b87f65
storage.file
stephenplusplus Nov 3, 2015
f12965f
storage.index
stephenplusplus Nov 3, 2015
85331d2
common.serviceobject
stephenplusplus Nov 3, 2015
4f0d3b1
common.service
stephenplusplus Nov 5, 2015
70151fb
fix examples
stephenplusplus Nov 5, 2015
fe2a9a5
docs + change.create
stephenplusplus Nov 5, 2015
01ce610
doc clean up
stephenplusplus Nov 5, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 139 additions & 99 deletions lib/bigquery/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,26 @@

var extend = require('extend');
var is = require('is');
var nodeutil = require('util');

/**
* @type {module:bigquery/table}
* @type {module:common/serviceObject}
* @private
*/
var Table = require('./table.js');
var ServiceObject = require('../common/service-object.js');

/**
* @type {module:common/streamrouter}
* @private
*/
var streamRouter = require('../common/stream-router.js');

/**
* @type {module:bigquery/table}
* @private
*/
var Table = require('./table.js');

/*! Developer Documentation
*
* @param {module:bigquery} bigQuery - BigQuery instance.
Expand All @@ -46,20 +53,121 @@ var streamRouter = require('../common/stream-router.js');
*
* @alias module:bigquery/dataset
* @constructor
*
* @example
* var gcloud = require('gcloud');
*
* var bigquery = gcloud.bigquery({
* keyFilename: '/path/to/keyfile.json',
* projectId: 'grape-spaceship-123'
* });
* var dataset = bigquery.dataset('institutions');
*/
function Dataset(bigQuery, id) {
var methods = {
/**
* Create a dataset.
*
* @example
* dataset.create(function(err, dataset, apiResponse) {
* if (!err) {
* // The dataset was created successfully.
* }
* });
*/
create: true,

/**
* Check if the dataset exists.
*
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {boolean} callback.exists - Whether the dataset exists or not.
*
* @example
* dataset.exists(function(err, exists) {});
*/
exists: true,

/**
* 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.
*
* @param {options=} options - Configuration object.
* @param {boolean} options.autoCreate - Automatically create the object if
* it does not exist. Default: `false`
*
* @example
* dataset.get(function(err, dataset, apiResponse) {
* if (!err) {
* // `dataset.metadata` has been populated.
* }
* });
*/
get: true,

/**
* Get the metadata for the Dataset.
*
* @resource [Datasets: get API Documentation]{@link https://cloud.google.com/bigquery/docs/reference/v2/datasets/get}
*
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {object} callback.metadata - The dataset's metadata.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* dataset.getMetadata(function(err, metadata, apiResponse) {});
*/
getMetadata: true,

/**
* Sets the metadata of the Dataset object.
*
* @resource [Datasets: patch API Documentation]{@link https://cloud.google.com/bigquery/docs/reference/v2/datasets/patch}
*
* @param {object} metadata - Metadata to save on the Dataset.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* var metadata = {
* description: 'Info for every institution in the 2013 IPEDS universe'
* };
*
* dataset.setMetadata(metadata, function(err, apiResponse) {});
*/
setMetadata: true
};

ServiceObject.call(this, {
parent: bigQuery,
baseUrl: '/datasets',
id: id,
createMethod: bigQuery.createDataset.bind(bigQuery),
methods: methods
});

this.bigQuery = bigQuery;
this.id = id;
this.metadata = {};
}

nodeutil.inherits(Dataset, ServiceObject);

/**
* Create a table given a tableId or configuration object.
*
* @resource [Tables: insert API Documentation]{@link https://cloud.google.com/bigquery/docs/reference/v2/tables/insert}
*
* @param {object} options - Table id or configuration object.
* @param {string} options.id - The id of the table.
* @param {string} id - Table id.
* @param {object=} options - Configuration object.
* @param {string|object} options.schema - 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".
Expand All @@ -79,37 +187,39 @@ function Dataset(bigQuery, id) {
* schema: 'UNITID,INSTNM,ADDR,CITY,STABBR,ZIP,FIPS,OBEREG,CHFNM,...'
* };
*
* var bigquery = gcloud.bigquery({
* projectId: 'grape-spaceship-123'
* });
* var dataset = bigquery.dataset();
*
* dataset.createTable(tableConfig, function(err, table, apiResponse) {});
*/
Dataset.prototype.createTable = function(options, callback) {
var that = this;
Dataset.prototype.createTable = function(id, options, callback) {
var self = this;

if (is.fn(options)) {
callback = options;
options = {};
}

extend(true, options, {
tableReference: {
datasetId: this.id,
projectId: this.bigQuery.projectId,
tableId: options.id
tableId: id
}
});

if (is.string(options.schema)) {
options.schema = Table.createSchemaFromString_(options.schema);
}

delete options.id;

this.makeReq_('POST', '/tables', null, options, function(err, resp) {
this.request({
method: 'POST',
uri: '/tables',
json: options
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

var table = that.table(resp.tableReference.tableId);
var table = self.table(resp.tableReference.tableId);
table.metadata = resp;

callback(null, table, resp);
Expand Down Expand Up @@ -149,34 +259,11 @@ Dataset.prototype.delete = function(options, callback) {
deleteContents: !!options.force
};

this.makeReq_('DELETE', '', query, null, callback);
};

/**
* Get the metadata for the Dataset.
*
* @resource [Datasets: get API Documentation]{@link https://cloud.google.com/bigquery/docs/reference/v2/datasets/get}
*
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request
* @param {object} callback.metadata - The dataset's metadata.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* dataset.getMetadata(function(err, metadata, apiResponse) {});
*/
Dataset.prototype.getMetadata = function(callback) {
var that = this;
this.makeReq_('GET', '', null, null, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

that.metadata = resp;

callback(null, that.metadata, resp);
});
this.request({
method: 'DELETE',
uri: '',

This comment was marked as spam.

This comment was marked as spam.

qs: query
}, callback);
};

/**
Expand Down Expand Up @@ -231,14 +318,16 @@ Dataset.prototype.getTables = function(query, callback) {

query = query || {};

this.makeReq_('GET', '/tables', query, null, function(err, resp) {
this.request({
uri: '/tables',
qs: query
}, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}

var nextQuery = null;

if (resp.nextPageToken) {
nextQuery = extend({}, query, {
pageToken: resp.nextPageToken
Expand Down Expand Up @@ -277,39 +366,7 @@ Dataset.prototype.query = function(options, callback) {
};

/**
* Sets the metadata of the Dataset object.
*
* @resource [Datasets: patch API Documentation]{@link https://cloud.google.com/bigquery/docs/reference/v2/datasets/patch}
*
* @param {object} metadata - Metadata to save on the Dataset.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request
* @param {object} callback.apiResponse - The full API response.
*
* @example
* var metadata = {
* description: 'Information for every institution in the 2013 IPEDS universe'
* };
*
* dataset.setMetadata(metadata, function(err, apiResponse) {});
*/
Dataset.prototype.setMetadata = function(metadata, callback) {
var that = this;

this.makeReq_('PATCH', '', null, metadata, function(err, resp) {
if (err) {
callback(err, resp);
return;
}

that.metadata = resp;

callback(null, resp);
});
};

/**
* Return a new instance of reference to an existing Table object.
* Create a Table object.
*
* @param {string} id - The ID of the table.
* @return {module:bigquery/table}
Expand All @@ -321,28 +378,11 @@ Dataset.prototype.table = function(id) {
return new Table(this, id);
};

/**
* Pass through this request to BigQuery's request handler, first prepending the
* path with the dataset.
*
* @private
*
* @param {string} method - Action.
* @param {string} path - Request path.
* @param {*} query - Request query object.
* @param {*} body - Request body contents.
* @param {function} callback - The callback function.
*/
Dataset.prototype.makeReq_ = function(method, path, query, body, callback) {
path = '/datasets/' + this.id + path;
this.bigQuery.makeReq_(method, path, query, body, callback);
};

/*! Developer Documentation
*
* These methods can be used with either a callback or as a readable object
* stream. `streamRouter` is used to add this dual behavior.
*/
streamRouter.extend(Dataset, 'getTables');
streamRouter.extend(Dataset, ['getTables']);

module.exports = Dataset;
Loading