1
- import { Spanner } from "@google-cloud/spanner" ;
1
+ import type { Spanner } from "@google-cloud/spanner" ;
2
2
import type { IInstance } from "@google-cloud/spanner/build/src/instance" ;
3
3
import type { StartedSpannerEmulatorContainer } from "./spanner-emulator-container" ;
4
4
@@ -17,7 +17,8 @@ export class SpannerEmulatorHelper {
17
17
/**
18
18
* Lazily get or create the Spanner client.
19
19
*/
20
- public get client ( ) : Spanner {
20
+ public async client ( ) : Promise < Spanner > {
21
+ const { Spanner } = await import ( "@google-cloud/spanner" ) ;
21
22
this . clientInstance ??= new Spanner ( {
22
23
projectId : this . emulator . getProjectId ( ) ,
23
24
apiEndpoint : this . emulator . getHost ( ) ,
@@ -30,24 +31,24 @@ export class SpannerEmulatorHelper {
30
31
/**
31
32
* Lazily get or create the InstanceAdminClient.
32
33
*/
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 ( ) ;
35
36
return this . instanceAdminClientInstance ;
36
37
}
37
38
38
39
/**
39
40
* Lazily get or create the DatabaseAdminClient.
40
41
*/
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 ( ) ;
43
44
return this . databaseAdminClientInstance ;
44
45
}
45
46
46
47
/**
47
48
* Lazily compute the instanceConfig path.
48
49
*/
49
- public get instanceConfig ( ) : string {
50
- this . instanceConfigValue ??= this . instanceAdminClient . instanceConfigPath (
50
+ public async instanceConfig ( ) : Promise < string > {
51
+ this . instanceConfigValue ??= ( await this . instanceAdminClient ( ) ) . instanceConfigPath (
51
52
this . emulator . getProjectId ( ) ,
52
53
"emulator-config"
53
54
) ;
@@ -58,9 +59,11 @@ export class SpannerEmulatorHelper {
58
59
* Creates a new Spanner instance in the emulator.
59
60
*/
60
61
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 ( {
62
65
instanceId,
63
- parent : this . instanceAdminClient . projectPath ( this . emulator . getProjectId ( ) ) ,
66
+ parent : ( await this . instanceAdminClient ( ) ) . projectPath ( this . emulator . getProjectId ( ) ) ,
64
67
instance : options ,
65
68
} ) ;
66
69
const [ result ] = await operation . promise ( ) ;
@@ -71,15 +74,17 @@ export class SpannerEmulatorHelper {
71
74
* Deletes an existing Spanner instance in the emulator.
72
75
*/
73
76
public async deleteInstance ( instanceId : string ) : Promise < void > {
74
- await this . client . instance ( instanceId ) . delete ( ) ;
77
+ await ( await this . client ( ) ) . instance ( instanceId ) . delete ( ) ;
75
78
}
76
79
77
80
/**
78
81
* Creates a new database under the specified instance in the emulator.
79
82
*/
80
83
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 ) ,
83
88
createStatement : `CREATE DATABASE \`${ databaseId } \`` ,
84
89
} ) ;
85
90
const [ result ] = await operation . promise ( ) ;
@@ -90,6 +95,6 @@ export class SpannerEmulatorHelper {
90
95
* Deletes a database under the specified instance in the emulator.
91
96
*/
92
97
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 ( ) ;
94
99
}
95
100
}
0 commit comments