Skip to content

fbielejec/cljs-firebase-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cljs-firebase-client

Build Status

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.

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

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. Example:

(.-uid (current-user)

update-user-profile

Takes a user and a map of field keys:

update-user-profile [user {:keys [:photo-url :display-name]}]

logout

Signs out a currently logged in user. Example:

(logout)

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:

email-login [{:keys [:email :password :on-success :on-error]}]

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:

credential [cert]

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.

(server-timestamp)

list-users

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

(list-users)

delete-user

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

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.

document->clj [doc]

snapshot->clj

Takes a query snapshot and returns a clojure representation.

snapshot->clj [snapshot]

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.

query [ref]

get-collection

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

get-collection [collection]

get-document

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

get-document [collection id]

get-document-field-value

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

(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

limit [coll-ref n]

events.firebase

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

About

Clojurescript library for interacting with Firebase

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published