Created
July 5, 2020 22:42
-
-
Save brandonhellman/101f28f75b70dfd728a739adee559802 to your computer and use it in GitHub Desktop.
Export HIT Tracker in multiple files instead of one large file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
How to use: