From 3af01b0f85f4ab9c21592f89d9016d443d756bc9 Mon Sep 17 00:00:00 2001 From: OrbisK Date: Fri, 31 Oct 2025 18:33:43 +0100 Subject: [PATCH 1/3] perf: replace internal object records with maps --- src/runtime/internal/database.client.ts | 20 +++++++++------ src/runtime/internal/database.server.ts | 33 ++++++++++++++----------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/runtime/internal/database.client.ts b/src/runtime/internal/database.client.ts index 6a389aa9e..3e106d58a 100644 --- a/src/runtime/internal/database.client.ts +++ b/src/runtime/internal/database.client.ts @@ -6,19 +6,23 @@ import type { DatabaseAdapter, DatabaseBindParams } from '@nuxt/content' import { checksums, tables } from '#content/manifest' let db: Database -const loadedCollections: Record = {} -const dbPromises: Record> = {} +const loadedCollections = new Map() +const dbPromises = new Map>() export function loadDatabaseAdapter(collection: T): DatabaseAdapter { async function loadAdapter(collection: T) { if (!db) { - dbPromises._ = dbPromises._ || initializeDatabase() - db = await dbPromises._ + if (!dbPromises.has('_')) { + dbPromises.set('_', initializeDatabase()) + } + db = await dbPromises.get('_')! Reflect.deleteProperty(dbPromises, '_') } - if (!loadedCollections[String(collection)]) { - dbPromises[String(collection)] = dbPromises[String(collection)] || loadCollectionDatabase(collection) - await dbPromises[String(collection)] - loadedCollections[String(collection)] = 'loaded' + if (!loadedCollections.has(String(collection))) { + if (!dbPromises.has(String(collection))) { + dbPromises.set(String(collection), loadCollectionDatabase(collection)) + } + await dbPromises.get(String(collection)) + loadedCollections.set(String(collection), 'loaded') Reflect.deleteProperty(dbPromises, String(collection)) } diff --git a/src/runtime/internal/database.server.ts b/src/runtime/internal/database.server.ts index c01e88c48..251c44c61 100644 --- a/src/runtime/internal/database.server.ts +++ b/src/runtime/internal/database.server.ts @@ -38,24 +38,27 @@ export default function loadDatabaseAdapter(config: RuntimeConfig['content']) { } } -const checkDatabaseIntegrity = {} as Record -const integrityCheckPromise = {} as Record | null> +const checkDatabaseIntegrity = new Map() +const integrityCheckPromise = new Map | null>() export async function checkAndImportDatabaseIntegrity(event: H3Event, collection: string, config: RuntimeConfig['content']): Promise { - if (checkDatabaseIntegrity[String(collection)] !== false) { - checkDatabaseIntegrity[String(collection)] = false - integrityCheckPromise[String(collection)] = integrityCheckPromise[String(collection)] || _checkAndImportDatabaseIntegrity(event, collection, checksums[String(collection)]!, checksumsStructure[String(collection)]!, config) - .then((isValid) => { - checkDatabaseIntegrity[String(collection)] = !isValid - }) - .catch((error) => { - console.error('Database integrity check failed', error) - checkDatabaseIntegrity[String(collection)] = true - integrityCheckPromise[String(collection)] = null - }) + if (checkDatabaseIntegrity.get(String(collection)) !== false) { + checkDatabaseIntegrity.set(String(collection), false) + if (!integrityCheckPromise.has(String(collection))) { + integrityCheckPromise.set(String(collection), _checkAndImportDatabaseIntegrity(event, collection, checksums[String(collection)]!, checksumsStructure[String(collection)]!, config) + .then((isValid) => { + checkDatabaseIntegrity.set(String(collection), !isValid) + }) + .catch((error) => { + console.error('Database integrity check failed', error) + checkDatabaseIntegrity.set(String(collection), true) + integrityCheckPromise.set(String(collection), null) + }), + ) + } } - if (integrityCheckPromise[String(collection)]) { - await integrityCheckPromise[String(collection)] + if (integrityCheckPromise.has(String(collection))) { + await integrityCheckPromise.get(String(collection))! } } From e09e1bc1e58452eeec4fe87209601bb3b71f88ef Mon Sep 17 00:00:00 2001 From: OrbisK Date: Fri, 31 Oct 2025 20:57:06 +0100 Subject: [PATCH 2/3] chore: use map delete --- src/runtime/internal/database.client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/internal/database.client.ts b/src/runtime/internal/database.client.ts index 3e106d58a..f9d9618ae 100644 --- a/src/runtime/internal/database.client.ts +++ b/src/runtime/internal/database.client.ts @@ -23,7 +23,7 @@ export function loadDatabaseAdapter(collection: T): DatabaseAdapter { } await dbPromises.get(String(collection)) loadedCollections.set(String(collection), 'loaded') - Reflect.deleteProperty(dbPromises, String(collection)) + dbPromises.delete(String(collection)) } return db From a3fc1070f58bfefab220975c04c6981c81d2160d Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Tue, 4 Nov 2025 09:21:31 +0100 Subject: [PATCH 3/3] chore: cleanup --- src/runtime/internal/database.client.ts | 16 +++++++++------- src/runtime/internal/database.server.ts | 25 +++++++++++++------------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/runtime/internal/database.client.ts b/src/runtime/internal/database.client.ts index f9d9618ae..a21bb01a4 100644 --- a/src/runtime/internal/database.client.ts +++ b/src/runtime/internal/database.client.ts @@ -10,20 +10,22 @@ const loadedCollections = new Map() const dbPromises = new Map>() export function loadDatabaseAdapter(collection: T): DatabaseAdapter { async function loadAdapter(collection: T) { + const collectionKey = String(collection) if (!db) { if (!dbPromises.has('_')) { dbPromises.set('_', initializeDatabase()) } db = await dbPromises.get('_')! - Reflect.deleteProperty(dbPromises, '_') + dbPromises.delete('_') } - if (!loadedCollections.has(String(collection))) { - if (!dbPromises.has(String(collection))) { - dbPromises.set(String(collection), loadCollectionDatabase(collection)) + if (!loadedCollections.has(collectionKey)) { + if (!dbPromises.has(collectionKey)) { + dbPromises.set(collectionKey, loadCollectionDatabase(collection)) } - await dbPromises.get(String(collection)) - loadedCollections.set(String(collection), 'loaded') - dbPromises.delete(String(collection)) + + await dbPromises.get(collectionKey) + loadedCollections.set(collectionKey, 'loaded') + dbPromises.delete(collectionKey) } return db diff --git a/src/runtime/internal/database.server.ts b/src/runtime/internal/database.server.ts index 251c44c61..9251f5802 100644 --- a/src/runtime/internal/database.server.ts +++ b/src/runtime/internal/database.server.ts @@ -41,24 +41,25 @@ export default function loadDatabaseAdapter(config: RuntimeConfig['content']) { const checkDatabaseIntegrity = new Map() const integrityCheckPromise = new Map | null>() export async function checkAndImportDatabaseIntegrity(event: H3Event, collection: string, config: RuntimeConfig['content']): Promise { - if (checkDatabaseIntegrity.get(String(collection)) !== false) { - checkDatabaseIntegrity.set(String(collection), false) - if (!integrityCheckPromise.has(String(collection))) { - integrityCheckPromise.set(String(collection), _checkAndImportDatabaseIntegrity(event, collection, checksums[String(collection)]!, checksumsStructure[String(collection)]!, config) + if (checkDatabaseIntegrity.get(collection) !== false) { + checkDatabaseIntegrity.set(collection, false) + if (!integrityCheckPromise.has(collection)) { + const _integrityCheck = _checkAndImportDatabaseIntegrity(event, collection, checksums[collection]!, checksumsStructure[collection]!, config) .then((isValid) => { - checkDatabaseIntegrity.set(String(collection), !isValid) + checkDatabaseIntegrity.set(collection, !isValid) }) .catch((error) => { console.error('Database integrity check failed', error) - checkDatabaseIntegrity.set(String(collection), true) - integrityCheckPromise.set(String(collection), null) - }), - ) + checkDatabaseIntegrity.set(collection, true) + integrityCheckPromise.delete(collection) + }) + + integrityCheckPromise.set(collection, _integrityCheck) } } - if (integrityCheckPromise.has(String(collection))) { - await integrityCheckPromise.get(String(collection))! + if (integrityCheckPromise.has(collection)) { + await integrityCheckPromise.get(collection)! } } @@ -196,7 +197,7 @@ async function waitUntilDatabaseIsReady(db: DatabaseAdapter, collection: string) } async function loadDatabaseDump(event: H3Event, collection: string): Promise { - return await fetchDatabase(event, String(collection)) + return await fetchDatabase(event, collection) .catch((e) => { console.error('Failed to fetch compressed dump', e) return ''