mrmcc3 / firebase-wrappers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sample Usage

theronic opened this issue · comments

Thank you for this - it's saved me a lot of time.

Please paste a usage example. I'm stuck on this:

(let [db-name "my-db-name"
      creds   (io/input-stream "creds.json")
      opts    (options "my-db.firebaseapp.com" creds)
      app     (get-app "[DEFAULT]")                                   ;(or (get-app db-name) (init-app opts db-name))
      db      (app->db app) ;; throws
        ]
    db)

java.net.URISyntaxException: Invalid scheme specified: my-db.firebaseapp.com
com.google.firebase.database.DatabaseException: Invalid Firebase Database url specified
       clojure.lang.Compiler$CompilerException: com.google.firebase.database.DatabaseException: Invalid Firebase Database url specified, compiling:

Managed to get it configured by passing https://my-db.../ URL to options, and options subsequently to init-app.

Surprised that Firebase is so stateful. How do I safely retrieve an app once I've made, or otherwise init-app? try...catch?

did this:

(defn get-or-init [db-name opts]
  (try
    (get-app db-name)
    ; should be more specific with IllegalStateException
    (catch Exception ex
      (init-app opts db-name))))

Now I'm stuck at geting a value. Clojure REPL just hangs when I try to deref the promise returned by get:

@(get (child (.getReference db) "some-path" "single-value-item"))) ;; hangs

Hello. You're right it's a bit annoying that firebase libs are stateful around app creation. I've used the try catch approach you mention above.

I'm not seeing the repl-hanging issue that you've mentioned though. Here was the example I just tried

(require '[clojure.java.io :as io])

(def opts
  (with-open [creds (io/input-stream "creds.json")]
    (options "https://my-db.firebaseio.com" creds)))

(def app
  (try
    (init-app opts)
    (catch Exception e (get-app))))

(def db
  (app->db app))

@(get (child (ref db) "a" "b"))
=> #object[com.google.firebase.database.DataSnapshot 0x2b08d91c "DataSnapshot { key = b, value = null }"]

It does take a while for the initial request to return.

FYI these wrappers were essentially a code dump from a project a while back. A fair bit has changed in the firebase ecosystem since then. For instance new versions of the firebase java admin SDK (now open source) have user management api and there are some changes to how the options are built. These wrappers could use a refresh along with docstrings

I've updated the code to use the latest firebase lib, added some user management stuff and put sample usage in the README. hope that helps