Skip to content

Instantly share code, notes, and snippets.

@billywhizz
Last active February 21, 2026 18:34
Show Gist options
  • Select an option

  • Save billywhizz/43cd45c1d9e3faf682cc0594879e11d6 to your computer and use it in GitHub Desktop.

Select an option

Save billywhizz/43cd45c1d9e3faf682cc0594879e11d6 to your computer and use it in GitHub Desktop.
function getDatabase (name = 'ConversationsDatabase') {
const req = window.indexedDB.open(name)
return new Promise((resolve, reject) => {
req.onsuccess = e => resolve(req.result)
req.onerror = req.onabort = reject
})
}
function getAllKeys (db, collection = 'conversations') {
const req = db.transaction([collection], 'readonly').objectStore(collection).getAllKeys()
return new Promise((resolve, reject) => {
req.onsuccess = e => resolve(req.result)
req.onerror = req.onabort = reject
})
}
function getByKey (db, key, collection = 'conversations') {
const req = db.transaction([collection], 'readonly').objectStore(collection).get(key)
return new Promise((resolve, reject) => {
req.onsuccess = e => resolve(req.result)
req.onerror = req.onabort = reject
})
}
function fileName (path) {
return path.slice(path.lastIndexOf('/') + 1)
}
let database
getDatabase()
.then(db => {
database = db
return getAllKeys(db)
})
.then(keys => {
return Promise.all(keys.map(k => getByKey(database, k)))
})
.then(all => {
const link = document.createElement('a')
const blob = new Blob([JSON.stringify(all, null, ' ')], { type: 'application/json' } )
link.href = URL.createObjectURL(blob)
link.download = fileName("conversations.json")
link.click()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment