pinafore/src/routes/_database/accounts.js

25 lines
1.1 KiB
JavaScript
Raw Normal View History

import { ACCOUNTS_STORE, USERNAME_LOWERCASE } from './constants'
import { accountsCache } from './cache'
2018-02-13 19:34:37 -08:00
import { cloneForStorage, getGenericEntityWithId, setGenericEntityWithId } from './helpers'
2018-03-24 18:04:54 -07:00
import { dbPromise, getDatabase } from './databaseLifecycle'
import { createAccountUsernamePrefixKeyRange } from './keys'
2018-02-08 22:04:10 -08:00
2018-02-08 22:29:29 -08:00
export async function getAccount (instanceName, accountId) {
return getGenericEntityWithId(ACCOUNTS_STORE, accountsCache, instanceName, accountId)
2018-02-08 22:04:10 -08:00
}
2018-02-08 22:29:29 -08:00
export async function setAccount (instanceName, account) {
2018-02-13 19:34:37 -08:00
return setGenericEntityWithId(ACCOUNTS_STORE, accountsCache, instanceName, cloneForStorage(account))
2018-02-08 22:04:10 -08:00
}
export async function searchAccountsByUsername (instanceName, usernamePrefix, limit) {
limit = limit || 20
2018-03-24 18:04:54 -07:00
const db = await getDatabase(instanceName)
return dbPromise(db, ACCOUNTS_STORE, 'readonly', (accountsStore, callback) => {
let keyRange = createAccountUsernamePrefixKeyRange(usernamePrefix.toLowerCase())
accountsStore.index(USERNAME_LOWERCASE).getAll(keyRange, limit).onsuccess = e => {
callback(e.target.result)
2018-03-24 18:04:54 -07:00
}
})
}