【发布时间】:2021-05-22 08:21:42
【问题描述】:
如何使用光标对搜索结果进行分页?
这是我的数据查询功能,有问题,因为当我加载更多数据时,有些项目与搜索的查询无关?
@UseMiddleware(isAuthenticated)
@Query(() => PaginatedQuizzes)
async quizzes(
@Arg('limit', () => Int) limit: number,
@Arg('cursor', () => String, { nullable: true }) cursor: string | null,
@Arg('query', () => String, { nullable: true }) query: string | null
): Promise<PaginatedQuizzes> {
const realLimit = Math.min(50, limit);
const realLimitPlusOne = realLimit + 1;
const qs = await getConnection()
.getRepository(Quiz)
.createQueryBuilder('q')
.leftJoinAndSelect('q.author', 'author')
.leftJoinAndSelect('q.questions', 'questions')
if (query) {
const formattedQuery = query.trim().replace(/ /g, ' <-> ');
qs.where(
`to_tsvector('simple',q.title) @@ to_tsquery('simple', :query)`,
{
query: `${formattedQuery}:*`,
}
).orWhere(
`to_tsvector('simple',q.description) @@ to_tsquery('simple', :query)`,
{
query: `${formattedQuery}:*`,
}
);
}
if (cursor) {
qs.where('q."created_at" < :cursor', {
cursor: new Date(parseInt(cursor)),
});
}
const quizzes = await qs
.orderBy('q.created_at', 'DESC')
.take(realLimitPlusOne)
.getMany();
return {
quizzes: (quizzes as [Quiz]).slice(0, realLimit),
hasMore: (quizzes as [Quiz]).length === realLimitPlusOne,
};
}
【问题讨论】:
标签: postgresql graphql typeorm apollo-server typegraphql