Firebase Firestore and Realtime Database: setting up Firebase project and Firestore database; basic CRUD operations on Firestore; structuring collections and subcollections

Firestore એ Firebase નું NoSQL cloud database છે: એ data ને documents તરીકે સાચવે છે, જે collections માં ભેગાં થાય છે અને subcollections માં અંદર ગોઠવાઈ શકે છે, અને ઉમેરવા, વાંચવા, બદલવા તથા કાઢી નાખવા માટે સાદા methods આપે છે, ઘણી વાર તમારી app ને real time માં અપડેટ કરતાં કરતાં.

11 min read · 7 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Firebase તરફથી મળતું cloud database

તમે Firebase ને authentication અને hosting માટે તો વાપરી જ ચૂક્યા છો. Firebase એક database પણ આપે છે: Firestore, એટલે કે cloud પર રહેતું NoSQL store, જેને FestConnect સીધું વાપરી શકે છે, ક્યારેક તો અલગ MongoDB backend ને બદલે, અને જે data બદલાય એમ app ને real time માં અપડેટ કરી શકે છે.

Firestore નું data model તમને MongoDB ને લીધે જાણીતું લાગશે: collections માં ભેગાં થયેલાં documents. આ પાઠમાં એને ગોઠવવું, એની collections અને documents વાળી રચના (subcollections સહિત), અને પાયાનું CRUD શીખશો. એથી FestConnect પાસે data સાચવવાના જે વિકલ્પો છે એ પૂરા થાય છે.

Theory

Collections, documents, subcollections

Firestore data ને એક સ્પષ્ટ ક્રમમાં ગોઠવે છે.

Collection એટલે documents નું જૂથ ('events' ને યાદ કરો). Document એટલે fields વાળો JSON જેવો એક record (એક event નું name, date, seats) અને એની અનન્ય id. ખાસ વાત એ કે document પોતે subcollections રાખી શકે છે, એટલે કે અંદર ગોઠવાયેલાં collections, જેથી કોઈ event ના document માં કોણે નામ નોંધાવ્યું એની 'registrations' subcollection રહી શકે.

આ અંદરની ગોઠવણ સંબંધિત data ને સહજ રીતે ઢાળવા દે છે: મુખ્ય entities માટે ઉપલા સ્તરનાં collections, અને કોઈ ચોક્કસ document ને લગતી વસ્તુઓ માટે subcollections. આ NoSQL model છે, લવચીક અને JSON ના ઘાટનું, MongoDB જેવું જ, પણ પૂરેપૂરું Firebase દ્વારા host અને સંભાળાતું.

Practical

Firestore CRUD (modular Web SDK)

import { getFirestore, collection, addDoc,
         getDocs, updateDoc, deleteDoc, doc } from 'firebase/firestore';

const db = getFirestore();

// create: 'events' collection માં એક document ઉમેરો
await addDoc(collection(db, 'events'), { name: 'Robotics', seats: 50 });

// read: collection નાં બધાં documents મેળવો
const snap = await getDocs(collection(db, 'events'));
snap.forEach(d => console.log(d.id, d.data()));

// update / delete: id વડે ચોક્કસ document બદલો કે કાઢો
await updateDoc(doc(db, 'events', id), { seats: 40 });
await deleteDoc(doc(db, 'events', id));

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Formula

Firestore સામે Realtime Database

Firebase ખરેખર બે databases આપે છે. Firestore નવું છે, documents અને collections વાળું store, જેમાં querying તથા રચના વધુ સમૃદ્ધ છે, અને મોટા ભાગની apps માટે એ જ ભલામણ કરાયેલી પસંદગી છે. Realtime Database જૂનું છે, જે બધું એક મોટા JSON tree તરીકે સાચવે છે; એ સાદું છે અને પાયાના real-time syncing માટે ખૂબ ઝડપી છે, પણ data વધે એમ એને ગોઠવવું અને એના પર query કરવી અઘરી પડે છે.

બંને જીવંત updates મોકલી શકે છે, પણ Firestore નું વ્યવસ્થિત model (collections, documents, subcollections) મોટા પાયે વધુ સારું ટકે છે. FestConnect માટે Firestore જ સમજદારીભરી પસંદગી છે. બંને અસ્તિત્વમાં છે એ જાણો, અને એ પણ કે Firestore આધુનિક તથા વ્યવસ્થિત વિકલ્પ છે.

Quiz

Firestore માં data કઈ રીતે ગોઠવાયેલો હોય છે?

  1. SQL ની જેમ નક્કી rows અને columns વાળાં tables તરીકે
  2. Documents (JSON જેવા records) નાં collections તરીકે, જ્યાં document ની અંદર subcollections પણ હોઈ શકે
  3. એક જ સપાટ text file તરીકે
  4. ફક્ત images તરીકે
Show the answer

Documents (JSON જેવા records) નાં collections તરીકે, જ્યાં document ની અંદર subcollections પણ હોઈ શકે

Firestore એ document આધારિત NoSQL database છે: data documents નાં collections માં રહે છે (fields અને એક id વાળા JSON જેવા records), અને document ની અંદર subcollections રહી શકે છે, જેથી સહજ રીતે અંદર ગોઠવણ થઈ શકે (દાખલા તરીકે event ના document માં registrations નામની subcollection). વિકલ્પ A કડક tables, rows અને columns વાળા relational કે SQL model ને વર્ણવે છે, જે Firestore (એક NoSQL store) ની રીત નથી. વિકલ્પ C ખોટો છે: એ વ્યવસ્થિત અને query કરી શકાય એવું database છે, એક સપાટ file નહીં. વિકલ્પ D નિરર્થક છે; Firestore કોઈ પણ પ્રકારનો વ્યવસ્થિત data સાચવે છે, ફક્ત images નહીં. આ ક્રમ યાદ રાખો: collections માં documents હોય, અને documents માં subcollections હોઈ શકે.

Think first

સામાન્ય request ની સરખામણીએ 'real-time' ખરેખર શું આપે છે?

Firestore તમારી app ને real time માં અપડેટ કરી શકે છે. સામાન્ય request વડે data મંગાવવા કરતાં એ કઈ રીતે જુદું છે, અને એનું મહત્ત્વ શા માટે છે? વિચારીને પછી tap કરો.

Show the answer

સામાન્ય request એટલે એક જ વારનું ખેંચાણ: તમે data માગો છો, એની અત્યારની સ્થિતિ મળે છે, અને વાત પૂરી; એ પછી data બદલાય તો તમારી app ને એની જાણ જ થતી નથી, જ્યાં સુધી એ ફરી ન પૂછે. Real-time સાંભળવું (Firestore નું onSnapshot) એ કાયમી ઉમેદવારી છે: તમે Firestore ને કહો છો કે 'આ data બદલાય ત્યારે મને જણાવજે', અને એ દરેક ફેરફાર જાતે જ તમારી app સુધી ધકેલે છે, એટલે વારંવાર પૂછ્યા વગર જ તમારું UI database સાથે જીવંત રીતે મળતું રહે છે.

એનું મહત્ત્વ શા માટે: સાથે મળીને કામ કરવાની કે જીવંત સુવિધાઓ માટે એ આખું ચિત્ર બદલી નાખે છે. ધારો કે FestConnect કોઈ event માટે કેટલી બેઠકો બાકી છે એ બતાવે છે. એક જ વારના fetch થી, બીજા બધાએ બેઠકો ભરી લીધા પછી પણ કોઈ એક વપરાશકારના પડદા પર લાંબા સમય સુધી '5 બેઠકો બાકી' લખેલું રહી શકે, જે વાસી અને ગેરમાર્ગે દોરનારું છે, સિવાય કે એ જાતે refresh કરે. Real-time listener હોય તો કોઈ નોંધણી કરે અને ગણતરી ઘટે એ જ ક્ષણે દરેક જોનારના પડદા પર જાતે જ '4 બાકી', પછી '3' થઈ જાય, એટલે દ્રશ્ય હંમેશાં તાજું રહે.

એ જ તાકાત જીવંત chats, સાથે લખાતા documents, dashboards અને notifications ચલાવે છે, એટલે કે જ્યાં જ્યાં અનેક વપરાશકારો કે ઉપકરણોએ તરત જ છેલ્લામાં છેલ્લી સ્થિતિ જોવી પડે ત્યાં. સામે કિંમત એ છે કે જીવંત listeners એક connection રોકે છે અને એમને સંભાળવા પડે છે (જરૂર ન હોય ત્યારે unsubscribe કરવા), અને દરેક પડદાને real-time જોઈતું પણ નથી. પણ જ્યાં તાજગી મહત્ત્વની હોય ત્યાં real-time updates સ્થિર છબીને જીવંત દ્રશ્યમાં ફેરવી નાખે છે, અને એ Firebase ની ઊડીને આંખે વળગતી તાકાતોમાંની એક છે. એક વાર ઉમેદવારી નોંધાવો, પછી data પોતે જ પોતાને તાજું રાખે છે.

Summary

Key takeaways

  • Firestore એ Firebase નું document આધારિત NoSQL cloud database છે.
  • Data documents નાં collections તરીકે ગોઠવાય છે (JSON જેવા records); document ની અંદર subcollections રહી શકે છે.
  • પહેલાં Firebase project ગોઠવો, Firestore ચાલુ કરો, પછી CRUD માટે SDK વાપરો.
  • CRUD: addDoc/setDoc (બનાવવું), getDocs/getDoc (વાંચવું), updateDoc (બદલવું), deleteDoc (કાઢી નાખવું).
  • Firestore real-time updates મોકલી શકે છે (onSnapshot), જેથી UI database સાથે જીવંત રીતે મળતું રહે.
  • Firebase નું જૂનું Realtime Database બધું એક JSON tree માં સાચવે છે; Firestore નવો, વધુ વ્યવસ્થિત અને ભલામણ કરાયેલો વિકલ્પ છે.
  • યાદ રાખવાની કડી: collections માં documents રહે, documents માં subcollections રહી શકે; અને Firestore app ને real time માં અપડેટ કરી શકે.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Firebase and React Integration

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Firebase Firestore and Realtime Database: setting up Firebase project and Firestore database; basic CRUD operations on Firestore; structuring collections and subcollections · Fundamentals of Full Stack Web Development (Major-15-01) · Gri-Learn