Skip to content

Commit d56f3e5

Browse files
Fix for Gcloud module eagerly importing @google-cloud/spanner (#1084)
1 parent 340c082 commit d56f3e5

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("SpannerEmulatorHelper", { timeout: 240_000 }, () => {
1717
await helper.createInstance(instanceId);
1818
await helper.createDatabase(instanceId, databaseId);
1919

20-
const client = helper.client;
20+
const client = await helper.client();
2121

2222
// verify instance exists
2323
const [instanceExists] = await client.instance(instanceId).exists();

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Spanner } from "@google-cloud/spanner";
1+
import type { Spanner } from "@google-cloud/spanner";
22
import type { IInstance } from "@google-cloud/spanner/build/src/instance";
33
import type { StartedSpannerEmulatorContainer } from "./spanner-emulator-container";
44

@@ -17,7 +17,8 @@ export class SpannerEmulatorHelper {
1717
/**
1818
* Lazily get or create the Spanner client.
1919
*/
20-
public get client(): Spanner {
20+
public async client(): Promise<Spanner> {
21+
const { Spanner } = await import("@google-cloud/spanner");
2122
this.clientInstance ??= new Spanner({
2223
projectId: this.emulator.getProjectId(),
2324
apiEndpoint: this.emulator.getHost(),
@@ -30,24 +31,24 @@ export class SpannerEmulatorHelper {
3031
/**
3132
* Lazily get or create the InstanceAdminClient.
3233
*/
33-
private get instanceAdminClient(): ReturnType<Spanner["getInstanceAdminClient"]> {
34-
this.instanceAdminClientInstance ??= this.client.getInstanceAdminClient();
34+
private async instanceAdminClient(): Promise<ReturnType<Spanner["getInstanceAdminClient"]>> {
35+
this.instanceAdminClientInstance ??= (await this.client()).getInstanceAdminClient();
3536
return this.instanceAdminClientInstance;
3637
}
3738

3839
/**
3940
* Lazily get or create the DatabaseAdminClient.
4041
*/
41-
private get databaseAdminClient(): ReturnType<Spanner["getDatabaseAdminClient"]> {
42-
this.databaseAdminClientInstance ??= this.client.getDatabaseAdminClient();
42+
private async databaseAdminClient(): Promise<ReturnType<Spanner["getDatabaseAdminClient"]>> {
43+
this.databaseAdminClientInstance ??= (await this.client()).getDatabaseAdminClient();
4344
return this.databaseAdminClientInstance;
4445
}
4546

4647
/**
4748
* Lazily compute the instanceConfig path.
4849
*/
49-
public get instanceConfig(): string {
50-
this.instanceConfigValue ??= this.instanceAdminClient.instanceConfigPath(
50+
public async instanceConfig(): Promise<string> {
51+
this.instanceConfigValue ??= (await this.instanceAdminClient()).instanceConfigPath(
5152
this.emulator.getProjectId(),
5253
"emulator-config"
5354
);
@@ -58,9 +59,11 @@ export class SpannerEmulatorHelper {
5859
* Creates a new Spanner instance in the emulator.
5960
*/
6061
public async createInstance(instanceId: string, options?: IInstance): Promise<unknown> {
61-
const [operation] = await this.instanceAdminClient.createInstance({
62+
const [operation] = await (
63+
await this.instanceAdminClient()
64+
).createInstance({
6265
instanceId,
63-
parent: this.instanceAdminClient.projectPath(this.emulator.getProjectId()),
66+
parent: (await this.instanceAdminClient()).projectPath(this.emulator.getProjectId()),
6467
instance: options,
6568
});
6669
const [result] = await operation.promise();
@@ -71,15 +74,17 @@ export class SpannerEmulatorHelper {
7174
* Deletes an existing Spanner instance in the emulator.
7275
*/
7376
public async deleteInstance(instanceId: string): Promise<void> {
74-
await this.client.instance(instanceId).delete();
77+
await (await this.client()).instance(instanceId).delete();
7578
}
7679

7780
/**
7881
* Creates a new database under the specified instance in the emulator.
7982
*/
8083
public async createDatabase(instanceId: string, databaseId: string): Promise<unknown> {
81-
const [operation] = await this.databaseAdminClient.createDatabase({
82-
parent: this.databaseAdminClient.instancePath(this.emulator.getProjectId(), instanceId),
84+
const [operation] = await (
85+
await this.databaseAdminClient()
86+
).createDatabase({
87+
parent: (await this.databaseAdminClient()).instancePath(this.emulator.getProjectId(), instanceId),
8388
createStatement: `CREATE DATABASE \`${databaseId}\``,
8489
});
8590
const [result] = await operation.promise();
@@ -90,6 +95,6 @@ export class SpannerEmulatorHelper {
9095
* Deletes a database under the specified instance in the emulator.
9196
*/
9297
public async deleteDatabase(instanceId: string, databaseId: string): Promise<void> {
93-
await this.client.instance(instanceId).database(databaseId).delete();
98+
await (await this.client()).instance(instanceId).database(databaseId).delete();
9499
}
95100
}

0 commit comments

Comments
 (0)