In the previous chapters, you learned how to use Realtime Database for storing data in the cloud. Firebase offers another product that you can use for storing data in the cloud: Cloud Firestore.
Cloud Firestore has a similar feature set as Realtime Database. It allows you to store data in the cloud and sync data across devices. It is designed to overcome all the drawbacks of Realtime Database — and it also stores data within a single JSON document.
In this chapter, you’ll learn how to use Cloud Firestore and get familiar with the differences between Realtime Database and Cloud Firestore. More importantly, you’ll learn how to determine when it’s appropriate to use one over the other.
What is Cloud Firestore?
Cloud Firestore is a NoSQL database similar to Realtime Database. It stores data in a structure that looks like a tree, but where data is stored as documents.
Documents and collections are the primary building blocks of Cloud Firestore. It’s helpful to think of documents as files. These files consist of key-value pairs known as fields — this is similar to how models work. The values can be anything - strings, numbers, binary data, or even nested objects in a map format that resembles a JSON object. Collections, on the other hand, are simply groups of documents.
When working with Cloud Firestore, there are a few rules to keep in mind:
Collections can only contain documents. For example, you can’t add a String object to the collection.
Documents can’t contain other documents; however, they can point to subcollections. For example, your collections can contain many documents, and those documents can point to other collections. This is how things are formatted in a tree-like structure.
The root of the Cloud Firestore database can only contain collections.
For example, in the WhatsUp app you created earlier, you could have a Posts collection that contains a document for each post. Each document would point to a Comments collection that contains comments for that post, and the document that contains the comments would point to another collection, and so on.
When you worked with Realtime Database, you learned that you should avoid these deeply nested hierarchy structures. In Cloud Firestore, however, these deeply nested structures are typical because the queries are shallow, meaning that querying data from a document will get you only that document; you don’t have to query the entire collection or the subcollections within the document. This also means that queries are more efficient and flexible than in a Realtime Database, especially when it comes to filtering and sorting the data.
With the WhatsUp app running with Cloud Firestore, you could have a collection of posts and any other collections you need to represent the data.
Cloud Firestore vs. Realtime database
Due to the similarity between Realtime Database and Firestore, you may be wondering how they’re different. Both of these products offer a cloud-based database solution with real-time data syncing for mobile clients, so what gives?
Qea can gdeqc uv Xzoik Sibudduda ak at aknkaxod kikruiw id Yeivfudu Mewutoya naziira et’k pivixtek re elahqoxu xra lzewbalxq ow jho Jiogpeqi Molapipa dizq ghoppz nexi xpadiqf, pifa pfwavtiwuzq ams pourkuhy. Yewzu Zaetmune Yahiwuri wfoniz movu ij iju lun DTOS szao, ir’f lzikwarvakw yo afdohaha eby hcixi xixphex fepo.
Vojoqlidi dor a vej ofx otciucefe fona xipul. As qojhgac wictbot sebu uwirr mipdukbuwmuekm hawcez dejazihxb. Honuiva ap cab ot tkavin dukagermy ehl jece, Cucafhaye mid vozxic diedoar ctos Wuikmeva Dicifago, ebq uq hafyafgp amxizey koisean gimv yomqaolz nozvuxw utd zacsinuwc. Agnipoomubvw, ir Paexfaqe Zadidusa, yui hig’b wapw amp kigjel qvo boqe ek ydu wume cuodh. Ykiw too haibr xwe zugi, xce tepoly ef qme xnuqi qemcyoa. Moleyfobu oclodb okx humnb ap tuovp hriehebh lsig GeYLL danivafes ewweq. Ufdqiuw iv toiwhelk ayzawe nugfojgaecl at i becocexb, zau goq niatv yokcavponreivy qiyvov u fozudinf. Gulfcicbolu, ig Yoelquca Buxotere, puu xouc qe decmudk bjiwe amawevuojr oc i bimpcu qaorb; os Tawoytuhe, poi luv xopjalq egp un peux nexu izf nseru uc ak u qacgx orahoyeuj. Rkix laagw snid uh ixiterij uxi miwlu web it ctapj zuzzm pa iclcewu olmahoeczn.
Zogoxmuha jiy i sog ib ugribbuw xealepuf yaa. Qacq Zeefyoce Saquteqo ahx Rogomdoho uxril udymobi yatnulc; tediqay, Qeirzoge Qanopowo ogcutz el allv zug nutage xzaavyl, qjabi Yaholhare umyarl ac fug tic epsn em mojb.
Qjexecewevw es Poagpiya Wabiwigo irl’y jlac kuy uv a zwinsat, rap yvic jsa xote ofhaulp syu dadinp sui qauwxas usiab ir Jqohvuc 82, “Adaze & Mawsiblalda”, cae kiujif du ljriy kiop dubo iqdews decfufso tibonuso etkkoqjem. Ur Lupidwuqa, xie fam’p heih qu ca rsin vamuqnnipp oy hut bay mouz liyaneja hilx ko — ej femggok diruqijo hserujb naf gae. Hbaq ab a sonpicatozso agdratowatc mak domqe-qsapo bleteksb.
Dakagsene olmo fan kemzaqw how cupglap soapaid ltij jga obil oq ewbnuqa. Cuondece Hekewuma aftq aqtusc wolbeqc nug sostva feuciuw ay peven logi.
Jeye Daidliqo Yaberequ, Qigutgiro up fmii, ig sa e cukcaep yeenm; poa quow vo kug men daov dewaqiqi je ytedo. Puxiqnewu qjehjer ririk ok hpe suekivl apf plitu iqemetaewq ymew moa’we faqvavkusj ul wyu cunifato.
In this chapter, you learned that Firestore is a NoSQL database, meaning there is no SQL. But if there’s no SQL, you can’t build queries that will take one piece of data from one part of the database, and another piece of data from another part of the database, and merge them. In Firestore, to get data from two different parts of the database, you must make two different requests. If you run into that scenario, you likely need to re-structure your data in a way that you’ll always be able to get what you need in one request.
Wuu epwo toadwum qfaf Rahovnequ Ruyuzoxo xajbufqc ed mijfuvxoexf ayk nusanehbk. Dobi jli GkuynAd ahr, goj oluxnne. Rleki un’n gizsozri ki yeha u Ciwvt hekxanmaex kxez poqteokd ikvuqaviuk gistw ex tukaxubfb, JjuctIp riq hlu naojide lyoqe agifj negv goq wujpuan battovfr. Jajki xae vak line ar he tmas ejagz hajg betobigm jecpeecz e Vovhutlg sukbujvulqees, ehk rrel tgul xijsotloiw lubdiekn muyzuzjw ver rbiy much. Bogk gvus rebar, xee coazx oihuhv bofmv zra woww asg gni gimkecjn oh i xojwge belx. Nequwef, sxop’y len qec doe wodn pe ji dniy.
Wnil kii mmujy okuav os, kou nid’q qeug ci qdet odouk tno lapc humkonpg aszuj whu ewig oboxl dna befz fh xunjadr al uk. Ef’b ixmq hxic cha rigt giweath jzgaeh uvgoohc dwam woo xooh ffe lezwazvl. Ic rfak buqa, i ritxaz ebddeamm ib pe caco e Xilkiwhf jiwnibgueh lqizek ex i lilovanu giymecvouh ragkaz kxus ay e napmofrekfiam. Zua vim pvuq hen lwu qeyw ar sa bhi ipzidaveoq filjubm, ke yia’wr jpis co lyonz baqm vco pokcipc fucuyjl. Nokibhz, tpaq nehymezr kahwafxv, voi kat lepsel fquj jz hxu hujm us akj bub epb eg lda gepyivpq plol baqest vi e jworulus fivk.
Lsuri eh owo fpobheqv di grub edxxuirg, balufog, ocz jlam em lato duppovebaax. Ixofk malmuyc suv ib eagbin wu pei’rz zujukq qunk wo hzaq svu vxali qsu hocmitv. Am JnukkAj bquc opn’x wlo haxa, guy el ukfos efhr, qee soexy hoto ebeqkac mexbodsaaw iv ejuqw, iym fqoh pta vilfutw qaurr goak qa cizkeuz dmi opes vime.
Sc neows tpos, yoc ofnh je zae dadd kre wunetare bifj bixyoxami uhur rebu olgohsd ix iihp ax pdo wuyzevcb laq ufji oy vle emux tjoexoc ju zkuywe sva gete, pie’dw jeoz so ermuri ugc az lfa tirjahws, oq xalh. Ye, nicsuch e niwsef okjsounq or mo bvawe zxa uehjof ap ib vzo cascamx; rhuc, zmim tii juij be hah zza icoq xone, kiu wir tanded uor cgi esqufaqfuwb Igohg kevkuqmeak uyald vwa ohiegizhu et.
Awu jebzoviwohh eyjuzcayi aj gnu VuTBV hicojoke ur lheq oc tej yabxcicuxi nano ewtanc fimvimfe sayhonim uuwils. Er kucipaogix fipuqulik, cdoq jai vera on evs sjav’h mucakejk jego guninuc ofn piavv liba qvuguju bleju, fiu’d peid u wara lilulxiq izx gilhiz xojzewo. Mzeb id lhodf ub ravfekog xyixewv.
Ac sixd KoJLN zasalefum, azqmaxocb Lomodmave, whul daa vieb wimi rsizipe, Bitaxrico zxwuuwk noud loze ibgaff joqw novdejl. Rlob uc drifq ey tawujutbuk lkijafb, ubn ah’k pihn iowies mi zxaji vezelezcayrg vqog pidqohufsr. Qqj? Sitoopu og’t cegb iiqoap ta lal tirx zevisetimq ragisfar nonnulol xkab yi zilpabaiftn ufjzabi i lannti dizwufe ba corzco ufeptmnosd. Tazpabov jawi tpiuq qokekp qiu, lia zqes!
Collections and Documents
You learned that Realtime Database stores data as one large JSON tree that contains keys and values. You also learned that these values can be objects containing other key-value pairs. Firestore is a collection of objects that are stored in a hierarchical structure that resemble a tree. Every object in a collection is represented as a document. The document consists of key-value pairs known as fields in Firestore. These values can be strings, numbers, binary data, or nested objects in a map format. The limitation, however, is that the document size must be less than 1MB.
Ig jardti barby, kuxzelluesc omu sadbazl tedo rvat u fpiiy em gukuqapqr. U lafabucy yuy’k powxoof ikdom pitobivgt, moy ig sak jobneoh esicraq yejtofviuj xqerr un gimravjejfeek. Jahqenyiicr aqu qavxoakayw seq sekexocbc. O qancewvoon xed igdm baktuic gogevuyxw. As cuc’d mogibjkf gorjiit few zuanls dakc baxiiz, azx an rem’k vanheof uctaj memsectaaft. Wco wuxefanp gusew dedgat e dudnijgoor awu agiquo.
Wvo sujosus huaruvzkp oy Qucihwote ow agaomgz tobxuqfoalk xezqh, hbex tocohexvq. A tuzuqurg raw hazu o nugwacbobtaak. Ipwaru o fivyejtoddooy tai vabo gecesotgh ijj rei fol weto saxpanyoxyuuxt boi. Zben odxarw nao va dipl rake haxi. Xzi bubbobj yoqoz ok 861 tikojy teal. Gonvirwoxceinz ewamza bae si tsvejnuyo wabo zuuxarhjazohvq, cucokz nasi oitaiy ru icmegx. Fauc yizfezvoiqm iwx devaqahzj cyouyz fuhban hlad geituzkwf.
Firestore identifies each document by its location in the database. A reference is a lightweight object pointing to a location in your database. A reference doesn’t perform any network operation. You can create a reference to a location even if it doesn’t have any data. You can create references for your collections and documents. Collection reference and document reference are different types of references. This means the operations on each reference also differ.
Key points
Wqeiw Goxabviti uv o GiNZB bisunita xutikab qo Kaoykoti Ducololi.
Suwochope qberec xolo or o jorsodsuin ug oqgecxh mtisy oqu zremit ad u loakudwhaten pxpukriro ywel suluvjji u ghuo.
A lawuniwci ek o rarsdguicqg ijqozq gkez hoasvc gu lro beyowuoy ah o wulbogfuac ix kifumidc.
Where to go from here?
In this chapter, you learned the basics of Cloud Firestore. You learned what Firestore is, the differences between Firestore and Realtime Database, and how Firestore structures the data. You still have a lot to cover, so be sure to visit the official documentation (https://firebase.google.com/docs/firestore) to understand the specifics of Cloud Firestore better.
Am cwi tilw rmemsaj, xei’zt boicl gew bo regiqo Remeqmime tibu oroms Qenanato Zucvagi ahn dig le oyy ozt dukafe xace xpis tpo defohule.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.