└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Firestore 9 cookbook 2 | 3 | 4 | #### Table of contents 5 | - [Initialization](#initialization) 6 | - [Read doc/docs](#read-doc-docs) 7 | * [Read collection](#read-collection) 8 | * [Read a single document](#read-a-single-document) 9 | * [Check a document exists or not](#check-a-document-exists-or-not) 10 | * [Conditionally querying data](#conditionally-querying-data) 11 | - [Store data](#store-data) 12 | * [Store a doc with auto generated id](#store-a-doc-with-auto-generated-id) 13 | * [Store a doc with custom id](#store-a-doc-with-custom-id) 14 | - [Deleting document](#deleting-document) 15 | * [Delete a document with id](#delete-a-document-with-id) 16 | - [Update document](#update-document) 17 | - [Sorting/limiting](#sorting-limiting) 18 | * [Get limited number of docs](#get-limited-number-of-docs) 19 | * [Limit with order](#limit-with-order) 20 | 21 | 22 | 23 | 24 | 25 | ### Initialization 26 | 27 | ```js 28 | import { initializeApp } from "firebase/app"; 29 | import { getFirestore } from "firebase/firestore"; 30 | 31 | const config = { 32 | apiKey: "xxxx", 33 | authDomain: "xxxx", 34 | projectId: "xxxx", 35 | storageBucket: "xxxx", 36 | messagingSenderId: "xxxx", 37 | appId: "xxxx", 38 | }; 39 | 40 | 41 | export const app = initializeApp(config); 42 | export const db = getFirestore(app); 43 | ``` 44 | 45 | 46 | ### Read doc/docs 47 | 48 | #### Read collection 49 | ```js 50 | const collectionRef = collection(db, "users"); 51 | 52 | getDocs(collectionRef).then((snapshot) => { 53 | snapshot.docs.forEach((doc) => { 54 | console.log(doc.data()); 55 | }); 56 | }); 57 | ``` 58 | 59 | #### Read a single document 60 | ```js 61 | const docRef = doc(db, "users", "G3OIFQ7qes9Rhc74XfRA"); 62 | // - OR 63 | // const docRef = doc(db, "users/G3OIFQ7qes9Rhc74XfRA"); 64 | 65 | getDoc(docRef).then((snapshot) => { 66 | console.log(snapshot.data()); 67 | }); 68 | ``` 69 | 70 | #### Check a document exists or not 71 | ```js 72 | const docSnap = await getDoc(docRef); 73 | 74 | if(!docSnap.exists()) { 75 | console.log("Document does not exist!"); 76 | } 77 | ``` 78 | 79 | 80 | #### Conditionally querying data 81 | ```js 82 | const collectionRef = collection(db, "users"); 83 | const q = query(collectionRef, where("username", "==", "kingrayhan")); 84 | 85 | const snapshot = await getDocs(q); 86 | console.log(snapshot.docs[0].data()); 87 | 88 | 89 | 90 | 91 | 92 | // -- More query example 93 | const stateQuery = query(citiesRef, where("state", "==", "CA")); 94 | const populationQuery = query(citiesRef, where("population", "<", 100000)); 95 | const nameQuery = query(citiesRef, where("name", ">=", "San Francisco")); 96 | ``` 97 | **Query operator** 98 | - `<` - less than 99 | - `<=` - less than or equal to 100 | - `==` - equal to 101 | - `>` - greater than 102 | - `>=` - greater than or equal to 103 | - `!=` - not equal to 104 | - `array-contains` 105 | - `array-contains-any` 106 | - `in` 107 | - `not-in` 108 | 109 | 110 | 111 | ### Store data 112 | 113 | #### Store a doc with auto generated id 114 | 115 | ```js 116 | const collectionRef = collection(db, "users"); 117 | const docRef = await addDoc(collectionRef, { 118 | username: "johndoe", 119 | avatar: "https://avatars0.githubusercontent.com/u/174825?v=4", 120 | }); 121 | 122 | console.log(docRef.id); 123 | ``` 124 | 125 | #### Store a doc with custom id 126 | 127 | ```js 128 | const docRef = doc(db, "users", "user-id-custom"); 129 | // - OR 130 | // const docRef = doc(db, "users/G3OIFQ7qes9Rhc74XfRA"); 131 | 132 | await setDoc(docRef, { 133 | username: "kingrayhan", 134 | avatar: "https://avatars0.githubusercontent.com/u/174825?v=4", 135 | }); 136 | ``` 137 | 138 | ### Deleting document 139 | 140 | #### Delete a document with id 141 | ```js 142 | const docRef = doc(db, "users", "xj7lxm0OGObV91xn3tE0"); 143 | await deleteDoc(docRef); 144 | ``` 145 | 146 | ### Update document 147 | ```js 148 | const docRef = doc(db, "users", "AvPfqaJs4hCmqpk0RUjU"); 149 | // - OR 150 | // const docRef = doc(db, "users/G3OIFQ7qes9Rhc74XfRA"); 151 | 152 | 153 | /** 154 | * Update document 155 | */ 156 | await updateDoc(docRef, { 157 | name: "Rayhan", 158 | username: "rayhan", 159 | }); 160 | 161 | /** 162 | * Override the whole document 163 | */ 164 | await setDoc(docRef, { 165 | x: 10, 166 | }); 167 | ``` 168 | 169 | ### Sorting/limiting 170 | 171 | #### Get limited number of docs 172 | ```js 173 | const colRef = collection(db, "posts"); 174 | 175 | const q = query(colRef, limit(5)) 176 | const postsSnap = await getDocs(q) 177 | 178 | const posts = postsSnap.docs.map((post) => post.data()) 179 | 180 | // -> console.log(posts) 181 | ``` 182 | 183 | #### Limit with order 184 | ```js 185 | const colRef = collection(db, "posts"); 186 | 187 | const q = query(colRef, limit(5), orderBy("createdAt", "desc")) 188 | const postsSnap = await getDocs(q) 189 | 190 | const posts = postsSnap.docs.map((post) => post.data()) 191 | 192 | // -> console.log(posts) 193 | ``` 194 | --------------------------------------------------------------------------------