Implementing a machanism to purge databases.

This commit is contained in:
Eric van der Vlist 2023-02-11 12:02:22 +01:00
parent 4a4ebdff50
commit 71bd820da9
1 changed files with 26 additions and 22 deletions

View File

@ -1,21 +1,26 @@
import { Signal } from 'solid-js'; import { Signal } from 'solid-js';
import PouchDB from 'pouchdb'; import PouchDB from 'pouchdb';
import indexeddb from 'pouchdb-adapter-indexeddb';
import { getSettings } from '../db/settings'; import { getSettings } from '../db/settings';
import { delay } from '../lib/delay'; import { delay } from '../lib/delay';
PouchDB.plugin(indexeddb);
export const compactDb = async (p: { signal: Signal<any>; params: any }) => { export const compactDb = async (p: { signal: Signal<any>; params: any }) => {
const { signal, params } = p; const { signal, params } = p;
const [, setStatus] = signal; const [, setStatus] = signal;
console.log({ caller: 'compactDb', p }); console.log({ caller: 'compactDb', p });
const db = new PouchDB('dyomedea', { auto_compaction: true }); const db = new PouchDB('_dyomedea_', {
auto_compaction: true,
adapter: 'indexeddb',
});
const dbinfo = await db.info(); const dbinfo = await db.info();
const tasks = PouchDB.activeTasks.list(); const tasks = PouchDB.activeTasks.list();
setStatus({ status: 'starting', db: 'dyomedea', dbinfo, tasks }); setStatus({ status: 'starting', db: '_dyomedea_', dbinfo, tasks });
return;
const timerId = setInterval(async () => { const timerId = setInterval(async () => {
const dbinfo = await db.info(); const dbinfo = await db.info();
const tasks = PouchDB.activeTasks.list(); const tasks = PouchDB.activeTasks.list();
@ -27,6 +32,8 @@ export const compactDb = async (p: { signal: Signal<any>; params: any }) => {
} }
}, 1000); }, 1000);
// db.compact();
const openIDb = (name: string) => const openIDb = (name: string) =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
const iDb = indexedDB.open(name); const iDb = indexedDB.open(name);
@ -71,10 +78,10 @@ export const compactDb = async (p: { signal: Signal<any>; params: any }) => {
}; };
}); });
const iDb = (await openIDb('_pouch_dyomedea')) as IDBDatabase; const iDb = (await openIDb('_pouch__dyomedea_')) as IDBDatabase;
const bySequence = iDb.transaction('by-sequence', 'readonly'); const bySequence = iDb.transaction('docs', 'readonly');
const store = bySequence.objectStore('by-sequence'); const store = bySequence.objectStore('docs');
console.log({ console.log({
caller: 'compactDb', caller: 'compactDb',
message: 'transaction opened', message: 'transaction opened',
@ -82,29 +89,26 @@ export const compactDb = async (p: { signal: Signal<any>; params: any }) => {
store, store,
}); });
const sequences = (await getAll(store)) as any[]; const docs = (await getAll(store)) as any[];
setStatus({ // setStatus({
status: 'revisions', // status: 'docs',
db: 'dyomedea', // db: 'dyomedea',
sequences, // docs,
}); // });
sequences.forEach(async (sequence) => { docs.forEach(async (doc) => {
const { _deleted, _doc_id_rev } = sequence; const { id, rev, deleted } = doc;
if (_deleted) { if (deleted !== 0) {
const [id, rev] = _doc_id_rev.split('::');
// const purge = await db.purge(id, rev); // const purge = await db.purge(id, rev);
console.log({ console.log({
caller: 'compactDb', caller: 'compactDb',
message: 'purging', message: 'purging',
id, id,
rev, rev,
sequence, doc,
purge, // purge,
}); });
await delay(100); await delay(1000);
} }
}); });
// db.compact();
}; };