Edits history of script submission #22363 for ' Set Document (firebase)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { initializeApp } from "[email protected]/app";
    import { doc, getFirestore, setDoc } from "[email protected]/firestore/lite";
    
    /**
     * Overwrites or updates a document.
     *
     * @param document_path Path to the document.
     * If you want to update a top-level document by ID, pass in an array with only
     * the ID in it.
     * *eg:* `[ 'my-id' ]`
     *
     * @param merge If `true`, the document will be updated with provided fields,
     * otherwise the document will be completely overwritten.
     * **WARNING:** Overwriting a document may result in data loss.
     *
     * @param mergeFields Controls which fields should be merged.
     */
    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,
      document_path: string[],
      document: Record<string, any>,
      merge?: boolean,
      mergeFields?: string[],
    ) {
      const app = initializeApp(auth);
      const store = getFirestore(app);
    
      const docRef = doc(store, collection_id, ...document_path);
      await setDoc(docRef, document, {
        merge: typeof merge === "boolean" ? merge : undefined,
        mergeFields:
          Array.isArray(mergeFields) && mergeFields.length
            ? mergeFields
            : undefined,
      });
    }
    

    Submitted by hugo989 6 days ago