pcvonz-bs-firebase
v0.3.0
Published
🚧 WIP
Downloads
5
Maintainers
Readme
bs-firebase
🚧 WIP
Initialize app
[@bs.module]
external firebaseConfig: firebaseConfig = "./config/firebase-config.json";
firebase->initializeApp(firebaseConfig);
Authentication
open BsFirebase;
firebase
->auth()
->Auth.onAuthStateChanged(user => {
switch (user->Js.Nullable.toOption) {
| None => ()
| Some(u) =>
Js.log3(
u->Auth.User.uid,
u->Auth.User.email,
u->Auth.User.displayName,
)
}
});
let signInWithGoogle = () => firebase->auth()->Auth.signInWithPopup(Auth.Provider.google());
let signInWithEmailAndPassword = (~email, ~password) => firebase->auth()->Auth.signInWithEmailAndPassword(~email, ~password);
let signOut = () => firebase->auth()->Auth.signOut();
Firestore
Fetch all
let fetchAll = () => {
firebase
->firestore()
->Firestore.collection("mycollection")
->Firestore.Collection.get()
|> Js.Promise.then_(querySnapshot =>
querySnapshot
->Firestore.QuerySnapshot.docs
->Belt.Array.map(snapshot => {
let data = snapshot->Firestore.DocRef.data();
Js.log(snapshot->Firestore.DocRef.id);
Js.log(data##someAttribute);
data;
})
|> Js.Promise.resolve
);
};
Fetch by id
let fetchItem = (id) => {
firebase
->firestore()
->Firestore.collection("mycollection")
->Firestore.Collection.doc(id)
->Firestore.Collection.Doc.get()
|> Js.Promise.then_(doc => {
let data = doc->Firestore.DocRef.data();
Js.log(data);
data;
});
}
Create
let create = (title, description) =>
firebase
->firestore()
->Firestore.collection("collection")
->Firestore.Collection.add({
"title": title,
"description": description,
});
Update
let update = (id, title) =>
firebase
->firestore()
->Firestore.collection("collection")
->Firestore.Collection.doc(id)
->Firestore.Collection.Doc.set({
"title": title
});
Remove
let remove = (id: string) =>
firebase
->firestore()
->Firestore.collection("mycollection")
->Firestore.Collection.doc(id)
->Firestore.Collection.Doc.delete();