|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { READ_RUN_ERROR, WRITE_RUN_ERROR } from './constants'; |
|
|
|
|
|
type RethinkDBQuery<O> = { |
|
|
toString: Function, |
|
|
run: () => Promise<O>, |
|
|
}; |
|
|
|
|
|
type ProcessFn<I, O> = (data: O) => Promise<*> | *; |
|
|
type TagsFn<I, O> = (data: O) => Array<?string>; |
|
|
|
|
|
type CreateReadQueryInput<I, O> = $Exact<{ |
|
|
query: RethinkDBQuery<O>, |
|
|
process?: ProcessFn<I, O>, |
|
|
tags: TagsFn<I, O>, |
|
|
}>; |
|
|
|
|
|
type CreateQueryCallback<I, O> = (...args: I) => O; |
|
|
|
|
|
export const createReadQuery = (callback: any) => { |
|
|
return async (...args: any) => { |
|
|
const input = callback(...args); |
|
|
if (typeof input.query.run !== 'function') throw new Error(READ_RUN_ERROR); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const result = await input.query |
|
|
.run() |
|
|
.then(input.process ? input.process : res => res); |
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
}; |
|
|
}; |
|
|
|
|
|
type CreateWriteQueryInput<I, O> = $Exact<{ |
|
|
query: Promise<O>, |
|
|
invalidateTags: TagsFn<I, O>, |
|
|
}>; |
|
|
|
|
|
export const createWriteQuery = <I: Array<*>, O: *>(callback: any) => { |
|
|
return async (...args: I) => { |
|
|
const input = callback(...args); |
|
|
const result = await input.query; |
|
|
if (typeof result.run === 'function') throw new Error(WRITE_RUN_ERROR); |
|
|
|
|
|
// const tags = input.invalidateTags(result).filter(Boolean); |
|
|
// await queryCache.invalidate(...tags); |
|
|
return result; |
|
|
}; |
|
|
}; |
|
|
|