Skip to content

Commit 0b71e09

Browse files
committed
v0fix: clientDabataseManager's deleteEntry now closes the database if it can
1 parent ead3b87 commit 0b71e09

1 file changed

Lines changed: 50 additions & 46 deletions

File tree

src/runtime/utils/clientDatabaseManager.ts

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -96,56 +96,59 @@ export class ClientDatabaseManager {
9696
initOptions: InitOptions = {}
9797
): ClientDatabaseEntry | undefined {
9898
const exists = this.databases.has(name)
99-
if (!exists) {
100-
if (!opts) {
101-
throw new Error("No config or client passed to useClientDb. Config/client must be passed at least once for each database name.")
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.")
102102
}
103+
return this.databases.get(name)
104+
}
105+
if (!opts) {
106+
throw new Error("No config or client passed to useClientDb. Config/client must be passed at least once for each database name.")
107+
}
103108

104-
const clientPgliteOptions = opts.clientPgliteOptions ?? {}
105-
if (opts.useWebWorker && clientPgliteOptions.extensions) {
106-
const unsupported = Object.keys(clientPgliteOptions.extensions).filter(_ => _ !== "live")
107-
if (unsupported.length) {
108-
throw new Error(`clientPgliteOptions.extensions contains unsupported extensions, if you need these, you'll need to create a custom worker, see options.webWorkerUrl.\nUnsupported: ${unsupported.join(", ")}`)
109-
}
110-
}
111-
if (!opts.schema && !opts.drizzleProxy) {
112-
// eslint-disable-next-line no-console
113-
console.warn("No schema for a client side database was provided. This is not recommended. Drizzle will not be able to do db.query type queries. Schema can only safely not be defined if using drizzleProxy (as it would be defined on the real instance).")
109+
const clientPgliteOptions = opts.clientPgliteOptions ?? {}
110+
if (opts.useWebWorker && clientPgliteOptions.extensions) {
111+
const unsupported = Object.keys(clientPgliteOptions.extensions).filter(_ => _ !== "live")
112+
if (unsupported.length) {
113+
throw new Error(`clientPgliteOptions.extensions contains unsupported extensions, if you need these, you'll need to create a custom worker, see options.webWorkerUrl.\nUnsupported: ${unsupported.join(", ")}`)
114114
}
115+
}
116+
if (!opts.schema && !opts.drizzleProxy) {
117+
// eslint-disable-next-line no-console
118+
console.warn("No schema for a client side database was provided. This is not recommended. Drizzle will not be able to do db.query type queries. Schema can only safely not be defined if using drizzleProxy (as it would be defined on the real instance).")
119+
}
115120

116-
const client = opts.drizzleProxy
117-
? undefined
118-
: (opts.useWebWorker
119-
? new PGliteWorker(
120-
new Worker(opts.webWorkerUrl ?? new URL("./../worker.js", import.meta.url), { type: "module" }),
121-
{
122-
dataDir: opts.clientPgLitePath ?? `idb:// ${name}`,
123-
meta: { options: clientPgliteOptions }
124-
// extensions
125-
}
126-
)
127-
: new PGlite(opts.clientPgLitePath ?? `idb://${name}`, clientPgliteOptions))
128-
129-
const migrationOptions = opts.clientMigrationOptions ?? {}
130-
131-
const entry = {
132-
options: opts,
133-
initOptions,
134-
client,
135-
db: opts.drizzleProxy
136-
? drizzleProxy(async (...params: [any, any, any]) => opts.drizzleProxy!(name, ...params))
137-
: drizzle({ client: client as any, schema: opts.schema }),
138-
migrationState: {
139-
...ClientDatabaseManager.defaultMigrationState,
140-
storage: migrationOptions.storage ?? ClientDatabaseManager.useDefaultStorage()
141-
},
142-
path: opts?.clientPgLitePath ?? `idb://${name}`
121+
const client = opts.drizzleProxy
122+
? undefined
123+
: (opts.useWebWorker
124+
? new PGliteWorker(
125+
new Worker(opts.webWorkerUrl ?? new URL("./../worker.js", import.meta.url), { type: "module" }),
126+
{
127+
dataDir: opts.clientPgLitePath ?? `idb:// ${name}`,
128+
meta: { options: clientPgliteOptions }
129+
// extensions
130+
}
131+
)
132+
: new PGlite(opts.clientPgLitePath ?? `idb://${name}`, clientPgliteOptions))
133+
134+
const migrationOptions = opts.clientMigrationOptions ?? {}
135+
136+
const entry = {
137+
options: opts,
138+
initOptions,
139+
client,
140+
db: opts.drizzleProxy
141+
? drizzleProxy(async (...params: [any, any, any]) => opts.drizzleProxy!(name, ...params))
142+
: drizzle({ client: client as any, schema: opts.schema }),
143+
migrationState: {
144+
...ClientDatabaseManager.defaultMigrationState,
145+
storage: migrationOptions.storage ?? ClientDatabaseManager.useDefaultStorage()
146+
},
147+
path: opts?.clientPgLitePath ?? `idb://${name}`
143148

144-
}
145-
this.databases.set(name, entry)
146-
return entry
147149
}
148-
return this.databases.get(name)
150+
this.databases.set(name, entry)
151+
return entry
149152
}
150153

151154
switchDatabase(name: string) {
@@ -161,8 +164,9 @@ export class ClientDatabaseManager {
161164
return entry
162165
}
163166

164-
deleteEntry(name: string, { errorIfNotFound = true } = {}): void {
167+
async deleteEntry(name: string, { errorIfNotFound = true } = {}): Promise<void> {
165168
const entry = this.databases.get(name)
169+
await entry?.client?.close()
166170
if (errorIfNotFound && !entry) {
167171
throw new Error(`No database found by the name of ${name}.`)
168172
}
@@ -188,7 +192,7 @@ export class ClientDatabaseManager {
188192
): Promise<void> {
189193
const entry = this.getEntry(name, { errorIfNotFound: true })!
190194

191-
this.deleteEntry(name, { errorIfNotFound: false })
195+
await this.deleteEntry(name, { errorIfNotFound: false })
192196
await this.useClientDb(name, entry.options, entry.initOptions)
193197
}
194198

0 commit comments

Comments
 (0)