UNPKG

@ocap/indexdb-prune

Version:

Tool to prune records from indexdb that do not exist in statedb

65 lines (49 loc) 1.97 kB
/* eslint-disable no-console, import/no-unresolved */ require('dotenv-flow').config(); async function deleteRecords(indexdb, recordsToDelete) { if (!recordsToDelete.tables || Object.keys(recordsToDelete.tables).length === 0) { console.log('No records to delete'); return {}; } console.log('\n----- DELETING RECORDS -----'); let totalDeleted = 0; // Track deleted IDs for each table const deletedRecords = {}; // Process each table for (const [tableName, ids] of Object.entries(recordsToDelete.tables)) { // Initialize array for this table's deleted IDs deletedRecords[tableName] = []; if (!ids.length) { console.log(`Table ${tableName}: No records to delete`); continue; } console.log(`Table ${tableName}: Deleting ${ids.length} records`); const indexTable = indexdb[tableName]; const indexName = indexTable.name; const BATCH_SIZE = 100; // Process in batches to avoid overloading the system for (let i = 0; i < ids.length; i += BATCH_SIZE) { const batch = ids.slice(i, i + BATCH_SIZE); // Process each ID in the batch for (const id of batch) { // Use proper Elasticsearch delete API - no try/catch await indexdb.client.delete({ index: indexName, id }); totalDeleted++; // Store the deleted ID deletedRecords[tableName].push(id); } console.log( `Deleted batch ${Math.floor(i / BATCH_SIZE) + 1}/${Math.ceil(ids.length / BATCH_SIZE)} from ${tableName}` ); } } console.log('\n----- DELETION SUMMARY -----'); for (const [tableName, deletedIds] of Object.entries(deletedRecords)) { console.log(`${tableName}: ${deletedIds.length} records deleted`); } console.log(`Total records deleted: ${totalDeleted}`); console.log(`Details: ${JSON.stringify(deletedRecords, null, 2)}`); // Return the deleted record IDs by table return deletedRecords; } module.exports = { deleteRecords };