Export HIT Tracker in multiple files instead of one large file
const fileSave = (data, name) => { | |
const json = JSON.stringify(data); | |
const save = document.createElement('a'); | |
save.href = window.URL.createObjectURL(new window.Blob([json], { type: 'application/json' })); | |
save.download = `${name}.json`; | |
document.body.appendChild(save); | |
save.click(); | |
document.body.removeChild(save); | |
}; | |
const hitTrackerExporter = async (maxHitsPerFile = 100000) => { | |
let db; | |
let count = 0; | |
let index = 0; | |
let indexFileSaved = 0; | |
const data = { | |
hits: [], | |
days: [], | |
}; | |
const open = window.indexedDB.open('hitTrackerDB', 1); | |
open.onsuccess = (event) => { | |
db = event.target.result; | |
const transaction = db.transaction(['hit'], 'readonly'); | |
const objectStore = transaction.objectStore('hit'); | |
const objectStoreCount = objectStore.count(); | |
objectStoreCount.onsuccess = (event) => { | |
count = event.target.result; | |
console.log(db, count); | |
const openCursor = objectStore.openCursor(); | |
openCursor.onsuccess = (event) => { | |
const cursor = event.target.result; | |
if (cursor) { | |
index++; | |
data.hits.push(cursor.value); | |
if (data.hits.length === maxHitsPerFile) { | |
fileSave({ ...data }, `HITs ${index - maxHitsPerFile + 1} to ${index} of ${count}`); | |
data.hits = []; | |
indexFileSaved = index; | |
} | |
cursor.continue(); | |
} else { | |
fileSave({ ...data }, `HITs ${indexFileSaved + 1} to ${index} of ${count}`); | |
} | |
}; | |
}; | |
}; | |
}; | |
hitTrackerExporter(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
How to use: