pinafore/routes/_utils/database/database.js

36 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-01-19 18:18:14 +01:00
import { cleanupOldStatuses } from './cleanup'
2018-01-20 06:51:22 +01:00
import { OBJECT_STORE, getDatabase } from './shared'
2018-01-19 18:18:14 +01:00
import { toReversePaddedBigInt, transformStatusForStorage } from './utils'
2018-01-20 03:04:31 +01:00
export async function getTimeline(instanceName, timeline, maxId = null, limit = 20) {
2018-01-19 18:18:14 +01:00
const db = await getDatabase(instanceName, timeline)
return await new Promise((resolve, reject) => {
const tx = db.transaction(OBJECT_STORE, 'readonly')
const store = tx.objectStore(OBJECT_STORE)
const index = store.index('pinafore_id_as_negative_big_int')
2018-01-20 03:04:31 +01:00
let sinceAsNegativeBigInt = maxId ? toReversePaddedBigInt(maxId) : null
let query = sinceAsNegativeBigInt ? IDBKeyRange.lowerBound(sinceAsNegativeBigInt, false) : null
2018-01-19 18:18:14 +01:00
let res
index.getAll(query, limit).onsuccess = (e) => {
res = e.target.result
}
tx.oncomplete = () => resolve(res)
tx.onerror = () => reject(tx.error.name + ' ' + tx.error.message)
})
}
export async function insertStatuses(instanceName, timeline, statuses) {
cleanupOldStatuses()
const db = await getDatabase(instanceName, timeline)
return await new Promise((resolve, reject) => {
const tx = db.transaction(OBJECT_STORE, 'readwrite')
const store = tx.objectStore(OBJECT_STORE)
for (let status of statuses) {
store.put(transformStatusForStorage(status))
}
tx.oncomplete = () => resolve()
tx.onerror = () => reject(tx.error.name + ' ' + tx.error.message)
})
}