{"id":16473660,"url":"https://github.com/kingrayhan/firestore-9-cookbook","last_synced_at":"2025-02-28T06:29:46.440Z","repository":{"id":96069610,"uuid":"438964657","full_name":"kingRayhan/firestore-9-cookbook","owner":"kingRayhan","description":null,"archived":false,"fork":false,"pushed_at":"2024-09-22T07:53:05.000Z","size":13,"stargazers_count":11,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-11T02:27:35.508Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kingRayhan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-16T11:26:37.000Z","updated_at":"2024-09-22T07:53:08.000Z","dependencies_parsed_at":null,"dependency_job_id":"47d4b839-ae2b-4d07-aca2-f742a926c253","html_url":"https://github.com/kingRayhan/firestore-9-cookbook","commit_stats":null,"previous_names":["kingrayhan/firestore-9-cookbook"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingRayhan%2Ffirestore-9-cookbook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingRayhan%2Ffirestore-9-cookbook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingRayhan%2Ffirestore-9-cookbook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingRayhan%2Ffirestore-9-cookbook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kingRayhan","download_url":"https://codeload.github.com/kingRayhan/firestore-9-cookbook/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241112042,"owners_count":19911614,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T12:27:48.153Z","updated_at":"2025-02-28T06:29:46.424Z","avatar_url":"https://github.com/kingRayhan.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Firestore 9 cookbook\n\n\n#### Table of contents\n- [Initialization](#initialization)\n- [Read doc/docs](#read-doc-docs)\n  * [Read collection](#read-collection)\n  * [Read a single document](#read-a-single-document)\n  * [Check a document exists or not](#check-a-document-exists-or-not)\n  * [Conditionally querying data](#conditionally-querying-data)\n- [Store data](#store-data)\n  * [Store a doc with auto generated id](#store-a-doc-with-auto-generated-id)\n  * [Store a doc with custom id](#store-a-doc-with-custom-id)\n- [Deleting document](#deleting-document)\n  * [Delete a document with id](#delete-a-document-with-id)\n- [Update document](#update-document)\n- [Sorting/limiting](#sorting-limiting)\n  * [Get limited number of docs](#get-limited-number-of-docs)\n  * [Limit with order](#limit-with-order)\n\n\n\n\n\n### Initialization\n\n```js\nimport { initializeApp } from \"firebase/app\";\nimport { getFirestore } from \"firebase/firestore\";\n\nconst config = {\n  apiKey: \"xxxx\",\n  authDomain: \"xxxx\",\n  projectId: \"xxxx\",\n  storageBucket: \"xxxx\",\n  messagingSenderId: \"xxxx\",\n  appId: \"xxxx\",\n};\n\n\nexport const app = initializeApp(config);\nexport const db = getFirestore(app);\n```\n\n\n### Read doc/docs\n\n#### Read collection\n```js\nconst collectionRef = collection(db, \"users\");\n\ngetDocs(collectionRef).then((snapshot) =\u003e {\n  snapshot.docs.forEach((doc) =\u003e {\n    console.log(doc.data());\n  });\n});\n```\n\n#### Read a single document\n```js\nconst docRef = doc(db, \"users\", \"G3OIFQ7qes9Rhc74XfRA\");\n// - OR\n// const docRef = doc(db, \"users/G3OIFQ7qes9Rhc74XfRA\");\n\ngetDoc(docRef).then((snapshot) =\u003e {\n  console.log(snapshot.data());\n});\n```\n\n#### Check a document exists or not\n```js\nconst docSnap = await getDoc(docRef);\n\nif(!docSnap.exists()) {\n  console.log(\"Document does not exist!\");\n}\n```\n\n\n#### Conditionally querying data\n```js\nconst collectionRef = collection(db, \"users\");\nconst q = query(collectionRef, where(\"username\", \"==\", \"kingrayhan\"));\n\nconst snapshot = await getDocs(q);\nconsole.log(snapshot.docs[0].data());\n\n\n\n\n\n// -- More query example\nconst stateQuery = query(citiesRef, where(\"state\", \"==\", \"CA\"));\nconst populationQuery = query(citiesRef, where(\"population\", \"\u003c\", 100000));\nconst nameQuery = query(citiesRef, where(\"name\", \"\u003e=\", \"San Francisco\"));\n```\n**Query operator**\n- `\u003c` - less than\n- `\u003c=` - less than or equal to\n- `==` - equal to\n- `\u003e` - greater than\n- `\u003e=` - greater than or equal to\n- `!=` - not equal to\n- `array-contains`\n- `array-contains-any`\n- `in`\n- `not-in`\n\n\n\n### Store data\n\n#### Store a doc with auto generated id\n\n```js\nconst collectionRef = collection(db, \"users\");\nconst docRef = await addDoc(collectionRef, {\n  username: \"johndoe\",\n  avatar: \"https://avatars0.githubusercontent.com/u/174825?v=4\",\n});\n\nconsole.log(docRef.id);\n```\n\n#### Store a doc with custom id\n\n```js\nconst docRef = doc(db, \"users\", \"user-id-custom\");\n// - OR\n// const docRef = doc(db, \"users/G3OIFQ7qes9Rhc74XfRA\");\n\nawait setDoc(docRef, {\n  username: \"kingrayhan\",\n  avatar: \"https://avatars0.githubusercontent.com/u/174825?v=4\",\n});\n```\n\n### Deleting document\n\n#### Delete a document with id\n```js\nconst docRef = doc(db, \"users\", \"xj7lxm0OGObV91xn3tE0\");\nawait deleteDoc(docRef);\n```\n\n### Update document\n```js\nconst docRef = doc(db, \"users\", \"AvPfqaJs4hCmqpk0RUjU\");\n// - OR\n// const docRef = doc(db, \"users/G3OIFQ7qes9Rhc74XfRA\");\n\n\n/**\n * Update document\n */\nawait updateDoc(docRef, {\n  name: \"Rayhan\",\n  username: \"rayhan\",\n});\n\n/**\n * Override the whole document\n */\nawait setDoc(docRef, {\n  x: 10,\n});\n```\n\n### Sorting/limiting\n\n#### Get limited number of docs\n```js\nconst colRef = collection(db, \"posts\");\n\nconst q = query(colRef, limit(5))\nconst postsSnap = await getDocs(q)\n\nconst posts = postsSnap.docs.map((post) =\u003e post.data())\n\n// -\u003e console.log(posts)\n```\n\n#### Limit with order\n```js\nconst colRef = collection(db, \"posts\");\n\nconst q = query(colRef, limit(5), orderBy(\"createdAt\", \"desc\"))\nconst postsSnap = await getDocs(q)\n\nconst posts = postsSnap.docs.map((post) =\u003e post.data())\n\n// -\u003e console.log(posts)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingrayhan%2Ffirestore-9-cookbook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkingrayhan%2Ffirestore-9-cookbook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingrayhan%2Ffirestore-9-cookbook/lists"}