Skip to content

Commit ba75399

Browse files
callmehiphopstephenplusplus
authored andcommitted
core: created streaming methods (#1665)
1 parent b6b018e commit ba75399

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+3371
-2191
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
},
3232
"scripts": {
3333
"postinstall": "node ./scripts/install.js",
34+
"link-common": "node ./scripts/link-common.js",
3435
"update-deps": "node ./scripts/update-deps.js",
3536
"docs": "node ./scripts/docs/packages.js",
3637
"bundle": "node ./scripts/docs/bundle.js",

packages/bigquery/src/dataset.js

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,28 @@ function Dataset(bigQuery, id) {
144144

145145
util.inherits(Dataset, common.ServiceObject);
146146

147+
/**
148+
* Run a query scoped to your dataset as a readable object stream.
149+
*
150+
* See {module:bigquery#createQueryStream} for full documentation of this
151+
* method.
152+
*/
153+
Dataset.prototype.createQueryStream = function(options) {
154+
if (is.string(options)) {
155+
options = {
156+
query: options
157+
};
158+
}
159+
160+
options = extend(true, {}, options, {
161+
defaultDataset: {
162+
datasetId: this.id
163+
}
164+
});
165+
166+
return this.bigQuery.createQueryStream(options);
167+
};
168+
147169
/**
148170
* Create a table given a tableId or configuration object.
149171
*
@@ -288,25 +310,6 @@ Dataset.prototype.delete = function(options, callback) {
288310
* dataset.getTables(function(err, tables, nextQuery, apiResponse) {
289311
* // If `nextQuery` is non-null, there are more results to fetch.
290312
* });
291-
*
292-
* //-
293-
* // Get the tables as a readable object stream. `table` is a Table object
294-
* //-
295-
* dataset.getTables()
296-
* .on('error', console.error)
297-
* .on('data', function(table) {})
298-
* .on('end', function() {
299-
* // All tables have been retrieved
300-
* });
301-
*
302-
* //-
303-
* // If you anticipate many results, you can end a stream early to prevent
304-
* // unnecessary processing and API requests.
305-
* //-
306-
* dataset.getTables()
307-
* .on('data', function(table) {
308-
* this.end();
309-
* });
310313
*/
311314
Dataset.prototype.getTables = function(query, callback) {
312315
var that = this;
@@ -344,6 +347,33 @@ Dataset.prototype.getTables = function(query, callback) {
344347
});
345348
};
346349

350+
/**
351+
* List all or some of the {module:bigquery/table} objects in your project as a
352+
* readable object stream.
353+
*
354+
* @param {object=} query - Configuration object. See
355+
* {module:bigquery/dataset#getTables} for a complete list of options.
356+
* @return {stream}
357+
*
358+
* @example
359+
* dataset.getTablesStream()
360+
* .on('error', console.error)
361+
* .on('data', function(table) {})
362+
* .on('end', function() {
363+
* // All tables have been retrieved
364+
* });
365+
*
366+
* //-
367+
* // If you anticipate many results, you can end a stream early to prevent
368+
* // unnecessary processing and API requests.
369+
* //-
370+
* dataset.getTablesStream()
371+
* .on('data', function(table) {
372+
* this.end();
373+
* });
374+
*/
375+
Dataset.prototype.getTablesStream = common.paginator.streamify('getTables');
376+
347377
/**
348378
* Run a query scoped to your dataset.
349379
*
@@ -380,9 +410,8 @@ Dataset.prototype.table = function(id) {
380410

381411
/*! Developer Documentation
382412
*
383-
* These methods can be used with either a callback or as a readable object
384-
* stream. `streamRouter` is used to add this dual behavior.
413+
* These methods can be auto-paginated.
385414
*/
386-
common.streamRouter.extend(Dataset, ['getTables']);
415+
common.paginator.extend(Dataset, ['getTables']);
387416

388417
module.exports = Dataset;

packages/bigquery/src/index.js

Lines changed: 94 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,36 @@ BigQuery.prototype.createDataset = function(id, options, callback) {
120120
});
121121
};
122122

123+
/**
124+
* Run a query scoped to your project as a readable object stream.
125+
*
126+
* @param {object=} query - Configuration object. See
127+
* {module:bigquery#query} for a complete list of options.
128+
* @return {stream}
129+
*
130+
* @example
131+
* var query = 'SELECT url FROM [publicdata:samples.github_nested] LIMIT 100';
132+
*
133+
* bigquery.createQueryStream(query)
134+
* .on('error', console.error)
135+
* .on('data', function(row) {
136+
* // row is a result from your query.
137+
* })
138+
* .on('end', function() {
139+
* // All rows retrieved.
140+
* });
141+
*
142+
* //-
143+
* // If you anticipate many results, you can end a stream early to prevent
144+
* // unnecessary processing and API requests.
145+
* //-
146+
* bigquery.createQueryStream(query)
147+
* .on('data', function(row) {
148+
* this.end();
149+
* });
150+
*/
151+
BigQuery.prototype.createQueryStream = common.paginator.streamify('query');
152+
123153
/**
124154
* Create a reference to a dataset.
125155
*
@@ -146,7 +176,7 @@ BigQuery.prototype.dataset = function(id) {
146176
* @param {number} query.maxResults - Maximum number of results to return.
147177
* @param {string} query.pageToken - Token returned from a previous call, to
148178
* request the next page of results.
149-
* @param {function=} callback - The callback function.
179+
* @param {function} callback - The callback function.
150180
* @param {?error} callback.err - An error returned while making this request
151181
* @param {module:bigquery/dataset[]} callback.datasets - The list of datasets
152182
* in your project.
@@ -173,27 +203,6 @@ BigQuery.prototype.dataset = function(id) {
173203
* bigquery.getDatasets({
174204
* autoPaginate: false
175205
* }, callback);
176-
*
177-
* //-
178-
* // Get the datasets from your project as a readable object stream.
179-
* //-
180-
* bigquery.getDatasets()
181-
* .on('error', console.error)
182-
* .on('data', function(dataset) {
183-
* // dataset is a Dataset object.
184-
* })
185-
* .on('end', function() {
186-
* // All datasets retrieved.
187-
* });
188-
*
189-
* //-
190-
* // If you anticipate many results, you can end a stream early to prevent
191-
* // unnecessary processing and API requests.
192-
* //-
193-
* bigquery.getDatasets()
194-
* .on('data', function(dataset) {
195-
* this.end();
196-
* });
197206
*/
198207
BigQuery.prototype.getDatasets = function(query, callback) {
199208
var that = this;
@@ -232,6 +241,36 @@ BigQuery.prototype.getDatasets = function(query, callback) {
232241
});
233242
};
234243

244+
/**
245+
* List all or some of the {module:bigquery/dataset} objects in your project as
246+
* a readable object stream.
247+
*
248+
* @param {object=} query - Configuration object. See
249+
* {module:bigquery#getDatasets} for a complete list of options.
250+
* @return {stream}
251+
*
252+
* @example
253+
* bigquery.getDatasetsStream()
254+
* .on('error', console.error)
255+
* .on('data', function(dataset) {
256+
* // dataset is a Dataset object.
257+
* })
258+
* .on('end', function() {
259+
* // All datasets retrieved.
260+
* });
261+
*
262+
* //-
263+
* // If you anticipate many results, you can end a stream early to prevent
264+
* // unnecessary processing and API requests.
265+
* //-
266+
* bigquery.getDatasetsStream()
267+
* .on('data', function(dataset) {
268+
* this.end();
269+
* });
270+
*/
271+
BigQuery.prototype.getDatasetsStream =
272+
common.paginator.streamify('getDatasets');
273+
235274
/**
236275
* Get all of the jobs from your project.
237276
*
@@ -251,7 +290,7 @@ BigQuery.prototype.getDatasets = function(query, callback) {
251290
* "minimal", to not include the job configuration.
252291
* @param {string=} options.stateFilter - Filter for job state. Acceptable
253292
* values are "done", "pending", and "running".
254-
* @param {function=} callback - The callback function.
293+
* @param {function} callback - The callback function.
255294
* @param {?error} callback.err - An error returned while making this request
256295
* @param {module:bigquery/job[]} callback.jobs - The list of jobs in your
257296
* project.
@@ -278,27 +317,6 @@ BigQuery.prototype.getDatasets = function(query, callback) {
278317
* bigquery.getJobs({
279318
* autoPaginate: false
280319
* }, callback);
281-
*
282-
* //-
283-
* // Get the jobs from your project as a readable object stream.
284-
* //-
285-
* bigquery.getJobs()
286-
* .on('error', console.error)
287-
* .on('data', function(job) {
288-
* // job is a Job object.
289-
* })
290-
* .on('end', function() {
291-
* // All jobs retrieved.
292-
* });
293-
*
294-
* //-
295-
* // If you anticipate many results, you can end a stream early to prevent
296-
* // unnecessary processing and API requests.
297-
* //-
298-
* bigquery.getJobs()
299-
* .on('data', function(job) {
300-
* this.end();
301-
* });
302320
*/
303321
BigQuery.prototype.getJobs = function(options, callback) {
304322
var that = this;
@@ -337,6 +355,35 @@ BigQuery.prototype.getJobs = function(options, callback) {
337355
});
338356
};
339357

358+
/**
359+
* List all or some of the {module:bigquery/job} objects in your project as a
360+
* readable object stream.
361+
*
362+
* @param {object=} query - Configuration object. See
363+
* {module:bigquery#getJobs} for a complete list of options.
364+
* @return {stream}
365+
*
366+
* @example
367+
* bigquery.getJobsStream()
368+
* .on('error', console.error)
369+
* .on('data', function(job) {
370+
* // job is a Job object.
371+
* })
372+
* .on('end', function() {
373+
* // All jobs retrieved.
374+
* });
375+
*
376+
* //-
377+
* // If you anticipate many results, you can end a stream early to prevent
378+
* // unnecessary processing and API requests.
379+
* //-
380+
* bigquery.getJobsStream()
381+
* .on('data', function(job) {
382+
* this.end();
383+
* });
384+
*/
385+
BigQuery.prototype.getJobsStream = common.paginator.streamify('getJobs');
386+
340387
/**
341388
* Create a reference to an existing job.
342389
*
@@ -353,8 +400,6 @@ BigQuery.prototype.job = function(id) {
353400
/**
354401
* Run a query scoped to your project.
355402
*
356-
* This method also runs as a readable stream if you do not provide a callback.
357-
*
358403
* @resource [Jobs: query API Documentation]{@link https://cloud.google.com/bigquery/docs/reference/v2/jobs/query}
359404
*
360405
* @param {string|object} options - A string SQL query or configuration object.
@@ -370,7 +415,7 @@ BigQuery.prototype.job = function(id) {
370415
* complete, in milliseconds, before returning. Default is to return
371416
* immediately. If the timeout passes before the job completes, the request
372417
* will fail with a `TIMEOUT` error.
373-
* @param {function=} callback - The callback function.
418+
* @param {function} callback - The callback function.
374419
* @param {?error} callback.err - An error returned while making this request
375420
* @param {array} callback.rows - The list of results from your query.
376421
* @param {object} callback.apiResponse - The full API response.
@@ -398,28 +443,6 @@ BigQuery.prototype.job = function(id) {
398443
* query: query,
399444
* autoPaginate: false
400445
* }, callback);
401-
*
402-
* //-
403-
* // You can also use the `query` method as a readable object stream by
404-
* // omitting the callback.
405-
* //-
406-
* bigquery.query(query)
407-
* .on('error', console.error)
408-
* .on('data', function(row) {
409-
* // row is a result from your query.
410-
* })
411-
* .on('end', function() {
412-
* // All rows retrieved.
413-
* });
414-
*
415-
* //-
416-
* // If you anticipate many results, you can end a stream early to prevent
417-
* // unnecessary processing and API requests.
418-
* //-
419-
* bigquery.query(query)
420-
* .on('data', function(row) {
421-
* this.end();
422-
* });
423446
*/
424447
BigQuery.prototype.query = function(options, callback) {
425448
var self = this;
@@ -590,10 +613,9 @@ BigQuery.prototype.startQuery = function(options, callback) {
590613

591614
/*! Developer Documentation
592615
*
593-
* These methods can be used with either a callback or as a readable object
594-
* stream. `streamRouter` is used to add this dual behavior.
616+
* These methods can be auto-paginated.
595617
*/
596-
common.streamRouter.extend(BigQuery, ['getDatasets', 'getJobs', 'query']);
618+
common.paginator.extend(BigQuery, ['getDatasets', 'getJobs', 'query']);
597619

598620
BigQuery.Dataset = Dataset;
599621
BigQuery.Job = Job;

0 commit comments

Comments
 (0)