-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How about supporting Raw result and join query? #68
Comments
and,,, typeorm querybuilder.take method is not working if with join query |
Sorry, I don’t get it, could you please explain it more? |
ok, |
But if it returns raw results, then the library cannot generate cursors. |
yes, so how about add paginateRaw that return generate cursor like paginate in your package like this. public async paginateRaw(builder: SelectQueryBuilder<Entity>): Promise<PagingRawResult> {
const raws = await this.appendPagingQuery(builder).getRawMany();
const hasMore = raws.length > this.limit;
if (hasMore) {
raws.splice(raws.length - 1, 1);
}
if (raws.length === 0) {
return this.toPagingResult(raws);
}
if (!this.hasAfterCursor() && this.hasBeforeCursor()) {
raws.reverse();
}
if (this.hasBeforeCursor() || hasMore) {
this.nextAfterCursor = this.encode(raws[raws.length - 1]);
}
if (this.hasAfterCursor() || (hasMore && this.hasBeforeCursor())) {
this.nextBeforeCursor = this.encode(raws[0]);
}
return this.toPagingRawResult(raws);
}
private toPagingRawResult(raws: any[]): PagingRawResult {
return {
data: raws,
cursor: this.getCursor(),
};
} this paginateRaw method logic equal paginate in your package except below code. const raws = await this.appendPagingQuery(builder).getRawMany(); |
Would you mind sending a PR for this feature with proper test cases? |
I've had this need as well so I had to implement myself, and I can tell it's not easy to do with typeorm, the mapping from raw results to entity results for cursor creation is tedious. |
For solving the join query, I notice Typeorm provides this Joining and mapping functionality. In this case the Message entity will be loaded in the PagingResulst NotificationEntity {
id: '123',
...
message: MessageEntity {
id: '123'
...
}
} |
example:
const queryBuilder = this.dataSource .createQueryBuilder(Notification, 'notification') .select('notification.id', 'id') .addSelect('message.title', 'title') .leftJoin('notification.receiver', 'user') .leftJoin('notification.message', 'message') .where('user.did =:did', { did: userDID }); const paginator = buildPaginator({ entity: Notification, paginationKeys: ['id'], query: pagingQuery, }); const { data, cursor } = await paginator.paginateRaw(queryBuilder); return { data, cursor };
The text was updated successfully, but these errors were encountered: