2021-07-05 05:19:04 +02:00
|
|
|
import { ACCOUNTS_STORE, USERNAME_LOWERCASE } from './constants.js'
|
|
|
|
import { accountsCache } from './cache.js'
|
|
|
|
import { cloneForStorage, getGenericEntityWithId, setGenericEntityWithId } from './helpers.js'
|
|
|
|
import { dbPromise, getDatabase } from './databaseLifecycle.js'
|
|
|
|
import { createAccountUsernamePrefixKeyRange } from './keys.js'
|
2018-02-09 07:04:10 +01:00
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
export async function getAccount (instanceName, accountId) {
|
|
|
|
return getGenericEntityWithId(ACCOUNTS_STORE, accountsCache, instanceName, accountId)
|
2018-02-09 07:04:10 +01:00
|
|
|
}
|
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
export async function setAccount (instanceName, account) {
|
2018-02-14 04:34:37 +01:00
|
|
|
return setGenericEntityWithId(ACCOUNTS_STORE, accountsCache, instanceName, cloneForStorage(account))
|
2018-02-09 07:04:10 +01:00
|
|
|
}
|
|
|
|
|
2018-08-30 04:03:12 +02:00
|
|
|
export async function searchAccountsByUsername (instanceName, usernamePrefix, limit) {
|
|
|
|
limit = limit || 20
|
2018-03-25 03:04:54 +02:00
|
|
|
const db = await getDatabase(instanceName)
|
|
|
|
return dbPromise(db, ACCOUNTS_STORE, 'readonly', (accountsStore, callback) => {
|
2019-08-03 22:49:37 +02:00
|
|
|
const keyRange = createAccountUsernamePrefixKeyRange(usernamePrefix.toLowerCase())
|
2018-03-25 03:04:54 +02:00
|
|
|
accountsStore.index(USERNAME_LOWERCASE).getAll(keyRange, limit).onsuccess = e => {
|
2019-05-06 04:16:02 +02:00
|
|
|
callback(e.target.result)
|
2018-03-25 03:04:54 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|