Skip to content

Lightweight wrapper for the backend InstantDB API (WIP)

Notifications You must be signed in to change notification settings

Mertzenich/taneous

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

taneous

Taneous is a lightweight wrapper for the backend InstantDB API using clj-http. This is a work-in-progress, there is little documentation, no tests, no error-handling, and no built-in async support at this time.

Usage

Initialize an application config. This is just a clj-http request map.

(def app
  (instant/init "00000000-0000-0000-0000-000000000000" ; Admin Token
                "00000000-0000-0000-0000-000000000000" ; App ID
                ;; Optional: specify an api url
                "https://api.instantdb.com"))

Transact

Transactions are represented using a map.

  • :tx => transaction type (i.e. update, merge, etc)
  • :ns => namespace you are working with
  • :id => identifer
  • :q => instaml query as a clojure map
(admin/transact
 app
 {:tx :update
  :ns :books
  :id (str (random-uuid))
  :q {:title "The Innocence of Father Brown"
      :author "G. K. Chesterton"
      :pages 256}})
;; => {:tx-id ...}

You can use multiple transactions at once.

(admin/transact
 app
 {:tx :update
  :ns :books
  :id (str (random-uuid))
  :q {:title "The Time Machine"
      :author "H. G. Wells"
      :pages 113}}
 {:tx :update
  :ns :books
  :id (str (random-uuid))
  :q {:title "Death Comes for the Archbishop"
      :author "Willa Cather"
      :pages 297}})
;; => {:tx-id ...}

An example of linking data:

(let [workout-id (str (random-uuid))
      health-id (str (random-uuid))]
  (admin/transact
   app
   {:tx :update :ns :todos :id workout-id :q {:title "Go on a run"}}
   {:tx :update :ns :goals :id health-id  :q {:title "Get fit"}}
   {:tx :link   :ns :goals :id health-id  :q {:todos workout-id}}))
;; => {:tx-id ...}

Query

Querying data is simple:

(admin/query app {:goals {}})
;; => {:goals [{:id "00000000-0000-0000-0000-000000000000"
;;              :title "Get fit"}]}

Example of fetching a specific entity:

(admin/query
 app
 {:goals {:$ {:where {:id "00000000-0000-0000-0000-000000000000"}}}})
;; => {:goals [{:id "00000000-0000-0000-0000-000000000000"
;;              :title "Get fit"}]}

Example of fetching associations:

(admin/query
 app
 {:goals {:todos {}}})
;; =>  {:goals [{:id "00000000-0000-0000-0000-000000000000",
;;               :title "Get fit",
;;               :todos [{:title "Go on a run",
;;                        :id "00000000-0000-0000-0000-000000000000"}]}]}

There is support for impersonation (as-guest, as-email, and as-token) These functions take your app structure and add the appropriate headers.

(let [guest (admin/as-guest app)]
  ;; Use `guest` for performing queries/transactions
  (admin/query guest {:goals {}}))

(let [user (admin/as-email app "john@example.com")]
  (admin/query user {:goals {}}))

Auth

See src/taneous/auth.clj for functions pertaining to authentication.

Credits

This project was made possible by:

About

Lightweight wrapper for the backend InstantDB API (WIP)

Resources

Stars

Watchers

Forks