c047ac7be1
* chore(server): move cursor utils to db helper * move collection
29 lines
784 B
TypeScript
29 lines
784 B
TypeScript
import { Collection } from '@/modules/shared/helpers/dbHelper'
|
|
import { 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
|
|
}
|
|
}
|