2018-08-25 22:03:33 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-29 19:03:12 -07: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 => {
|
2019-05-05 19:16:02 -07:00
|
|
|
callback(e.target.result)
|
2018-03-24 18:04:54 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|