@google-cloud/spanner 5.14.0 → 5.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -61,6 +61,7 @@ const RETRY_INFO_BIN = 'google.rpc.retryinfo-bin';
61
61
  * @see [Timestamp Bounds API Documentation](https://cloud.google.com/spanner/docs/timestamp-bounds)
62
62
  *
63
63
  * @example
64
+ * ```
64
65
  * const {Spanner} = require('@google-cloud/spanner');
65
66
  * const spanner = new Spanner();
66
67
  *
@@ -79,6 +80,7 @@ const RETRY_INFO_BIN = 'google.rpc.retryinfo-bin';
79
80
  * // It should be called when the snapshot finishes.
80
81
  * transaction.end();
81
82
  * });
83
+ * ```
82
84
  */
83
85
  class Snapshot extends events_1.EventEmitter {
84
86
  /**
@@ -138,47 +140,6 @@ class Snapshot extends events_1.EventEmitter {
138
140
  [common_1.CLOUD_RESOURCE_HEADER]: this.session.parent.formattedName_,
139
141
  };
140
142
  }
141
- /**
142
- * @typedef {object} TransactionResponse
143
- * @property {string|Buffer} id The transaction ID.
144
- * @property {?google.protobuf.Timestamp} readTimestamp For snapshot read-only
145
- * transactions, the read timestamp chosen for the transaction.
146
- */
147
- /**
148
- * @typedef {array} TransactionBeginResponse
149
- * @property {TransactionResponse} 0 The raw transaction object.
150
- */
151
- /**
152
- * @callback TransactionBeginCallback
153
- * @param {?Error} err Request error, if any.
154
- * @param {TransactionResponse} apiResponse The raw transaction object.
155
- */
156
- /**
157
- * Begin a new transaction. Typically, you need not call this unless
158
- * manually creating transactions via {@link Session} objects.
159
- *
160
- * @see [BeginTransaction API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.Spanner.BeginTransaction)
161
- *
162
- * @param {object} [gaxOptions] Request configuration options,
163
- * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
164
- * for more details.
165
- * @param {TransactionBeginCallback} [callback] Callback function.
166
- * @returns {Promise<TransactionBeginResponse>}
167
- *
168
- * @example
169
- * transaction.begin(function(err) {
170
- * if (!err) {
171
- * // transaction began successfully.
172
- * }
173
- * });
174
- *
175
- * @example <caption>If the callback is omitted, the function returns a Promise
176
- * </caption>
177
- * transaction.begin()
178
- * .then(function(data) {
179
- * const apiResponse = data[0];
180
- * });
181
- */
182
143
  begin(gaxOptionsOrCallback, cb) {
183
144
  const gaxOpts = typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
184
145
  const callback = typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb;
@@ -291,6 +252,7 @@ class Snapshot extends events_1.EventEmitter {
291
252
  * @returns {ReadableStream} A readable stream that emits rows.
292
253
  *
293
254
  * @example
255
+ * ```
294
256
  * transaction.createReadStream('Singers', {
295
257
  * keys: ['1'],
296
258
  * columns: ['SingerId', 'name']
@@ -312,8 +274,10 @@ class Snapshot extends events_1.EventEmitter {
312
274
  * // All results retrieved.
313
275
  * });
314
276
  *
315
- * @example <caption>Provide an array for `query.keys` to read with a
316
- * composite key.</caption>
277
+ * ```
278
+ * @example Provide an array for `query.keys` to read with a
279
+ * composite key.
280
+ * ```
317
281
  * const query = {
318
282
  * keys: [
319
283
  * [
@@ -327,10 +291,12 @@ class Snapshot extends events_1.EventEmitter {
327
291
  * ],
328
292
  * // ...
329
293
  * };
294
+ * ```
330
295
  *
331
- * @example <caption>Rows are returned as an array of object arrays. Each
296
+ * @example Rows are returned as an array of object arrays. Each
332
297
  * object has a `name` and `value` property. To get a serialized object, call
333
- * `toJSON()`.</caption>
298
+ * `toJSON()`.
299
+ * ```
334
300
  * transaction.createReadStream('Singers', {
335
301
  * keys: ['1'],
336
302
  * columns: ['SingerId', 'name']
@@ -345,9 +311,11 @@ class Snapshot extends events_1.EventEmitter {
345
311
  * .on('end', function() {
346
312
  * // All results retrieved.
347
313
  * });
314
+ * ```
348
315
  *
349
- * @example <caption>Alternatively, set `query.json` to `true`, and this step
350
- * will perform automatically.</caption>
316
+ * @example Alternatively, set `query.json` to `true`, and this step
317
+ * will perform automatically.
318
+ * ```
351
319
  * transaction.createReadStream('Singers', {
352
320
  * keys: ['1'],
353
321
  * columns: ['SingerId', 'name'],
@@ -363,9 +331,11 @@ class Snapshot extends events_1.EventEmitter {
363
331
  * .on('end', function() {
364
332
  * // All results retrieved.
365
333
  * });
334
+ * ```
366
335
  *
367
- * @example <caption>If you anticipate many results, you can end a stream
368
- * early to prevent unnecessary processing and API requests.</caption>
336
+ * @example If you anticipate many results, you can end a stream
337
+ * early to prevent unnecessary processing and API requests.
338
+ * ```
369
339
  * transaction.createReadStream('Singers', {
370
340
  * keys: ['1'],
371
341
  * columns: ['SingerId', 'name']
@@ -373,6 +343,7 @@ class Snapshot extends events_1.EventEmitter {
373
343
  * .on('data', function(row) {
374
344
  * this.end();
375
345
  * });
346
+ * ```
376
347
  */
377
348
  createReadStream(table, request = {}) {
378
349
  var _a;
@@ -420,7 +391,8 @@ class Snapshot extends events_1.EventEmitter {
420
391
  * mainly be called for {@link Snapshot} objects, however in certain cases
421
392
  * you may want to call them for {@link Transaction} objects as well.
422
393
  *
423
- * @example <caption>Calling `end` on a read only snapshot</caption>
394
+ * @example Calling `end` on a read only snapshot
395
+ * ```
424
396
  * database.getSnapshot((err, transaction) => {
425
397
  * if (err) {
426
398
  * // Error handling omitted.
@@ -435,8 +407,10 @@ class Snapshot extends events_1.EventEmitter {
435
407
  * transaction.end();
436
408
  * });
437
409
  * });
410
+ * ```
438
411
  *
439
- * @example <caption>Calling `end` on a read/write transaction</caption>
412
+ * @example Calling `end` on a read/write transaction
413
+ * ```
440
414
  * database.runTransaction((err, transaction) => {
441
415
  * if (err) {
442
416
  * // Error handling omitted.
@@ -455,6 +429,7 @@ class Snapshot extends events_1.EventEmitter {
455
429
  * transaction.commit(err => {});
456
430
  * });
457
431
  * });
432
+ * ```
458
433
  */
459
434
  end() {
460
435
  if (this.ended) {
@@ -463,115 +438,6 @@ class Snapshot extends events_1.EventEmitter {
463
438
  this.ended = true;
464
439
  process.nextTick(() => this.emit('end'));
465
440
  }
466
- /**
467
- * @typedef {array} ReadResponse
468
- * @property {array[]} 0 Rows are returned as an array of object arrays. Each
469
- * object has a `name` and `value` property. To get a serialized object,
470
- * call `toJSON()`. Optionally, provide an options object to `toJSON()`
471
- * specifying `wrapNumbers: true` to protect large integer values outside
472
- * of the range of JavaScript Number. If set, FLOAT64 values are returned
473
- * as {@link Spanner.Float} objects and INT64 values as {@link
474
- * Spanner.Int}.
475
- */
476
- /**
477
- * @callback ReadCallback
478
- * @param {?Error} err Request error, if any.
479
- * @param {array[]} rows Rows are returned as an array of object arrays. Each
480
- * object has a `name` and `value` property. To get a serialized object,
481
- * call `toJSON()`. Optionally, provide an options object to `toJSON()`
482
- * specifying `wrapNumbers: true` to protect large integer values outside
483
- * of the range of JavaScript Number. If set, FLOAT64 values are returned
484
- * as {@link Spanner.Float} objects and INT64 values as {@link
485
- * Spanner.Int}.
486
- */
487
- /**
488
- * Performs a read request against the specified Table.
489
- *
490
- * Wrapper around {@link v1.SpannerClient#read}.
491
- *
492
- * @see {@link v1.SpannerClient#read}
493
- *
494
- * @param {string} table The table to read from.
495
- * @param {ReadRequest} query Configuration object. See official
496
- * [`ReadRequest`](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.ReadRequest).
497
- * API documentation.
498
- * @param {ReadCallback} [callback] Callback function.
499
- * @returns {Promise<ReadResponse>}
500
- *
501
- * @example
502
- * const query = {
503
- * keys: ['1'],
504
- * columns: ['SingerId', 'name']
505
- * };
506
- *
507
- * transaction.read('Singers', query, function(err, rows) {
508
- * if (err) {
509
- * // Error handling omitted.
510
- * }
511
- *
512
- * const firstRow = rows[0];
513
- *
514
- * // firstRow = [
515
- * // {
516
- * // name: 'SingerId',
517
- * // value: '1'
518
- * // },
519
- * // {
520
- * // name: 'Name',
521
- * // value: 'Eddie Wilson'
522
- * // }
523
- * // ]
524
- * });
525
- *
526
- * @example <caption>Provide an array for `query.keys` to read with a
527
- * composite key.</caption>
528
- * const query = {
529
- * keys: [
530
- * [
531
- * 'Id1',
532
- * 'Name1'
533
- * ],
534
- * [
535
- * 'Id2',
536
- * 'Name2'
537
- * ]
538
- * ],
539
- * // ...
540
- * };
541
- *
542
- * @example <caption>Rows are returned as an array of object arrays. Each
543
- * object has a `name` and `value` property. To get a serialized object, call
544
- * `toJSON()`.</caption>
545
- * transaction.read('Singers', query, function(err, rows) {
546
- * if (err) {
547
- * // Error handling omitted.
548
- * }
549
- *
550
- * const firstRow = rows[0];
551
- *
552
- * // firstRow.toJSON() = {
553
- * // SingerId: '1',
554
- * // Name: 'Eddie Wilson'
555
- * // }
556
- * });
557
- *
558
- * @example <caption>Alternatively, set `query.json` to `true`, and this step
559
- * will perform automatically.</caption>
560
- * query.json = true;
561
- *
562
- * transaction.read('Singers', query, function(err, rows) {
563
- * if (err) {
564
- * // Error handling omitted.
565
- * }
566
- *
567
- * const firstRow = rows[0];
568
- *
569
- * // firstRow = {
570
- * // SingerId: '1',
571
- * // Name: 'Eddie Wilson'
572
- * // }
573
- * });
574
- */
575
441
  read(table, requestOrCallback, cb) {
576
442
  const rows = [];
577
443
  let request;
@@ -589,79 +455,6 @@ class Snapshot extends events_1.EventEmitter {
589
455
  .on('data', row => rows.push(row))
590
456
  .on('end', () => callback(null, rows));
591
457
  }
592
- /**
593
- * Execute a SQL statement on this database inside of a transaction.
594
- *
595
- * **Performance Considerations:**
596
- *
597
- * This method wraps the streaming method,
598
- * {@link Snapshot#run} for your convenience. All rows are stored in memory
599
- * before releasing to your callback. If you intend to receive a lot of
600
- * results from your query, consider using the streaming method,
601
- * so you can free each result from memory after consuming it.
602
- *
603
- * Wrapper around {@link v1.SpannerClient#executeStreamingSql}.
604
- *
605
- * @see {@link v1.SpannerClient#executeStreamingSql}
606
- * @see [ExecuteStreamingSql API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.Spanner.ExecuteStreamingSql)
607
- * @see [ExecuteSqlRequest API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest)
608
- *
609
- * @param {string|ExecuteSqlRequest} query A SQL query or
610
- * {@link ExecuteSqlRequest} object.
611
- * @param {RunCallback} [callback] Callback function.
612
- * @returns {Promise<RunResponse>}
613
- *
614
- * @example
615
- * transaction.run(query, function(err, rows) {
616
- * if (err) {
617
- * // Error handling omitted.
618
- * }
619
- *
620
- * // rows = [
621
- * // {
622
- * // SingerId: '1',
623
- * // Name: 'Eddie Wilson'
624
- * // }
625
- * // ]
626
- * });
627
- *
628
- * @example <caption>The SQL query string can contain parameter placeholders.
629
- * A parameter placeholder consists of '@' followed by the parameter name.
630
- * </caption>
631
- * const query = {
632
- * sql: 'SELECT * FROM Singers WHERE name = @name',
633
- * params: {
634
- * name: 'Eddie Wilson'
635
- * }
636
- * };
637
- *
638
- * transaction.run(query, function(err, rows) {
639
- * if (err) {
640
- * // Error handling omitted.
641
- * }
642
- * });
643
- *
644
- * @example <caption>If you need to enforce a specific param type, a types map
645
- * can be provided. This is typically useful if your param value can be null.
646
- * </caption>
647
- * const query = {
648
- * sql: 'SELECT * FROM Singers WHERE name = @name AND id = @id',
649
- * params: {
650
- * id: spanner.int(8),
651
- * name: null
652
- * },
653
- * types: {
654
- * id: 'int64',
655
- * name: 'string'
656
- * }
657
- * };
658
- *
659
- * transaction.run(query, function(err, rows) {
660
- * if (err) {
661
- * // Error handling omitted.
662
- * }
663
- * });
664
- */
665
458
  run(query, callback) {
666
459
  const rows = [];
667
460
  let stats;
@@ -733,6 +526,7 @@ class Snapshot extends events_1.EventEmitter {
733
526
  * @returns {ReadableStream}
734
527
  *
735
528
  * @example
529
+ * ```
736
530
  * const query = 'SELECT * FROM Singers';
737
531
  *
738
532
  * transaction.runStream(query)
@@ -747,9 +541,10 @@ class Snapshot extends events_1.EventEmitter {
747
541
  * // All results retrieved.
748
542
  * });
749
543
  *
750
- * @example <caption>The SQL query string can contain parameter placeholders.
544
+ * ```
545
+ * @example The SQL query string can contain parameter placeholders.
751
546
  * A parameter placeholder consists of '@' followed by the parameter name.
752
- * </caption>
547
+ * ```
753
548
  * const query = {
754
549
  * sql: 'SELECT * FROM Singers WHERE name = @name',
755
550
  * params: {
@@ -761,13 +556,16 @@ class Snapshot extends events_1.EventEmitter {
761
556
  * .on('error', function(err) {})
762
557
  * .on('data', function(row) {})
763
558
  * .on('end', function() {});
559
+ * ```
764
560
  *
765
- * @example <caption>If you anticipate many results, you can end a stream
766
- * early to prevent unnecessary processing and API requests.</caption>
561
+ * @example If you anticipate many results, you can end a stream
562
+ * early to prevent unnecessary processing and API requests.
563
+ * ```
767
564
  * transaction.runStream(query)
768
565
  * .on('data', function(row) {
769
566
  * this.end();
770
567
  * });
568
+ * ```
771
569
  */
772
570
  runStream(query) {
773
571
  if (typeof query === 'string') {
@@ -948,30 +746,6 @@ promisify_1.promisifyAll(Snapshot, {
948
746
  * @class
949
747
  */
950
748
  class Dml extends Snapshot {
951
- /**
952
- * @typedef {array} RunUpdateResponse
953
- * @property {number} 0 Affected row count.
954
- */
955
- /**
956
- * @callback RunUpdateCallback
957
- * @param {?Error} err Request error, if any.
958
- * @param {number} rowCount Affected row count.
959
- */
960
- /**
961
- * Execute a DML statement and get the affected row count.
962
- *
963
- * @private
964
- *
965
- * @see {@link Transaction#run}
966
- *
967
- * @param {string|object} query A DML statement or
968
- * [`ExecuteSqlRequest`](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest)
969
- * object.
970
- * @param {object} [query.params] A map of parameter name to values.
971
- * @param {object} [query.types] A map of parameter types.
972
- * @param {RunUpdateCallback} [callback] Callback function.
973
- * @returns {Promise<RunUpdateResponse>}
974
- */
975
749
  runUpdate(query, callback) {
976
750
  if (typeof query === 'string') {
977
751
  query = { sql: query };
@@ -1017,6 +791,7 @@ promisify_1.promisifyAll(Dml);
1017
791
  * @param {Session} session The parent Session object.
1018
792
  *
1019
793
  * @example
794
+ * ```
1020
795
  * const {Spanner} = require('@google-cloud/spanner');
1021
796
  * const spanner = new Spanner();
1022
797
  *
@@ -1027,11 +802,14 @@ promisify_1.promisifyAll(Dml);
1027
802
  * // The `transaction` object is ready for use.
1028
803
  * });
1029
804
  *
1030
- * @example <caption>To manually control retrying the transaction, use the
1031
- * `getTransaction` method.</caption>
805
+ * ```
806
+ * @example To manually control retrying the transaction, use the
807
+ * `getTransaction` method.
808
+ * ```
1032
809
  * database.getTransaction(function(err, transaction) {
1033
810
  * // The `transaction` object is ready for use.
1034
811
  * });
812
+ * ```
1035
813
  */
1036
814
  class Transaction extends Dml {
1037
815
  /**
@@ -1064,6 +842,7 @@ class Transaction extends Dml {
1064
842
  * @returns {Promise<RunUpdateResponse>}
1065
843
  *
1066
844
  * @example
845
+ * ```
1067
846
  * const query = 'UPDATE Account SET Balance = 1000 WHERE Key = 1';
1068
847
  *
1069
848
  * transaction.runUpdate(query, (err, rowCount) => {
@@ -1071,6 +850,7 @@ class Transaction extends Dml {
1071
850
  * // Error handling omitted.
1072
851
  * }
1073
852
  * });
853
+ * ```
1074
854
  */
1075
855
  constructor(session, options = {}, queryOptions, requestOptions) {
1076
856
  super(session, undefined, queryOptions);
@@ -1078,71 +858,6 @@ class Transaction extends Dml {
1078
858
  this._options = { readWrite: options };
1079
859
  this.requestOptions = requestOptions;
1080
860
  }
1081
- /**
1082
- * @typedef {error} BatchUpdateError
1083
- * @property {number} code gRPC status code.
1084
- * @property {?object} metadata gRPC metadata.
1085
- * @property {number[]} rowCounts The affected row counts for any DML
1086
- * statements that were executed successfully before this error occurred.
1087
- */
1088
- /**
1089
- * @typedef {object} BatchUpdateOptions
1090
- * @property {object} [gaxOptions] Request configuration options,
1091
- * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
1092
- * for more details.
1093
- * @property {google.spanner.v1.IRequestOptions} [requestOptions] The request options to include
1094
- * with the commit request.
1095
- */
1096
- /**
1097
- * @typedef {array} BatchUpdateResponse
1098
- * @property {number[]} 0 Affected row counts.
1099
- * @property {object} 1 The full API response.
1100
- */
1101
- /**
1102
- * @callback BatchUpdateCallback
1103
- * @param {?BatchUpdateError} err Request error, if any.
1104
- * @param {number[]} rowCounts Affected row counts.
1105
- * @param {object} apiResponse The full API response.
1106
- */
1107
- /**
1108
- * Execute a series of DML statements and get the affected row counts.
1109
- *
1110
- * If any of the DML statements fail, the returned error will contain a list
1111
- * of results for all successfully executed statements.
1112
- *
1113
- * @param {string[]|object[]} query A DML statement or
1114
- * [`ExecuteSqlRequest`](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest)
1115
- * object.
1116
- * @param {object} [query.params] A map of parameter name to values.
1117
- * @param {object} [query.types] A map of parameter types.
1118
- * @param {object} [gaxOptions] Request configuration options,
1119
- * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
1120
- * for more details.
1121
- * @param {BatchUpdateOptions} [options] Options for configuring the request.
1122
- * @param {RunUpdateCallback} [callback] Callback function.
1123
- * @returns {Promise<RunUpdateResponse>}
1124
- *
1125
- * @example
1126
- * const queries = [
1127
- * {
1128
- * sql: 'INSERT INTO MyTable (Key, Value) VALUES (@key, @value)',
1129
- * params: {key: 'my-key', value: 'my-value'},
1130
- * },
1131
- * {
1132
- * sql: 'UPDATE MyTable t SET t.Value = @value WHERE t.KEY = @key',
1133
- * params: {key: 'my-other-key', value: 'my-other-value'}
1134
- * }
1135
- * ];
1136
- *
1137
- * transaction.batchUpdate(queries, (err, rowCounts, apiResponse) => {
1138
- * if (err) {
1139
- * // Error handling omitted.
1140
- * }
1141
- * });
1142
- *
1143
- * @example <caption>If the callback is omitted, we'll return a Promise.</caption>
1144
- * const [rowCounts, apiResponse] = await transaction.batchUpdate(queries);
1145
- */
1146
861
  batchUpdate(queries, optionsOrCallback, cb) {
1147
862
  var _a;
1148
863
  const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
@@ -1218,66 +933,6 @@ class Transaction extends Dml {
1218
933
  }
1219
934
  return undefined;
1220
935
  }
1221
- /**
1222
- * @typedef {object} CommitOptions
1223
- * @property {google.spanner.v1.IRequestOptions} requestOptions The request options to include
1224
- * with the commit request.
1225
- * @property {boolean} returnCommitStats Include statistics related to the
1226
- * transaction in the {@link CommitResponse}.
1227
- * @property {object} [gaxOptions] The request configuration options,
1228
- * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
1229
- * for more details.
1230
- */
1231
- /**
1232
- * @typedef {object} CommitResponse
1233
- * @property {google.protobuf.Timestamp} commitTimestamp The transaction
1234
- * commit timestamp.
1235
- * @property {google.spanner.v1.CommitResponse.ICommitStats|null} commitStats
1236
- * The statistics about this commit. Only populated if requested in
1237
- * {@link CommitOptions}.
1238
- */
1239
- /**
1240
- * @typedef {array} CommitPromiseResponse
1241
- * @property {CommitResponse} 0 The commit response.
1242
- */
1243
- /**
1244
- * @callback CommitCallback
1245
- * @param {?Error} error Request error, if any.
1246
- * @param {CommitResponse} apiResponse The full API response.
1247
- */
1248
- /**
1249
- * Commit the transaction.
1250
- *
1251
- * Wrapper around {@link v1.SpannerClient#commit}.
1252
- *
1253
- * @see {@link v1.SpannerClient#commit}
1254
- * @see [Commit API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.Spanner.Commit)
1255
- *
1256
- * @param {CommitOptions} [options] Options for configuring the request.
1257
- * @param {CommitCallback} [callback] Callback function.
1258
- * @returns {Promise<CommitPromiseResponse>}
1259
- *
1260
- * @example
1261
- * database.runTransaction(function(err, transaction) {
1262
- * if (err) {
1263
- * // Error handling omitted.
1264
- * }
1265
- *
1266
- * // Queue a mutation (note that there is no callback passed to `insert`).
1267
- * transaction.insert('Singers', {
1268
- * SingerId: 'Id3b',
1269
- * Name: 'Joe West'
1270
- * });
1271
- *
1272
- * // Commit the transaction.
1273
- * transaction.commit(function(err, apiResponse) {
1274
- * if (!err) {
1275
- * // Get the commit timestamp on successful commits.
1276
- * const {commitTimestamp} = apiResponse;
1277
- * }
1278
- * });
1279
- * });
1280
- */
1281
936
  commit(optionsOrCallback, cb) {
1282
937
  const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
1283
938
  const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
@@ -1309,9 +964,80 @@ class Transaction extends Dml {
1309
964
  this.commitTimestampProto = resp.commitTimestamp;
1310
965
  this.commitTimestamp = new precise_date_1.PreciseDate(resp.commitTimestamp);
1311
966
  }
967
+ err = Transaction.decorateCommitError(err, mutations);
1312
968
  callback(err, resp);
1313
969
  });
1314
970
  }
971
+ /**
972
+ * Decorates an error returned by a commit with additional information for
973
+ * specific known errors.
974
+ * @param err the error to check and decorate with additional information if possible
975
+ * @param mutations the mutations included in the commit request
976
+ * @private
977
+ */
978
+ static decorateCommitError(err, mutations) {
979
+ if (!err) {
980
+ return err;
981
+ }
982
+ if (err.code === google_gax_1.Status.FAILED_PRECONDITION) {
983
+ const mismatchErr = Transaction.decoratePossibleJsonMismatchError(err, mutations);
984
+ if (mismatchErr) {
985
+ return mismatchErr;
986
+ }
987
+ }
988
+ return err;
989
+ }
990
+ /**
991
+ * Decorates an error returned by a commit with additional information if the
992
+ * error was returned because the application tried to insert an array of
993
+ * objects into a JSON column. An array of objects will by default be encoded
994
+ * as ARRAY<JSON>, but can also be interpreted as JSON. An application must
995
+ * specify a top-level array of objects that should be inserted into a JSON
996
+ * column as a string instead of as an array of objects.
997
+ * @param err the error returned by the commit RPC
998
+ * @param mutations the mutations included in the commit request
999
+ * @private
1000
+ */
1001
+ static decoratePossibleJsonMismatchError(err, mutations) {
1002
+ var _a, _b;
1003
+ const errorMessage = /Invalid value for column (?<column>.+) in table (?<table>.+): Expected JSON./;
1004
+ const found = err.message && err.message.match(errorMessage);
1005
+ if (found && found.groups) {
1006
+ const table = found.groups.table;
1007
+ const column = found.groups.column;
1008
+ for (const mutation of mutations) {
1009
+ const write = mutation.insert ||
1010
+ mutation.update ||
1011
+ mutation.insertOrUpdate ||
1012
+ mutation.replace;
1013
+ if (write && write.table === table) {
1014
+ const index = (_a = write.columns) === null || _a === void 0 ? void 0 : _a.indexOf(column);
1015
+ if (index && index > -1 && write.values) {
1016
+ for (const row of write.values) {
1017
+ if (((_b = row.values) === null || _b === void 0 ? void 0 : _b.length) &&
1018
+ row.values.length > index &&
1019
+ row.values[index].listValue) {
1020
+ // If the value is an array, the client library also encoded it as
1021
+ // an array. Inserting an array into a JSON column is not possible,
1022
+ // although if it is encoded as a string containing a top-level JSON
1023
+ // array it will work.
1024
+ const additionalMessage = 'The value is an array. Convert the value to a JSON string containing an array instead in order to insert it into a JSON column. Example: `[{"key": "value 1"}, {"key": "value 2"}]` instead of [{key: "value 1"}, {key: "value 2"}]';
1025
+ return {
1026
+ code: err.code,
1027
+ details: `${err.details} ${additionalMessage}`,
1028
+ message: `${err.message} ${additionalMessage}`,
1029
+ metadata: err.metadata,
1030
+ name: err.name,
1031
+ stack: err.stack,
1032
+ };
1033
+ }
1034
+ }
1035
+ }
1036
+ }
1037
+ }
1038
+ }
1039
+ return null;
1040
+ }
1315
1041
  /**
1316
1042
  * Delete rows from a table.
1317
1043
  *
@@ -1323,6 +1049,7 @@ class Transaction extends Dml {
1323
1049
  * below.
1324
1050
  *
1325
1051
  * @example
1052
+ * ```
1326
1053
  * const keys = ['Id1', 'Id2', 'Id3'];
1327
1054
  *
1328
1055
  * database.runTransaction(function(err, transaction) {
@@ -1342,8 +1069,10 @@ class Transaction extends Dml {
1342
1069
  * });
1343
1070
  * });
1344
1071
  *
1345
- * @example <caption>Provide an array for `keys` to delete rows with a
1346
- * composite key.</caption>
1072
+ * ```
1073
+ * @example Provide an array for `keys` to delete rows with a
1074
+ * composite key.
1075
+ * ```
1347
1076
  * const keys = [
1348
1077
  * [
1349
1078
  * 'Id1',
@@ -1354,6 +1083,7 @@ class Transaction extends Dml {
1354
1083
  * 'Name2'
1355
1084
  * ]
1356
1085
  * ];
1086
+ * ```
1357
1087
  */
1358
1088
  deleteRows(table, keys) {
1359
1089
  const keySet = {
@@ -1374,6 +1104,7 @@ class Transaction extends Dml {
1374
1104
  * into this table.
1375
1105
  *
1376
1106
  * @example
1107
+ * ```
1377
1108
  * const row = {
1378
1109
  * SingerId: 'Id3',
1379
1110
  * Name: 'Eddie Wilson'
@@ -1396,7 +1127,9 @@ class Transaction extends Dml {
1396
1127
  * });
1397
1128
  * });
1398
1129
  *
1399
- * @example <caption>Multiple rows can be inserted at once.</caption>
1130
+ * ```
1131
+ * @example Multiple rows can be inserted at once.
1132
+ * ```
1400
1133
  * const row2 = {
1401
1134
  * SingerId: 'Id3b',
1402
1135
  * Name: 'Joe West'
@@ -1421,6 +1154,7 @@ class Transaction extends Dml {
1421
1154
  * }
1422
1155
  * });
1423
1156
  * });
1157
+ * ```
1424
1158
  */
1425
1159
  insert(table, rows) {
1426
1160
  this._mutate('insert', table, rows);
@@ -1435,6 +1169,7 @@ class Transaction extends Dml {
1435
1169
  * into this table.
1436
1170
  *
1437
1171
  * @example
1172
+ * ```
1438
1173
  * const row = {
1439
1174
  * SingerId: 'Id3',
1440
1175
  * Name: 'Joe West'
@@ -1456,39 +1191,11 @@ class Transaction extends Dml {
1456
1191
  * }
1457
1192
  * });
1458
1193
  * });
1194
+ * ```
1459
1195
  */
1460
1196
  replace(table, rows) {
1461
1197
  this._mutate('replace', table, rows);
1462
1198
  }
1463
- /**
1464
- * Roll back a transaction, releasing any locks it holds. It is a good idea to
1465
- * call this for any transaction that includes one or more queries that you
1466
- * decide not to commit.
1467
- *
1468
- * Wrapper around {@link v1.SpannerClient#rollback}.
1469
- *
1470
- * @see {@link v1.SpannerClient#rollback}
1471
- * @see [Rollback API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.Spanner.Rollback)
1472
- *
1473
- * @param {object} [gaxOptions] Request configuration options,
1474
- * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
1475
- * for more details.
1476
- * @param {BasicCallback} [callback] Callback function.
1477
- * @returns {Promise<BasicResponse>}
1478
- *
1479
- * @example
1480
- * database.runTransaction(function(err, transaction) {
1481
- * if (err) {
1482
- * // Error handling omitted.
1483
- * }
1484
- *
1485
- * transaction.rollback(function(err) {
1486
- * if (!err) {
1487
- * // Transaction rolled back successfully.
1488
- * }
1489
- * });
1490
- * });
1491
- */
1492
1199
  rollback(gaxOptionsOrCallback, cb) {
1493
1200
  const gaxOpts = typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
1494
1201
  const callback = typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb;
@@ -1523,6 +1230,7 @@ class Transaction extends Dml {
1523
1230
  * into this table.
1524
1231
  *
1525
1232
  * @example
1233
+ * ```
1526
1234
  * const row = {
1527
1235
  * SingerId: 'Id3',
1528
1236
  * Name: 'Joe West'
@@ -1544,6 +1252,7 @@ class Transaction extends Dml {
1544
1252
  * }
1545
1253
  * });
1546
1254
  * });
1255
+ * ```
1547
1256
  */
1548
1257
  update(table, rows) {
1549
1258
  this._mutate('update', table, rows);
@@ -1558,6 +1267,7 @@ class Transaction extends Dml {
1558
1267
  * into this table.
1559
1268
  *
1560
1269
  * @example
1270
+ * ```
1561
1271
  * const row = {
1562
1272
  * SingerId: 'Id3',
1563
1273
  * Name: 'Joe West'
@@ -1579,6 +1289,7 @@ class Transaction extends Dml {
1579
1289
  * }
1580
1290
  * });
1581
1291
  * });
1292
+ * ```
1582
1293
  */
1583
1294
  upsert(table, rows) {
1584
1295
  this._mutate('insertOrUpdate', table, rows);
@@ -1657,29 +1368,6 @@ class PartitionedDml extends Dml {
1657
1368
  super(session);
1658
1369
  this._options = { partitionedDml: options };
1659
1370
  }
1660
- /**
1661
- * Execute a DML statement and get the affected row count. Unlike
1662
- * {@link Transaction#runUpdate} after using this method you should
1663
- * immediately discard this transaction, internally it will invoke
1664
- * {@link PartitionedDml#end}.
1665
- *
1666
- * @see Database#runPartitionedUpdate
1667
- *
1668
- * @param {string|object} query A DML statement or
1669
- * [`ExecuteSqlRequest`](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest)
1670
- * object.
1671
- * @param {object} [query.params] A map of parameter name to values.
1672
- * @param {object} [query.types] A map of parameter types.
1673
- * @param {RunUpdateCallback} [callback] Callback function.
1674
- * @returns {Promise<RunUpdateResponse>}
1675
- *
1676
- * @example
1677
- * transaction.runUpdate(query, (err, rowRount) => {
1678
- * if (err) {
1679
- * // Error handling omitted.
1680
- * }
1681
- * });
1682
- */
1683
1371
  runUpdate(query, callback) {
1684
1372
  super.runUpdate(query, (err, count) => {
1685
1373
  this.end();