Pinafore-Web-Client-Frontend/src/routes/_database/accounts.js

25 lines
1.1 KiB
JavaScript
Raw Normal View History

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
}
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 => {
callback(e.target.result)
2018-03-25 03:04:54 +02:00
}
})
}