@google-cloud/spanner 7.7.0 → 7.9.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.
Files changed (43) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +5 -0
  3. package/build/protos/google/spanner/admin/database/v1/backup.proto +190 -109
  4. package/build/protos/google/spanner/admin/database/v1/common.proto +30 -10
  5. package/build/protos/google/spanner/admin/database/v1/spanner_database_admin.proto +166 -100
  6. package/build/protos/google/spanner/admin/instance/v1/common.proto +1 -1
  7. package/build/protos/google/spanner/executor/v1/cloud_executor.proto +1 -1
  8. package/build/protos/google/spanner/v1/commit_response.proto +1 -1
  9. package/build/protos/google/spanner/v1/keys.proto +1 -1
  10. package/build/protos/google/spanner/v1/mutation.proto +1 -1
  11. package/build/protos/google/spanner/v1/query_plan.proto +1 -1
  12. package/build/protos/google/spanner/v1/result_set.proto +1 -1
  13. package/build/protos/google/spanner/v1/spanner.proto +1 -1
  14. package/build/protos/google/spanner/v1/transaction.proto +1 -1
  15. package/build/protos/google/spanner/v1/type.proto +1 -1
  16. package/build/protos/protos.d.ts +30 -0
  17. package/build/protos/protos.js +204 -0
  18. package/build/protos/protos.json +43 -0
  19. package/build/src/codec.d.ts +55 -1
  20. package/build/src/codec.js +109 -4
  21. package/build/src/common-grpc/service.d.ts +0 -1
  22. package/build/src/common.js +2 -2
  23. package/build/src/database.d.ts +94 -7
  24. package/build/src/database.js +131 -0
  25. package/build/src/index.d.ts +59 -3
  26. package/build/src/index.js +55 -1
  27. package/build/src/instance.d.ts +0 -2
  28. package/build/src/partial-result-stream.d.ts +41 -1
  29. package/build/src/partial-result-stream.js +8 -3
  30. package/build/src/session-pool.d.ts +0 -2
  31. package/build/src/session-pool.js +7 -7
  32. package/build/src/table.d.ts +1 -0
  33. package/build/src/table.js +36 -2
  34. package/build/src/transaction-runner.d.ts +1 -0
  35. package/build/src/transaction-runner.js +5 -2
  36. package/build/src/transaction.d.ts +100 -2
  37. package/build/src/transaction.js +135 -30
  38. package/build/src/v1/database_admin_client.d.ts +175 -127
  39. package/build/src/v1/database_admin_client.js +85 -57
  40. package/build/src/v1/instance_admin_client.d.ts +0 -1
  41. package/build/src/v1/spanner_client.d.ts +0 -1
  42. package/build/src/v1/spanner_client_config.json +15 -14
  43. package/package.json +6 -6
@@ -13,8 +13,6 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- /// <reference types="node" />
17
- /// <reference types="node" />
18
16
  import { PreciseDate } from '@google-cloud/precise-date';
19
17
  import Long = require('long');
20
18
  import { EventEmitter } from 'events';
@@ -40,11 +38,52 @@ export interface TimestampBounds {
40
38
  exactStaleness?: number | spannerClient.protobuf.IDuration;
41
39
  returnReadTimestamp?: boolean;
42
40
  }
41
+ export interface BatchWriteOptions {
42
+ requestOptions?: Pick<IRequestOptions, 'priority' | 'transactionTag'>;
43
+ gaxOptions?: CallOptions;
44
+ }
43
45
  export interface RequestOptions {
44
46
  json?: boolean;
45
47
  jsonOptions?: JSONOptions;
46
48
  gaxOptions?: CallOptions;
47
49
  maxResumeRetries?: number;
50
+ /**
51
+ * An object where column names as keys and custom objects as corresponding
52
+ * values for deserialization. This is only needed for proto columns
53
+ * where deserialization logic is on user-specific code. When provided,
54
+ * the custom object enables deserialization of backend-received column data.
55
+ * If not provided, data remains serialized as buffer for Proto Messages and
56
+ * integer for Proto Enums.
57
+ *
58
+ * @example
59
+ * To obtain Proto Messages and Proto Enums as JSON objects, you must supply
60
+ * additional metadata. This metadata should include the protobufjs-cli
61
+ * generated proto message function and enum object. It encompasses the essential
62
+ * logic for proper data deserialization.
63
+ *
64
+ * Eg: To read data from Proto Columns in json format using DQL, you should pass
65
+ * columnsMetadata where key is the name of the column and value is the protobufjs-cli
66
+ * generated proto message function and enum object.
67
+ *
68
+ * const query = {
69
+ * sql: `SELECT SingerId,
70
+ * FirstName,
71
+ * LastName,
72
+ * SingerInfo,
73
+ * SingerGenre,
74
+ * SingerInfoArray,
75
+ * SingerGenreArray
76
+ * FROM Singers
77
+ * WHERE SingerId = 6`,
78
+ * columnsMetadata: {
79
+ * SingerInfo: music.SingerInfo,
80
+ * SingerInfoArray: music.SingerInfo,
81
+ * SingerGenre: music.Genre,
82
+ * SingerGenreArray: music.Genre,
83
+ * },
84
+ * };
85
+ */
86
+ columnsMetadata?: object;
48
87
  }
49
88
  export interface CommitOptions {
50
89
  requestOptions?: Pick<IRequestOptions, 'priority'>;
@@ -1452,6 +1491,56 @@ export declare class Transaction extends Dml {
1452
1491
  * mutations to the read data at a commit timestamp after the read timestamp).
1453
1492
  */
1454
1493
  useOptimisticLock(): void;
1494
+ /**
1495
+ * Use option excludeTxnFromChangeStreams to exclude read/write transactions
1496
+ * from being tracked in change streams.
1497
+ *
1498
+ * Enabling this options to true will effectively disable change stream tracking
1499
+ * for a specified transaction, allowing read/write transaction to operate without being
1500
+ * included in change streams.
1501
+ */
1502
+ excludeTxnFromChangeStreams(): void;
1503
+ }
1504
+ /**
1505
+ * A group of mutations to be committed together.
1506
+ * Related mutations should be placed in a group.
1507
+ *
1508
+ * For example, two mutations inserting rows with the same primary
1509
+ * key prefix in both parent and child tables are related.
1510
+ *
1511
+ * This object is created and returned from {@link Database#MutationGroup}.
1512
+ *
1513
+ * @example
1514
+ * ```
1515
+ * const {Spanner} = require('@google-cloud/spanner');
1516
+ * const spanner = new Spanner();
1517
+ *
1518
+ * const instance = spanner.instance('my-instance');
1519
+ * const database = instance.database('my-database');
1520
+ *
1521
+ * const mutationGroup = new MutationGroup();
1522
+ * mutationGroup.insert('Singers', {SingerId: '123', FirstName: 'David'});
1523
+ * mutationGroup.update('Singers', {SingerId: '123', FirstName: 'Marc'});
1524
+ *
1525
+ * database.batchWriteAtLeastOnce([mutationGroup], {})
1526
+ * .on('error', console.error)
1527
+ * .on('data', response => {
1528
+ * console.log('response: ', response);
1529
+ * })
1530
+ * .on('end', () => {
1531
+ * console.log('Request completed successfully');
1532
+ * });
1533
+ * ```
1534
+ */
1535
+ export declare class MutationGroup {
1536
+ private _proto;
1537
+ constructor();
1538
+ insert(table: string, rows: object | object[]): void;
1539
+ update(table: string, rows: object | object[]): void;
1540
+ upsert(table: string, rows: object | object[]): void;
1541
+ replace(table: string, rows: object | object[]): void;
1542
+ deleteRows(table: string, keys: Key[]): void;
1543
+ proto(): spannerClient.spanner.v1.BatchWriteRequest.IMutationGroup;
1455
1544
  }
1456
1545
  /**
1457
1546
  * This type of transaction is used to execute a single Partitioned DML
@@ -1469,6 +1558,15 @@ export declare class Transaction extends Dml {
1469
1558
  */
1470
1559
  export declare class PartitionedDml extends Dml {
1471
1560
  constructor(session: Session, options?: spannerClient.spanner.v1.TransactionOptions.PartitionedDml);
1561
+ /**
1562
+ * Use option excludeTxnFromChangeStreams to exclude partitionedDml
1563
+ * queries from being tracked in change streams.
1564
+ *
1565
+ * Enabling this options to true will effectively disable change stream tracking
1566
+ * for a specified partitionedDml query, allowing write queries to operate
1567
+ * without being included in change streams.
1568
+ */
1569
+ excludeTxnFromChangeStreams(): void;
1472
1570
  /**
1473
1571
  * Execute a DML statement and get the affected row count. Unlike
1474
1572
  * {@link Transaction#runUpdate} after using this method you should
@@ -15,7 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.PartitionedDml = exports.Transaction = exports.Dml = exports.Snapshot = void 0;
18
+ exports.PartitionedDml = exports.MutationGroup = exports.Transaction = exports.Dml = exports.Snapshot = void 0;
19
19
  const precise_date_1 = require("@google-cloud/precise-date");
20
20
  const promisify_1 = require("@google-cloud/promisify");
21
21
  const arrify = require("arrify");
@@ -25,9 +25,10 @@ const is = require("is");
25
25
  const stream_1 = require("stream");
26
26
  const codec_1 = require("./codec");
27
27
  const partial_result_stream_1 = require("./partial-result-stream");
28
- const common_1 = require("./common");
29
28
  const protos_1 = require("../protos/protos");
30
- var ReadLockMode = protos_1.google.spanner.v1.TransactionOptions.ReadWrite.ReadLockMode;
29
+ const common_1 = require("./common");
30
+ const protos_2 = require("../protos/protos");
31
+ var ReadLockMode = protos_2.google.spanner.v1.TransactionOptions.ReadWrite.ReadLockMode;
31
32
  const RETRY_INFO_TYPE = 'type.googleapis.com/google.rpc.retryinfo';
32
33
  const RETRY_INFO_BIN = 'google.rpc.retryinfo-bin';
33
34
  /**
@@ -354,7 +355,7 @@ class Snapshot extends events_1.EventEmitter {
354
355
  */
355
356
  createReadStream(table, request = {}) {
356
357
  var _a, _b, _c;
357
- const { gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions } = request;
358
+ const { gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions, columnsMetadata, } = request;
358
359
  const keySet = Snapshot.encodeKeySet(request);
359
360
  const transaction = {};
360
361
  if (this.id) {
@@ -376,6 +377,7 @@ class Snapshot extends events_1.EventEmitter {
376
377
  delete request.ranges;
377
378
  delete request.requestOptions;
378
379
  delete request.directedReadOptions;
380
+ delete request.columnsMetadata;
379
381
  const reqOpts = Object.assign(request, {
380
382
  session: this.session.formattedName_,
381
383
  requestOptions: this.configureTagOptions(typeof transaction.singleUse !== 'undefined', (_b = (_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.transactionTag) !== null && _b !== void 0 ? _b : undefined, requestOptions),
@@ -407,6 +409,7 @@ class Snapshot extends events_1.EventEmitter {
407
409
  json,
408
410
  jsonOptions,
409
411
  maxResumeRetries,
412
+ columnsMetadata,
410
413
  })) === null || _c === void 0 ? void 0 : _c.on('response', response => {
411
414
  if (response.metadata && response.metadata.transaction && !this.id) {
412
415
  this._update(response.metadata.transaction);
@@ -609,7 +612,7 @@ class Snapshot extends events_1.EventEmitter {
609
612
  }
610
613
  query = Object.assign({}, query);
611
614
  query.queryOptions = Object.assign(Object.assign({}, this.queryOptions), query.queryOptions);
612
- const { gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions } = query;
615
+ const { gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions, columnsMetadata, } = query;
613
616
  let reqOpts;
614
617
  const directedReadOptions = this._getDirectedReadOptions(query.directedReadOptions);
615
618
  const sanitizeRequest = () => {
@@ -633,6 +636,7 @@ class Snapshot extends events_1.EventEmitter {
633
636
  delete query.requestOptions;
634
637
  delete query.types;
635
638
  delete query.directedReadOptions;
639
+ delete query.columnsMetadata;
636
640
  reqOpts = Object.assign(query, {
637
641
  session: this.session.formattedName_,
638
642
  seqno: this._seqno++,
@@ -672,6 +676,7 @@ class Snapshot extends events_1.EventEmitter {
672
676
  json,
673
677
  jsonOptions,
674
678
  maxResumeRetries,
679
+ columnsMetadata,
675
680
  })
676
681
  .on('response', response => {
677
682
  if (response.metadata && response.metadata.transaction && !this.id) {
@@ -1247,13 +1252,7 @@ class Transaction extends Dml {
1247
1252
  * ```
1248
1253
  */
1249
1254
  deleteRows(table, keys) {
1250
- const keySet = {
1251
- keys: arrify(keys).map(codec_1.codec.convertToListValue),
1252
- };
1253
- const mutation = {
1254
- delete: { table, keySet },
1255
- };
1256
- this._queuedMutations.push(mutation);
1255
+ this._queuedMutations.push(buildDeleteMutation(table, keys));
1257
1256
  }
1258
1257
  /**
1259
1258
  * Insert rows of data into this table.
@@ -1471,24 +1470,7 @@ class Transaction extends Dml {
1471
1470
  * @param {object} rows Hash of key value pairs.
1472
1471
  */
1473
1472
  _mutate(method, table, keyVals) {
1474
- const rows = arrify(keyVals);
1475
- const columns = Transaction.getUniqueKeys(rows);
1476
- const values = rows.map((row, index) => {
1477
- const keys = Object.keys(row);
1478
- const missingColumns = columns.filter(column => !keys.includes(column));
1479
- if (missingColumns.length > 0) {
1480
- throw new google_gax_1.GoogleError([
1481
- `Row at index ${index} does not contain the correct number of columns.`,
1482
- `Missing columns: ${JSON.stringify(missingColumns)}`,
1483
- ].join('\n\n'));
1484
- }
1485
- const values = columns.map(column => row[column]);
1486
- return codec_1.codec.convertToListValue(values);
1487
- });
1488
- const mutation = {
1489
- [method]: { table, columns, values },
1490
- };
1491
- this._queuedMutations.push(mutation);
1473
+ this._queuedMutations.push(buildMutation(method, table, keyVals));
1492
1474
  }
1493
1475
  /**
1494
1476
  * Takes a list of rows and returns all unique column names.
@@ -1523,6 +1505,17 @@ class Transaction extends Dml {
1523
1505
  useOptimisticLock() {
1524
1506
  this._options.readWrite.readLockMode = ReadLockMode.OPTIMISTIC;
1525
1507
  }
1508
+ /**
1509
+ * Use option excludeTxnFromChangeStreams to exclude read/write transactions
1510
+ * from being tracked in change streams.
1511
+ *
1512
+ * Enabling this options to true will effectively disable change stream tracking
1513
+ * for a specified transaction, allowing read/write transaction to operate without being
1514
+ * included in change streams.
1515
+ */
1516
+ excludeTxnFromChangeStreams() {
1517
+ this._options.excludeTxnFromChangeStreams = true;
1518
+ }
1526
1519
  }
1527
1520
  exports.Transaction = Transaction;
1528
1521
  /*! Developer Documentation
@@ -1533,6 +1526,107 @@ exports.Transaction = Transaction;
1533
1526
  (0, promisify_1.promisifyAll)(Transaction, {
1534
1527
  exclude: ['deleteRows', 'insert', 'replace', 'update', 'upsert'],
1535
1528
  });
1529
+ /**
1530
+ * Builds an array of protobuf Mutations from the given row(s).
1531
+ *
1532
+ * @param {string} method - CRUD method (insert, update, etc.).
1533
+ * @param {string} table - Table to perform mutations in.
1534
+ * @param {object | object[]} keyVals - Hash of key-value pairs representing the rows.
1535
+ * @returns {spannerClient.spanner.v1.Mutation} - The formatted mutation.
1536
+ * @throws {GoogleError} - If a row does not contain the correct number of columns.
1537
+ */
1538
+ function buildMutation(method, table, keyVals) {
1539
+ const rows = arrify(keyVals);
1540
+ const columns = Transaction.getUniqueKeys(rows);
1541
+ const values = rows.map((row, index) => {
1542
+ const keys = Object.keys(row);
1543
+ const missingColumns = columns.filter(column => !keys.includes(column));
1544
+ if (missingColumns.length > 0) {
1545
+ throw new google_gax_1.GoogleError([
1546
+ `Row at index ${index} does not contain the correct number of columns.`,
1547
+ `Missing columns: ${JSON.stringify(missingColumns)}`,
1548
+ ].join('\n\n'));
1549
+ }
1550
+ const values = columns.map(column => row[column]);
1551
+ return codec_1.codec.convertToListValue(values);
1552
+ });
1553
+ const mutation = {
1554
+ [method]: { table, columns, values },
1555
+ };
1556
+ return mutation;
1557
+ }
1558
+ /**
1559
+ * Builds a delete mutation.
1560
+ *
1561
+ * @param {string} table - The name of the table.
1562
+ * @param {Key[]} keys - The keys for the rows to delete.
1563
+ * @returns {spannerClient.spanner.v1.Mutation} - The formatted delete mutation.
1564
+ */
1565
+ function buildDeleteMutation(table, keys) {
1566
+ const keySet = {
1567
+ keys: arrify(keys).map(codec_1.codec.convertToListValue),
1568
+ };
1569
+ const mutation = {
1570
+ delete: { table, keySet },
1571
+ };
1572
+ return mutation;
1573
+ }
1574
+ /**
1575
+ * A group of mutations to be committed together.
1576
+ * Related mutations should be placed in a group.
1577
+ *
1578
+ * For example, two mutations inserting rows with the same primary
1579
+ * key prefix in both parent and child tables are related.
1580
+ *
1581
+ * This object is created and returned from {@link Database#MutationGroup}.
1582
+ *
1583
+ * @example
1584
+ * ```
1585
+ * const {Spanner} = require('@google-cloud/spanner');
1586
+ * const spanner = new Spanner();
1587
+ *
1588
+ * const instance = spanner.instance('my-instance');
1589
+ * const database = instance.database('my-database');
1590
+ *
1591
+ * const mutationGroup = new MutationGroup();
1592
+ * mutationGroup.insert('Singers', {SingerId: '123', FirstName: 'David'});
1593
+ * mutationGroup.update('Singers', {SingerId: '123', FirstName: 'Marc'});
1594
+ *
1595
+ * database.batchWriteAtLeastOnce([mutationGroup], {})
1596
+ * .on('error', console.error)
1597
+ * .on('data', response => {
1598
+ * console.log('response: ', response);
1599
+ * })
1600
+ * .on('end', () => {
1601
+ * console.log('Request completed successfully');
1602
+ * });
1603
+ * ```
1604
+ */
1605
+ class MutationGroup {
1606
+ constructor() {
1607
+ this._proto =
1608
+ new protos_1.google.spanner.v1.BatchWriteRequest.MutationGroup();
1609
+ }
1610
+ insert(table, rows) {
1611
+ this._proto.mutations.push(buildMutation('insert', table, rows));
1612
+ }
1613
+ update(table, rows) {
1614
+ this._proto.mutations.push(buildMutation('update', table, rows));
1615
+ }
1616
+ upsert(table, rows) {
1617
+ this._proto.mutations.push(buildMutation('insertOrUpdate', table, rows));
1618
+ }
1619
+ replace(table, rows) {
1620
+ this._proto.mutations.push(buildMutation('replace', table, rows));
1621
+ }
1622
+ deleteRows(table, keys) {
1623
+ this._proto.mutations.push(buildDeleteMutation(table, keys));
1624
+ }
1625
+ proto() {
1626
+ return this._proto;
1627
+ }
1628
+ }
1629
+ exports.MutationGroup = MutationGroup;
1536
1630
  /**
1537
1631
  * This type of transaction is used to execute a single Partitioned DML
1538
1632
  * statement. Partitioned DML partitions the key space and runs the DML
@@ -1552,6 +1646,17 @@ class PartitionedDml extends Dml {
1552
1646
  super(session);
1553
1647
  this._options = { partitionedDml: options };
1554
1648
  }
1649
+ /**
1650
+ * Use option excludeTxnFromChangeStreams to exclude partitionedDml
1651
+ * queries from being tracked in change streams.
1652
+ *
1653
+ * Enabling this options to true will effectively disable change stream tracking
1654
+ * for a specified partitionedDml query, allowing write queries to operate
1655
+ * without being included in change streams.
1656
+ */
1657
+ excludeTxnFromChangeStreams() {
1658
+ this._options.excludeTxnFromChangeStreams = true;
1659
+ }
1555
1660
  runUpdate(query, callback) {
1556
1661
  super.runUpdate(query, (err, count) => {
1557
1662
  this.end();