Skip to content

Instantly share code, notes, and snippets.

@Bogdan808
Last active September 11, 2025 10:11
Show Gist options
  • Select an option

  • Save Bogdan808/07b52670a53cb39bfa7c651f2d26876b to your computer and use it in GitHub Desktop.

Select an option

Save Bogdan808/07b52670a53cb39bfa7c651f2d26876b to your computer and use it in GitHub Desktop.
getWithPagination
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