0

Create Document

by
Published Jun 6, 2022
Script firebase Verified

The script

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

4
/**
5
 * Learn more about adding documents at
6
 * https://firebase.google.com/docs/firestore/manage-data/add-data
7
 *
8
 * @returns The ID of the new document.
9
 */
10
type Firebase = {
11
  apiKey: string;
12
  authDomain: string;
13
  projectId: string;
14
  storageBucket: string;
15
  messagingSenderId: string;
16
  appId: string;
17
  measurementId: string;
18
};
19
export async function main(
20
  auth: Firebase,
21
  collection_id: string,
22
  document: Record<string, any>,
23
) {
24
  const app = initializeApp(auth);
25
  const store = getFirestore(app);
26

27
  const colRef = collection(store, collection_id);
28
  const res = await addDoc(colRef, document);
29

30
  return res.id;
31
}
32

Other submissions
  • Submitted by adam186 Deno
    Created 398 days ago
    1
    import { initializeApp } from "npm:[email protected]/app";
    2
    import { addDoc, collection, getFirestore } from "npm:firebase/firestore/lite";
    3
    
    
    4
    /**
    5
     * Learn more about adding documents at
    6
     * https://firebase.google.com/docs/firestore/manage-data/add-data
    7
     *
    8
     * @returns The ID of the new document.
    9
     */
    10
    type Firebase = {
    11
      apiKey: string;
    12
      authDomain: string;
    13
      projectId: string;
    14
      storageBucket: string;
    15
      messagingSenderId: string;
    16
      appId: string;
    17
      measurementId: string;
    18
    };
    19
    export async function main(
    20
      auth: Firebase,
    21
      collection_id: string,
    22
      document: Record<string, any>,
    23
    ) {
    24
      const app = initializeApp(auth);
    25
      const store = getFirestore(app);
    26
    
    
    27
      const colRef = collection(store, collection_id);
    28
      const res = await addDoc(colRef, document);
    29
    
    
    30
      return res.id;
    31
    }
    32