0
Create Document
One script reply has been approved by the moderators Verified
Created by aurelieiriden 930 days ago Viewed 9403 times
0
Submitted by adam186 Deno
Verified 606 days ago
1
import { initializeApp } from "npm:firebase@9.20.0/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