firebase_rs_sdk/database/mod.rs
1//! # Firebase Realtime Database module
2//!
3//! This module ports core pieces of the Realtime Database from the Firebase JS SDK to Rust.
4//!
5//! It wires the Database component into the `FirebaseApp`, provides an in-memory backend for quick tests, and can fall back to the REST API for basic reads and writes against an emulator or hosted backend.
6//!
7//! Live streaming connections and the richer reference/query surface from the JS SDK are still pending.
8//!
9//! It includes error handling, configuration options, and integration with Firebase apps.
10//!
11//! ## Features
12//!
13//! - Component registration and shared get_database resolution
14//! - Reference CRUD with auto-ID push and path navigation (parent/root)
15//! - Priority-aware writes plus server value helpers (server_timestamp increment)
16//! - Snapshot traversal (child, has_child, size, to_json) and value/child listeners
17//! - Dual backends (in-memory + REST) with unit test coverage
18//!
19//!
20//! ## References to the Firebase JS SDK - firestore module
21//!
22//! - QuickStart: <https://firebase.google.com/docs/database/web/start>
23//! - API: <https://firebase.google.com/docs/reference/js/database.md#firestore_package>
24//! - Github Repo - Module: <https://github.com/firebase/firebase-js-sdk/tree/main/packages/database>
25//! - Github Repo - API: <https://github.com/firebase/firebase-js-sdk/tree/main/packages/firebase/database>
26//!
27//! ## Development status as of 14th October 2025
28//!
29//! - Core functionalities: Mostly implemented (see the module's [README.md](https://github.com/dgasparri/firebase-rs-sdk/tree/main/src/firestore) for details)
30//! - Testing: 30 tests (passed)
31//! - Documentation: Most public functions are documented
32//! - Examples: 2 examples
33//!
34//! DISCLAIMER: This is not an official Firebase product, nor it is guaranteed that it has no bugs or that it will work as intended.
35//!
36//!
37//! ## Quick Start Example
38//!
39//! ```no_run
40//! use firebase_rs_sdk::app::*;
41//! use firebase_rs_sdk::database::{*, query as compose_query};
42//!
43//! use serde_json::json;
44//!
45//! fn main() -> Result<(), Box<dyn std::error::Error>> {
46//! // Point to the Realtime Database emulator or a database URL.
47//! let options = FirebaseOptions {
48//! project_id: Some("demo-project".into()),
49//! database_url: Some("http://127.0.0.1:9000/?ns=demo".into()),
50//! ..Default::default()
51//! };
52//! let app = initialize_app(options, Some(FirebaseAppSettings::default()))?;
53//! let database = get_database(Some(app))?;
54//!
55//! let messages = database.reference("/messages")?;
56//! messages.set(json!({ "greeting": "hello" }))?;
57//! let value = messages.get()?;
58//! assert_eq!(value, json!({ "greeting": "hello" }));
59//!
60//! let recent = compose_query(
61//! messages,
62//! vec![order_by_child("timestamp"), limit_to_last(10)],
63//! )?;
64//! let latest = recent.get()?;
65//! println!("latest snapshot: {latest}");
66//!
67//! Ok(())
68//! }
69//! ```
70
71mod api;
72mod backend;
73mod constants;
74pub mod error;
75mod on_disconnect;
76mod push_id;
77mod query;
78mod realtime;
79mod server_value;
80
81#[doc(inline)]
82pub use api::{
83 end_at, end_at_with_key, end_before, end_before_with_key, equal_to, equal_to_with_key,
84 get_database, limit_to_first, limit_to_last, on_child_added, on_child_changed,
85 on_child_removed, order_by_child, order_by_key, order_by_priority, order_by_value, push,
86 push_with_value, query, register_database_component, run_transaction, set_priority,
87 set_with_priority, start_after, start_after_with_key, start_at, start_at_with_key,
88 DataSnapshot, Database, DatabaseQuery, DatabaseReference, ListenerRegistration,
89 QueryConstraint,
90};
91
92#[doc(inline)]
93pub use error::DatabaseResult;
94
95#[doc(inline)]
96pub use on_disconnect::OnDisconnect;
97
98#[doc(inline)]
99pub use server_value::{increment, server_timestamp};