Skip to content

Commit

Permalink
filling in some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Oct 21, 2014
1 parent a3709cf commit 9491ce5
Show file tree
Hide file tree
Showing 6 changed files with 577 additions and 162 deletions.
147 changes: 124 additions & 23 deletions lib/bigquery/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,49 @@

'use strict';

var extend = require('extend');

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

/**
* Create a Dataset object.
* @type {module:common/util}
* @private
*/
var util = require('../common/util.js');

/*! Developer Documentation
*
* @param {module:bigquery} bigQuery - The parent BigQuery instance.
* @param {string} datasetId - The id of the Dataset.
*/
/**
* Interact with your BigQuery dataset. Create a Dataset instance with
* {@link module:bigquery#createDataset} or {@link module:bigquery#dataset}.
*
* @alias module:bigquery/dataset
* @constructor
*
* @param {string} datasetId - The id of the Dataset.
*/
function Dataset(datasetId) {
if (!(this instanceof Dataset)) {
return new Dataset(datasetId);
}
function Dataset(bigQuery, datasetId) {
this.bigQuery = bigQuery;
this.id = datasetId;
}

/**
* Create a table given a tableId or configuration object.
*
* @param {string|object} options - Table id or configuration object.
* @param {object} options - Table id or configuration object.
* @param {string} options.id - The id of the table.
* @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".
* Example: "name:string, age:integer". Schemas can also be specified as a
* JSON array of fields, which allows for nested and repeated fields.
* JSON array of fields, which allows for nested and repeated fields. See
* https://cloud.google.com/bigquery/docs/reference/v2/tables#resource for
* more detailed information.
* @param {function} callback - The callback function.
*
* @example
Expand All @@ -69,14 +81,44 @@ function Dataset(datasetId) {
* });
*/
Dataset.prototype.createTable = function(options, callback) {
throw new Error('Not implemented.');
if (util.is(options.schema, 'string')) {
options.schema =
options.schema.split(/\s*,\s*/).reduce(function(acc, pair) {
acc.fields.push({
name: pair.split(':')[0],
type: pair.split(':')[1] || 'string'
});
return acc;
}, { fields: [] });
}
var body = {
schema: options.schema,
tableReference: {
datasetId: this.id,
projectId: this.bigQuery.projectId,
tableId: options.id
}
};
delete options.id;
delete options.schema;
extend(true, body, options);
this.makeReq_('POST', '/tables', null, body, function(err, resp) {
if (err) {
callback(err);
return;
}
var table = this.table(resp.tableReference.tableId);
table.metadata = resp;
callback(null, table);
}.bind(this));
};

/**
* Delete the dataset.
*
* @param {object=} options - The configuration object.
* @param {boolean} options.force - Force delete dataset and all tables.
* (default: false)
* @param {function} callback - The callback function.
*
* @example
Expand All @@ -89,7 +131,12 @@ Dataset.prototype.createTable = function(options, callback) {
* });
*/
Dataset.prototype.delete = function(options, callback) {
throw new Error('Not implemented.');
if (!callback) {
callback = options;
options = {};
}
var query = { deleteContents: !!options.force };
this.makeReq_('DELETE', '', query, null, callback);
};

/**
Expand All @@ -105,24 +152,57 @@ Dataset.prototype.delete = function(options, callback) {
* });
*/
Dataset.prototype.getMetadata = function(callback) {
throw new Error('Not implemented.');
this.makeReq_('GET', '', null, null, function(err, resp) {
if (err) {
callback(err);
return;
}
this.metadata = resp;
callback(null, this.metadata);
}.bind(this));
};

/**
* Get a list of tables.
*
* @param {object=} options - The configuration object.
* @param {object=} query - Configuration object.
* @param {number} query.maxResults - Maximum number of results to return.
* @param {string} query.pageToken - Token returned from a previous call, to
* request the next page of results.
* @param {function} callback - The callback function.
*
* @example
* var myDataset = bigquery.dataset(datasetId);
*
* myDataset.getTables(function(err, tables) {
* // Use the tables.
* myDataset.getTables(function(err, tables, nextQuery) {
* // If `nextQuery` is non-null, there are more results to fetch.
* });
*/
Dataset.prototype.getTables = function(options, callback) {
throw new Error('Not implemented.');
Dataset.prototype.getTables = function(query, callback) {
var that = this;
if (!callback) {
callback = query;
query = {};
}
query = query || {};
this.makeReq_('GET', '/tables', query, null, function(err, resp) {
if (err) {
callback(err);
return;
}
var nextQuery = null;
if (resp.nextPageToken) {
nextQuery = extend({}, query, {
pageToken: resp.nextPageToken
});
}
var tables = (resp.tables || []).map(function(tableObject) {
var table = that.table(tableObject.id);
table.metadata = tableObject;
return table;
});
callback(null, tables, nextQuery);
});
};

/**
Expand All @@ -143,23 +223,44 @@ Dataset.prototype.getTables = function(options, callback) {
* });
*/
Dataset.prototype.setMetadata = function(metadata, callback) {
throw new Error('Not implemented.');
this.makeReq_('PUT', '', null, metadata, function(err, resp) {
if (err) {
callback(err);
return;
}
this.metadata = resp;
callback(null, this.metadata);
}.bind(this));
};

/**
* Return a new instance of reference to an existing Table object.
*
* @param {string} tableId - The ID of the table.
* @return {module:bigquery/table} Reference to existing Table object.
* @return {module:bigquery/table}
*
* @example
* var kittens = myDataset.table('my-kittens');
*/
Dataset.prototype.table = function(tableId) {
return new Table({
dataset: this,
id: tableId
});
return new Table(this, tableId);
};

/**
* 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);
};

module.exports = Dataset;
57 changes: 3 additions & 54 deletions lib/bigquery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,39 +101,20 @@ BigQuery.prototype.createDataset = function(datasetId, callback) {
callback(err);
return;
}
var dataset = this.dataset(datasetId);
var dataset = this.dataset(this, datasetId);
dataset.metadata = resp;
callback(null, dataset);
}.bind(this));
};

/**
* Create a new job.
*
* @param {object} config - The configuration object.
* @param {function} callback - The callback function.
*
* @example
*
* var config = {
*
* };
* bigquery.createJob(config, function(err, job) {
* // Use your newly created job.
* });
*/
BigQuery.prototype.createJob = function(options, callback) {
throw new Error('Not implemented.');
};

/**
* Create a reference to an existing dataset.
*
* @param {string} datasetId - ID of the dataset.
* @return {module:bigquery/dataset}
*/
BigQuery.prototype.dataset = function(datasetId) {
return new Dataset(datasetId);
return new Dataset(this, datasetId);
};

/**
Expand Down Expand Up @@ -173,7 +154,7 @@ BigQuery.prototype.getDatasets = function(query, callback) {
});
}
var datasets = (resp.datasets || []).map(function(dataset) {
var ds = that.dataset(dataset.datasetReference.datasetId);
var ds = that.dataset(that, dataset.datasetReference.datasetId);
ds.metadata = dataset;
return ds;
});
Expand Down Expand Up @@ -249,38 +230,6 @@ BigQuery.prototype.job = function(jobId) {
return new Job(jobId);
};

/**
* Run a query in BigQuery.
*
* var myQuery = {
* query: ‘SELECT * FROM users’,
* dryRun: true,
* maxResults: 123,
* useQueryCache: false,
*
* // Automatically concat pages together up to maxResults limit.
* auto: true
* };
*
* bigquery.query(myQuery, function(err, results) {
* if (err) {
* // An error occurred.
* }
* if (!results.jobCompleted) {
* // No results because job still going...
* } else {
* // Results is just what was returned from BigQuery for now.
* // TODO: Is there a better format that we can return results in?
* }
* });
*
* @param {object} options - The configuration object.
* @param {function} callback - The callback function.
*/
BigQuery.prototype.query = function(options, callback) {
throw new Error('Not implemented.');
};

/**
* Make a new request object from the provided arguments and wrap the callback
* to intercept non-successful responses.
Expand Down
Loading

0 comments on commit 9491ce5

Please sign in to comment.