Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

Commit cde4fa6

Browse files
committed
feat: refactor and deprecate databaseRole and Session Labels SessionPoolOptions
1 parent 9b0a820 commit cde4fa6

6 files changed

Lines changed: 62 additions & 12 deletions

File tree

src/database.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ class Database extends common.GrpcServiceObject {
370370
name: string,
371371
poolOptions?: SessionPoolConstructor | SessionPoolOptions,
372372
queryOptions?: spannerClient.spanner.v1.ExecuteSqlRequest.IQueryOptions,
373+
databaseRole?: string | null,
373374
) {
374375
const methods = {
375376
/**
@@ -468,12 +469,15 @@ class Database extends common.GrpcServiceObject {
468469
},
469470
} as {} as ServiceObjectConfig);
470471

471-
if (typeof poolOptions === 'object') {
472-
this.databaseRole = poolOptions.databaseRole || null;
473-
this.labels = poolOptions.labels || null;
474-
}
475472
this.formattedName_ = formattedName_;
476473
this.instance = instance;
474+
475+
const poolOpts = typeof poolOptions === 'object' ? poolOptions : null;
476+
this.databaseRole =
477+
databaseRole || (poolOpts && poolOpts.databaseRole) || null;
478+
this.labels =
479+
this._getSpanner().sessionLabels || (poolOpts && poolOpts.labels) || null;
480+
477481
this._observabilityOptions = instance._observabilityOptions;
478482
this._traceConfig = {
479483
opts: this._observabilityOptions,
@@ -707,7 +711,7 @@ class Database extends common.GrpcServiceObject {
707711
}
708712

709713
const count = options.count;
710-
const labels = options.labels || {};
714+
const labels = options.labels || this.labels || null;
711715
const databaseRole = options.databaseRole || this.databaseRole || null;
712716

713717
const reqOpts: google.spanner.v1.IBatchCreateSessionsRequest = {

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export interface SpannerOptions extends GrpcClientOptions {
167167
observabilityOptions?: ObservabilityOptions;
168168
disableBuiltInMetrics?: boolean;
169169
interceptors?: any[];
170+
sessionLabels?: {[key: string]: string};
170171
/**
171172
* The Trusted Cloud Domain (TPC) DNS of the service used to make requests.
172173
* Defaults to `googleapis.com`.
@@ -321,6 +322,7 @@ class Spanner extends GrpcService {
321322
directedReadOptions: google.spanner.v1.IDirectedReadOptions | null;
322323
defaultTransactionOptions: RunTransactionOptions;
323324
_observabilityOptions: ObservabilityOptions | undefined;
325+
sessionLabels: {[key: string]: string} | null;
324326
private _universeDomain: string;
325327
private _isInSecureCredentials: boolean;
326328
private static _isAFEServerTimingEnabled: boolean | undefined;
@@ -493,6 +495,7 @@ class Spanner extends GrpcService {
493495
this.directedReadOptions = directedReadOptions;
494496
this.defaultTransactionOptions = defaultTransactionOptions;
495497
this._observabilityOptions = options.observabilityOptions;
498+
this.sessionLabels = options.sessionLabels || null;
496499
this.commonHeaders_ = getCommonHeaders(
497500
this.projectFormattedName_,
498501
this._observabilityOptions?.enableEndToEndTracing,

src/instance.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,7 @@ class Instance extends common.GrpcServiceObject {
962962
name: string,
963963
poolOptions?: SessionPoolOptions | SessionPoolConstructor,
964964
queryOptions?: spannerClient.spanner.v1.ExecuteSqlRequest.IQueryOptions,
965+
databaseRole?: string | null,
965966
): Database {
966967
if (!name) {
967968
throw new GoogleError('A name is required to access a Database object.');
@@ -978,7 +979,13 @@ class Instance extends common.GrpcServiceObject {
978979
}
979980
const key = name.split('/').pop() + optionsKey;
980981
if (!this.databases_.has(key!)) {
981-
const db = new Database(this, name, poolOptions, queryOptions);
982+
const db = new Database(
983+
this,
984+
name,
985+
poolOptions,
986+
queryOptions,
987+
databaseRole,
988+
);
982989
db._observabilityOptions = this._observabilityOptions;
983990
const parent = this.parent as Spanner;
984991
if (parent && parent._nthClientId) {

src/session-pool.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ export interface SessionPoolOptions {
152152
fail?: boolean;
153153
idlesAfter?: number;
154154
keepAlive?: number;
155+
/**
156+
* @deprecated Use SpannerOptions.sessionLabels instead.
157+
*/
155158
labels?: {[label: string]: string};
156159
max?: number;
157160
maxIdle?: number;
@@ -162,6 +165,9 @@ export interface SessionPoolOptions {
162165
*/
163166
writes?: number;
164167
incStep?: number;
168+
/**
169+
* @deprecated Use Database constructor to pass databaseRole.
170+
*/
165171
databaseRole?: string | null;
166172
}
167173

@@ -171,12 +177,10 @@ const DEFAULTS: SessionPoolOptions = {
171177
fail: false,
172178
idlesAfter: 10,
173179
keepAlive: 30,
174-
labels: {},
175180
max: 100,
176181
maxIdle: 1,
177182
min: 25,
178183
incStep: 25,
179-
databaseRole: null,
180184
};
181185

182186
/**
@@ -667,8 +671,8 @@ export class SessionPool extends EventEmitter implements SessionPoolInterface {
667671
* @emits SessionPool#createError
668672
*/
669673
async _createSessions(amount: number): Promise<void> {
670-
const labels = this.options.labels!;
671-
const databaseRole = this.options.databaseRole!;
674+
const labels = this.options.labels;
675+
const databaseRole = this.options.databaseRole;
672676

673677
if (amount <= 0) {
674678
return;

test/database.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ describe('Database', () => {
259259

260260
const SPANNER = {
261261
routeToLeaderEnabled: true,
262+
options: {},
262263
} as {} as Spanner;
263264

264265
const INSTANCE = {
@@ -405,6 +406,36 @@ describe('Database', () => {
405406
[AFE_SERVER_TIMING_HEADER]: 'true',
406407
});
407408
});
409+
410+
it('should accept databaseRole in constructor', () => {
411+
const databaseRole = 'child_role';
412+
const database = new Database(
413+
INSTANCE,
414+
NAME,
415+
POOL_OPTIONS,
416+
undefined,
417+
databaseRole,
418+
);
419+
assert.strictEqual(database.databaseRole, databaseRole);
420+
});
421+
422+
it('should use sessionLabels from Spanner options', () => {
423+
const sessionLabels = {env: 'test'};
424+
const spanner = new Spanner({
425+
sessionLabels,
426+
});
427+
const instanceCtx = {
428+
parent: spanner,
429+
_observabilityOptions: {},
430+
request: util.noop,
431+
requestStream: util.noop,
432+
formattedName_: 'instance-name',
433+
databases_: new Map(),
434+
} as {} as Instance;
435+
436+
const database = new Database(instanceCtx, NAME);
437+
assert.deepStrictEqual(database.labels, sessionLabels);
438+
});
408439
});
409440

410441
describe('formatName_', () => {
@@ -468,7 +499,7 @@ describe('Database', () => {
468499

469500
const {reqOpts} = stub.lastCall.args[0];
470501

471-
assert.strictEqual(reqOpts.sessionTemplate.labels, labels);
502+
assert.deepStrictEqual(reqOpts.sessionTemplate.labels, labels);
472503
});
473504

474505
it('should accept session databaseRole', () => {
@@ -3069,6 +3100,7 @@ describe('Database', () => {
30693100
database.parent.parent = {
30703101
routeToLeaderEnabled: true,
30713102
directedReadOptions: fakeDirectedReadOptions,
3103+
options: {},
30723104
};
30733105

30743106
database.runPartitionedUpdate(

test/session-pool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ describe('SessionPool', () => {
219219
assert.strictEqual(sessionPool.options.fail, false);
220220
assert.strictEqual(sessionPool.options.idlesAfter, 10);
221221
assert.strictEqual(sessionPool.options.keepAlive, 30);
222-
assert.deepStrictEqual(sessionPool.options.labels, {});
222+
assert.strictEqual(sessionPool.options.labels, undefined);
223223
assert.strictEqual(sessionPool.options.min, 25);
224224
assert.strictEqual(sessionPool.options.max, 100);
225225
assert.strictEqual(sessionPool.options.maxIdle, 1);

0 commit comments

Comments
 (0)