Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add catch to cachDB fallback, defer admin store init #877

Merged
merged 2 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/pages/admin/Admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@ import { Link } from 'react-router-dom'
import Text from 'src/components/Text'
import Flex from 'src/components/Flex'
import { Box } from 'rebass'
import { inject, observer } from 'mobx-react'
import { AdminStore } from 'src/stores/Admin/admin.store'

class AdminPageClass extends React.Component<null, null> {
interface IProps {}
interface IInjectedProps extends IProps {
adminStore: AdminStore
}
@inject('adminStore')
@observer
class AdminPageClass extends React.Component<IProps, any> {
componentDidMount() {
this.injected.adminStore.init()
}
get injected() {
return this.props as IInjectedProps
}
public render() {
return (
<div id="AdminPage">
Expand Down
14 changes: 6 additions & 8 deletions src/stores/Admin/admin.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ export class AdminStore extends ModuleStore {
public tags: ITag[] = []
constructor(rootStore: RootStore) {
super(rootStore)
this._init()
}

/*********************************************************************************
* User Admin
*********************************************************************************/
@action
private async _init() {
public async init() {
this.admins = await this._getUsersByRole('admin')
this.superAdmins = await this._getUsersByRole('admin')
}

/*********************************************************************************
* User Admin
*********************************************************************************/
public async addUserRole(username: string, role: UserRole) {
const userRef = this.db.collection<IUser>('v3_users').doc(username)
const user = await userRef.get('server')
Expand All @@ -40,7 +38,7 @@ export class AdminStore extends ModuleStore {
if (!userRoles.includes(role)) {
userRoles.push(role)
await userRef.set({ ...user, userRoles })
this._init()
this.init()
}
}

Expand All @@ -54,7 +52,7 @@ export class AdminStore extends ModuleStore {
if (userRoles.includes(role)) {
userRoles.splice(userRoles.indexOf(role), 1)
await userRef.set({ ...user, userRoles })
this._init()
this.init()
}
}

Expand Down
40 changes: 26 additions & 14 deletions src/stores/databaseV2/clients/dexie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,30 @@ export class DexieClient implements AbstractDBClient {
}

// mapping to generate firebase query from standard db queryOpts
private async _processQuery<T>(
private _processQuery<T>(
endpoint: IDBEndpoint,
queryOpts: DBQueryOptions,
) {
const query = { ...DB_QUERY_DEFAULTS, ...queryOpts }
const { limit, orderBy, order, where } = query
// all queries sent with a common list of conditions
const table = db.table<T>(endpoint)
const filtered = where
? this._generateQueryWhereRef(table, where)
: table.toCollection()
const directed = order === 'desc' ? filtered.reverse() : filtered
// as sortBy is a manual operation specify all other criteria first
const sorted = await directed.sortBy(orderBy!)
return limit ? sorted.slice(0, limit) : sorted
): Promise<(T & DBDoc)[]> {
return new Promise((resolve, reject) => {
const query = { ...DB_QUERY_DEFAULTS, ...queryOpts }
const { limit, orderBy, order, where } = query
// all queries sent with a common list of conditions
const table = db.table<T>(endpoint)
const filtered = where
? this._generateQueryWhereRef(table, where)
: table.toCollection()
const directed = order === 'desc' ? filtered.reverse() : filtered
// as sortBy is a manual operation specify all other criteria first
directed
.sortBy(orderBy!)
.then(sorted => {
resolve(limit ? (sorted.slice(0, limit) as any) : sorted)
})
// error will be thrown if index doesn't exist, simply pass up the chain
.catch(err => {
reject(err)
})
})
}

private _generateQueryWhereRef<T>(
Expand All @@ -77,7 +86,10 @@ export class DexieClient implements AbstractDBClient {
case '>':
return ref.where(field).below(value)
default:
throw new Error('mapping has not been created for dexie query')
console.error('no dexie query mapping for ' + operator)
throw new Error(
'mapping has not been created for dexie query: ' + operator,
)
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/stores/databaseV2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,16 @@ class CollectionReference<T> {
where: { field, operator, value },
})
// if not found on live try find on cached (might be offline)
// use catch as not all endpoints are cached or some might not be indexed
if (docs.length === 0) {
docs = await cacheDB.queryCollection<T>(this.endpoint, {
where: { field, operator, value },
})
try {
docs = await cacheDB.queryCollection<T>(this.endpoint, {
where: { field, operator, value },
})
} catch (error) {
console.error(error)
// at least we can say we tried...
}
}
return docs
}
Expand Down