Skip to content

Commit 1aa3108

Browse files
committed
Lazy initialization of the database
1 parent c5a77e8 commit 1aa3108

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/OceanNode.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,19 @@ export class OceanNode {
343343
return this.config.hasP2P || false
344344
}
345345

346+
private dbInitPromise: Promise<Database> | null = null
346347
async getDatabase(forceReload: boolean = false): Promise<Database> {
347348
if (!this.database || forceReload) {
348-
const { dbConfig } = this.config
349-
if (dbConfig && dbConfig.url) {
350-
this.database = await Database.init(dbConfig)
349+
if (!this.dbInitPromise || forceReload) {
350+
const { dbConfig } = this.config
351+
if (dbConfig && dbConfig.url) {
352+
this.dbInitPromise = Database.init(dbConfig).then((db) => {
353+
this.database = db
354+
return db
355+
})
356+
}
351357
}
358+
return await this.dbInitPromise
352359
}
353360
return this.database
354361
}

0 commit comments

Comments
 (0)