Last active
February 18, 2024 18:55
-
-
Save ZachHandley/5217a15c8cf8b13aea1e236cb7445d13 to your computer and use it in GitHub Desktop.
Appwrite Pagination Function
This file contains 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
/** | |
* listDocumentsPaginated | |
* Takes a collectionId, lastId, queries, and limit and returns a list of documents | |
* It's very important to note, if it's the first request, do not pass in a lastId, if it's not the first request, pass in the lastId | |
* Otherwise known as the current last document's ID in the paginated list | |
* | |
* This is best for dynamic lists or LARGE amounts of data | |
*/ | |
const listDocumentsPaginated = async (collectionId: string, lastId?: string, queries: string[] = [], limit: number = 25, forward: boolean = true): Promise<{ type: "success" | "error", message: string, data: Models.DocumentList<Models.Document>}> => { | |
try { | |
if (lastId) { | |
const documents = await database.listDocuments(maindb, collectionId, [...queries, Query.limit(limit), forward ? Query.cursorAfter(lastId) : Query.cursorBefore(lastId)]) | |
return { type: "success", message: "Documents listed successfully", data: documents }; | |
} else { | |
const documents = await database.listDocuments(maindb, collectionId, [...queries, Query.limit(limit)]) | |
return { type: "success", message: "Documents listed successfully", data: documents }; | |
} | |
} catch (e) { | |
console.error(e); | |
return { type: "error", message: "Documents listing failed", data: { documents: [], total: 0 } }; | |
} | |
} | |
// For Example to retrieve something | |
// Something.parse is a ZOD type that I created, ZOD is a JSON type definition library I greatly enjoy and | |
// recommend you check out | |
// Anyways, here ya go ya filthy animal | |
const getSomethingsPaginated = async ( | |
limit: number, | |
queriesToInclude?: string[], | |
lastDocId?: string, | |
sortBy?: string, | |
sortByDesc?: boolean, | |
forward: boolean = true | |
) => { | |
try { | |
const queries: string[] = queriesToInclude ?? []; | |
if (sortBy !== undefined && sortByDesc !== undefined) { | |
queries.push(Query.orderDesc(sortBy)); | |
} else if (sortBy !== undefined) { | |
queries.push(Query.orderAsc(sortBy)); | |
} | |
const somethingsResponse = await appwrite.listDocumentsPaginated( | |
"something", | |
lastDocId, | |
queries, | |
limit, | |
forward | |
); | |
if (somethingsResponse.type === "success" && somethingsResponse.data) { | |
return { | |
type: "success", | |
message: "Somethings found", | |
data: somethingsResponse.data.documents.map((something) => Something.parse(something)), | |
total: somethingsResponse.data.total, | |
}; | |
} else { | |
return { type: "error", message: "Error getting tools" }; | |
} | |
} catch (error: any) { | |
console.error("Error getting somethings paginated: ", error); | |
return { type: "error", message: error.message }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment