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