Exporting

This commit is contained in:
Mr. Stallion 2020-04-11 14:14:58 -05:00
parent 47f329b3f5
commit 73528dd51d
1 changed files with 92 additions and 34 deletions

View File

@ -1,50 +1,108 @@
(() => {
const saveRecords = (records, iteration, startTime) => {
return new Promise(
(resolve, reject) => {
const jsonStr = JSON.stringify(records);
console.log(`Iteration ${iteration}, saving ${records.length} records, taking ${jsonStr.length} characters, at ${(Date.now() - startTime)/1000}s`);
const blob = new Blob([jsonStr], { type: 'application/json;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const el = document.createElement('a');
const d = new Date();
el.href = url;
el.download = `flist-profile-cache-${d.toISOString()}-${iteration}.json`;
document.body.appendChild(el);
el.click();
setTimeout(
() => {
document.body.removeChild(el);
window.URL.revokeObjectURL(url);
resolve();
},
0
);
}
);
}
const conn = indexedDB.open('flist-ascending-profiles', 1);
conn.onsuccess = (e) => {
const db = e.target.result;
const txn = db.transaction('profiles', 'readonly');
const store = txn.objectStore('profiles');
const cursorReq = store.openCursor();
const records = [];
cursorReq.onsuccess = (ce) => {
const cursor = ce.target.result;
function getRecords(iteration, recordLimit, startTime) {
return new Promise(
(resolve, reject) => {
const txn = db.transaction('profiles', 'readonly');
const store = txn.objectStore('profiles');
if (!cursor) {
const cursorReq = store.openCursor();
const records = [];
let hasAdvanced = false;
cursorReq.onsuccess = (ce) => {
const cursor = ce.target.result;
if (!cursor) {
if (records.length > 0) {
saveRecords(records, iteration, startTime)
.then(() => resolve(false));
}
resolve(false);
return;
}
if ((iteration > 0) && (hasAdvanced === false)) {
console.log(`Advancing ${iteration * recordLimit} records`);
hasAdvanced = true;
cursor.advance(iteration * recordLimit);
return;
}
records.push(cursor.value);
if (records.length >= recordLimit) {
saveRecords(records, iteration, startTime)
.then(() => resolve(true));
txn.abort();
return;
}
cursor.continue();
};
}
);
}
let iteration = 0;
const recordLimit = 2500;
const startTime = Date.now();
const getRecordsManager = async (shouldContinue) => {
if (!shouldContinue) {
console.log('Export Completed');
return;
}
records.push(cursor.value);
cursor.continue();
};
iteration++;
txn.oncomplete = (te) => {
console.log('Found', records.length, 'records to download');
const blob = new Blob([JSON.stringify(records)], { type: 'application/json;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const el = document.createElement('a');
const d = new Date();
const result = await getRecords(iteration, recordLimit, startTime);
el.href = url;
el.download = `flist-profile-cache-${d.toISOString()}.json`;
return getRecordsManager(result);
}
document.body.appendChild(el);
el.click();
setTimeout(
() => {
document.body.removeChild(el);
window.URL.revokeObjectURL(url);
},
0
);
// const a = records.length;
// debugger;
};
getRecords(iteration, recordLimit, startTime)
.then((shouldContinue) => getRecordsManager(shouldContinue));
};
})();