2023-02-11 15:08:07 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
2023-02-11 19:23:18 +00:00
|
|
|
console.log({ caller: 'open', accounts, settings });
|
2023-02-11 15:08:07 +00:00
|
|
|
|
|
|
|
if (globalThis.db === undefined) {
|
|
|
|
globalThis.db = new PouchDB(currentAccount.localDb, {
|
|
|
|
adapter: 'indexeddb',
|
|
|
|
auto_compaction: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentAccount;
|
|
|
|
};
|