Skip to content

Instantly share code, notes, and snippets.

@ZachHandley
Last active February 18, 2024 18:55
Show Gist options
  • Save ZachHandley/5217a15c8cf8b13aea1e236cb7445d13 to your computer and use it in GitHub Desktop.
Save ZachHandley/5217a15c8cf8b13aea1e236cb7445d13 to your computer and use it in GitHub Desktop.

Revisions

  1. ZachHandley revised this gist Feb 18, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion pagination_func.ts
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,6 @@ const getSomethingsPaginated = async (
    ) => {
    try {
    const queries: string[] = queriesToInclude ?? [];
    sortByIncluded = sortBy ?? sortBy == "";
    if (sortBy !== undefined && sortByDesc !== undefined) {
    queries.push(Query.orderDesc(sortBy));
    } else if (sortBy !== undefined) {
  2. ZachHandley revised this gist Feb 18, 2024. 1 changed file with 3 additions and 6 deletions.
    9 changes: 3 additions & 6 deletions pagination_func.ts
    Original file line number Diff line number Diff line change
    @@ -32,19 +32,16 @@ const getSomethingsPaginated = async (
    lastDocId?: string,
    sortBy?: string,
    sortByDesc?: boolean,
    search?: string,
    forward: boolean = true
    ) => {
    try {
    const queries: string[] = queriesToInclude ?? [];
    if (sortBy && sortByDesc) {
    sortByIncluded = sortBy ?? sortBy == "";
    if (sortBy !== undefined && sortByDesc !== undefined) {
    queries.push(Query.orderDesc(sortBy));
    } else if (sortBy) {
    } else if (sortBy !== undefined) {
    queries.push(Query.orderAsc(sortBy));
    }
    if (search) {
    queries.push(Query.search("search", search));
    }
    const somethingsResponse = await appwrite.listDocumentsPaginated(
    "something",
    lastDocId,
  3. ZachHandley revised this gist Feb 18, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pagination_func.ts
    Original 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 tools paginated: ", error);
    console.error("Error getting somethings paginated: ", error);
    return { type: "error", message: error.message };
    }
    }
  4. ZachHandley created this gist Feb 18, 2024.
    69 changes: 69 additions & 0 deletions pagination_func.ts
    Original 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 };
    }
    }