From 79d0b6ff85b31605dc174a3272a11a9fef319b54 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 10 Sep 2020 14:51:00 -0400 Subject: [PATCH] refactor, do not create sessions after getDatabasesStream call --- src/database.ts | 30 ++++++++++++++---------------- src/index.ts | 27 +++++++++++++-------------- src/instance.ts | 41 ++++++++++++++++------------------------- 3 files changed, 43 insertions(+), 55 deletions(-) diff --git a/src/database.ts b/src/database.ts index dab3df1e8..c866751dd 100644 --- a/src/database.ts +++ b/src/database.ts @@ -361,8 +361,7 @@ class Database extends common.GrpcServiceObject { [CLOUD_RESOURCE_HEADER]: this.formattedName_, }; this.request = instance.request; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - this.requestStream = instance.requestStream as any; + this.requestStream = instance.requestStream; this.pool_.on('error', this.emit.bind(this, 'error')); this.pool_.open(); this.queryOptions_ = Object.assign( @@ -1484,27 +1483,26 @@ class Database extends common.GrpcServiceObject { delete gaxOpts.pageToken; } - // eslint-disable-next-line @typescript-eslint/no-this-alias - const self = this; - const transform = function ( - this: Transform, - chunk: databaseAdmin.spanner.v1.ISession, - enc: string, - callback: Function - ) { - const session = self.session(chunk.name!); - session.metadata = chunk; - callback(null, session); - }; - return new pumpify.obj([ this.requestStream({ client: 'SpannerClient', method: 'listSessionsStream', reqOpts, gaxOpts, + headers: this.resourceHeader_, + }), + new Transform({ + objectMode: true, + transform: ( + chunk: databaseAdmin.spanner.v1.ISession, + enc: string, + cb: Function + ) => { + const session = this.session(chunk.name!); + session.metadata = chunk; + cb(null, session); + }, }), - new Transform({objectMode: true, transform}), ]); } diff --git a/src/index.ts b/src/index.ts index fc9db61a9..9b255fb1d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -621,27 +621,26 @@ class Spanner extends GrpcService { delete gaxOpts.pageToken; } - // eslint-disable-next-line @typescript-eslint/no-this-alias - const self = this; - const transform = function ( - this: Transform, - chunk: instanceAdmin.spanner.admin.instance.v1.IInstance, - enc: string, - callback: Function - ) { - const instance = self.instance(chunk.name!); - instance.metadata = chunk; - callback(null, instance); - }; - return new pumpify.obj([ this.requestStream({ client: 'InstanceAdminClient', method: 'listInstancesStream', reqOpts, gaxOpts, + headers: this.resourceHeader_, + }), + new Transform({ + objectMode: true, + transform: ( + chunk: instanceAdmin.spanner.admin.instance.v1.IInstance, + enc: string, + cb: Function + ) => { + const instance = this.instance(chunk.name!); + instance.metadata = chunk; + cb(null, instance); + }, }), - new Transform({objectMode: true, transform}), ]); } diff --git a/src/instance.ts b/src/instance.ts index c3bd46456..882581d70 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -435,16 +435,6 @@ class Instance extends common.GrpcServiceObject { delete gaxOpts.pageSize; delete gaxOpts.pageToken; } - // eslint-disable-next-line @typescript-eslint/no-this-alias - const self = this; - const transform = function ( - this: Transform, - chunk: instanceAdmin.spanner.admin.database.v1.IBackup, - enc: string, - callback: Function - ) { - callback(null, self.backup(chunk.name!)); - }; return new pumpify.obj([ this.requestStream({ @@ -454,7 +444,14 @@ class Instance extends common.GrpcServiceObject { gaxOpts, headers: this.resourceHeader_, }), - new Transform({objectMode: true, transform}), + new Transform({ + objectMode: true, + transform: (chunk: IBackup, enc: string, cb: Function) => { + const backup = this.backup(chunk.name!); + backup.metadata = chunk; + cb(null, backup); + }, + }), ]); } @@ -1342,19 +1339,6 @@ class Instance extends common.GrpcServiceObject { delete gaxOpts.pageToken; } - // eslint-disable-next-line @typescript-eslint/no-this-alias - const self = this; - const transform = function ( - this: Transform, - chunk: databaseAdmin.spanner.admin.database.v1.IDatabase, - enc: string, - callback: Function - ) { - const database = self.database(chunk.name!); - database.metadata = chunk; - callback(null, database); - }; - return new pumpify.obj([ this.requestStream({ client: 'DatabaseAdminClient', @@ -1363,7 +1347,14 @@ class Instance extends common.GrpcServiceObject { gaxOpts, headers: this.resourceHeader_, }), - new Transform({objectMode: true, transform}), + new Transform({ + objectMode: true, + transform: (chunk: IDatabase, enc: string, cb: Function) => { + const database = this.database(chunk.name!, {min: 0}); + database.metadata = chunk; + cb(null, database); + }, + }), ]); }