diff --git a/protos/google/spanner/v1/type.proto b/protos/google/spanner/v1/type.proto index 126e4a0c2..712ec187f 100644 --- a/protos/google/spanner/v1/type.proto +++ b/protos/google/spanner/v1/type.proto @@ -163,4 +163,12 @@ enum TypeAnnotationCode { // [NUMERIC][google.spanner.v1.TypeCode.NUMERIC] when a client interacts with PostgreSQL-enabled // Spanner databases. PG_NUMERIC = 2; + + // PostgreSQL compatible JSONB type. This annotation needs to be applied to + // [Type][google.spanner.v1.Type] instances having [JSON][google.spanner.v1.TypeCode.JSON] + // type code to specify that values of this type should be treated as + // PostgreSQL JSONB values. Currently this annotation is always needed for + // [JSON][google.spanner.v1.TypeCode.JSON] when a client interacts with PostgreSQL-enabled + // Spanner databases. + PG_JSONB = 3; } diff --git a/protos/protos.d.ts b/protos/protos.d.ts index baf40049c..ae640e293 100644 --- a/protos/protos.d.ts +++ b/protos/protos.d.ts @@ -15017,7 +15017,8 @@ export namespace google { /** TypeAnnotationCode enum. */ enum TypeAnnotationCode { TYPE_ANNOTATION_CODE_UNSPECIFIED = 0, - PG_NUMERIC = 2 + PG_NUMERIC = 2, + PG_JSONB = 3 } /** Represents a Spanner */ diff --git a/protos/protos.js b/protos/protos.js index 3b346ae20..622a43cec 100644 --- a/protos/protos.js +++ b/protos/protos.js @@ -36668,6 +36668,7 @@ return "typeAnnotation: enum value expected"; case 0: case 2: + case 3: break; } return null; @@ -36754,6 +36755,10 @@ case 2: message.typeAnnotation = 2; break; + case "PG_JSONB": + case 3: + message.typeAnnotation = 3; + break; } return message; }; @@ -37313,11 +37318,13 @@ * @enum {number} * @property {number} TYPE_ANNOTATION_CODE_UNSPECIFIED=0 TYPE_ANNOTATION_CODE_UNSPECIFIED value * @property {number} PG_NUMERIC=2 PG_NUMERIC value + * @property {number} PG_JSONB=3 PG_JSONB value */ v1.TypeAnnotationCode = (function() { var valuesById = {}, values = Object.create(valuesById); values[valuesById[0] = "TYPE_ANNOTATION_CODE_UNSPECIFIED"] = 0; values[valuesById[2] = "PG_NUMERIC"] = 2; + values[valuesById[3] = "PG_JSONB"] = 3; return values; })(); diff --git a/protos/protos.json b/protos/protos.json index 68da420de..387e3c654 100644 --- a/protos/protos.json +++ b/protos/protos.json @@ -3764,7 +3764,8 @@ "TypeAnnotationCode": { "values": { "TYPE_ANNOTATION_CODE_UNSPECIFIED": 0, - "PG_NUMERIC": 2 + "PG_NUMERIC": 2, + "PG_JSONB": 3 } }, "Spanner": { diff --git a/src/v1/database_admin_client.ts b/src/v1/database_admin_client.ts index 2cc7898be..2e6ba105d 100644 --- a/src/v1/database_admin_client.ts +++ b/src/v1/database_admin_client.ts @@ -17,8 +17,8 @@ // ** All changes to this file may be overwritten. ** /* global window */ -import * as gax from 'google-gax'; -import { +import type * as gax from 'google-gax'; +import type { Callback, CallOptions, Descriptors, @@ -28,7 +28,6 @@ import { PaginationCallback, GaxCall, } from 'google-gax'; - import {Transform} from 'stream'; import * as protos from '../../protos/protos'; import jsonProtos = require('../../protos/protos.json'); @@ -38,7 +37,6 @@ import jsonProtos = require('../../protos/protos.json'); * This file defines retry strategy and timeouts for all API methods in this library. */ import * as gapicConfig from './database_admin_client_config.json'; -import {operationsProtos} from 'google-gax'; const version = require('../../../package.json').version; /** @@ -105,8 +103,18 @@ export class DatabaseAdminClient { * Pass "rest" to use HTTP/1.1 REST API instead of gRPC. * For more information, please check the * {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}. + * @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you + * need to avoid loading the default gRPC version and want to use the fallback + * HTTP implementation. Load only fallback version and pass it to the constructor: + * ``` + * const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC + * const client = new DatabaseAdminClient({fallback: 'rest'}, gax); + * ``` */ - constructor(opts?: ClientOptions) { + constructor( + opts?: ClientOptions, + gaxInstance?: typeof gax | typeof gax.fallback + ) { // Ensure that options include all the required fields. const staticMembers = this.constructor as typeof DatabaseAdminClient; const servicePath = @@ -126,8 +134,13 @@ export class DatabaseAdminClient { opts['scopes'] = staticMembers.scopes; } + // Load google-gax module synchronously if needed + if (!gaxInstance) { + gaxInstance = require('google-gax') as typeof gax; + } + // Choose either gRPC or proto-over-HTTP implementation of google-gax. - this._gaxModule = opts.fallback ? gax.fallback : gax; + this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance; // Create a `gaxGrpc` object, with any grpc-specific options sent to the client. this._gaxGrpc = new this._gaxModule.GrpcClient(opts); @@ -351,7 +364,7 @@ export class DatabaseAdminClient { this.innerApiCalls = {}; // Add a warn function to the client constructor so it can be easily tested. - this.warn = gax.warn; + this.warn = this._gaxModule.warn; } /** @@ -581,7 +594,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ name: request.name || '', }); this.initialize(); @@ -676,7 +689,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ database: request.database || '', }); this.initialize(); @@ -777,7 +790,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ database: request.database || '', }); this.initialize(); @@ -878,7 +891,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ resource: request.resource || '', }); this.initialize(); @@ -972,7 +985,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ resource: request.resource || '', }); this.initialize(); @@ -1070,7 +1083,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ resource: request.resource || '', }); this.initialize(); @@ -1163,7 +1176,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ name: request.name || '', }); this.initialize(); @@ -1263,7 +1276,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ 'backup.name': request.backup!.name || '', }); this.initialize(); @@ -1356,7 +1369,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ name: request.name || '', }); this.initialize(); @@ -1485,7 +1498,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -1511,11 +1524,12 @@ export class DatabaseAdminClient { protos.google.spanner.admin.database.v1.CreateDatabaseMetadata > > { - const request = new operationsProtos.google.longrunning.GetOperationRequest( - {name} - ); + const request = + new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest( + {name} + ); const [operation] = await this.operationsClient.getOperation(request); - const decodeOperation = new gax.Operation( + const decodeOperation = new this._gaxModule.Operation( operation, this.descriptors.longrunning.createDatabase, this._gaxModule.createDefaultBackoffSettings() @@ -1650,7 +1664,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ database: request.database || '', }); this.initialize(); @@ -1676,11 +1690,12 @@ export class DatabaseAdminClient { protos.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata > > { - const request = new operationsProtos.google.longrunning.GetOperationRequest( - {name} - ); + const request = + new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest( + {name} + ); const [operation] = await this.operationsClient.getOperation(request); - const decodeOperation = new gax.Operation( + const decodeOperation = new this._gaxModule.Operation( operation, this.descriptors.longrunning.updateDatabaseDdl, this._gaxModule.createDefaultBackoffSettings() @@ -1815,7 +1830,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -1841,11 +1856,12 @@ export class DatabaseAdminClient { protos.google.spanner.admin.database.v1.CreateBackupMetadata > > { - const request = new operationsProtos.google.longrunning.GetOperationRequest( - {name} - ); + const request = + new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest( + {name} + ); const [operation] = await this.operationsClient.getOperation(request); - const decodeOperation = new gax.Operation( + const decodeOperation = new this._gaxModule.Operation( operation, this.descriptors.longrunning.createBackup, this._gaxModule.createDefaultBackoffSettings() @@ -1987,7 +2003,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -2013,11 +2029,12 @@ export class DatabaseAdminClient { protos.google.spanner.admin.database.v1.CopyBackupMetadata > > { - const request = new operationsProtos.google.longrunning.GetOperationRequest( - {name} - ); + const request = + new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest( + {name} + ); const [operation] = await this.operationsClient.getOperation(request); - const decodeOperation = new gax.Operation( + const decodeOperation = new this._gaxModule.Operation( operation, this.descriptors.longrunning.copyBackup, this._gaxModule.createDefaultBackoffSettings() @@ -2159,7 +2176,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -2185,11 +2202,12 @@ export class DatabaseAdminClient { protos.google.spanner.admin.database.v1.RestoreDatabaseMetadata > > { - const request = new operationsProtos.google.longrunning.GetOperationRequest( - {name} - ); + const request = + new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest( + {name} + ); const [operation] = await this.operationsClient.getOperation(request); - const decodeOperation = new gax.Operation( + const decodeOperation = new this._gaxModule.Operation( operation, this.descriptors.longrunning.restoreDatabase, this._gaxModule.createDefaultBackoffSettings() @@ -2295,7 +2313,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -2337,7 +2355,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listDatabases']; @@ -2388,7 +2406,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listDatabases']; @@ -2535,7 +2553,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -2614,7 +2632,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listBackups']; @@ -2702,7 +2720,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listBackups']; @@ -2861,7 +2879,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -2951,7 +2969,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listDatabaseOperations']; @@ -3046,7 +3064,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listDatabaseOperations']; @@ -3231,7 +3249,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -3341,7 +3359,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listBackupOperations']; @@ -3460,7 +3478,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listBackupOperations']; @@ -3569,7 +3587,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -3612,7 +3630,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listDatabaseRoles']; @@ -3664,7 +3682,7 @@ export class DatabaseAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listDatabaseRoles']; diff --git a/src/v1/instance_admin_client.ts b/src/v1/instance_admin_client.ts index d2eb5b89f..a252b5ef3 100644 --- a/src/v1/instance_admin_client.ts +++ b/src/v1/instance_admin_client.ts @@ -17,8 +17,8 @@ // ** All changes to this file may be overwritten. ** /* global window */ -import * as gax from 'google-gax'; -import { +import type * as gax from 'google-gax'; +import type { Callback, CallOptions, Descriptors, @@ -28,7 +28,6 @@ import { PaginationCallback, GaxCall, } from 'google-gax'; - import {Transform} from 'stream'; import * as protos from '../../protos/protos'; import jsonProtos = require('../../protos/protos.json'); @@ -38,7 +37,6 @@ import jsonProtos = require('../../protos/protos.json'); * This file defines retry strategy and timeouts for all API methods in this library. */ import * as gapicConfig from './instance_admin_client_config.json'; -import {operationsProtos} from 'google-gax'; const version = require('../../../package.json').version; /** @@ -119,8 +117,18 @@ export class InstanceAdminClient { * Pass "rest" to use HTTP/1.1 REST API instead of gRPC. * For more information, please check the * {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}. + * @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you + * need to avoid loading the default gRPC version and want to use the fallback + * HTTP implementation. Load only fallback version and pass it to the constructor: + * ``` + * const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC + * const client = new InstanceAdminClient({fallback: 'rest'}, gax); + * ``` */ - constructor(opts?: ClientOptions) { + constructor( + opts?: ClientOptions, + gaxInstance?: typeof gax | typeof gax.fallback + ) { // Ensure that options include all the required fields. const staticMembers = this.constructor as typeof InstanceAdminClient; const servicePath = @@ -140,8 +148,13 @@ export class InstanceAdminClient { opts['scopes'] = staticMembers.scopes; } + // Load google-gax module synchronously if needed + if (!gaxInstance) { + gaxInstance = require('google-gax') as typeof gax; + } + // Choose either gRPC or proto-over-HTTP implementation of google-gax. - this._gaxModule = opts.fallback ? gax.fallback : gax; + this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance; // Create a `gaxGrpc` object, with any grpc-specific options sent to the client. this._gaxGrpc = new this._gaxModule.GrpcClient(opts); @@ -296,7 +309,7 @@ export class InstanceAdminClient { this.innerApiCalls = {}; // Add a warn function to the client constructor so it can be easily tested. - this.warn = gax.warn; + this.warn = this._gaxModule.warn; } /** @@ -523,7 +536,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ name: request.name || '', }); this.initialize(); @@ -619,7 +632,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ name: request.name || '', }); this.initialize(); @@ -727,7 +740,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ name: request.name || '', }); this.initialize(); @@ -826,7 +839,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ resource: request.resource || '', }); this.initialize(); @@ -917,7 +930,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ resource: request.resource || '', }); this.initialize(); @@ -1011,7 +1024,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ resource: request.resource || '', }); this.initialize(); @@ -1156,7 +1169,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -1182,11 +1195,12 @@ export class InstanceAdminClient { protos.google.spanner.admin.instance.v1.CreateInstanceMetadata > > { - const request = new operationsProtos.google.longrunning.GetOperationRequest( - {name} - ); + const request = + new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest( + {name} + ); const [operation] = await this.operationsClient.getOperation(request); - const decodeOperation = new gax.Operation( + const decodeOperation = new this._gaxModule.Operation( operation, this.descriptors.longrunning.createInstance, this._gaxModule.createDefaultBackoffSettings() @@ -1338,7 +1352,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ 'instance.name': request.instance!.name || '', }); this.initialize(); @@ -1364,11 +1378,12 @@ export class InstanceAdminClient { protos.google.spanner.admin.instance.v1.UpdateInstanceMetadata > > { - const request = new operationsProtos.google.longrunning.GetOperationRequest( - {name} - ); + const request = + new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest( + {name} + ); const [operation] = await this.operationsClient.getOperation(request); - const decodeOperation = new gax.Operation( + const decodeOperation = new this._gaxModule.Operation( operation, this.descriptors.longrunning.updateInstance, this._gaxModule.createDefaultBackoffSettings() @@ -1475,7 +1490,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -1518,7 +1533,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listInstanceConfigs']; @@ -1570,7 +1585,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listInstanceConfigs']; @@ -1698,7 +1713,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); this.initialize(); @@ -1760,7 +1775,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listInstances']; @@ -1831,7 +1846,7 @@ export class InstanceAdminClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ parent: request.parent || '', }); const defaultCallSettings = this._defaults['listInstances']; diff --git a/src/v1/spanner_client.ts b/src/v1/spanner_client.ts index a6189ce24..ce1041545 100644 --- a/src/v1/spanner_client.ts +++ b/src/v1/spanner_client.ts @@ -17,19 +17,16 @@ // ** All changes to this file may be overwritten. ** /* global window */ -import * as gax from 'google-gax'; -import { +import type * as gax from 'google-gax'; +import type { Callback, CallOptions, Descriptors, ClientOptions, PaginationCallback, GaxCall, - GoogleError, } from 'google-gax'; - -import {Transform} from 'stream'; -import {PassThrough} from 'stream'; +import {Transform, PassThrough} from 'stream'; import * as protos from '../../protos/protos'; import jsonProtos = require('../../protos/protos.json'); /** @@ -38,7 +35,6 @@ import jsonProtos = require('../../protos/protos.json'); * This file defines retry strategy and timeouts for all API methods in this library. */ import * as gapicConfig from './spanner_client_config.json'; - const version = require('../../../package.json').version; /** @@ -101,8 +97,18 @@ export class SpannerClient { * Pass "rest" to use HTTP/1.1 REST API instead of gRPC. * For more information, please check the * {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}. + * @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you + * need to avoid loading the default gRPC version and want to use the fallback + * HTTP implementation. Load only fallback version and pass it to the constructor: + * ``` + * const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC + * const client = new SpannerClient({fallback: 'rest'}, gax); + * ``` */ - constructor(opts?: ClientOptions) { + constructor( + opts?: ClientOptions, + gaxInstance?: typeof gax | typeof gax.fallback + ) { // Ensure that options include all the required fields. const staticMembers = this.constructor as typeof SpannerClient; const servicePath = @@ -122,8 +128,13 @@ export class SpannerClient { opts['scopes'] = staticMembers.scopes; } + // Load google-gax module synchronously if needed + if (!gaxInstance) { + gaxInstance = require('google-gax') as typeof gax; + } + // Choose either gRPC or proto-over-HTTP implementation of google-gax. - this._gaxModule = opts.fallback ? gax.fallback : gax; + this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance; // Create a `gaxGrpc` object, with any grpc-specific options sent to the client. this._gaxGrpc = new this._gaxModule.GrpcClient(opts); @@ -190,11 +201,11 @@ export class SpannerClient { // Provide descriptors for these. this.descriptors.stream = { executeStreamingSql: new this._gaxModule.StreamDescriptor( - gax.StreamType.SERVER_STREAMING, + this._gaxModule.StreamType.SERVER_STREAMING, opts.fallback === 'rest' ), streamingRead: new this._gaxModule.StreamDescriptor( - gax.StreamType.SERVER_STREAMING, + this._gaxModule.StreamType.SERVER_STREAMING, opts.fallback === 'rest' ), }; @@ -213,7 +224,7 @@ export class SpannerClient { this.innerApiCalls = {}; // Add a warn function to the client constructor so it can be easily tested. - this.warn = gax.warn; + this.warn = this._gaxModule.warn; } /** @@ -275,7 +286,9 @@ export class SpannerClient { setImmediate(() => { stream.emit( 'error', - new GoogleError('The client has already been closed.') + new this._gaxModule.GoogleError( + 'The client has already been closed.' + ) ); }); return stream; @@ -458,7 +471,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ database: request.database || '', }); this.initialize(); @@ -552,7 +565,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ database: request.database || '', }); this.initialize(); @@ -635,7 +648,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ name: request.name || '', }); this.initialize(); @@ -718,7 +731,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ name: request.name || '', }); this.initialize(); @@ -876,7 +889,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ session: request.session || '', }); this.initialize(); @@ -991,7 +1004,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ session: request.session || '', }); this.initialize(); @@ -1127,7 +1140,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ session: request.session || '', }); this.initialize(); @@ -1219,7 +1232,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ session: request.session || '', }); this.initialize(); @@ -1335,7 +1348,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ session: request.session || '', }); this.initialize(); @@ -1425,7 +1438,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ session: request.session || '', }); this.initialize(); @@ -1554,7 +1567,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ session: request.session || '', }); this.initialize(); @@ -1669,7 +1682,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ session: request.session || '', }); this.initialize(); @@ -1771,7 +1784,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ session: request.session || '', }); this.initialize(); @@ -1849,7 +1862,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ session: request.session || '', }); this.initialize(); @@ -1954,7 +1967,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ database: request.database || '', }); this.initialize(); @@ -2006,7 +2019,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ database: request.database || '', }); const defaultCallSettings = this._defaults['listSessions']; @@ -2065,7 +2078,7 @@ export class SpannerClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ database: request.database || '', }); const defaultCallSettings = this._defaults['listSessions']; diff --git a/system-test/spanner.ts b/system-test/spanner.ts index feea97ff9..452d7c8b9 100644 --- a/system-test/spanner.ts +++ b/system-test/spanner.ts @@ -135,35 +135,41 @@ describe('Spanner', () => { }); after(async () => { - if (generateInstanceForTest) { - // Deleting all backups before an instance can be deleted. - await Promise.all( - RESOURCES_TO_CLEAN.filter(resource => resource instanceof Backup).map( - backup => backup.delete(GAX_OPTIONS) - ) - ); - /** - * Deleting instances created during this test. - * All databasess will automatically be deleted with instance. - * @see {@link https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.DeleteInstance} - */ - await Promise.all( - RESOURCES_TO_CLEAN.filter(resource => resource instanceof Instance).map( - instance => instance.delete(GAX_OPTIONS) - ) - ); - } else { - /** - * Limit the number of concurrent 'Administrative requests per minute' - * Not to exceed quota - * @see {@link https://cloud.google.com/spanner/quotas#administrative_limits} - */ - const limit = pLimit(5); - await Promise.all( - RESOURCES_TO_CLEAN.map(resource => - limit(() => resource.delete(GAX_OPTIONS)) - ) - ); + try { + if (generateInstanceForTest) { + // Sleep for 30 seconds before cleanup, just in case + await new Promise(resolve => setTimeout(resolve, 30000)); + // Deleting all backups before an instance can be deleted. + await Promise.all( + RESOURCES_TO_CLEAN.filter(resource => resource instanceof Backup).map( + backup => backup.delete(GAX_OPTIONS) + ) + ); + /** + * Deleting instances created during this test. + * All databasess will automatically be deleted with instance. + * @see {@link https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.DeleteInstance} + */ + await Promise.all( + RESOURCES_TO_CLEAN.filter( + resource => resource instanceof Instance + ).map(instance => instance.delete(GAX_OPTIONS)) + ); + } else { + /** + * Limit the number of concurrent 'Administrative requests per minute' + * Not to exceed quota + * @see {@link https://cloud.google.com/spanner/quotas#administrative_limits} + */ + const limit = pLimit(5); + await Promise.all( + RESOURCES_TO_CLEAN.map(resource => + limit(() => resource.delete(GAX_OPTIONS)) + ) + ); + } + } catch (err) { + console.error('Cleanup failed:', err); } });