Skip to content

core: add support for promises #1142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Google Cloud Node.js Client
> Node.js idiomatic client for [Google Cloud Platform](https://cloud.google.com/) services.
> Node.js idiomatic client for [Google Cloud Platform](https://cloud.google.com/) services, now with more promises!

This comment was marked as spam.


[![NPM Version](https://img.shields.io/npm/v/google-cloud.svg)](https://www.npmjs.org/package/google-cloud)
[![Travis Build Status](https://travis-ci.org/GoogleCloudPlatform/google-cloud-node.svg)](https://travis-ci.org/GoogleCloudPlatform/google-cloud-node/)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"scripts": {
"postinstall": "node ./scripts/install.js",
"link-common": "node ./scripts/link-common.js",
"update-deps": "node ./scripts/update-deps.js",
"docs": "node ./scripts/docs/packages.js",
"bundle": "node ./scripts/docs/bundle.js",
Expand Down
136 changes: 117 additions & 19 deletions packages/bigquery/src/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ function Dataset(bigQuery, id) {
* // The dataset was created successfully.
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* dataset.create().then(function(data) {
* var dataset = data[0];
* var apiResponse = data[1];
* });
*/
create: true,

Expand All @@ -70,6 +78,13 @@ function Dataset(bigQuery, id) {
*
* @example
* dataset.exists(function(err, exists) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* dataset.exists().then(function(data) {
* var exists = data[0];
* });
*/
exists: true,

Expand All @@ -91,6 +106,14 @@ function Dataset(bigQuery, id) {
* // `dataset.metadata` has been populated.
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* dataset.get().then(function(data) {
* var dataset = data[0];
* var apiResponse = data[1];
* });
*/
get: true,

Expand All @@ -107,6 +130,14 @@ function Dataset(bigQuery, id) {
*
* @example
* dataset.getMetadata(function(err, metadata, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* dataset.getMetadata().then(function(data) {
* var metadata = data[0];
* var apiResponse = data[1];
* });
*/
getMetadata: true,

Expand All @@ -127,6 +158,13 @@ function Dataset(bigQuery, id) {
* };
*
* dataset.setMetadata(metadata, function(err, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* dataset.setMetadata(metadata).then(function(data) {
* var apiResponse = data[0];
* });
*/
setMetadata: true
};
Expand All @@ -144,6 +182,28 @@ function Dataset(bigQuery, id) {

util.inherits(Dataset, common.ServiceObject);

/**
* Run a query scoped to your dataset as a readable object stream.
*
* See {module:bigquery#createQueryStream} for full documentation of this
* method.
*/
Dataset.prototype.createQueryStream = function(options) {
if (is.string(options)) {
options = {
query: options
};
}

options = extend(true, {}, options, {
defaultDataset: {
datasetId: this.id
}
});

return this.bigQuery.createQueryStream(options);
};

/**
* Create a table given a tableId or configuration object.
*
Expand Down Expand Up @@ -172,6 +232,14 @@ util.inherits(Dataset, common.ServiceObject);
* };
*
* dataset.createTable(tableId, options, function(err, table, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* dataset.createTable(tableId, options).then(function(data) {
* var table = data[0];
* var apiResponse = data[1];
* });
*/
Dataset.prototype.createTable = function(id, options, callback) {
var self = this;
Expand Down Expand Up @@ -248,6 +316,13 @@ Dataset.prototype.createTable = function(id, options, callback) {
* // Delete the dataset and any tables it contains.
* //-
* dataset.delete({ force: true }, function(err, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* dataset.delete().then(function(data) {
* var apiResponse = data[0];
* });
*/
Dataset.prototype.delete = function(options, callback) {
if (!callback) {
Expand Down Expand Up @@ -290,23 +365,11 @@ Dataset.prototype.delete = function(options, callback) {
* });
*
* //-
* // Get the tables as a readable object stream. `table` is a Table object
* //-
* dataset.getTables()
* .on('error', console.error)
* .on('data', function(table) {})
* .on('end', function() {
* // All tables have been retrieved
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* // If the callback is omitted, we'll return a Promise.
* //-
* dataset.getTables()
* .on('data', function(table) {
* this.end();
* });
* dataset.getTables().then(function(data) {
* var tables = data[0];
* });
*/
Dataset.prototype.getTables = function(query, callback) {
var that = this;
Expand Down Expand Up @@ -344,6 +407,33 @@ Dataset.prototype.getTables = function(query, callback) {
});
};

/**
* List all or some of the {module:bigquery/table} objects in your project as a
* readable object stream.
*
* @param {object=} query - Configuration object. See
* {module:bigquery/dataset#getTables} for a complete list of options.
* @return {stream}
*
* @example
* dataset.getTablesStream()
* .on('error', console.error)
* .on('data', function(table) {})
* .on('end', function() {
* // 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();
* });
*/
Dataset.prototype.getTablesStream = common.paginator.streamify('getTables');

/**
* Run a query scoped to your dataset.
*
Expand Down Expand Up @@ -380,9 +470,17 @@ Dataset.prototype.table = function(id) {

/*! 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.
* These methods can be auto-paginated.
*/
common.paginator.extend(Dataset, ['getTables']);

/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.streamRouter.extend(Dataset, ['getTables']);
common.util.promisifyAll(Dataset, {
exclude: ['table']
});

module.exports = Dataset;
Loading