Last active
September 11, 2025 10:11
-
-
Save Bogdan808/07b52670a53cb39bfa7c651f2d26876b to your computer and use it in GitHub Desktop.
getWithPagination
This file contains hidden or 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
| async getWithPagination(params: IGetArticlesParamsRepo = {}): Promise<IGetArticlesWithPaginationRepo> { | |
| const { page = 1, limit = 12 } = params; | |
| const offset = (page - 1) * limit; | |
| // Получаем общее количество статей | |
| const totalResult = await db | |
| .select({ count: count() }) | |
| .from(ArticleSchema); | |
| const total = Number(totalResult[0]?.count || 0); | |
| // Получаем статьи с пагинацией | |
| const articles = await db | |
| .select() | |
| .from(ArticleSchema) | |
| .limit(limit) | |
| .offset(offset); | |
| const totalPages = Math.ceil(total / limit); | |
| const data = articles.map(a => articleValidation.parse(a)); | |
| return { | |
| data, | |
| meta: { | |
| page, | |
| limit, | |
| total, | |
| totalPages, | |
| hasNext: page < totalPages, | |
| hasPrev: page > 1, | |
| }, | |
| }; | |
| }, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment