@@ -18,7 +18,15 @@ import {PreciseDate} from '@google-cloud/precise-date';
1818import { promisifyAll } from '@google-cloud/promisify' ;
1919import * as extend from 'extend' ;
2020import * as is from 'is' ;
21- import { ReadRequest , ExecuteSqlRequest , Snapshot } from './transaction' ;
21+ import {
22+ ExecuteSqlRequest ,
23+ ReadCallback ,
24+ ReadRequest ,
25+ ReadResponse ,
26+ RunCallback ,
27+ RunResponse ,
28+ Snapshot ,
29+ } from './transaction' ;
2230import { google } from '../protos/protos' ;
2331import { Session , Database } from '.' ;
2432import {
@@ -36,22 +44,22 @@ export interface TransactionIdentifier {
3644}
3745
3846export type CreateReadPartitionsResponse = [
39- google . spanner . v1 . IPartitionReadRequest ,
47+ ReadRequest [ ] ,
4048 google . spanner . v1 . IPartitionResponse ,
4149] ;
4250
4351export type CreateReadPartitionsCallback = ResourceCallback <
44- google . spanner . v1 . IPartitionReadRequest ,
52+ ReadRequest [ ] ,
4553 google . spanner . v1 . IPartitionResponse
4654> ;
4755
4856export type CreateQueryPartitionsResponse = [
49- google . spanner . v1 . IPartitionQueryRequest ,
57+ ExecuteSqlRequest [ ] ,
5058 google . spanner . v1 . IPartitionResponse ,
5159] ;
5260
5361export type CreateQueryPartitionsCallback = ResourceCallback <
54- google . spanner . v1 . IPartitionQueryRequest ,
62+ ExecuteSqlRequest [ ] ,
5563 google . spanner . v1 . IPartitionResponse
5664> ;
5765
@@ -114,35 +122,35 @@ class BatchTransaction extends Snapshot {
114122 /**
115123 * @see [`ExecuteSqlRequest`](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest)
116124 * @typedef {object } QueryPartition
117- * @property {string } partitionToken The partition token .
125+ * @property {string } partitionToken A token representing the partition, used to identify and execute the partition at a later time .
118126 */
119127 /**
120128 * @typedef {array } CreateQueryPartitionsResponse
121- * @property {QueryPartition [] } 0 List of query partitions.
129+ * @property {ExecuteSqlRequest [] } 0 Array of ExecuteSqlRequest partitions.
122130 * @property {object } 1 The full API response.
123131 */
124132 /**
125133 * @callback CreateQueryPartitionsCallback
126134 * @param {?Error } err Request error, if any.
127- * @param {QueryPartition [] } partitions List of query partitions.
135+ * @param {ExecuteSqlRequest [] } partitions Array of ExecuteSqlRequest partitions.
128136 * @param {object } apiResponse The full API response.
129137 */
130138 /**
131139 * Creates a set of query partitions that can be used to execute a query
132140 * operation in parallel. Partitions become invalid when the transaction used
133141 * to create them is closed.
134142 *
135- * @param {string|object } query A SQL query or
136- * [`ExecuteSqlRequest`](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest)
137- * object.
143+ * @param {string|ExecuteSqlRequest } query - A SQL query string or an {@link ExecuteSqlRequest} object.
144+ * If a string is provided, it will be wrapped into an `ExecuteSqlRequest`.
138145 * @param {object } [query.gaxOptions] Request configuration options,
139146 * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
140147 * for more details.
141148 * @param {object } [query.params] A map of parameter name to values.
142149 * @param {object } [query.partitionOptions] A map of partition options.
143150 * @param {object } [query.types] A map of parameter types.
144- * @param {CreateQueryPartitionsCallback } [callback] Callback callback function.
145- * @returns {Promise<CreateQueryPartitionsResponse> }
151+ * @param {CreateQueryPartitionsCallback } [callback] - Optional Callback function. If not provided, a promise is returned.
152+ * @returns {Promise<CreateQueryPartitionsResponse>|void } A promise resolving to an array of
153+ * `ExecuteSqlRequest' partitions and `IPartitionResponse` , or void if a callback is provided.
146154 *
147155 * @example <caption>include:samples/batch.js</caption>
148156 * region_tag:spanner_batch_client
@@ -268,31 +276,32 @@ class BatchTransaction extends Snapshot {
268276 /**
269277 * @typedef {object } ReadPartition
270278 * @mixes ReadRequestOptions
271- * @property {string } partitionToken The partition token.
272- * @property {object } [gaxOptions] Request configuration options,
273- * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
279+ * @property {string } partitionToken partitionToken A token representing the partition, used to identify and execute the partition at a later time .
280+ * @property {object } [gaxOptions] optional request configuration options,
281+ * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
274282 * for more details.
275283 */
276284 /**
277285 * @typedef {array } CreateReadPartitionsResponse
278- * @property {ReadPartition[] } 0 List of read partitions.
286+ * @property {ReadPartition[] } 0 Array of read partitions.
279287 * @property {object } 1 The full API response.
280288 */
281289 /**
282290 * @callback CreateReadPartitionsCallback
283291 * @param {?Error } err Request error, if any.
284- * @param {ReadPartition[] } partitions List of read partitions.
292+ * @param {ReadPartition[] } partitions Array of read partitions.
285293 * @param {object } apiResponse The full API response.
286294 */
287295 /**
288296 * Creates a set of read partitions that can be used to execute a read
289297 * operation in parallel. Partitions become invalid when the transaction used
290298 * to create them is closed.
291299 *
292- * @param {ReadRequestOptions } options Configuration object, describing what to
300+ * @param {ReadRequest } options Configuration object, describing what to
293301 * read from.
294302 * @param {CreateReadPartitionsCallback } [callback] Callback function.
295- * @returns {Promise<CreateReadPartitionsResponse> }
303+ * @returns {Promise<CreateReadPartitionsResponse>|void } A promise that resolves
304+ * to an array containing the read partitions and the full API response, or `void` if a callback is provided.
296305 */
297306 createReadPartitions (
298307 options : ReadRequest ,
@@ -348,28 +357,49 @@ class BatchTransaction extends Snapshot {
348357 ) ;
349358 }
350359 /**
351- * Executes partition.
360+ * Executes partition using either a read or a SQL query, depending on the type of partition provided .
352361 *
353- * @see { @link Transaction#read } when using { @link ReadPartition } .
354- * @see { @link Transaction#run } when using { @link QueryParition } .
362+ * @param { ReadRequest|ExecuteSqlRequest } partition The partition object to execute .
363+ * This can either be a `ReadPartition` or a `QueryPartition` .
355364 *
356- * @param {ReadPartition|QueryParition } partition The partition object.
357- * @param {object } [partition.gaxOptions] Request configuration options,
358- * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
359- * for more details.
360- * @param {TransactionRequestReadCallback|RunCallback } [callback] Callback
361- * function.
362- * @returns {Promise<RunResponse>|Promise<TransactionRequestReadResponse> }
365+ * @param {ReadCallback|RunCallback } [callback] Optional Callback function. If not provided,
366+ * a promise will be returned.
367+ *
368+ * If the partition is a read partition, it will execute a read using {@link Transaction#read}
369+ * @see {@link Transaction#read } when using {@link ReadRequest }.
370+ *
371+ * If the partition is query partition, it will execute a SQL query using {@link Transaction#run}
372+ * @see {@link Transaction#run } when using {@link ExecuteSqlRequest }.
373+ *
374+ * @returns {Promise<ReadResponse | RunResponse>|void } Returns a promise when no callback is provided,
375+ * or void when a callback is used.
363376 *
364377 * @example <caption>include:samples/batch.js</caption>
365378 * region_tag:spanner_batch_execute_partitions
366379 */
367- execute ( partition , callback ) {
368- if ( is . string ( partition . table ) ) {
369- this . read ( partition . table , partition , callback ) ;
380+ execute (
381+ partition : ReadRequest | ExecuteSqlRequest ,
382+ ) : Promise < ReadResponse | RunResponse > ;
383+ execute (
384+ partition : ReadRequest | ExecuteSqlRequest ,
385+ callback : ReadCallback | RunCallback ,
386+ ) : void ;
387+ execute (
388+ partition : ReadRequest | ExecuteSqlRequest ,
389+ cb ?: ReadCallback | RunCallback ,
390+ ) : void | Promise < ReadResponse | RunResponse > {
391+ const isRead = typeof ( partition as ReadRequest ) . table === 'string' ;
392+
393+ if ( isRead ) {
394+ this . read (
395+ ( partition as ReadRequest ) . table ! ,
396+ partition as ReadRequest ,
397+ cb as ReadCallback ,
398+ ) ;
370399 return ;
371400 }
372- this . run ( partition , callback ) ;
401+
402+ this . run ( partition as ExecuteSqlRequest , cb as RunCallback ) ;
373403 }
374404 /**
375405 * Executes partition in streaming mode.
0 commit comments