Skip to content

Commit 82425a5

Browse files
authoredFeb 24, 2025··
fix(client-database): separate db init from collection dump fetch (#3188)
1 parent 895c220 commit 82425a5

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed
 

‎src/runtime/internal/database.client.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@ import { fetchDatabase } from './api'
66
import { checksums, tables } from '#content/manifest'
77

88
let db: Database
9-
let dbPromise: Promise<Database>
9+
const loadedCollections: Record<string, string> = {}
10+
const dbPromises: Record<string, Promise<Database>> = {}
1011
export function loadDatabaseAdapter<T>(collection: T): DatabaseAdapter {
1112
async function loadAdapter(collection: T) {
1213
if (!db) {
13-
dbPromise = dbPromise || loadAndInitializeDatabase(collection)
14-
db = await dbPromise
14+
dbPromises._ = dbPromises._ || initializeDatabase()
15+
db = await dbPromises._
16+
dbPromises._ = undefined
1517
}
18+
if (!loadedCollections[String(collection)]) {
19+
dbPromises[String(collection)] = dbPromises[String(collection)] || loadCollectionDatabase(collection)
20+
await dbPromises[String(collection)]
21+
loadedCollections[String(collection)] = 'loaded'
22+
dbPromises[String(collection)] = undefined
23+
}
24+
1625
return db
1726
}
1827

@@ -42,7 +51,7 @@ export function loadDatabaseAdapter<T>(collection: T): DatabaseAdapter {
4251
}
4352
}
4453

45-
async function loadAndInitializeDatabase<T>(collection: T) {
54+
async function initializeDatabase() {
4655
if (!db) {
4756
const sqlite3InitModule = await import('@sqlite.org/sqlite-wasm').then(m => m.default)
4857
// @ts-expect-error sqlite3ApiConfig is not defined in the module
@@ -63,7 +72,10 @@ async function loadAndInitializeDatabase<T>(collection: T) {
6372
const sqlite3 = await sqlite3InitModule()
6473
db = new sqlite3.oo1.DB()
6574
}
75+
return db
76+
}
6677

78+
async function loadCollectionDatabase<T>(collection: T) {
6779
// Do not initialize database with dump for preview mode
6880
if (window.sessionStorage.getItem('previewToken')) {
6981
return db

0 commit comments

Comments
 (0)
Please sign in to comment.