pinafore/routes/_database/databaseLifecycle.js

93 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-01-23 18:03:31 +01:00
import {
META_STORE,
STATUS_TIMELINES_STORE,
2018-01-23 18:21:21 +01:00
STATUSES_STORE,
2018-01-28 21:51:48 +01:00
ACCOUNTS_STORE,
RELATIONSHIPS_STORE,
NOTIFICATIONS_STORE,
2018-02-11 19:35:25 +01:00
NOTIFICATION_TIMELINES_STORE,
2018-02-14 04:34:37 +01:00
PINNED_STATUSES_STORE,
2018-03-09 08:18:18 +01:00
TIMESTAMP,
REBLOG_ID,
THREADS_STORE
2018-01-23 18:03:31 +01:00
} from './constants'
2018-02-09 07:29:29 +01:00
const openReqs = {}
const databaseCache = {}
2018-03-09 08:18:18 +01:00
const DB_VERSION = 5
2018-02-14 04:34:37 +01:00
2018-02-09 07:29:29 +01:00
export function getDatabase (instanceName) {
2018-01-27 17:22:23 +01:00
if (!instanceName) {
2018-01-25 09:01:56 +01:00
throw new Error('instanceName is undefined in getDatabase()')
}
2018-01-23 18:03:31 +01:00
if (databaseCache[instanceName]) {
return Promise.resolve(databaseCache[instanceName])
}
databaseCache[instanceName] = new Promise((resolve, reject) => {
2018-01-28 21:51:48 +01:00
let req = indexedDB.open(instanceName, DB_VERSION)
2018-01-23 18:03:31 +01:00
openReqs[instanceName] = req
req.onerror = reject
req.onblocked = () => {
console.log('idb blocked')
}
2018-01-28 21:51:48 +01:00
req.onupgradeneeded = (e) => {
2018-02-09 07:29:29 +01:00
let db = req.result
2018-02-17 04:38:21 +01:00
let tx = e.currentTarget.transaction
2018-03-09 08:18:18 +01:00
if (e.oldVersion < 5) {
2018-02-17 04:38:21 +01:00
db.createObjectStore(STATUSES_STORE, {keyPath: 'id'})
.createIndex(TIMESTAMP, TIMESTAMP)
2018-03-09 08:18:18 +01:00
db.createObjectStore(STATUS_TIMELINES_STORE)
.createIndex('statusId', '')
2018-02-17 04:38:21 +01:00
db.createObjectStore(NOTIFICATIONS_STORE, {keyPath: 'id'})
.createIndex(TIMESTAMP, TIMESTAMP)
2018-03-09 08:18:18 +01:00
db.createObjectStore(NOTIFICATION_TIMELINES_STORE)
.createIndex('notificationId', '')
2018-02-17 04:38:21 +01:00
db.createObjectStore(ACCOUNTS_STORE, {keyPath: 'id'})
.createIndex(TIMESTAMP, TIMESTAMP)
db.createObjectStore(RELATIONSHIPS_STORE, {keyPath: 'id'})
.createIndex(TIMESTAMP, TIMESTAMP)
2018-03-09 08:18:18 +01:00
db.createObjectStore(META_STORE)
db.createObjectStore(PINNED_STATUSES_STORE)
2018-02-17 04:38:21 +01:00
tx.objectStore(STATUSES_STORE).createIndex(REBLOG_ID, REBLOG_ID)
tx.objectStore(NOTIFICATIONS_STORE).createIndex('statusId', 'statusId')
2018-03-09 08:18:18 +01:00
db.createObjectStore(THREADS_STORE)
2018-03-09 03:31:59 +01:00
}
2018-01-23 18:03:31 +01:00
}
req.onsuccess = () => resolve(req.result)
})
return databaseCache[instanceName]
}
2018-02-09 07:29:29 +01:00
export async function dbPromise (db, storeName, readOnlyOrReadWrite, cb) {
return new Promise((resolve, reject) => {
2018-01-23 18:03:31 +01:00
const tx = db.transaction(storeName, readOnlyOrReadWrite)
2018-02-09 07:29:29 +01:00
let store = typeof storeName === 'string'
? tx.objectStore(storeName)
: storeName.map(name => tx.objectStore(name))
2018-01-23 18:03:31 +01:00
let res
cb(store, (result) => {
res = result
})
tx.oncomplete = () => resolve(res)
2018-02-09 07:29:29 +01:00
tx.onerror = () => reject(tx.error)
2018-01-23 18:03:31 +01:00
})
}
2018-02-09 07:29:29 +01:00
export function deleteDatabase (instanceName) {
2018-01-23 18:03:31 +01:00
return new Promise((resolve, reject) => {
// close any open requests
2018-02-09 07:29:29 +01:00
let openReq = openReqs[instanceName]
2018-01-23 18:03:31 +01:00
if (openReq && openReq.result) {
openReq.result.close()
}
delete openReqs[instanceName]
delete databaseCache[instanceName]
let req = indexedDB.deleteDatabase(instanceName)
req.onsuccess = () => resolve()
2018-02-09 07:29:29 +01:00
req.onerror = () => reject(req.error)
2018-01-23 18:03:31 +01:00
})
2018-02-09 07:29:29 +01:00
}