2019-03-03 17:21:22 -08:00
|
|
|
import { DB_VERSION_CURRENT } from './constants'
|
2018-08-29 19:03:12 -07:00
|
|
|
import { addKnownInstance, deleteKnownInstance } from './knownInstances'
|
2019-03-03 17:21:22 -08:00
|
|
|
import { migrations } from './migrations'
|
2019-03-03 18:34:10 -08:00
|
|
|
import { clearAllCaches } from './cache'
|
2018-01-23 09:03:31 -08:00
|
|
|
|
2018-02-08 22:29:29 -08:00
|
|
|
const openReqs = {}
|
|
|
|
const databaseCache = {}
|
|
|
|
|
2018-09-05 19:52:51 -07:00
|
|
|
function createDatabase (instanceName) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-03-03 18:34:10 -08:00
|
|
|
let req = indexedDB.open(instanceName, DB_VERSION_CURRENT.version)
|
2018-01-23 09:03:31 -08:00
|
|
|
openReqs[instanceName] = req
|
|
|
|
req.onerror = reject
|
|
|
|
req.onblocked = () => {
|
|
|
|
console.log('idb blocked')
|
|
|
|
}
|
2018-01-28 12:51:48 -08:00
|
|
|
req.onupgradeneeded = (e) => {
|
2018-02-08 22:29:29 -08:00
|
|
|
let db = req.result
|
2018-03-24 18:04:54 -07:00
|
|
|
let tx = e.currentTarget.transaction
|
2018-03-10 20:24:07 -08:00
|
|
|
|
2019-03-03 17:21:22 -08:00
|
|
|
let migrationsToDo = migrations.filter(({ version }) => e.oldVersion < version)
|
2018-03-10 20:24:07 -08:00
|
|
|
|
2019-03-03 17:21:22 -08:00
|
|
|
function doNextMigration () {
|
|
|
|
if (!migrationsToDo.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let { migration } = migrationsToDo.shift()
|
|
|
|
migration(db, tx, doNextMigration)
|
2018-03-24 18:04:54 -07:00
|
|
|
}
|
2019-03-03 17:21:22 -08:00
|
|
|
doNextMigration()
|
2018-01-23 09:03:31 -08:00
|
|
|
}
|
|
|
|
req.onsuccess = () => resolve(req.result)
|
|
|
|
})
|
2018-09-05 19:52:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getDatabase (instanceName) {
|
|
|
|
if (!instanceName) {
|
|
|
|
throw new Error('instanceName is undefined in getDatabase()')
|
|
|
|
}
|
|
|
|
if (!databaseCache[instanceName]) {
|
|
|
|
databaseCache[instanceName] = await createDatabase(instanceName)
|
|
|
|
await addKnownInstance(instanceName)
|
|
|
|
}
|
2018-01-23 09:03:31 -08:00
|
|
|
return databaseCache[instanceName]
|
|
|
|
}
|
|
|
|
|
2018-02-08 22:29:29 -08:00
|
|
|
export async function dbPromise (db, storeName, readOnlyOrReadWrite, cb) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-01-23 09:03:31 -08:00
|
|
|
const tx = db.transaction(storeName, readOnlyOrReadWrite)
|
2018-02-08 22:29:29 -08:00
|
|
|
let store = typeof storeName === 'string'
|
|
|
|
? tx.objectStore(storeName)
|
|
|
|
: storeName.map(name => tx.objectStore(name))
|
2018-01-23 09:03:31 -08:00
|
|
|
let res
|
|
|
|
cb(store, (result) => {
|
|
|
|
res = result
|
|
|
|
})
|
|
|
|
|
|
|
|
tx.oncomplete = () => resolve(res)
|
2018-02-08 22:29:29 -08:00
|
|
|
tx.onerror = () => reject(tx.error)
|
2018-01-23 09:03:31 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-08 22:29:29 -08:00
|
|
|
export function deleteDatabase (instanceName) {
|
2018-01-23 09:03:31 -08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// close any open requests
|
2018-02-08 22:29:29 -08:00
|
|
|
let openReq = openReqs[instanceName]
|
2018-01-23 09:03:31 -08:00
|
|
|
if (openReq && openReq.result) {
|
|
|
|
openReq.result.close()
|
|
|
|
}
|
|
|
|
delete openReqs[instanceName]
|
|
|
|
delete databaseCache[instanceName]
|
|
|
|
let req = indexedDB.deleteDatabase(instanceName)
|
|
|
|
req.onsuccess = () => resolve()
|
2018-02-08 22:29:29 -08:00
|
|
|
req.onerror = () => reject(req.error)
|
2018-11-03 17:06:01 -07:00
|
|
|
req.onblocked = () => console.error(`database ${instanceName} blocked`)
|
2018-08-29 19:03:12 -07:00
|
|
|
}).then(() => deleteKnownInstance(instanceName))
|
2019-03-03 18:34:10 -08:00
|
|
|
.then(() => clearAllCaches(instanceName))
|
|
|
|
}
|
|
|
|
|
|
|
|
// this should probably only be used in unit tests
|
|
|
|
export function closeDatabase (instanceName) {
|
|
|
|
// close any open requests
|
|
|
|
let openReq = openReqs[instanceName]
|
|
|
|
if (openReq && openReq.result) {
|
|
|
|
openReq.result.close()
|
|
|
|
}
|
|
|
|
delete openReqs[instanceName]
|
|
|
|
delete databaseCache[instanceName]
|
|
|
|
clearAllCaches(instanceName)
|
2018-02-08 22:29:29 -08:00
|
|
|
}
|