ClojureScript library for calling the Firebase API
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.
firebase.coreCore namespace contains only one function which initializes the Firebase client.
initInitialize 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"})
firebase.authFunctions to authenticate users to your app. See doc for more references.
email-sign-inemail-sign-in [{:keys [:email :password]}]
email-create-userExample:
(-> (auth/email-create-user {:email "me@here.com"
:password "pass"})
(.then #(-> % .-user .-uid)))
on-auth-changeon-auth-change [{:keys [:on-change]}]
current-userReturns a currently logged in user.
update-user-profileTakes a user and a map of field keys.
logoutSigns out a currently logged in user.
email-loginSame as email-sign-in extra :on-success :on-error keys to the options map with callbacks executed after a sign-in success or failure.
firebase.adminAdmin SDK lets you interact with Firebase from privileged environments.
NOTE
This namespace will only work in a NodeJS runtime environment.
credentialCreate google application credentials from a service account file.
initInitialize 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)})
server-timestampSee documentation for more details.
list-usersReturns a JS/Promise with a list of all users:
(list-users)
delete-userDeletes a user, take suser 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)))))))
firebase.firestoreThis namespace contains functions for interacting with the Cloud Firestore.
coll-refObtain a reference to a collection:
(coll-ref "following")
doc-refObtain 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")
document-setSets (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})))
document-addSame 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}))))
document->cljTakes a document and returns a clojure representation.
snapshot->cljTakes a query snapshot and returns a clojure representation.
queryExecutes a query (get). Takes a document or collection ref as an argument, returns a JS/Promise resolving to a snapshot with the results.
get-collectionExecutes a query on a collection, return a JS/Promise with the collection content.
get-documentExecutes a query on a document, return a JS/Promise with the document content.
get-document-field-valueTakes a document and a field name, returns the value.
(get-document-field-value (doc-ref "followers/ml8mnGIEINP1eol2PSWSfnlen0Q2") "id")
delete-documentDeletes document from a collection. Takes a path or collection and document id as arguments.
(delete-document "followers" "ml8mnGIEINP1eol2PSWSfnlen0Q2")
;; same as
(delete-document "followers/ml8mnGIEINP1eol2PSWSfnlen0Q2")
whereTakes 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-byorder-by [coll-ref field & direction]
start-atstart-at [coll-ref index]
start-afterstart-after [coll-ref index]
limitCan be combined with other query statements to paginate a query:
(let [batch-size 3
user-id (.-uid (current-user))
first-batch (-> (coll-ref "following")
(where ">=" user-id true)
(order-by user-id)
(limit batch-size)
query)]
(promise-> first-batch
(fn [snapshot]
(let [docs->clj #(->> (db/snapshot->clj %)
(map keys)
flatten)
docs (.-docs snapshot)
last-doc (aget docs (dec (-> docs .-length)))
next-batch (-> (coll-ref "following")
(where ">=" user-id true)
(order-by user-id)
(start-after (get-document-field-value last-doc user-id))
(limit batch-size)
query)]
(promise-> first-batch
#(prn (docs->clj %))
(promise-> next-batch
#(prn (docs->clj %))))))))
events.firebaseThis namespace contains re-frame event wrappers around other api functions.
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |