-
Notifications
You must be signed in to change notification settings - Fork 0
/
initiate_firebaseadmin_sdk.clj
37 lines (33 loc) · 2.19 KB
/
initiate_firebaseadmin_sdk.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
; if using luminus and compojure,
; initiate function should be in core.clj and its always initiated once
; else it will throw error
(defn initiate []
(let [fileinput "path/to/firebase-adminsdk.json"
googleserv (FileInputStream. fileinput)
options (-> (FirebaseOptions$Builder.)
(.setCredentials (GoogleCredentials/fromStream googleserv))
(.setDatabaseUrl "https://firebaseio.com/") ; note firebase URL
(.build))
_ (FirebaseApp/initializeApp options)]))
; below will pull records for a particular collection e.g test_feeds/
; if firebase path to collection is https://firebaseio.com/test_feeds/
; running (serviceacc) will print collection record
; then you can parse and manipulate the records to suite your taste
(defn serviceacc []
(let [ref (-> (FirebaseDatabase/getInstance)
(.getReference "test_feeds/")
(.addListenerForSingleValueEvent (reify ValueEventListener
(onDataChange [this datasnap]
(println (.getValue datasnap))
;(log/info "value=" (js/read-str (json/generate-string (.getValue datasnap)) :key-fn keyword))
))))]))
; below will insert a hashmap into a firebase collection
; assuming test_feeds has three records i.e content, image_url,uuid
(defn addingvalues [content image_url]
(let [ref (-> (FirebaseDatabase/getInstance)
(.getReference "test_feeds/")) ; still using test_feeds/ collections
feedref (.child ref "feedinfo") ; child collection under test_feeds/ i.e https://firebaseio.com/test_feeds/feedinfo
key (.getKey (.push feedref)) ; create a unique key automatically and push as child of collection
values (assoc {} "content" content "image_url" image_url "uuid" key)]
;(log/infof "processing values into firebase:%s|%s" values key)
(.setValueAsync (.child feedref key) values))) ; add values as children under the unique key