From 71bd820da9623b52c3993a17b0f43f61745c989e Mon Sep 17 00:00:00 2001 From: evlist Date: Sat, 11 Feb 2023 12:02:22 +0100 Subject: [PATCH] Implementing a machanism to purge databases. --- src/db-admin/compact.ts | 48 ++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/db-admin/compact.ts b/src/db-admin/compact.ts index c6e21cd..04be334 100644 --- a/src/db-admin/compact.ts +++ b/src/db-admin/compact.ts @@ -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; 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; 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; 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; 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('::'); - // const purge = await db.purge(id, rev); + 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(); };