GeezerVison is showing github.com — feat: add tagging support (#1419) · googleapis/nodejs-spanner@4770dab
This repository was archived by the owner on Oct 20, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,3 +12,4 @@ system-test/*key.json | 12 | 12 | .DS_Store | 13 | 13 | package-lock.json | 14 | 14 | __pycache__ | | 15 | +.idea |
Collapse file Copy file name to clipboardExpand all lines: src/database.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2662,6 +2662,10 @@ class Database extends common.GrpcServiceObject { | 2662 | 2662 | while (true) { | 2663 | 2663 | try { | 2664 | 2664 | const [session, transaction] = await promisify(getWriteSession)(); | | 2665 | + transaction.requestOptions = Object.assign( | | 2666 | + transaction.requestOptions || {}, | | 2667 | + options.requestOptions | | 2668 | + ); | 2665 | 2669 | const runner = new AsyncTransactionRunner<T>( | 2666 | 2670 | session, | 2667 | 2671 | transaction, | |
Collapse file Copy file name to clipboardExpand all lines: src/session.ts +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,7 @@ import { | 38 | 38 | import {ServiceObjectConfig} from '@google-cloud/common'; | 39 | 39 | import {NormalCallback, CLOUD_RESOURCE_HEADER} from './common'; | 40 | 40 | import {grpc, CallOptions} from 'google-gax'; | | 41 | +import IRequestOptions = google.spanner.v1.IRequestOptions; | 41 | 42 |
| 42 | 43 | export type GetSessionResponse = [Session, r.Response]; | 43 | 44 |
| @@ -450,9 +451,10 @@ export class Session extends common.GrpcServiceObject { | 450 | 451 | * const transaction = session.transaction(); | 451 | 452 | */ | 452 | 453 | transaction( | 453 | | - queryOptions?: google.spanner.v1.ExecuteSqlRequest.IQueryOptions | | 454 | + queryOptions?: google.spanner.v1.ExecuteSqlRequest.IQueryOptions, | | 455 | + requestOptions?: Pick<IRequestOptions, 'transactionTag'> | 454 | 456 | ) { | 455 | | - return new Transaction(this, undefined, queryOptions); | | 457 | + return new Transaction(this, undefined, queryOptions, requestOptions); | 456 | 458 | } | 457 | 459 | /** | 458 | 460 | * Format the session name to include the parent database's name. | |
Collapse file Copy file name to clipboardExpand all lines: src/table.ts +15-7Lines changed: 15 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,7 @@ import { | 30 | 30 | } from './transaction'; | 31 | 31 | import {google as databaseAdmin} from '../protos/protos'; | 32 | 32 | import {Schema, LongRunningCallback} from './common'; | | 33 | +import IRequestOptions = databaseAdmin.spanner.v1.IRequestOptions; | 33 | 34 |
| 34 | 35 | export type Key = string | string[]; | 35 | 36 |
| @@ -43,25 +44,30 @@ export type CreateTableCallback = LongRunningCallback<Table>; | 43 | 44 | export type DropTableResponse = UpdateSchemaResponse; | 44 | 45 | export type DropTableCallback = UpdateSchemaCallback; | 45 | 46 |
| | 47 | +interface MutateRowsOptions extends CommitOptions { | | 48 | + requestOptions?: Omit<IRequestOptions, 'requestTag'>; | | 49 | +} | | 50 | + | 46 | 51 | export type DeleteRowsCallback = CommitCallback; | 47 | 52 | export type DeleteRowsResponse = CommitResponse; | 48 | | -export type DeleteRowsOptions = CommitOptions; | | 53 | +export type DeleteRowsOptions = MutateRowsOptions; | 49 | 54 |
| 50 | 55 | export type InsertRowsCallback = CommitCallback; | 51 | 56 | export type InsertRowsResponse = CommitResponse; | 52 | | -export type InsertRowsOptions = CommitOptions; | | 57 | +export type InsertRowsOptions = MutateRowsOptions; | 53 | 58 |
| 54 | 59 | export type ReplaceRowsCallback = CommitCallback; | 55 | 60 | export type ReplaceRowsResponse = CommitResponse; | 56 | | -export type ReplaceRowsOptions = CommitOptions; | | 61 | +export type ReplaceRowsOptions = MutateRowsOptions; | 57 | 62 |
| 58 | 63 | export type UpdateRowsCallback = CommitCallback; | 59 | 64 | export type UpdateRowsResponse = CommitResponse; | 60 | | -export type UpdateRowsOptions = CommitOptions; | | 65 | +export type UpdateRowsOptions = MutateRowsOptions; | 61 | 66 |
| 62 | 67 | export type UpsertRowsCallback = CommitCallback; | 63 | 68 | export type UpsertRowsResponse = CommitResponse; | 64 | | -export type UpsertRowsOptions = CommitOptions; | | 69 | +export type UpsertRowsOptions = MutateRowsOptions; | | 70 | + | 65 | 71 | /** | 66 | 72 | * Create a Table object to interact with a table in a Cloud Spanner | 67 | 73 | * database. | @@ -1012,10 +1018,12 @@ class Table { | 1012 | 1018 | private _mutate( | 1013 | 1019 | method: 'deleteRows' | 'insert' | 'replace' | 'update' | 'upsert', | 1014 | 1020 | rows: object | object[], | 1015 | | - options: CommitOptions | CallOptions, | | 1021 | + options: MutateRowsOptions | CallOptions = {}, | 1016 | 1022 | callback: CommitCallback | 1017 | 1023 | ): void { | 1018 | | - this.database.runTransaction((err, transaction) => { | | 1024 | + const requestOptions = | | 1025 | + 'requestOptions' in options ? options.requestOptions : {}; | | 1026 | + this.database.runTransaction({requestOptions}, (err, transaction) => { | 1019 | 1027 | if (err) { | 1020 | 1028 | callback(err); | 1021 | 1029 | return; | |
Collapse file Copy file name to clipboardExpand all lines: src/transaction-runner.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,8 @@ import {Transaction} from './transaction'; | 24 | 24 | import {NormalCallback} from './common'; | 25 | 25 | import {isSessionNotFoundError} from './session-pool'; | 26 | 26 | import {Database} from './database'; | | 27 | +import {google} from '../protos/protos'; | | 28 | +import IRequestOptions = google.spanner.v1.IRequestOptions; | 27 | 29 |
| 28 | 30 | // eslint-disable-next-line @typescript-eslint/no-var-requires | 29 | 31 | const jsonProtos = require('../protos/protos.json'); | @@ -41,6 +43,7 @@ const RetryInfo = Root.fromJSON(jsonProtos).lookup('google.rpc.RetryInfo'); | 41 | 43 | */ | 42 | 44 | export interface RunTransactionOptions { | 43 | 45 | timeout?: number; | | 46 | + requestOptions?: Pick<IRequestOptions, 'transactionTag'>; | 44 | 47 | } | 45 | 48 |
| 46 | 49 | /** | |
Collapse file Copy file name to clipboardExpand all lines: src/transaction.ts +68-11Lines changed: 68 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -125,6 +125,10 @@ export type RunResponse = [ | 125 | 125 | ]; | 126 | 126 | export type RunUpdateResponse = [number]; | 127 | 127 |
| | 128 | +export interface BatchUpdateOptions { | | 129 | + requestOptions?: Omit<IRequestOptions, 'transactionTag'>; | | 130 | + gaxOptions?: CallOptions; | | 131 | +} | 128 | 132 | export interface BatchUpdateCallback { | 129 | 133 | ( | 130 | 134 | err: null | BatchUpdateError, | @@ -220,6 +224,7 @@ export class Snapshot extends EventEmitter { | 220 | 224 | session: Session; | 221 | 225 | queryOptions?: IQueryOptions; | 222 | 226 | resourceHeader_: {[k: string]: string}; | | 227 | + requestOptions?: Pick<IRequestOptions, 'transactionTag'>; | 223 | 228 |
| 224 | 229 | /** | 225 | 230 | * The transaction ID. | @@ -344,6 +349,14 @@ export class Snapshot extends EventEmitter { | 344 | 349 | options, | 345 | 350 | }; | 346 | 351 |
| | 352 | + // Only hand crafted read-write transactions will be able to set a | | 353 | + // transaction tag for the BeginTransaction RPC. Also, this.requestOptions | | 354 | + // is only set in the constructor of Transaction, which is the constructor | | 355 | + // for read/write transactions. | | 356 | + if (this.requestOptions) { | | 357 | + reqOpts.requestOptions = this.requestOptions; | | 358 | + } | | 359 | + | 347 | 360 | this.request( | 348 | 361 | { | 349 | 362 | client: 'SpannerClient', | @@ -538,7 +551,8 @@ export class Snapshot extends EventEmitter { | 538 | 551 | table: string, | 539 | 552 | request = {} as ReadRequest | 540 | 553 | ): PartialResultStream { | 541 | | - const {gaxOptions, json, jsonOptions, maxResumeRetries} = request; | | 554 | + const {gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions} = | | 555 | + request; | 542 | 556 | const keySet = Snapshot.encodeKeySet(request); | 543 | 557 | const transaction: spannerClient.spanner.v1.ITransactionSelector = {}; | 544 | 558 |
| @@ -556,13 +570,22 @@ export class Snapshot extends EventEmitter { | 556 | 570 | delete request.maxResumeRetries; | 557 | 571 | delete request.keys; | 558 | 572 | delete request.ranges; | | 573 | + delete request.requestOptions; | 559 | 574 |
| 560 | | - const reqOpts: ReadRequest = Object.assign(request, { | 561 | | - session: this.session.formattedName_!, | 562 | | - transaction, | 563 | | - table, | 564 | | - keySet, | 565 | | - }); | | 575 | + const reqOpts: spannerClient.spanner.v1.IReadRequest = Object.assign( | | 576 | + request, | | 577 | + { | | 578 | + session: this.session.formattedName_!, | | 579 | + requestOptions: this.configureTagOptions( | | 580 | + typeof transaction.singleUse !== 'undefined', | | 581 | + this.requestOptions?.transactionTag!, | | 582 | + requestOptions | | 583 | + ), | | 584 | + transaction, | | 585 | + table, | | 586 | + keySet, | | 587 | + } | | 588 | + ); | 566 | 589 |
| 567 | 590 | const makeRequest = (resumeToken?: ResumeToken): Readable => { | 568 | 591 | return this.requestStream({ | @@ -965,7 +988,8 @@ export class Snapshot extends EventEmitter { | 965 | 988 | query.queryOptions | 966 | 989 | ); | 967 | 990 |
| 968 | | - const {gaxOptions, json, jsonOptions, maxResumeRetries} = query; | | 991 | + const {gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions} = | | 992 | + query; | 969 | 993 | let reqOpts; | 970 | 994 |
| 971 | 995 | const sanitizeRequest = () => { | @@ -981,10 +1005,17 @@ export class Snapshot extends EventEmitter { | 981 | 1005 | delete query.json; | 982 | 1006 | delete query.jsonOptions; | 983 | 1007 | delete query.maxResumeRetries; | | 1008 | + delete query.requestOptions; | 984 | 1009 | delete query.types; | | 1010 | + | 985 | 1011 | reqOpts = Object.assign(query, { | 986 | 1012 | session: this.session.formattedName_!, | 987 | 1013 | seqno: this._seqno++, | | 1014 | + requestOptions: this.configureTagOptions( | | 1015 | + typeof transaction.singleUse !== 'undefined', | | 1016 | + this.requestOptions?.transactionTag!, | | 1017 | + requestOptions | | 1018 | + ), | 988 | 1019 | transaction, | 989 | 1020 | params, | 990 | 1021 | paramTypes, | @@ -1018,6 +1049,22 @@ export class Snapshot extends EventEmitter { | 1018 | 1049 | }); | 1019 | 1050 | } | 1020 | 1051 |
| | 1052 | + /** | | 1053 | + * | | 1054 | + * @private | | 1055 | + */ | | 1056 | + configureTagOptions( | | 1057 | + singleUse?: boolean, | | 1058 | + transactionTag?: string, | | 1059 | + requestOptions = {} | | 1060 | + ): IRequestOptions | null { | | 1061 | + if (!singleUse && transactionTag) { | | 1062 | + (requestOptions as IRequestOptions).transactionTag = transactionTag; | | 1063 | + } | | 1064 | + | | 1065 | + return requestOptions!; | | 1066 | + } | | 1067 | + | 1021 | 1068 | /** | 1022 | 1069 | * Transforms convenience options `keys` and `ranges` into a KeySet object. | 1023 | 1070 | * | @@ -1151,7 +1198,7 @@ export class Snapshot extends EventEmitter { | 1151 | 1198 | * that a callback is omitted. | 1152 | 1199 | */ | 1153 | 1200 | promisifyAll(Snapshot, { | 1154 | | - exclude: ['end'], | | 1201 | + exclude: ['configureTagOptions', 'end'], | 1155 | 1202 | }); | 1156 | 1203 |
| 1157 | 1204 | /** | @@ -1312,12 +1359,14 @@ export class Transaction extends Dml { | 1312 | 1359 | constructor( | 1313 | 1360 | session: Session, | 1314 | 1361 | options = {} as spannerClient.spanner.v1.TransactionOptions.ReadWrite, | 1315 | | - queryOptions?: IQueryOptions | | 1362 | + queryOptions?: IQueryOptions, | | 1363 | + requestOptions?: Pick<IRequestOptions, 'transactionTag'> | 1316 | 1364 | ) { | 1317 | 1365 | super(session, undefined, queryOptions); | 1318 | 1366 |
| 1319 | 1367 | this._queuedMutations = []; | 1320 | 1368 | this._options = {readWrite: options}; | | 1369 | + this.requestOptions = requestOptions; | 1321 | 1370 | } | 1322 | 1371 |
| 1323 | 1372 | batchUpdate( | @@ -1435,9 +1484,13 @@ export class Transaction extends Dml { | 1435 | 1484 |
| 1436 | 1485 | const reqOpts: spannerClient.spanner.v1.ExecuteBatchDmlRequest = { | 1437 | 1486 | session: this.session.formattedName_!, | | 1487 | + requestOptions: this.configureTagOptions( | | 1488 | + false, | | 1489 | + this.requestOptions?.transactionTag!, | | 1490 | + (options as BatchUpdateOptions).requestOptions | | 1491 | + ), | 1438 | 1492 | transaction: {id: this.id!}, | 1439 | 1493 | seqno: this._seqno++, | 1440 | | - requestOptions: (options as BatchUpdateOptions).requestOptions, | 1441 | 1494 | statements, | 1442 | 1495 | } as spannerClient.spanner.v1.ExecuteBatchDmlRequest; | 1443 | 1496 |
| @@ -1595,6 +1648,10 @@ export class Transaction extends Dml { | 1595 | 1648 | ) { | 1596 | 1649 | reqOpts.returnCommitStats = (options as CommitOptions).returnCommitStats; | 1597 | 1650 | } | | 1651 | + reqOpts.requestOptions = Object.assign( | | 1652 | + requestOptions || {}, | | 1653 | + this.requestOptions | | 1654 | + ); | 1598 | 1655 |
| 1599 | 1656 | this.request( | 1600 | 1657 | { | |
Collapse file Copy file name to clipboardExpand all lines: test/mockserver/mockspanner.ts +21-12Lines changed: 21 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -870,24 +870,33 @@ export class MockSpanner { | 870 | 870 | } | 871 | 871 | const session = this.sessions.get(call.request!.session); | 872 | 872 | if (session) { | 873 | | - const buffer = Buffer.from(call.request!.transactionId as string); | 874 | | - const transactionId = buffer.toString(); | 875 | | - const fullTransactionId = | 876 | | - session.name + '/transactions/' + transactionId; | 877 | | - const transaction = this.transactions.get(fullTransactionId); | 878 | | - if (transaction) { | 879 | | - this.transactions.delete(fullTransactionId); | 880 | | - this.transactionOptions.delete(fullTransactionId); | | 873 | + if (call.request!.transactionId) { | | 874 | + const buffer = Buffer.from(call.request!.transactionId as string); | | 875 | + const transactionId = buffer.toString(); | | 876 | + const fullTransactionId = | | 877 | + session.name + '/transactions/' + transactionId; | | 878 | + const transaction = this.transactions.get(fullTransactionId); | | 879 | + if (transaction) { | | 880 | + this.transactions.delete(fullTransactionId); | | 881 | + this.transactionOptions.delete(fullTransactionId); | | 882 | + callback( | | 883 | + null, | | 884 | + protobuf.CommitResponse.create({ | | 885 | + commitTimestamp: now(), | | 886 | + }) | | 887 | + ); | | 888 | + } else { | | 889 | + callback( | | 890 | + MockSpanner.createTransactionNotFoundError(fullTransactionId) | | 891 | + ); | | 892 | + } | | 893 | + } else if (call.request!.singleUseTransaction) { | 881 | 894 | callback( | 882 | 895 | null, | 883 | 896 | protobuf.CommitResponse.create({ | 884 | 897 | commitTimestamp: now(), | 885 | 898 | }) | 886 | 899 | ); | 887 | | - } else { | 888 | | - callback( | 889 | | - MockSpanner.createTransactionNotFoundError(fullTransactionId) | 890 | | - ); | 891 | 900 | } | 892 | 901 | } else { | 893 | 902 | callback( | |
|