0

Set Document

by
Published Apr 27, 2023
Script firebase Verified

The script

Submitted by hugo989 Bun
Verified 6 days ago
1
import { initializeApp } from "[email protected]/app";
2
import { doc, getFirestore, setDoc } from "[email protected]/firestore/lite";
3

4
/**
5
 * Overwrites or updates a document.
6
 *
7
 * @param document_path Path to the document.
8
 * If you want to update a top-level document by ID, pass in an array with only
9
 * the ID in it.
10
 * *eg:* `[ 'my-id' ]`
11
 *
12
 * @param merge If `true`, the document will be updated with provided fields,
13
 * otherwise the document will be completely overwritten.
14
 * **WARNING:** Overwriting a document may result in data loss.
15
 *
16
 * @param mergeFields Controls which fields should be merged.
17
 */
18
type Firebase = {
19
  apiKey: string;
20
  authDomain: string;
21
  projectId: string;
22
  storageBucket: string;
23
  messagingSenderId: string;
24
  appId: string;
25
  measurementId: string;
26
};
27
export async function main(
28
  auth: Firebase,
29
  collection_id: string,
30
  document_path: string[],
31
  document: Record<string, any>,
32
  merge?: boolean,
33
  mergeFields?: string[],
34
) {
35
  const app = initializeApp(auth);
36
  const store = getFirestore(app);
37

38
  const docRef = doc(store, collection_id, ...document_path);
39
  await setDoc(docRef, document, {
40
    merge: typeof merge === "boolean" ? merge : undefined,
41
    mergeFields:
42
      Array.isArray(mergeFields) && mergeFields.length
43
        ? mergeFields
44
        : undefined,
45
  });
46
}
47

Other submissions
  • Submitted by adam186 Deno
    Created 398 days ago
    1
    import { initializeApp } from "npm:[email protected]/app";
    2
    import { doc, getFirestore, setDoc } from "npm:firebase/firestore/lite";
    3
    
    
    4
    /**
    5
     * Overwrites or updates a document.
    6
     *
    7
     * @param document_path Path to the document.
    8
     * If you want to update a top-level document by ID, pass in an array with only
    9
     * the ID in it.
    10
     * *eg:* `[ 'my-id' ]`
    11
     *
    12
     * @param merge If `true`, the document will be updated with provided fields,
    13
     * otherwise the document will be completely overwritten.
    14
     * **WARNING:** Overwriting a document may result in data loss.
    15
     *
    16
     * @param mergeFields Controls which fields should be merged.
    17
     */
    18
    type Firebase = {
    19
      apiKey: string;
    20
      authDomain: string;
    21
      projectId: string;
    22
      storageBucket: string;
    23
      messagingSenderId: string;
    24
      appId: string;
    25
      measurementId: string;
    26
    };
    27
    export async function main(
    28
      auth: Firebase,
    29
      collection_id: string,
    30
      document_path: string[],
    31
      document: Record<string, any>,
    32
      merge?: boolean,
    33
      mergeFields?: string[],
    34
    ) {
    35
      const app = initializeApp(auth);
    36
      const store = getFirestore(app);
    37
    
    
    38
      const docRef = doc(store, collection_id, ...document_path);
    39
      await setDoc(docRef, document, {
    40
        merge: typeof merge === "boolean" ? merge : undefined,
    41
        mergeFields:
    42
          Array.isArray(mergeFields) && mergeFields.length
    43
            ? mergeFields
    44
            : undefined,
    45
      });
    46
    }
    47