4b06f42db7
* sort of works * type fixes * added option to run old way too
29 lines
794 B
TypeScript
29 lines
794 B
TypeScript
import type { Collection } from '@/modules/shared/helpers/dbHelper'
|
|
import type { MaybeNullOrUndefined } from '@speckle/shared'
|
|
|
|
type GetPaginatedItemsArgs = {
|
|
limit: number
|
|
cursor?: MaybeNullOrUndefined<string>
|
|
}
|
|
|
|
export const getPaginatedItemsFactory =
|
|
<TArgs extends GetPaginatedItemsArgs, T>({
|
|
getItems,
|
|
getTotalCount
|
|
}: {
|
|
getItems: (args: TArgs) => Promise<{ items: T[]; cursor: string | null }>
|
|
getTotalCount: (args: Omit<TArgs, 'cursor' | 'limit'>) => Promise<number>
|
|
}) =>
|
|
async (args: TArgs): Promise<Collection<T>> => {
|
|
const [totalCount, { items, cursor }] = await Promise.all([
|
|
getTotalCount(args),
|
|
args.limit === 0 ? { cursor: null, items: [] } : getItems(args)
|
|
])
|
|
|
|
return {
|
|
items,
|
|
cursor,
|
|
totalCount
|
|
}
|
|
}
|