Skip to content

Instantly share code, notes, and snippets.

@composite
Created May 8, 2025 01:59
Show Gist options
  • Save composite/691d052dd3cb8699c833ee1267bcb9ca to your computer and use it in GitHub Desktop.
Save composite/691d052dd3cb8699c833ee1267bcb9ca to your computer and use it in GitHub Desktop.
Save whole code to snippet in devtools and run it. learn more about snippets in devtools: https://developer.chrome.com/docs/devtools/javascript/snippets
void (async () => {
const dbName = prompt("What's the name of the database to export?");
if (!dbName) return;
try {
const dbExists = await new Promise(resolve => {
const request = window.indexedDB.open(dbName);
request.onupgradeneeded = e => {
e.target.transaction.abort();
resolve(false);
};
request.onerror = () => resolve(true);
request.onsuccess = () => resolve(true);
});
if (!dbExists) {
throw new Error('Database does not exist');
}
const idbDatabase = await new Promise((resolve, reject) => {
const request = window.indexedDB.open(dbName);
request.onerror = () => reject('Could not open the database');
request.onsuccess = () => resolve(request.result);
});
const json = await new Promise((resolve, reject) => {
const exportObject = {};
if (idbDatabase.objectStoreNames.length === 0) {
resolve(JSON.stringify(exportObject));
} else {
const transaction = idbDatabase.transaction(
idbDatabase.objectStoreNames,
'readonly'
);
transaction.addEventListener('error', reject);
for (const storeName of idbDatabase.objectStoreNames) {
const allObjects = [];
transaction
.objectStore(storeName)
.openCursor()
.addEventListener('success', event => {
const cursor = event.target.result;
if (cursor) {
// Cursor holds value, put it into store data
allObjects.push(cursor.value);
cursor.continue();
} else {
// No more values, store is done
exportObject[storeName] = allObjects;
// Last store was handled
if (
idbDatabase.objectStoreNames.length ===
Object.keys(exportObject).length
) {
resolve(JSON.stringify(exportObject));
}
}
});
}
}
});
// File download function
const downloadFile = (content, fileName, contentType) => {
const blob = new Blob([content], { type: contentType });
const url = URL.createObjectURL(blob);
const downloadLink = document.createElement('a');
downloadLink.href = url;
downloadLink.download = fileName;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
URL.revokeObjectURL(url);
};
// add datetime in file name
const now = new Date();
const timestamp = `${now.getFullYear()}${(now.getMonth() + 1).toString().padStart(2, '0')}${now.getDate().toString().padStart(2, '0')}_${now.getHours().toString().padStart(2, '0')}${now.getMinutes().toString().padStart(2, '0')}`;
const fileName = `${dbName}_backup_${timestamp}.json`;
// proceed JSON download
downloadFile(json, fileName, 'application/json');
console.log(`Database "${dbName}" has been exported and downloaded as ${fileName}`);
} catch(error) {
console.error(error);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment