GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .gitignore
  • src
    • database.ts
    • session.ts
    • table.ts
    • transaction-runner.ts
    • transaction.ts
  • test
    • mockserver
      • mockspanner.ts
    • spanner.ts
    • table.ts
    • transaction.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.gitignore‎

Copy file name to clipboardExpand all lines: .gitignore
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ system-test/*key.json
1212
.DS_Store
1313
package-lock.json
1414
__pycache__
15+
.idea
Collapse file

‎src/database.ts‎

Copy file name to clipboardExpand all lines: src/database.ts
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,6 +2662,10 @@ class Database extends common.GrpcServiceObject {
26622662
while (true) {
26632663
try {
26642664
const [session, transaction] = await promisify(getWriteSession)();
2665+
transaction.requestOptions = Object.assign(
2666+
transaction.requestOptions || {},
2667+
options.requestOptions
2668+
);
26652669
const runner = new AsyncTransactionRunner<T>(
26662670
session,
26672671
transaction,
Collapse file

‎src/session.ts‎

Copy file name to clipboardExpand all lines: src/session.ts
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
import {ServiceObjectConfig} from '@google-cloud/common';
3939
import {NormalCallback, CLOUD_RESOURCE_HEADER} from './common';
4040
import {grpc, CallOptions} from 'google-gax';
41+
import IRequestOptions = google.spanner.v1.IRequestOptions;
4142

4243
export type GetSessionResponse = [Session, r.Response];
4344

@@ -450,9 +451,10 @@ export class Session extends common.GrpcServiceObject {
450451
* const transaction = session.transaction();
451452
*/
452453
transaction(
453-
queryOptions?: google.spanner.v1.ExecuteSqlRequest.IQueryOptions
454+
queryOptions?: google.spanner.v1.ExecuteSqlRequest.IQueryOptions,
455+
requestOptions?: Pick<IRequestOptions, 'transactionTag'>
454456
) {
455-
return new Transaction(this, undefined, queryOptions);
457+
return new Transaction(this, undefined, queryOptions, requestOptions);
456458
}
457459
/**
458460
* Format the session name to include the parent database's name.
Collapse file

‎src/table.ts‎

Copy file name to clipboardExpand all lines: src/table.ts
+15-7Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from './transaction';
3131
import {google as databaseAdmin} from '../protos/protos';
3232
import {Schema, LongRunningCallback} from './common';
33+
import IRequestOptions = databaseAdmin.spanner.v1.IRequestOptions;
3334

3435
export type Key = string | string[];
3536

@@ -43,25 +44,30 @@ export type CreateTableCallback = LongRunningCallback<Table>;
4344
export type DropTableResponse = UpdateSchemaResponse;
4445
export type DropTableCallback = UpdateSchemaCallback;
4546

47+
interface MutateRowsOptions extends CommitOptions {
48+
requestOptions?: Omit<IRequestOptions, 'requestTag'>;
49+
}
50+
4651
export type DeleteRowsCallback = CommitCallback;
4752
export type DeleteRowsResponse = CommitResponse;
48-
export type DeleteRowsOptions = CommitOptions;
53+
export type DeleteRowsOptions = MutateRowsOptions;
4954

5055
export type InsertRowsCallback = CommitCallback;
5156
export type InsertRowsResponse = CommitResponse;
52-
export type InsertRowsOptions = CommitOptions;
57+
export type InsertRowsOptions = MutateRowsOptions;
5358

5459
export type ReplaceRowsCallback = CommitCallback;
5560
export type ReplaceRowsResponse = CommitResponse;
56-
export type ReplaceRowsOptions = CommitOptions;
61+
export type ReplaceRowsOptions = MutateRowsOptions;
5762

5863
export type UpdateRowsCallback = CommitCallback;
5964
export type UpdateRowsResponse = CommitResponse;
60-
export type UpdateRowsOptions = CommitOptions;
65+
export type UpdateRowsOptions = MutateRowsOptions;
6166

6267
export type UpsertRowsCallback = CommitCallback;
6368
export type UpsertRowsResponse = CommitResponse;
64-
export type UpsertRowsOptions = CommitOptions;
69+
export type UpsertRowsOptions = MutateRowsOptions;
70+
6571
/**
6672
* Create a Table object to interact with a table in a Cloud Spanner
6773
* database.
@@ -1012,10 +1018,12 @@ class Table {
10121018
private _mutate(
10131019
method: 'deleteRows' | 'insert' | 'replace' | 'update' | 'upsert',
10141020
rows: object | object[],
1015-
options: CommitOptions | CallOptions,
1021+
options: MutateRowsOptions | CallOptions = {},
10161022
callback: CommitCallback
10171023
): void {
1018-
this.database.runTransaction((err, transaction) => {
1024+
const requestOptions =
1025+
'requestOptions' in options ? options.requestOptions : {};
1026+
this.database.runTransaction({requestOptions}, (err, transaction) => {
10191027
if (err) {
10201028
callback(err);
10211029
return;
Collapse file

‎src/transaction-runner.ts‎

Copy file name to clipboardExpand all lines: src/transaction-runner.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {Transaction} from './transaction';
2424
import {NormalCallback} from './common';
2525
import {isSessionNotFoundError} from './session-pool';
2626
import {Database} from './database';
27+
import {google} from '../protos/protos';
28+
import IRequestOptions = google.spanner.v1.IRequestOptions;
2729

2830
// eslint-disable-next-line @typescript-eslint/no-var-requires
2931
const jsonProtos = require('../protos/protos.json');
@@ -41,6 +43,7 @@ const RetryInfo = Root.fromJSON(jsonProtos).lookup('google.rpc.RetryInfo');
4143
*/
4244
export interface RunTransactionOptions {
4345
timeout?: number;
46+
requestOptions?: Pick<IRequestOptions, 'transactionTag'>;
4447
}
4548

4649
/**
Collapse file

‎src/transaction.ts‎

Copy file name to clipboardExpand all lines: src/transaction.ts
+68-11Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ export type RunResponse = [
125125
];
126126
export type RunUpdateResponse = [number];
127127

128+
export interface BatchUpdateOptions {
129+
requestOptions?: Omit<IRequestOptions, 'transactionTag'>;
130+
gaxOptions?: CallOptions;
131+
}
128132
export interface BatchUpdateCallback {
129133
(
130134
err: null | BatchUpdateError,
@@ -220,6 +224,7 @@ export class Snapshot extends EventEmitter {
220224
session: Session;
221225
queryOptions?: IQueryOptions;
222226
resourceHeader_: {[k: string]: string};
227+
requestOptions?: Pick<IRequestOptions, 'transactionTag'>;
223228

224229
/**
225230
* The transaction ID.
@@ -344,6 +349,14 @@ export class Snapshot extends EventEmitter {
344349
options,
345350
};
346351

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+
347360
this.request(
348361
{
349362
client: 'SpannerClient',
@@ -538,7 +551,8 @@ export class Snapshot extends EventEmitter {
538551
table: string,
539552
request = {} as ReadRequest
540553
): PartialResultStream {
541-
const {gaxOptions, json, jsonOptions, maxResumeRetries} = request;
554+
const {gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions} =
555+
request;
542556
const keySet = Snapshot.encodeKeySet(request);
543557
const transaction: spannerClient.spanner.v1.ITransactionSelector = {};
544558

@@ -556,13 +570,22 @@ export class Snapshot extends EventEmitter {
556570
delete request.maxResumeRetries;
557571
delete request.keys;
558572
delete request.ranges;
573+
delete request.requestOptions;
559574

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+
);
566589

567590
const makeRequest = (resumeToken?: ResumeToken): Readable => {
568591
return this.requestStream({
@@ -965,7 +988,8 @@ export class Snapshot extends EventEmitter {
965988
query.queryOptions
966989
);
967990

968-
const {gaxOptions, json, jsonOptions, maxResumeRetries} = query;
991+
const {gaxOptions, json, jsonOptions, maxResumeRetries, requestOptions} =
992+
query;
969993
let reqOpts;
970994

971995
const sanitizeRequest = () => {
@@ -981,10 +1005,17 @@ export class Snapshot extends EventEmitter {
9811005
delete query.json;
9821006
delete query.jsonOptions;
9831007
delete query.maxResumeRetries;
1008+
delete query.requestOptions;
9841009
delete query.types;
1010+
9851011
reqOpts = Object.assign(query, {
9861012
session: this.session.formattedName_!,
9871013
seqno: this._seqno++,
1014+
requestOptions: this.configureTagOptions(
1015+
typeof transaction.singleUse !== 'undefined',
1016+
this.requestOptions?.transactionTag!,
1017+
requestOptions
1018+
),
9881019
transaction,
9891020
params,
9901021
paramTypes,
@@ -1018,6 +1049,22 @@ export class Snapshot extends EventEmitter {
10181049
});
10191050
}
10201051

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+
10211068
/**
10221069
* Transforms convenience options `keys` and `ranges` into a KeySet object.
10231070
*
@@ -1151,7 +1198,7 @@ export class Snapshot extends EventEmitter {
11511198
* that a callback is omitted.
11521199
*/
11531200
promisifyAll(Snapshot, {
1154-
exclude: ['end'],
1201+
exclude: ['configureTagOptions', 'end'],
11551202
});
11561203

11571204
/**
@@ -1312,12 +1359,14 @@ export class Transaction extends Dml {
13121359
constructor(
13131360
session: Session,
13141361
options = {} as spannerClient.spanner.v1.TransactionOptions.ReadWrite,
1315-
queryOptions?: IQueryOptions
1362+
queryOptions?: IQueryOptions,
1363+
requestOptions?: Pick<IRequestOptions, 'transactionTag'>
13161364
) {
13171365
super(session, undefined, queryOptions);
13181366

13191367
this._queuedMutations = [];
13201368
this._options = {readWrite: options};
1369+
this.requestOptions = requestOptions;
13211370
}
13221371

13231372
batchUpdate(
@@ -1435,9 +1484,13 @@ export class Transaction extends Dml {
14351484

14361485
const reqOpts: spannerClient.spanner.v1.ExecuteBatchDmlRequest = {
14371486
session: this.session.formattedName_!,
1487+
requestOptions: this.configureTagOptions(
1488+
false,
1489+
this.requestOptions?.transactionTag!,
1490+
(options as BatchUpdateOptions).requestOptions
1491+
),
14381492
transaction: {id: this.id!},
14391493
seqno: this._seqno++,
1440-
requestOptions: (options as BatchUpdateOptions).requestOptions,
14411494
statements,
14421495
} as spannerClient.spanner.v1.ExecuteBatchDmlRequest;
14431496

@@ -1595,6 +1648,10 @@ export class Transaction extends Dml {
15951648
) {
15961649
reqOpts.returnCommitStats = (options as CommitOptions).returnCommitStats;
15971650
}
1651+
reqOpts.requestOptions = Object.assign(
1652+
requestOptions || {},
1653+
this.requestOptions
1654+
);
15981655

15991656
this.request(
16001657
{
Collapse file

‎test/mockserver/mockspanner.ts‎

Copy file name to clipboardExpand all lines: test/mockserver/mockspanner.ts
+21-12Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -870,24 +870,33 @@ export class MockSpanner {
870870
}
871871
const session = this.sessions.get(call.request!.session);
872872
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) {
881894
callback(
882895
null,
883896
protobuf.CommitResponse.create({
884897
commitTimestamp: now(),
885898
})
886899
);
887-
} else {
888-
callback(
889-
MockSpanner.createTransactionNotFoundError(fullTransactionId)
890-
);
891900
}
892901
} else {
893902
callback(

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.