Liking cljdoc? Tell your friends :D

fbielejec/cljs-firebase-client

Build Status

ClojureScript library for calling the Firebase API

Installation

Latest released version of this library:
Clojars Project

API Overview

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.core

Core namespace contains only one function which initializes the Firebase client.

init

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"})

firebase.auth

Functions to authenticate users to your app. See doc for more references.

email-sign-in

email-sign-in [{:keys [:email :password]}]

email-create-user

Example:

(-> (auth/email-create-user {:email "me@here.com"
                             :password "pass"})
    (.then #(-> % .-user .-uid)))

on-auth-change

on-auth-change [{:keys [:on-change]}]

current-user

Returns a currently logged in user.

update-user-profile

Takes a user and a map of field keys.

logout

Signs out a currently logged in user.

email-login

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.

firebase.admin

Admin SDK lets you interact with Firebase from privileged environments.


NOTE

This namespace will only work in a NodeJS runtime environment.


credential

Create google application credentials from a service account file.

init

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)})

server-timestamp

See documentation for more details.

list-users

Returns a JS/Promise with a list of all users:

(list-users)

delete-user

Deletes 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.firestore

This namespace contains functions for interacting with the Cloud Firestore.

coll-ref

Obtain a reference to a collection:

(coll-ref "following")

doc-ref

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")

document-set

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})))

document-add

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}))))

document->clj

Takes a document and returns a clojure representation.

snapshot->clj

Takes a query snapshot and returns a clojure representation.

query

Executes a query (get). Takes a document or collection ref as an argument, returns a JS/Promise resolving to a snapshot with the results.

get-collection

Executes a query on a collection, return a JS/Promise with the collection content.

get-document

Executes a query on a document, return a JS/Promise with the document content.

get-document-field-value

Takes a document and a field name, returns the value.

(get-document-field-value (doc-ref "followers/ml8mnGIEINP1eol2PSWSfnlen0Q2") "id")

delete-document

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")

where

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

order-by [coll-ref field & direction]

start-at

start-at [coll-ref index]

start-after

start-after [coll-ref index]

limit

Can 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.firebase

This namespace contains re-frame event wrappers around other api functions.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close