pinafore/routes/_utils/database/meta.js

28 lines
779 B
JavaScript
Raw Normal View History

2018-01-22 02:18:56 +01:00
import { addKnownDb } from './knownDbs'
2018-01-22 03:36:40 +01:00
import { openReqs, databaseCache } from './cache'
2018-01-22 02:18:56 +01:00
export const META_STORE = 'meta'
export function getMetaDatabase(instanceName) {
2018-01-22 03:36:40 +01:00
const dbName = `${instanceName}_${META_STORE}`
if (databaseCache[dbName]) {
return Promise.resolve(databaseCache[dbName])
2018-01-22 02:18:56 +01:00
}
addKnownDb(instanceName, 'meta', dbName)
2018-01-22 03:36:40 +01:00
databaseCache[dbName] = new Promise((resolve, reject) => {
2018-01-22 02:18:56 +01:00
let req = indexedDB.open(dbName, 1)
2018-01-22 03:36:40 +01:00
openReqs[dbName] = req
2018-01-22 02:18:56 +01:00
req.onerror = reject
req.onblocked = () => {
console.log('idb blocked')
}
req.onupgradeneeded = () => {
let db = req.result;
db.createObjectStore(META_STORE, {keyPath: 'key'})
}
req.onsuccess = () => resolve(req.result)
})
2018-01-22 03:36:40 +01:00
return databaseCache[dbName]
2018-01-22 02:18:56 +01:00
}