generated from mathix420/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ paginate datasource entries fetching
- Loading branch information
Showing
6 changed files
with
89 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { sleep } from "./utils"; | ||
|
||
export type PaginateCallback<T> = ( | ||
limit: number, | ||
page: number, | ||
) => Promise<{ | ||
data: T[]; | ||
total: number; | ||
}>; | ||
|
||
export async function paginate<T>( | ||
callback: PaginateCallback<T>, | ||
limit: number = 1000, | ||
coolDown: number = 400, | ||
): Promise<T[]> { | ||
const res = []; | ||
let lastTotal = limit; | ||
let page = 1; | ||
|
||
while (lastTotal >= limit * page) { | ||
if (page > 1) await sleep(coolDown); | ||
const { data, total } = await callback(limit, page); | ||
console.log(data); | ||
lastTotal = total; | ||
res.push(...data); | ||
page++; | ||
} | ||
|
||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters