ClojureScript wrapper library for interacting with Firebase
This library uses shadow-cljs for JavaScript integration. For this reason you should probably use it only if your project is also using shadow-cljs / deps.edn to resolve JS dependencies.
For rationale and some possible uses see the accompanying blog post.
Latest released version of this library:
Stick with the API of the javascript client docs, all of the documented methods have their version in the JS library. There are helper methods to kebab-case and keywordize the return values and responses. Most of the functions will return a JS/Promise. The events namespace provides re-frame events which wrap some of the API (work-in-progress).
Core namespace contains only one function which initializes the Firebase client.
Initialize Firebase client. Takes a map of config values as an argument. Example:
(firebase/init {:apiKey "key"
:authDomain "my-1b84b.firebaseapp.com"
:databaseURL "https://my-1b84b.firebaseio.com"
:projectId "my-1b84b"
:storageBucket "my-1b84b.appspot.com"
:messagingSenderId "386365028401"})
Functions to authenticate users to your app. See doc for more references.
email-sign-in [{:keys [:email :password]}]
Example:
(-> (auth/email-create-user {:email "me@here.com"
:password "pass"})
(.then #(-> % .-user .-uid)))
on-auth-change [{:keys [:on-change]}]
Returns a currently logged in user. Example:
(.-uid (current-user)
Takes a user and a map of field keys:
update-user-profile [user {:keys [:photo-url :display-name]}]
Signs out a currently logged in user. Example:
(logout)
Same as email-sign-in extra :on-success
:on-error
keys to the options map with callbacks executed after a sign-in success or failure:
email-login [{:keys [:email :password :on-success :on-error]}]
Admin SDK lets you interact with Firebase from privileged environments.
NOTE
This namespace will only work in a NodeJS runtime environment.
Create google application credentials from a service account file:
credential [cert]
Initialize Firebase Admin SDK. Example:
(ns my-app
(:require [app.firebase.admin :as admin]))
(defonce service-account (js/require "./assets/my-1b84b-firebase-adminsdk-svaar-a0a51894fa.json"))
(admin/init {:databaseURL "https://my-1b84b.firebaseio.com"
:credential (admin/credential service-account)})
See documentation for more details.
(server-timestamp)
Returns a JS/Promise with a list of all users:
(list-users)
Deletes a user, takes the user id as an argument. Example (deletes all users):
(-> (list-users)
(.then #(js/Promise.all (->> (js->clj % :keywordize-keys true)
:users
(map (fn [user]
(let [uid (.-uid user)]
(delete-user uid)
uid)))))))
This namespace contains functions for interacting with the Cloud Firestore.
Obtain a reference to a collection:
(coll-ref "following")
Obtain a reference to a document, takes a path or collection and document id as arguments:
(doc-ref "followers" "ml8mnGIEINP1eol2PSWSfnlen0Q2")
;; same as
(doc-ref "followers/ml8mnGIEINP1eol2PSWSfnlen0Q2")
Sets (creates) a document with a given id under a cpecified collection.
Creates or overwrite a single document inside a collection, returns a JS/Promise. If the documetn does not exist it will be created, unless the merge option is specified. Example:
(-> (document-set {:collection "followers"
:id "VFdERNhp94PIJijeuxjJYXTLEZm2"
:document {"ml8mnGIEINP1eol2PSWSfnlen0Q2" true}}
{:merge true})
(.then #(document-set {:collection "following"
:id "ml8mnGIEINP1eol2PSWSfnlen0Q2"
:document {"VFdERNhp94PIJijeuxjJYXTLEZm2" true}}
{:merge true})))
Same as document-set but without specifying an id, instead lets Cloud Firestore auto-generate an ID. Returns a JS/Promise which resolves to a generated doc-ref. Example:
(let [sender-id "ml8mnGIEINP1eol2PSWSfnlen0Q2"
receiver-id "VFdERNhp94PIJijeuxjJYXTLEZm2"
message "Hi Bob!"
doc {:message message
:sender-id sender-id
:receiver-id receiver-id
:created-at (.getTime (js/Date.))}]
(-> (document-add {:path (string/join "/" ["messages" receiver-id sender-id])
:document doc})
(.then #(document-add {:path (string/join "/" ["messages" sender-id receiver-id])
:document doc}))))
Takes a document and returns a clojure representation.
document->clj [doc]
Takes a query snapshot and returns a clojure representation.
snapshot->clj [snapshot]
Executes a query (get). Takes a document or collection ref as an argument, returns a JS/Promise resolving to a snapshot with the results.
query [ref]
Executes a query on a collection, return a JS/Promise with the collection content.
get-collection [collection]
Executes a query on a document, return a JS/Promise with the document content.
get-document [collection id]
Takes a document and a field name, returns the value. Example:
(get-document-field-value (doc-ref "followers/ml8mnGIEINP1eol2PSWSfnlen0Q2") "id")
Deletes document from a collection. Takes a path
or collection and document id as arguments.
(delete-document "followers" "ml8mnGIEINP1eol2PSWSfnlen0Q2")
;; same as
(delete-document "followers/ml8mnGIEINP1eol2PSWSfnlen0Q2")
Takes a collection ref, operator, key and a value, returns the same ref, but now you need can call a query to execute the search.
This query will return a snapshot of documents in the "following"
collection where the key "ml8mnGIEINP1eol2PSWSfnlen0Q2"
has the value true
:
(-> (coll-ref "following")
(where ">=" "ml8mnGIEINP1eol2PSWSfnlen0Q2" true)
query)
order-by [coll-ref field & direction]
start-at [coll-ref index]
start-after [coll-ref index]
limit [coll-ref n]
This namespace contains re-frame event wrappers around other api functions.