Skip to content

Commit a524768

Browse files
Update emulator helper
1 parent d017f3b commit a524768

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

packages/modules/gcloud/src/spanner-emulator-helper.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { StartedSpannerEmulatorContainer } from "./spanner-emulator-contain
44

55
/**
66
* Helper class that encapsulates all Spanner client interactions against the emulator.
7-
* Clients and configs are lazily instantiated; user must call setAsEmulatorHost().
7+
* Clients and configs are lazily instantiated.
88
*/
99
export class SpannerEmulatorHelper {
1010
private clientInstance?: Spanner;
@@ -14,56 +14,43 @@ export class SpannerEmulatorHelper {
1414

1515
constructor(private readonly emulator: StartedSpannerEmulatorContainer) {}
1616

17-
public setEmulatorHost(): void {
18-
process.env.SPANNER_EMULATOR_HOST = this.emulator.getEmulatorGrpcEndpoint();
19-
}
20-
2117
/**
2218
* Lazily get or create the Spanner client.
2319
*/
2420
public get client(): Spanner {
25-
if (!this.clientInstance) {
26-
// Provide fake credentials so the auth library never tries metadata
27-
this.clientInstance = new Spanner({
28-
projectId: this.emulator.getProjectId(),
29-
apiEndpoint: this.emulator.getHost(),
30-
port: this.emulator.getGrpcPort(),
31-
sslCreds: this.emulator.getSslCredentials(),
32-
});
33-
}
21+
this.clientInstance ??= new Spanner({
22+
projectId: this.emulator.getProjectId(),
23+
apiEndpoint: this.emulator.getHost(),
24+
port: this.emulator.getGrpcPort(),
25+
sslCreds: this.emulator.getSslCredentials(),
26+
});
3427
return this.clientInstance;
3528
}
3629

3730
/**
3831
* Lazily get or create the InstanceAdminClient.
3932
*/
4033
private get instanceAdminClient(): ReturnType<Spanner["getInstanceAdminClient"]> {
41-
if (!this.instanceAdminClientInstance) {
42-
this.instanceAdminClientInstance = this.client.getInstanceAdminClient();
43-
}
34+
this.instanceAdminClientInstance ??= this.client.getInstanceAdminClient();
4435
return this.instanceAdminClientInstance;
4536
}
4637

4738
/**
4839
* Lazily get or create the DatabaseAdminClient.
4940
*/
5041
private get databaseAdminClient(): ReturnType<Spanner["getDatabaseAdminClient"]> {
51-
if (!this.databaseAdminClientInstance) {
52-
this.databaseAdminClientInstance = this.client.getDatabaseAdminClient();
53-
}
42+
this.databaseAdminClientInstance ??= this.client.getDatabaseAdminClient();
5443
return this.databaseAdminClientInstance;
5544
}
5645

5746
/**
5847
* Lazily compute the instanceConfig path.
5948
*/
6049
public get instanceConfig(): string {
61-
if (!this.instanceConfigValue) {
62-
this.instanceConfigValue = this.instanceAdminClient.instanceConfigPath(
63-
this.emulator.getProjectId(),
64-
"emulator-config"
65-
);
66-
}
50+
this.instanceConfigValue ??= this.instanceAdminClient.instanceConfigPath(
51+
this.emulator.getProjectId(),
52+
"emulator-config"
53+
);
6754
return this.instanceConfigValue;
6855
}
6956

0 commit comments

Comments
 (0)