Skip to content

Instantly share code, notes, and snippets.

@CaledoniaProject
Created September 29, 2020 12:25
Show Gist options
  • Save CaledoniaProject/b8c1adc969db5a04a4353e8fc0f4e84f to your computer and use it in GitHub Desktop.
Save CaledoniaProject/b8c1adc969db5a04a4353e8fc0f4e84f to your computer and use it in GitHub Desktop.
IndexDB to SQLite3
const sqlite3 = require('sqlite3')
const fs = require('fs')
let dbpath = 'common.db'
let finished = 0, total = 0
let mydb = new sqlite3.Database(dbpath)
if (fs.existsSync(dbpath)) {
fs.unlinkSync(dbpath)
}
mydb.run('CREATE TABLE data(id INTEGER PRIMARY KEY AUTOINCREMENT, `database` TEXT, `table` TEXT, `key` TEXT, `value` TEXT)')
function onOpen(event)
{
let storeNames = event.target.result.objectStoreNames
Object.values(storeNames).forEach((name) => {
total ++
onStore(event.target.result, name)
})
}
function onStore(cursor, storeName)
{
let store = cursor.transaction([storeName], 'readonly').objectStore(storeName)
store.openCursor().onsuccess = (event) => {
let result = event.target.result
if (result == null) {
finished ++
console.log('Done writing ' + storeName + ' (' + finished + ' / ' + total + ')')
if (finished == total)
{
console.log('All done, closing window')
mydb.close()
}
return
}
mydb.run('INSERT INTO data(`database`, `table`, `key`, `value`) VALUES(?, ?, ?, ?)',
[cursor.name, storeName, result.key, JSON.stringify(result.value)],
function (err) {
if (err) {
return console.log(err.message);
}
})
result.continue()
}
}
setTimeout(() => {
indexedDB.databases().then(result => {
result.forEach((row) => {
indexedDB.open(row.name).onsuccess = onOpen
})
})
}, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment