Edits history of script submission #22365 for ' List Documents (firebase)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { initializeApp } from "[email protected]/app";
    import {
      collection,
      endAt,
      getDocs,
      getFirestore,
      limit as limitItems,
      orderBy,
      query,
      QueryConstraint,
      startAt,
    } from "[email protected]/firestore/lite";
    
    /**
     * @param order_by Name of the field to order by.
     *
     * @param start_at Can only be used if `order_by` is present,
     * because it'll filter based on the value of that field.
     *
     * @param end_at Can only be used if `order_by` is present,
     * because it'll filter based on the value of that field.
     */
    type Firebase = {
      apiKey: string;
      authDomain: string;
      projectId: string;
      storageBucket: string;
      messagingSenderId: string;
      appId: string;
      measurementId: string;
    };
    export async function main(
      auth: Firebase,
      collection_id: string,
      order_by?: string,
      order_direction: "asc" | "desc" = "asc",
      start_at?: number | string,
      end_at?: number | string,
      limit?: number,
    ) {
      const app = initializeApp(auth);
      const store = getFirestore(app);
    
      const colRef = collection(store, collection_id);
      const constraints = [
        order_by ? orderBy(order_by, order_direction) : undefined,
        start_at ? startAt(start_at) : undefined,
        end_at ? endAt(end_at) : undefined,
        limit ? limitItems(limit) : undefined,
      ].filter(Boolean) as QueryConstraint[];
      const snapshot = await getDocs(query(colRef, ...constraints));
      const list = snapshot.docs.map((doc) => doc.data());
    
      return list;
    }
    

    Submitted by hugo989 6 days ago