Adding documents origin.

This commit is contained in:
Eric van der Vlist 2023-02-12 21:23:30 +01:00
parent 9dacad9af3
commit cbd5eb1e04
2 changed files with 58 additions and 3 deletions

View File

@ -26,13 +26,12 @@ export const watchDbLegacy = async () => {
const [status, setStatus] = createSignal<any>({});
const [syncState, setSyncState] = createSignal<any>({});
const [remoteUrl, setRemoteUrl] = createSignal<string>();
const updateStatus = async () => {
const dbInfo = await db.info();
const localDbInfo = await localDb.info();
const remoteDbInfo = remoteDb
? await remoteDb.info()
: undefined;
const remoteDbInfo = remoteDb ? await remoteDb.info() : undefined;
const tasks = PouchDB.activeTasks.list();
const newStatus = {
...status(),
@ -107,6 +106,59 @@ export const watchDbLegacy = async () => {
const timerId = setInterval(updateStatus, 15000);
const findDocumentsWithoutOrigin = async () => {
const allDocs = await db.allDocs({ include_docs: true });
return allDocs.rows
.filter((row: any) => !row.doc.origin)
.map((row: any) => row.doc._id);
};
createEffect(async () => {
if (!!state().remoteUrl && remoteUrl() !== state().remoteUrl) {
setRemoteUrl(state().remoteUrl);
// Check documents and update without origin
const documentsWithoutOrigin = await findDocumentsWithoutOrigin();
console.log({
caller: 'watchDbLegacy / addOrigin',
remoteUrl: remoteUrl(),
nbDocumentsWithoutOrigin: documentsWithoutOrigin.length,
});
for (const docId of documentsWithoutOrigin) {
while (true) {
await sleep(100);
const doc = await db.get(docId);
console.log({
caller: 'watchDbLegacy / addOrigin',
remoteUrl: remoteUrl(),
docId,
doc,
});
try {
db.put({ ...doc, origin: remoteUrl() });
break;
} catch (error) {
if (error.name === 'conflict') {
console.log({
caller: 'watchDbLegacy / addOrigin',
docId,
doc,
error,
});
} else {
console.error({
caller: 'watchDbLegacy / addOrigin',
docId,
doc,
error,
});
break;
}
}
}
}
}
});
// db.compact();
const openIDb = (name: string) =>
new Promise((resolve, reject) => {

View File

@ -1,4 +1,5 @@
import { cloneDeep } from 'lodash';
import { state } from '../db-admin/health-legacy';
declare global {
var localDb: any;
@ -25,6 +26,8 @@ export const put = async (
_id,
_rev: current._rev,
type,
shared: current.shared,
origin: current.origin ?? state().remoteUrl,
doc: update(current.doc),
};
await targetDb.put(putDoc);