Last active
February 18, 2024 18:55
-
-
Save ZachHandley/5217a15c8cf8b13aea1e236cb7445d13 to your computer and use it in GitHub Desktop.
Revisions
-
ZachHandley revised this gist
Feb 18, 2024 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -36,7 +36,6 @@ const getSomethingsPaginated = async ( ) => { try { const queries: string[] = queriesToInclude ?? []; if (sortBy !== undefined && sortByDesc !== undefined) { queries.push(Query.orderDesc(sortBy)); } else if (sortBy !== undefined) { -
ZachHandley revised this gist
Feb 18, 2024 . 1 changed file with 3 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -32,19 +32,16 @@ const getSomethingsPaginated = async ( lastDocId?: string, sortBy?: string, sortByDesc?: boolean, forward: boolean = true ) => { try { const queries: string[] = queriesToInclude ?? []; sortByIncluded = sortBy ?? sortBy == ""; 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, -
ZachHandley revised this gist
Feb 18, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -63,7 +63,7 @@ const getSomethingsPaginated = async ( return { type: "error", message: "Error getting tools" }; } } catch (error: any) { console.error("Error getting somethings paginated: ", error); return { type: "error", message: error.message }; } } -
ZachHandley created this gist
Feb 18, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ /** * 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, search?: string, forward: boolean = true ) => { try { const queries: string[] = queriesToInclude ?? []; if (sortBy && sortByDesc) { queries.push(Query.orderDesc(sortBy)); } else if (sortBy) { queries.push(Query.orderAsc(sortBy)); } if (search) { queries.push(Query.search("search", search)); } 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 tools paginated: ", error); return { type: "error", message: error.message }; } }