firebase_js/database.rs
1use firebase_js_sys::ModuleDatabase;
2use wasm_bindgen::{JsValue, prelude::Closure};
3
4use crate::{app::FirebaseApp, closure, FirebaseError};
5
6// pub struct FirebaseDatabase<'a>(&'a JsValue);
7pub struct Database(JsValue);
8// pub struct FirebaseDbReference<'a>(&'a JsValue);
9pub struct DatabaseReference(JsValue);
10
11#[cfg(test)]
12mod tests {
13 use super::*;
14
15 #[test]
16 fn test_db_ref_from() {
17 let db_ref = Database(JsValue::from_str("test"));
18 let _js_obj: JsValue = db_ref.0;
19 }
20}
21
22/// Takes a [FirebaseApp] instance and a [url] and returns a [FirebaseDatabase] instance.
23/// Fails only if underlying JS function fails.
24///
25/// ## Examples
26/// ```rs
27/// use firebase_js_sys::{app::initialize_app, database::get_database};
28///
29/// let app = initialize_app(config); // config is a FirebaseConfig instance
30/// let db = get_database(&app, "https://my-project.firebaseio.com");
31/// ```
32pub fn get_database<'a>(app: &'a FirebaseApp, url: String) -> Result<Database, FirebaseError> {
33 let database = ModuleDatabase::get_database_from_url(app.get_js_value(), url);
34 Ok(Database(database))
35}
36
37/// Takes a [FirebaseDatabase] instance and a [path] and returns a [FirebaseDbReference] instance.
38/// Fails only if underlying JS function fails.
39///
40/// You can think of the returne [FirebaseDbReference] as a pointer into a specific part of your database,
41/// which you can use in conjunction with other functions to read and write data.
42///
43/// ## Examples
44/// ```rs
45/// use firebase_js_sys::{app::initialize_app, database::{get_database, get_ref}};
46///
47/// let app = initialize_app(config); // config is a FirebaseConfig instance
48/// let db = get_database(&app, "https://my-project.firebaseio.com");
49///
50/// let db_ref = get_ref(&db, "users/1234");
51/// let db_ref2 = get_ref(&db, "users/1234/name");
52/// ```
53pub fn get_ref(db: &Database, path: String) -> Result<DatabaseReference, FirebaseError> {
54 let reference: JsValue = ModuleDatabase::get_ref(&db.0, path);
55 Ok(DatabaseReference(reference))
56}
57
58/// See [get_ref] documentation. Basically gains a reference to the root of your database,
59/// like (but not equivalent I don't think) to calling [get_ref] with a path of `""` or `"/"`.
60///
61/// ## Examples
62/// ```rs
63/// use firebase_js_sys::database::get_ref_of_root;
64///
65/// let db_ref = get_ref_of_root(&db, "/");
66/// ```
67pub fn get_ref_of_root(db: &Database) -> Result<DatabaseReference, FirebaseError> {
68 let reference: JsValue = ModuleDatabase::get_ref_no_path(&db.0);
69 Ok(DatabaseReference(reference))
70}
71
72/// Registers a [callback] to be executed every time some data at the specified [DatabaseReference] changes.
73/// Note: This closure will be called the first time the data becomes available.
74///
75/// ## Examples
76/// ```rs
77/// use firebase_js::database::{get_database, get_ref, on_value_changed};
78///
79/// let db = get_database(&app, "https://my-project.firebaseio.com");
80/// let db_ref = get_ref(&db, "users/1234");
81///
82/// on_value_changed(&db_ref, |data| {
83/// // do something with data
84/// });
85/// ```
86///
87/// ## WIP:
88/// - Unsubscribing does not work yet
89/// - Potential for convenienve func to take [String] instead of &[DatabaseReference]
90pub fn on_value_changed(db_location_reference: &DatabaseReference, callback: &'static dyn Fn(JsValue)) {
91 let closure: closure<JsValue> = Closure::wrap(Box::new(callback));
92
93 // TODO: implement unsubscribe, I've not needed it yet
94 #[allow(unused_variables)]
95 let unsubscribe = ModuleDatabase::on_value(&db_location_reference.0, &closure);
96}