Theory
એક અલગ પ્રકારનું database
તમે relational databasesમાં tables, rows અને columns વાપર્યા છે. Firebase Realtime Database અલગ છે: તે NoSQL database છે અને બધું એક મોટા JSON tree તરીકે store કરે છે. Data store અથવા fetch કરવા માટે તમે treeના કોઈ ચોક્કસ path તરફ point કરો છો.
આ unitમાં FestConnectનું data Realtime Databaseમાં store થશે, appમાંથી write થશે, પાછું read થશે અને full CRUD થશે. આ opening lesson NoSQLનો અર્થ અને data સુધી પહોંચવા માટેના મુખ્ય object DatabaseReferenceને સમજાવે છે. Tree સમજાઈ જાય પછી આગળનું બધું સરળ બને છે.
Theory
NoSQL અને JSON tree
NoSQL databases dataને rigid tables અને rows વગર store કરે છે. Fixed columnsવાળા schemaની જગ્યાએ flexible અને ઘણીવાર hierarchical data રાખવામાં આવે છે. તેથી data shape બદલાતી હોય અને scale કરવાની જરૂર હોય તેવી apps માટે તે યોગ્ય છે.
Realtime Database આ ideaને સૌથી simple formમાં વાપરે છે: આખી appનું data keys અને nested valuesવાળું એક JSON tree છે. કોઈ event events/event123 path પર રહી શકે છે અને તેની નીચે name તથા date keys હોઈ શકે છે. Tables define કરવાની જરૂર નથી; તમે paths પર values write કરો છો. અને કારણ કે આ Realtime Database છે, changes connected apps વચ્ચે live sync થાય છે અને listener updates તરત જોઈ શકે છે.
Practical
DatabaseReference મેળવવું
val database = FirebaseDatabase.getInstance()
val rootRef = database.reference // the root of the tree
val eventsRef = database.getReference("events") // points at the 'events' path
// child() navigates deeper into the tree:
val oneEventRef = eventsRef.child("event123") // events/event123
val nameRef = oneEventRef.child("name") // events/event123/name
// A DatabaseReference does not hold data itself; it points at a location.Formula
DatabaseReference pointer છે, data નહીં
મુખ્ય idea: DatabaseReference JSON treeમાં કોઈ locationનો pointer છે; તે location પર રહેલું actual data નથી. getReference("events") તમને events pathનું reference આપે છે અને child("event123") pointerને treeમાં વધુ અંદર લઈ જાય છે.
આ referencesનો ઉપયોગ કોઈ path પર write કરવા અને કોઈ path પરથી read અથવા listen કરવા થાય છે. એટલે આખો pattern છે: સાચા pathનું reference મેળવો, પછી તેના પર action કરો. child() અથવા slash-separated paths વડે tree navigate કરીને ચોક્કસ data target કરી શકાય છે. Reference સમજાઈ જાય તો reading અને writing સરળતાથી follow થાય છે.
Quiz
Firebase Realtime Databaseમાં DatabaseReference શું છે?
- Databaseમાં stored actual data
- JSON treeમાં ચોક્કસ location (path) તરફ pointer, જેનાથી તે location પરથી read અથવા ત્યાં write કરી શકાય
- SQL table definition
- Userનો login token
Show the answer
JSON treeમાં ચોક્કસ location (path) તરફ pointer, જેનાથી તે location પરથી read અથવા ત્યાં write કરી શકાય
DatabaseReference Realtime Databaseના JSON treeમાં ચોક્કસ path તરફનો pointer છે. તેનો ઉપયોગ તે location પરથી data read કરવા અથવા ત્યાં data write કરવા થાય છે. ઉદાહરણ તરીકે, getReference('events').child('event123') events/event123 તરફ point કરે છે. Option A ખોટું છે: reference data પોતે નથી, data જ્યાં રહે છે ત્યાંનું address છે. Option C ખોટું છે: Realtime Database NoSQL JSON tree છે, SQL tables નહીં. Option D પણ ખોટું છે: login token Authenticationમાંથી આવે છે, DatabaseReferenceમાંથી નહીં. DatabaseReferenceને treeમાંના address તરીકે યાદ રાખો.
Think first
અહીં SQL tables કરતાં NoSQL JSON tree mobile app માટે યોગ્ય કેમ હોઈ શકે?
Relational databases શક્તિશાળી છે. તો real-time mobile app માટે Firebase flexible NoSQL tree શા માટે વાપરે છે? પછી tap.
Show the answer
કારણ કે mobile appનું data ઘણીવાર HIERARCHICAL અને FLEXIBLE હોય છે અને devices વચ્ચે REAL TIMEમાં SYNC થવું જરૂરી હોય છે. JSON tree rigid SQL tables કરતાં આ જરૂરિયાતોને ઓછા setup સાથે fit કરે છે.
પ્રથમ, structure: app data naturally nested હોય છે. Eventની registrations હોઈ શકે છે અને userની preferences હોઈ શકે છે. JSON tree આ nestingને tables અને joins પહેલેથી design કર્યા વગર સીધું represent કરે છે. બીજું, flexibility: NoSQL fixed schema force કરતું નથી, તેથી migration કર્યા વગર કેટલાક recordsમાં નવી field add કરી શકાય છે. Fast-changing app માટે આ ઉપયોગી છે.
ત્રીજું, અને આ database માટે સૌથી મહત્વનું, real-time sync: clients connected treeના કોઈ path પર subscribe કરી શકે છે અને Realtime Database changes connected devicesને તરત push કરી શકે છે. એટલે નવી registration બીજી device પર તરત દેખાઈ શકે છે. Offline use અને reconnect પછી sync પણ શક્ય છે અને ઘણા simultaneous clients માટે scale કરી શકે છે.
Trade-offs છે: NoSQL treesમાં SQL કરતાં querying નબળી હોઈ શકે છે અને enforced relationships નથી. Complex, highly relational data અથવા rich queries જોઈએ ત્યારે Firestore અથવા SQL database યોગ્ય હોઈ શકે છે. પરંતુ hierarchical data, instant syncing અને ઓછું setup જોઈએ તેવી mobile app માટે flexible JSON-tree model natural fit છે.
Summary
Key takeaways
- Firebase Realtime Database એક NoSQL store છે, જે બધું keys અને nested valuesવાળા એક મોટા JSON treeમાં રાખે છે.
- NoSQLનો અર્થ rigid tables અથવા fixed schema વગરનું data model; તે flexible અને hierarchical data માટે યોગ્ય છે.
- Data access કરવા DatabaseReference વાપરો, જે treeના ચોક્કસ path તરફનો pointer છે.
FirebaseDatabase.getInstance().referenceroot છે;getReference('events')અનેchild('event123')treeમાં વધુ અંદર point કરે છે.- DatabaseReference actual data નથી; તે treeમાંના address જેવું છે, જેના દ્વારા read અને write થાય છે.
- Data connected apps વચ્ચે real timeમાં sync થાય છે, તેથી listeners changes તરત જોઈ શકે છે.
- Memory hook: Realtime Database એક JSON tree છે; DatabaseReference તેમાંના path તરફ point કરે છે.