@@ -96,7 +96,12 @@ export class ClientDatabaseManager {
96
96
initOptions : InitOptions = { }
97
97
) : ClientDatabaseEntry | undefined {
98
98
const exists = this . databases . has ( name )
99
- if ( ! exists ) {
99
+ if ( exists ) {
100
+ if ( opts ) {
101
+ throw new Error ( "Cannot re-initialize a client database with different options unless it's entry is removed first." )
102
+ }
103
+ return this . databases . get ( name )
104
+ }
100
105
if ( ! opts ) {
101
106
throw new Error ( "No config or client passed to useClientDb. Config/client must be passed at least once for each database name." )
102
107
}
@@ -144,8 +149,6 @@ export class ClientDatabaseManager {
144
149
}
145
150
this . databases . set ( name , entry )
146
151
return entry
147
- }
148
- return this . databases . get ( name )
149
152
}
150
153
151
154
switchDatabase ( name : string ) {
@@ -161,8 +164,9 @@ export class ClientDatabaseManager {
161
164
return entry
162
165
}
163
166
164
- deleteEntry ( name : string , { errorIfNotFound = true } = { } ) : void {
167
+ async deleteEntry ( name : string , { errorIfNotFound = true } = { } ) : void {
165
168
const entry = this . databases . get ( name )
169
+ await entry ?. client ?. close ( )
166
170
if ( errorIfNotFound && ! entry ) {
167
171
throw new Error ( `No database found by the name of ${ name } .` )
168
172
}
@@ -188,7 +192,7 @@ export class ClientDatabaseManager {
188
192
) : Promise < void > {
189
193
const entry = this . getEntry ( name , { errorIfNotFound : true } ) !
190
194
191
- this . deleteEntry ( name , { errorIfNotFound : false } )
195
+ await this . deleteEntry ( name , { errorIfNotFound : false } )
192
196
await this . useClientDb ( name , entry . options , entry . initOptions )
193
197
}
194
198
0 commit comments