add cleanup script (apply policy to events created before it applied)

This commit is contained in:
Siddharth Singh 2024-11-17 22:17:26 +05:30
parent d2d29aef8f
commit a985d1add1
No known key found for this signature in database
2 changed files with 23 additions and 0 deletions

View file

@ -6,6 +6,7 @@
"hook": "deno run --allow-read --allow-run --allow-write https://deno.land/x/deno_hooks@0.1.1/mod.ts",
"db:export": "deno run -A --env-file --deny-read=.env scripts/db-export.ts",
"db:import": "deno run -A --env-file --deny-read=.env scripts/db-import.ts",
"db:cleanup": "deno run -A --env-file --deny-read=.env scripts/db-policy.ts",
"db:migrate": "deno run -A --env-file --deny-read=.env scripts/db-migrate.ts",
"nostr:pull": "deno run -A --env-file --deny-read=.env scripts/nostr-pull.ts",
"debug": "deno run -A --env-file --deny-read=.env --inspect src/server.ts",

22
scripts/db-policy.ts Normal file
View file

@ -0,0 +1,22 @@
import { policyWorker } from '@/workers/policy.ts';
import { Storages } from '@/storages.ts';
const db = await Storages.db();
const ids = [];
for await (const msg of db.req([{}])) {
const [type, , event] = msg;
if (type === 'EOSE') console.log('EOSE');
if (type !== 'EVENT') continue;
const [, , ok] = await policyWorker.call(event, AbortSignal.timeout(5000));
if (!ok) ids.push(event.id);
}
try {
await db.remove([{ ids }]);
console.log(`Cleaned up ${ids.length} events from the db.`);
Deno.exit(0);
} catch (e) {
console.error(e);
Deno.exit(1);
}