Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,7 @@ data/

# Test Database
prisma/*.db
prisma/*.db-journal
prisma/*.db-journal

# JetBrains IDE
.idea
24 changes: 20 additions & 4 deletions src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export const paginateWithCursor = async <R, C>(
model.findMany({
...query,
cursor,
skip: 1,
take: limit === null ? undefined : -limit - 1,
take: limit === null ? undefined : -limit - 2,
}),
model.findMany({
...query,
Expand All @@ -42,6 +41,15 @@ export const paginateWithCursor = async <R, C>(
}),
]);

if (
JSON.stringify(parseCursor(getCursor(results[results.length - 1]))) ===
JSON.stringify(cursor)
) {
results.pop();
} else if (limit !== null && results.length === limit + 2) {
results.shift();
}

if (limit !== null && results.length > limit) {
hasPreviousPage = Boolean(results.shift());
}
Expand All @@ -54,8 +62,7 @@ export const paginateWithCursor = async <R, C>(
model.findMany({
...query,
cursor,
skip: 1,
take: limit === null ? undefined : limit + 1,
take: limit === null ? undefined : limit + 2,
}),
model.findMany({
...query,
Expand All @@ -65,6 +72,15 @@ export const paginateWithCursor = async <R, C>(
}),
]);

if (
JSON.stringify(parseCursor(getCursor(results[0]))) ===
JSON.stringify(cursor)
) {
results.shift();
} else if (limit !== null && results.length === limit + 2) {
results.pop();
}

hasPreviousPage = Boolean(previousResult.length);
if (limit !== null && results.length > limit) {
hasNextPage = Boolean(results.pop());
Expand Down