Adding a mechanism to postpone DB access before the DB is ready.
This commit is contained in:
1 parent
a36c8a28a4
commit
5ca12b6780
5 files changed
+67
-7
No files matched your search
+8
-2
@@ -1,11 +1,17 @@
|
||||
import { getFamily, put } from './lib';
|
||||
import getUri from '../lib/ids';
|
||||
|
||||
export const initialAccount = {
|
||||
id: 'initial',
|
||||
name: '???',
|
||||
localDb: 'dyomedea',
|
||||
};
|
||||
|
||||
export const getAccounts = async () => {
|
||||
return await getFamily('account', {}, true);
|
||||
};
|
||||
|
||||
export const putAccount = async (name: string, account: any) => {
|
||||
const uri = getUri('account', name);
|
||||
export const putAccount = async (id: string, account: any) => {
|
||||
const uri = getUri('account', { account: id });
|
||||
return await put(uri, 'account', (_: any) => account, {}, true);
|
||||
};
|
||||
+13
-2
@@ -1,6 +1,7 @@
|
||||
import _ from 'lodash';
|
||||
import PouchDB from 'pouchdb';
|
||||
import uri from '../lib/ids';
|
||||
import { getAccounts, initialAccount, putAccount } from './account';
|
||||
import changeHandler from './change-handler';
|
||||
|
||||
const dbDefinitionId = uri('dbdef', {});
|
||||
@@ -27,6 +28,14 @@ export const initDb = async (params: any) => {
|
||||
});
|
||||
}
|
||||
|
||||
const accounts = (await getAccounts()).rows;
|
||||
console.log({ caller: 'initDb', accounts });
|
||||
|
||||
if (accounts.length === 0) {
|
||||
accounts[0] = initialAccount;
|
||||
await putAccount(initialAccount.id, initialAccount);
|
||||
}
|
||||
|
||||
if (globalThis.db === undefined) {
|
||||
globalThis.db = new PouchDB('dyomedea', {
|
||||
auto_compaction: false,
|
||||
@@ -56,8 +65,7 @@ export const initDb = async (params: any) => {
|
||||
}
|
||||
//await await db.compact();
|
||||
|
||||
globalThis.dbReady = true;
|
||||
|
||||
|
||||
const sync = PouchDB.sync(
|
||||
'dyomedea',
|
||||
'http://admin:password@localhost:5984/dyomedea',
|
||||
@@ -106,4 +114,7 @@ export const initDb = async (params: any) => {
|
||||
// console.log({ caller: 'initDb / back from db.changes', changes });
|
||||
|
||||
// changes.cancel();
|
||||
|
||||
globalThis.dbReady = true;
|
||||
|
||||
};
|
||||
+3
-1
@@ -1,11 +1,13 @@
|
||||
import { get, put } from './lib';
|
||||
|
||||
const emptySettings = {};
|
||||
|
||||
export const getSettings = async () => {
|
||||
try {
|
||||
return await get('settings', true);
|
||||
} catch (err) {
|
||||
console.error({ caller: 'getSettings', err });
|
||||
return undefined;
|
||||
return emptySettings;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user