import PouchDB from 'pouchdb'; import indexeddb from 'pouchdb-adapter-indexeddb'; import { getAccounts, initialAccount, putAccount, getAccountById, } from '../db/account'; import { getSettings, putSettings } from '../db/settings'; PouchDB.plugin(indexeddb); declare global { var localDb: any; var db: any; } export const openDatabases = async () => { globalThis.localDb = new PouchDB('_local_dyomedea_', { adapter: 'indexeddb', auto_compaction: true, }); const accounts = await getAccounts(); console.log({ caller: 'initDb', accounts }); if (accounts.length === 0) { accounts[0] = initialAccount; await putAccount({ id: initialAccount.id, account: initialAccount }); } const settings = await getSettings(); let currentAccount = getAccountById(accounts, settings.currentAccountId); if (currentAccount === undefined) { settings.currentAccountId = accounts[0].id; await putSettings({ settings }); currentAccount = accounts[0]; } console.log({ caller: 'initDb', settings, currentAccount }); if (globalThis.db === undefined) { globalThis.db = new PouchDB(currentAccount.localDb, { adapter: 'indexeddb', auto_compaction: true, }); } return currentAccount; };