@google-cloud/spanner 6.4.0 → 6.6.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.
@@ -24,20 +24,28 @@ import { Database } from './database';
24
24
  import { NormalCallback } from './common';
25
25
  import { grpc, CallOptions } from 'google-gax';
26
26
  import IRequestOptions = google.spanner.v1.IRequestOptions;
27
- export declare type GetSessionResponse = [Session, r.Response];
27
+ export type GetSessionResponse = [Session, r.Response];
28
28
  /**
29
- * enum to capture the possible session types
29
+ * @deprecated. enum to capture the possible session types
30
30
  */
31
31
  export declare const enum types {
32
32
  ReadOnly = "readonly",
33
33
  ReadWrite = "readwrite"
34
34
  }
35
- export declare type GetSessionMetadataCallback = NormalCallback<google.spanner.v1.ISession>;
36
- export declare type GetSessionMetadataResponse = [google.spanner.v1.ISession];
37
- export declare type KeepAliveCallback = NormalCallback<google.spanner.v1.IResultSet>;
38
- export declare type KeepAliveResponse = [google.spanner.v1.IResultSet];
39
- export declare type DeleteSessionResponse = [google.protobuf.IEmpty];
40
- export declare type DeleteSessionCallback = NormalCallback<google.protobuf.IEmpty>;
35
+ export interface GetSessionMetadataResponse {
36
+ name?: string | null;
37
+ labels?: {
38
+ [k: string]: string;
39
+ } | null;
40
+ createTime?: google.protobuf.ITimestamp | null;
41
+ approximateLastUseTime?: google.protobuf.ITimestamp | null;
42
+ databaseRole?: string | null;
43
+ }
44
+ export type GetSessionMetadataCallback = NormalCallback<GetSessionMetadataResponse>;
45
+ export type KeepAliveCallback = NormalCallback<google.spanner.v1.IResultSet>;
46
+ export type KeepAliveResponse = [google.spanner.v1.IResultSet];
47
+ export type DeleteSessionResponse = [google.protobuf.IEmpty];
48
+ export type DeleteSessionCallback = NormalCallback<google.protobuf.IEmpty>;
41
49
  /**
42
50
  * Create a Session object to interact with a Cloud Spanner session.
43
51
  *
@@ -81,7 +89,6 @@ export declare type DeleteSessionCallback = NormalCallback<google.protobuf.IEmpt
81
89
  export declare class Session extends common.GrpcServiceObject {
82
90
  id: string;
83
91
  formattedName_?: string;
84
- type?: types;
85
92
  txn?: Transaction;
86
93
  lastUsed?: number;
87
94
  lastError?: grpc.ServiceError;
@@ -185,6 +185,9 @@ class Session extends common.GrpcServiceObject {
185
185
  typeof optionsOrCallback === 'function'
186
186
  ? optionsOrCallback
187
187
  : callback;
188
+ this.labels = options.labels || null;
189
+ this.databaseRole =
190
+ options.databaseRole || database.databaseRole || null;
188
191
  return database.createSession(options, (err, session, apiResponse) => {
189
192
  if (err) {
190
193
  callback(err, null, apiResponse);
@@ -232,6 +235,8 @@ class Session extends common.GrpcServiceObject {
232
235
  headers: this.resourceHeader_,
233
236
  }, (err, resp) => {
234
237
  if (resp) {
238
+ resp.databaseRole = resp.creatorRole;
239
+ delete resp.creatorRole;
235
240
  this.metadata = resp;
236
241
  }
237
242
  callback(err, resp);
@@ -20,33 +20,33 @@ import { ReadRequest, TimestampBounds, CommitOptions, CommitResponse, ReadRespon
20
20
  import { google as databaseAdmin } from '../protos/protos';
21
21
  import { Schema, LongRunningCallback } from './common';
22
22
  import IRequestOptions = databaseAdmin.spanner.v1.IRequestOptions;
23
- export declare type Key = string | string[];
24
- export declare type CreateTableResponse = [
23
+ export type Key = string | string[];
24
+ export type CreateTableResponse = [
25
25
  Table,
26
26
  GaxOperation,
27
27
  databaseAdmin.longrunning.IOperation
28
28
  ];
29
- export declare type CreateTableCallback = LongRunningCallback<Table>;
30
- export declare type DropTableResponse = UpdateSchemaResponse;
31
- export declare type DropTableCallback = UpdateSchemaCallback;
29
+ export type CreateTableCallback = LongRunningCallback<Table>;
30
+ export type DropTableResponse = UpdateSchemaResponse;
31
+ export type DropTableCallback = UpdateSchemaCallback;
32
32
  interface MutateRowsOptions extends CommitOptions {
33
33
  requestOptions?: Omit<IRequestOptions, 'requestTag'>;
34
34
  }
35
- export declare type DeleteRowsCallback = CommitCallback;
36
- export declare type DeleteRowsResponse = CommitResponse;
37
- export declare type DeleteRowsOptions = MutateRowsOptions;
38
- export declare type InsertRowsCallback = CommitCallback;
39
- export declare type InsertRowsResponse = CommitResponse;
40
- export declare type InsertRowsOptions = MutateRowsOptions;
41
- export declare type ReplaceRowsCallback = CommitCallback;
42
- export declare type ReplaceRowsResponse = CommitResponse;
43
- export declare type ReplaceRowsOptions = MutateRowsOptions;
44
- export declare type UpdateRowsCallback = CommitCallback;
45
- export declare type UpdateRowsResponse = CommitResponse;
46
- export declare type UpdateRowsOptions = MutateRowsOptions;
47
- export declare type UpsertRowsCallback = CommitCallback;
48
- export declare type UpsertRowsResponse = CommitResponse;
49
- export declare type UpsertRowsOptions = MutateRowsOptions;
35
+ export type DeleteRowsCallback = CommitCallback;
36
+ export type DeleteRowsResponse = CommitResponse;
37
+ export type DeleteRowsOptions = MutateRowsOptions;
38
+ export type InsertRowsCallback = CommitCallback;
39
+ export type InsertRowsResponse = CommitResponse;
40
+ export type InsertRowsOptions = MutateRowsOptions;
41
+ export type ReplaceRowsCallback = CommitCallback;
42
+ export type ReplaceRowsResponse = CommitResponse;
43
+ export type ReplaceRowsOptions = MutateRowsOptions;
44
+ export type UpdateRowsCallback = CommitCallback;
45
+ export type UpdateRowsResponse = CommitResponse;
46
+ export type UpdateRowsOptions = MutateRowsOptions;
47
+ export type UpsertRowsCallback = CommitCallback;
48
+ export type UpsertRowsResponse = CommitResponse;
49
+ export type UpsertRowsOptions = MutateRowsOptions;
50
50
  /**
51
51
  * Create a Table object to interact with a table in a Cloud Spanner
52
52
  * database.
@@ -35,7 +35,7 @@ export interface RunTransactionOptions {
35
35
  * @param {Transaction} transaction The transaction object. The transaction has
36
36
  * already been created, and is ready to be queried and committed against.
37
37
  */
38
- export declare type RunTransactionCallback = NormalCallback<Transaction>;
38
+ export type RunTransactionCallback = NormalCallback<Transaction>;
39
39
  /**
40
40
  * A function to execute in the context of a transaction.
41
41
  * @callback AsyncRunTransactionCallback
@@ -63,6 +63,7 @@ class Runner {
63
63
  this.attempts = 0;
64
64
  this.session = session;
65
65
  this.transaction = transaction;
66
+ this.transaction.useInRunner();
66
67
  const defaults = { timeout: 3600000 };
67
68
  this.options = Object.assign(defaults, options);
68
69
  }
@@ -115,7 +116,9 @@ class Runner {
115
116
  return transaction;
116
117
  }
117
118
  const transaction = this.session.transaction(this.session.parent.queryOptions_);
118
- await transaction.begin();
119
+ if (this.attempts > 0) {
120
+ await transaction.begin();
121
+ }
119
122
  return transaction;
120
123
  }
121
124
  /**
@@ -30,7 +30,7 @@ import { NormalCallback } from './common';
30
30
  import { google } from '../protos/protos';
31
31
  import IQueryOptions = google.spanner.v1.ExecuteSqlRequest.IQueryOptions;
32
32
  import IRequestOptions = google.spanner.v1.IRequestOptions;
33
- export declare type Rows = Array<Row | Json>;
33
+ export type Rows = Array<Row | Json>;
34
34
  export interface TimestampBounds {
35
35
  strong?: boolean;
36
36
  minReadTimestamp?: PreciseDate | spannerClient.protobuf.ITimestamp;
@@ -88,21 +88,21 @@ export interface ReadRequest extends RequestOptions {
88
88
  export interface BatchUpdateError extends grpc.ServiceError {
89
89
  rowCounts: number[];
90
90
  }
91
- export declare type CommitRequest = spannerClient.spanner.v1.ICommitRequest;
92
- export declare type BatchUpdateResponse = [
91
+ export type CommitRequest = spannerClient.spanner.v1.ICommitRequest;
92
+ export type BatchUpdateResponse = [
93
93
  number[],
94
94
  spannerClient.spanner.v1.ExecuteBatchDmlResponse
95
95
  ];
96
- export declare type BeginResponse = [spannerClient.spanner.v1.ITransaction];
97
- export declare type BeginTransactionCallback = NormalCallback<spannerClient.spanner.v1.ITransaction>;
98
- export declare type CommitResponse = [spannerClient.spanner.v1.ICommitResponse];
99
- export declare type ReadResponse = [Rows];
100
- export declare type RunResponse = [
96
+ export type BeginResponse = [spannerClient.spanner.v1.ITransaction];
97
+ export type BeginTransactionCallback = NormalCallback<spannerClient.spanner.v1.ITransaction>;
98
+ export type CommitResponse = [spannerClient.spanner.v1.ICommitResponse];
99
+ export type ReadResponse = [Rows];
100
+ export type RunResponse = [
101
101
  Rows,
102
102
  spannerClient.spanner.v1.ResultSetStats,
103
103
  spannerClient.spanner.v1.ResultSetMetadata
104
104
  ];
105
- export declare type RunUpdateResponse = [number];
105
+ export type RunUpdateResponse = [number];
106
106
  export interface BatchUpdateOptions {
107
107
  requestOptions?: Omit<IRequestOptions, 'transactionTag'>;
108
108
  gaxOptions?: CallOptions;
@@ -114,14 +114,14 @@ export interface BatchUpdateOptions {
114
114
  requestOptions?: Omit<IRequestOptions, 'transactionTag'>;
115
115
  gaxOptions?: CallOptions;
116
116
  }
117
- export declare type ReadCallback = NormalCallback<Rows>;
117
+ export type ReadCallback = NormalCallback<Rows>;
118
118
  export interface RunCallback {
119
119
  (err: null | grpc.ServiceError, rows: Rows, stats: spannerClient.spanner.v1.ResultSetStats, metadata?: spannerClient.spanner.v1.ResultSetMetadata): void;
120
120
  }
121
121
  export interface RunUpdateCallback {
122
122
  (err: null | grpc.ServiceError, rowCount: number): void;
123
123
  }
124
- export declare type CommitCallback = NormalCallback<spannerClient.spanner.v1.ICommitResponse>;
124
+ export type CommitCallback = NormalCallback<spannerClient.spanner.v1.ICommitResponse>;
125
125
  /**
126
126
  * @typedef {object} TimestampBounds
127
127
  * @property {boolean} [strong=true] Read at a timestamp where all previously
@@ -179,6 +179,9 @@ export declare type CommitCallback = NormalCallback<spannerClient.spanner.v1.ICo
179
179
  export declare class Snapshot extends EventEmitter {
180
180
  protected _options: spannerClient.spanner.v1.ITransactionOptions;
181
181
  protected _seqno: number;
182
+ protected _idWaiter: Readable;
183
+ protected _inlineBeginStarted: any;
184
+ protected _useInRunner: boolean;
182
185
  id?: Uint8Array | string;
183
186
  ended: boolean;
184
187
  metadata?: spannerClient.spanner.v1.ITransaction;
@@ -835,6 +838,22 @@ export declare class Snapshot extends EventEmitter {
835
838
  [field: string]: spannerClient.spanner.v1.Type;
836
839
  };
837
840
  };
841
+ /**
842
+ * Update transaction properties from the response.
843
+ *
844
+ * @private
845
+ *
846
+ * @param {spannerClient.spanner.v1.ITransaction} resp Response object.
847
+ */
848
+ protected _update(resp: spannerClient.spanner.v1.ITransaction): void;
849
+ /**
850
+ * Wrap `makeRequest` function with the lock to make sure the inline begin
851
+ * transaction can happen only once.
852
+ *
853
+ * @param makeRequest
854
+ * @private
855
+ */
856
+ private _wrapWithIdWaiter;
838
857
  }
839
858
  /**
840
859
  * Never use DML class directly. Instead, it should be extended upon
@@ -1390,6 +1409,10 @@ export declare class Transaction extends Dml {
1390
1409
  * @returns {string[]}
1391
1410
  */
1392
1411
  static getUniqueKeys(rows: object[]): string[];
1412
+ /**
1413
+ * Mark transaction as started from the runner.
1414
+ */
1415
+ useInRunner(): void;
1393
1416
  }
1394
1417
  /**
1395
1418
  * This type of transaction is used to execute a single Partitioned DML
@@ -129,6 +129,7 @@ class Snapshot extends events_1.EventEmitter {
129
129
  constructor(session, options, queryOptions) {
130
130
  super();
131
131
  this._seqno = 1;
132
+ this._useInRunner = false;
132
133
  this.ended = false;
133
134
  this.session = session;
134
135
  this.queryOptions = Object.assign({}, queryOptions);
@@ -139,6 +140,10 @@ class Snapshot extends events_1.EventEmitter {
139
140
  this.resourceHeader_ = {
140
141
  [common_1.CLOUD_RESOURCE_HEADER]: this.session.parent.formattedName_,
141
142
  };
143
+ this._idWaiter = new stream_1.Readable({
144
+ read() { },
145
+ });
146
+ this._inlineBeginStarted = false;
142
147
  }
143
148
  begin(gaxOptionsOrCallback, cb) {
144
149
  const gaxOpts = typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {};
@@ -167,13 +172,7 @@ class Snapshot extends events_1.EventEmitter {
167
172
  callback(err, resp);
168
173
  return;
169
174
  }
170
- const { id, readTimestamp } = resp;
171
- this.id = id;
172
- this.metadata = resp;
173
- if (readTimestamp) {
174
- this.readTimestampProto = readTimestamp;
175
- this.readTimestamp = new precise_date_1.PreciseDate(readTimestamp);
176
- }
175
+ this._update(resp);
177
176
  callback(null, resp);
178
177
  });
179
178
  }
@@ -346,13 +345,16 @@ class Snapshot extends events_1.EventEmitter {
346
345
  * ```
347
346
  */
348
347
  createReadStream(table, request = {}) {
349
- var _a, _b;
348
+ var _a, _b, _c;
350
349
  const { gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions } = request;
351
350
  const keySet = Snapshot.encodeKeySet(request);
352
351
  const transaction = {};
353
352
  if (this.id) {
354
353
  transaction.id = this.id;
355
354
  }
355
+ else if (this._options.readWrite) {
356
+ transaction.begin = this._options;
357
+ }
356
358
  else {
357
359
  transaction.singleUse = this._options;
358
360
  }
@@ -372,6 +374,10 @@ class Snapshot extends events_1.EventEmitter {
372
374
  keySet,
373
375
  });
374
376
  const makeRequest = (resumeToken) => {
377
+ if (this.id && transaction.begin) {
378
+ delete transaction.begin;
379
+ transaction.id = this.id;
380
+ }
375
381
  return this.requestStream({
376
382
  client: 'SpannerClient',
377
383
  method: 'streamingRead',
@@ -380,10 +386,18 @@ class Snapshot extends events_1.EventEmitter {
380
386
  headers: this.resourceHeader_,
381
387
  });
382
388
  };
383
- return (0, partial_result_stream_1.partialResultStream)(makeRequest, {
389
+ return (_c = (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
384
390
  json,
385
391
  jsonOptions,
386
392
  maxResumeRetries,
393
+ })) === null || _c === void 0 ? void 0 : _c.on('response', response => {
394
+ if (response.metadata && response.metadata.transaction && !this.id) {
395
+ this._update(response.metadata.transaction);
396
+ }
397
+ }).on('error', () => {
398
+ if (!this.id && this._useInRunner) {
399
+ this.begin();
400
+ }
387
401
  });
388
402
  }
389
403
  /**
@@ -464,6 +478,9 @@ class Snapshot extends events_1.EventEmitter {
464
478
  .on('response', response => {
465
479
  if (response.metadata) {
466
480
  metadata = response.metadata;
481
+ if (metadata.transaction && !this.id) {
482
+ this._update(metadata.transaction);
483
+ }
467
484
  }
468
485
  })
469
486
  .on('data', row => rows.push(row))
@@ -583,6 +600,9 @@ class Snapshot extends events_1.EventEmitter {
583
600
  if (this.id) {
584
601
  transaction.id = this.id;
585
602
  }
603
+ else if (this._options.readWrite) {
604
+ transaction.begin = this._options;
605
+ }
586
606
  else {
587
607
  transaction.singleUse = this._options;
588
608
  }
@@ -602,7 +622,7 @@ class Snapshot extends events_1.EventEmitter {
602
622
  });
603
623
  };
604
624
  const makeRequest = (resumeToken) => {
605
- if (!reqOpts) {
625
+ if (!reqOpts || (this.id && !reqOpts.transaction.id)) {
606
626
  try {
607
627
  sanitizeRequest();
608
628
  }
@@ -620,10 +640,20 @@ class Snapshot extends events_1.EventEmitter {
620
640
  headers: this.resourceHeader_,
621
641
  });
622
642
  };
623
- return (0, partial_result_stream_1.partialResultStream)(makeRequest, {
643
+ return (0, partial_result_stream_1.partialResultStream)(this._wrapWithIdWaiter(makeRequest), {
624
644
  json,
625
645
  jsonOptions,
626
646
  maxResumeRetries,
647
+ })
648
+ .on('response', response => {
649
+ if (response.metadata && response.metadata.transaction && !this.id) {
650
+ this._update(response.metadata.transaction);
651
+ }
652
+ })
653
+ .on('error', () => {
654
+ if (!this.id && this._useInRunner) {
655
+ this.begin();
656
+ }
627
657
  });
628
658
  }
629
659
  /**
@@ -728,6 +758,42 @@ class Snapshot extends events_1.EventEmitter {
728
758
  }
729
759
  return { params, paramTypes };
730
760
  }
761
+ /**
762
+ * Update transaction properties from the response.
763
+ *
764
+ * @private
765
+ *
766
+ * @param {spannerClient.spanner.v1.ITransaction} resp Response object.
767
+ */
768
+ _update(resp) {
769
+ const { id, readTimestamp } = resp;
770
+ this.id = id;
771
+ this.metadata = resp;
772
+ if (readTimestamp) {
773
+ this.readTimestampProto = readTimestamp;
774
+ this.readTimestamp = new precise_date_1.PreciseDate(readTimestamp);
775
+ }
776
+ this._idWaiter.emit('notify');
777
+ }
778
+ /**
779
+ * Wrap `makeRequest` function with the lock to make sure the inline begin
780
+ * transaction can happen only once.
781
+ *
782
+ * @param makeRequest
783
+ * @private
784
+ */
785
+ _wrapWithIdWaiter(makeRequest) {
786
+ if (this.id || !this._options.readWrite) {
787
+ return makeRequest;
788
+ }
789
+ if (!this._inlineBeginStarted) {
790
+ this._inlineBeginStarted = true;
791
+ return makeRequest;
792
+ }
793
+ return (resumeToken) => this._idWaiter.once('notify', () => makeRequest(resumeToken)
794
+ .on('data', chunk => this._idWaiter.emit('data', chunk))
795
+ .once('end', () => this._idWaiter.emit('end')));
796
+ }
731
797
  }
732
798
  exports.Snapshot = Snapshot;
733
799
  /*! Developer Documentation
@@ -883,10 +949,17 @@ class Transaction extends Dml {
883
949
  const { params, paramTypes } = Snapshot.encodeParams(query);
884
950
  return { sql, params, paramTypes };
885
951
  });
952
+ const transaction = {};
953
+ if (this.id) {
954
+ transaction.id = this.id;
955
+ }
956
+ else {
957
+ transaction.begin = this._options;
958
+ }
886
959
  const reqOpts = {
887
960
  session: this.session.formattedName_,
888
961
  requestOptions: this.configureTagOptions(false, (_b = (_a = this.requestOptions) === null || _a === void 0 ? void 0 : _a.transactionTag) !== null && _b !== void 0 ? _b : undefined, options.requestOptions),
889
- transaction: { id: this.id },
962
+ transaction,
890
963
  seqno: this._seqno++,
891
964
  statements,
892
965
  };
@@ -897,6 +970,7 @@ class Transaction extends Dml {
897
970
  gaxOpts,
898
971
  headers: this.resourceHeader_,
899
972
  }, (err, resp) => {
973
+ var _a;
900
974
  let batchUpdateError;
901
975
  if (err) {
902
976
  const rowCounts = [];
@@ -905,6 +979,11 @@ class Transaction extends Dml {
905
979
  return;
906
980
  }
907
981
  const { resultSets, status } = resp;
982
+ for (const resultSet of resultSets) {
983
+ if (!this.id && ((_a = resultSet.metadata) === null || _a === void 0 ? void 0 : _a.transaction)) {
984
+ this._update(resultSet.metadata.transaction);
985
+ }
986
+ }
908
987
  const rowCounts = resultSets.map(({ stats }) => {
909
988
  return ((stats &&
910
989
  Number(stats[stats.rowCount])) ||
@@ -944,9 +1023,13 @@ class Transaction extends Dml {
944
1023
  if (this.id) {
945
1024
  reqOpts.transactionId = this.id;
946
1025
  }
947
- else {
1026
+ else if (!this._useInRunner) {
948
1027
  reqOpts.singleUseTransaction = this._options;
949
1028
  }
1029
+ else {
1030
+ this.begin().then(() => this.commit(options, callback));
1031
+ return;
1032
+ }
950
1033
  if ('returnCommitStats' in options &&
951
1034
  options.returnCommitStats) {
952
1035
  reqOpts.returnCommitStats = options.returnCommitStats;
@@ -1339,6 +1422,12 @@ class Transaction extends Dml {
1339
1422
  const unique = new Set(allKeys);
1340
1423
  return Array.from(unique).sort();
1341
1424
  }
1425
+ /**
1426
+ * Mark transaction as started from the runner.
1427
+ */
1428
+ useInRunner() {
1429
+ this._useInRunner = true;
1430
+ }
1342
1431
  }
1343
1432
  exports.Transaction = Transaction;
1344
1433
  /*! Developer Documentation
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@google-cloud/spanner",
3
3
  "description": "Cloud Spanner Client Library for Node.js",
4
- "version": "6.4.0",
4
+ "version": "6.6.0",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Google Inc.",
7
7
  "engines": {
@@ -64,7 +64,7 @@
64
64
  "events-intercept": "^2.0.0",
65
65
  "extend": "^3.0.2",
66
66
  "google-auth-library": "^8.0.2",
67
- "google-gax": "^3.3.0",
67
+ "google-gax": "^3.5.2",
68
68
  "grpc-gcp": "^1.0.0",
69
69
  "is": "^3.2.1",
70
70
  "lodash.snakecase": "^4.1.1",
@@ -91,7 +91,7 @@
91
91
  "@types/request": "^2.48.3",
92
92
  "@types/sinon": "^10.0.0",
93
93
  "@types/through2": "^2.0.34",
94
- "@types/uuid": "^8.0.0",
94
+ "@types/uuid": "^9.0.0",
95
95
  "binary-search-bounds": "^2.0.4",
96
96
  "c8": "^7.0.0",
97
97
  "codecov": "^3.0.2",
@@ -99,7 +99,7 @@
99
99
  "dedent": "^0.7.0",
100
100
  "execa": "^5.0.0",
101
101
  "gts": "^3.1.0",
102
- "jsdoc": "^3.6.2",
102
+ "jsdoc": "^4.0.0",
103
103
  "jsdoc-fresh": "^2.0.0",
104
104
  "jsdoc-region-tag": "^2.0.0",
105
105
  "linkinator": "^4.0.0",