Simplification
This commit is contained in:
1 parent
0e714ac361
commit
79fed3cc28
3 files changed
+97
-144
No files matched your search
+45
-26
@@ -10,44 +10,63 @@ import {
|
||||
putAccount,
|
||||
getAccountById,
|
||||
} from '../db/account';
|
||||
import { openDatabases } from './open';
|
||||
import { toHex } from '../lib/to-hex';
|
||||
|
||||
PouchDB.plugin(indexeddb);
|
||||
|
||||
declare global {
|
||||
var localDb: any;
|
||||
var db: any;
|
||||
var currentAccount: any;
|
||||
}
|
||||
|
||||
const initDb = async () => {
|
||||
if (globalThis.localDb === undefined) {
|
||||
globalThis.localDb = new PouchDB('_local_dyomedea_', {
|
||||
adapter: 'indexeddb',
|
||||
auto_compaction: true,
|
||||
});
|
||||
}
|
||||
globalThis.currentAccount = await openDatabases();
|
||||
const currentAccount = globalThis.currentAccount;
|
||||
|
||||
const accounts = await getAccounts();
|
||||
console.log({ caller: 'initDb', accounts });
|
||||
if (currentAccount.remoteDbServer) {
|
||||
const url = `${currentAccount.remoteDbServer}/userdb-${toHex(
|
||||
currentAccount.remoteDbUser
|
||||
)}`;
|
||||
|
||||
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,
|
||||
});
|
||||
const remoteDb = new PouchDB(url, {
|
||||
auth: {
|
||||
username: currentAccount.remoteDbUser,
|
||||
password: currentAccount.remoteDbPassword,
|
||||
},
|
||||
skip_setup: true,
|
||||
});
|
||||
const sync = PouchDB.sync(db, remoteDb, {
|
||||
live: true,
|
||||
retry: true,
|
||||
})
|
||||
.on('change', function (info) {
|
||||
// handle change
|
||||
console.log({ caller: 'Sync / change', info });
|
||||
})
|
||||
.on('paused', function (err) {
|
||||
// replication paused (e.g. replication up to date, user went offline)
|
||||
console.log({ caller: 'Sync / paused', err });
|
||||
})
|
||||
.on('active', function () {
|
||||
// replicate resumed (e.g. new changes replicating, user went back online)
|
||||
console.log({ caller: 'Sync / active' });
|
||||
})
|
||||
.on('denied', function (err) {
|
||||
// a document failed to replicate (e.g. due to permissions)
|
||||
console.error({ caller: 'Sync / denied', err });
|
||||
})
|
||||
.on('complete', function (info) {
|
||||
// handle complete
|
||||
console.log({ caller: 'Sync / complete', info });
|
||||
})
|
||||
.on('error', function (err) {
|
||||
// handle error
|
||||
console.error({ caller: 'Sync / error', err });
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
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;
|
||||
};
|
||||
Reference in new issue
Block a user