Theory
एक अलग तरह का Database
आपने tables, rows, और columns वाले relational databases इस्तेमाल किए हैं। Firebase Realtime Database अलग है: यह एक NoSQL database है, और यह सब कुछ एक बड़े JSON tree की तरह store करता है। Data store और fetch करने के लिए, आप उस tree में एक path पर point करते हैं।
यह unit FestConnect का data Realtime Database में store करता है, इसे app से लिखते हुए, इसे वापस पढ़ते हुए, और पूरा CRUD करते हुए। यह opening lesson explain करता है यहाँ NoSQL का क्या मतलब है और वह एक object introduce करता है जो आप data तक पहुँचने के लिए इस्तेमाल करते हैं: DatabaseReference। Tree समझना आगे आने वाली हर चीज़ straightforward बनाता है।
Theory
NoSQL और JSON Tree
NoSQL databases data को rigid tables और rows के बिना store करते हैं। Columns के एक fixed schema की बजाय, ये flexible, अक्सर hierarchical data hold करते हैं, जो उन apps को suit करता है जिनका data shape change होता है और जिन्हें scale करना होता है।
Realtime Database इसे इसके simplest form में ले जाता है: आपकी पूरी app का data keys और nested values का एक JSON tree है। एक event events/event123 path पर रह सकता है, इसके नीचे keys की तरह name और date के साथ। Define करने के लिए कोई tables नहीं हैं; आप बस paths पर values लिखते हैं। और चूँकि यह Realtime Database है, changes हर connected app को live sync होते हैं, एक listener updates को होते ही देखता है।
Practical
एक DatabaseReference पाना (Tree में एक Pointer)
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 नहीं
Key idea: एक DatabaseReference JSON tree में एक location का pointer है, उस location पर data नहीं। getReference("events") आपको events path का एक reference देता है; child("event123") pointer को गहराई में move करता है।
आप इन references को एक path पर लिखने (अगला lesson) और एक path पर पढ़ने/listen करने (उसके बाद वाला lesson) के लिए इस्तेमाल करते हैं। तो हर चीज़ के लिए pattern है: सही path का एक reference पाइए, फिर इस पर act कीजिए। child() (या slash-separated paths) से tree navigate करना ही exactly वह data target करने का तरीका है जो आप चाहते हैं। Reference master कीजिए, और पढ़ना और लिखना easily follow होते हैं।
Quiz
Firebase Realtime Database में, एक DatabaseReference क्या है?
- Database में stored actual data
- JSON tree में एक specific location (path) का एक pointer, उस location से पढ़ने या इसमें लिखने के लिए इस्तेमाल होता है
- एक SQL table definition
- User का login token
Show the answer
JSON tree में एक specific location (path) का एक pointer, उस location से पढ़ने या इसमें लिखने के लिए इस्तेमाल होता है
एक DatabaseReference Realtime Database के JSON tree में एक specific path का एक pointer है; आप इसे उस location से पढ़ने या इसमें लिखने के लिए इस्तेमाल करते हैं (उदाहरण के लिए, getReference('events').child('event123') events/event123 पर point करता है)। Option A wrong है: reference खुद data नहीं है, यह सिर्फ़ point करता है data कहाँ रहता है; आप reference के through data fetch करते हैं। Option C wrong है: Realtime Database NoSQL है (एक JSON tree), SQL tables नहीं, तो कोई table definitions नहीं हैं। Option D systems को confuse करता है: एक login token Authentication से आता है, database reference से नहीं। एक DatabaseReference को tree में एक address की तरह सोचिए, फिर आप उस address पर पढ़ते या लिखते हैं।
Think first
यहाँ SQL Tables से एक NoSQL JSON Tree एक Mobile App को Better क्यों Suit कर सकता है?
Relational databases powerful हैं। Firebase एक real-time mobile app के लिए एक flexible NoSQL tree क्यों इस्तेमाल करता है? फिर tap कीजिए।
Show the answer
क्योंकि एक mobile app का data अक्सर HIERARCHICAL, FLEXIBLE होता है, और devices के across real time में SYNC होना चाहिए, और एक JSON tree rigid SQL tables से कम ceremony के साथ उन needs को fit करता है। पहले, structure: बहुत सारा app data naturally nested है, एक event में registrations हैं, एक user की preferences हैं, और एक JSON tree उस nesting को directly represent करता है, tables और join relationships upfront design किए बिना। दूसरा, flexibility: NoSQL एक fixed schema force नहीं करता, तो आप एक migration के बिना कुछ records में एक field add कर सकते हैं, जो fast-moving apps को suit करता है जहाँ data shape evolve होता है। तीसरा, और THIS database के लिए crucially, real-time sync: चूँकि data एक connected tree है जिसे clients subscribe करते हैं, Realtime Database हर listening device को changes push कर सकता है instant ये होते हैं, जो exactly वह है जो live, collaborative mobile features चाहते हैं (एक नई registration immediately appear होते देखना)। चौथा, offline और scale: यह offline काम करने और reconnect होने पर sync होने के लिए, और कई simultaneous clients तक scale करने के लिए built है। Trade-offs real हैं, NoSQL trees SQL की तुलना में weaker querying और कोई enforced relationships offer नहीं करते, यही वजह है complex, highly relational data (या rich queries चाहने वाली apps) शायद Firestore या एक SQL database prefer करें। पर एक mobile app के लिए जो ज़्यादातर hierarchical data पढ़ती और लिखती है और minimal setup के साथ instant syncing चाहती है, flexible JSON-tree model एक natural fit है। Store को data से match कीजिए: nested और real-time एक NoSQL tree favour करते हैं।
Summary
Key takeaways
- Firebase Realtime Database एक NoSQL store है जो सारा data keys और nested values के एक बड़े JSON tree की तरह hold करता है।
- NoSQL का मतलब है कोई rigid tables या fixed schema नहीं; यह flexible, hierarchical data को suit करता है और अच्छी तरह scale करता है।
- आप एक DatabaseReference के through data access करते हैं, tree में एक specific path का एक pointer।
- FirebaseDatabase.getInstance().reference root है; getReference('events') और child('event123') गहराई में point करते हैं।
- एक DatabaseReference tree में एक address है, खुद data नहीं; आप इसे पढ़ने और लिखने के लिए इस्तेमाल करते हैं।
- Data हर connected app को real time में sync होता है, तो listeners changes को होते ही देखते हैं।
- Memory hook: Realtime Database एक JSON tree है; एक DatabaseReference इसमें एक path पर point करता है।