Fork me on GitHub
#portal
<
2022-04-24
>
djblue03:04:47

Just wrote up a guide around https://github.com/djblue/portal/blob/master/doc/guides/promises.md and JavaScript promises>, hope it can be useful to some :thumbsup:

🙏 1
djblue03:04:21

This gist of it is, you can specialize tap> for promises:

(require '[portal.api :as p])

(defn async-submit [value]
  (if-not (instance? js/Promise value)
    (p/submit value)
    (.then value p/submit)))

(add-tap #'async-submit)

(tap> (async-fn)) ;; :hello

djblue20:04:12

Also, if you aren't a fan of the default tap list behavior in portal, https://github.com/djblue/portal/blob/master/doc/guides/custom-taps.md 👌

nate21:04:50

Awesome! Thanks for documenting this!

💯 1
Carlo21:04:11

This sounds amazing! If I'm not mistaken, this can be used just for changing the default portal view ❤️

djblue21:04:18

Yup, or having multiple portal instances for different purposes 👌

💯 1
djblue21:04:51

At work I have two instances, one for eval/taps and another for clj/cljs logs

1
djblue22:04:12

Ohh, also, a long time ago Portal use to automatically datafy all submitted values but I felt like that shouldn't be the default. If you want that behavior, try:

(require '[clojure.datafy :as d])
(require '[portal.api :as p])

(def submit (comp p/submit d/datafy))
(add-tap #'submit)

(tap> *ns*)

1
😍 1
Carlo02:04:59

what is this useful for? Can I navigate cljs objects more easily?

djblue02:04:51

This is useful for folks who are dealing with Java/JavaScript objects and have extended the clojure.core.protocols/Datafiable protocol to those objects. Or perhaps, the objects are coming from a library that has done the work.

🙌 1
seancorfield03:04:56

next.jdbc is a good example on the backend so you can navigate thru the database.

🙌 1
Carlo21:04:51

@U04V70XH6 I'd like to try the next.jdbc example specifically. Is there some blogpost/guide on this?

seancorfield21:04:43

next.jdbc automatically makes result sets datafiable and navigable, with an assumption that FKs are named <table>id or <table>_id, so if you have a result set from next.jdbc displayed in Portal, you can datafy a row and then nav to other tables from any FK columns. I don't know if I have an up-to-date video example of that online -- and I haven't tried it since Portal 24 came out (which removes the auto-datafication of values).

🙌 1
seancorfield21:04:18

I blogged about the machinery when it was first introduced, https://corfield.org/blog/2018/12/03/datafy-nav/ (three and a half years ago -- yikes, time flies!).

seancorfield21:04:52

For example, suppose I execute this code:

(jdbc/execute! (-> a :database :pooled-db :datasource)
                 ["select * from membership m join membershipPackage p on m.membershippackageid = p.id"])
and that produces rows that have a user_id column. When I highlight the value of user_id in one of those rows in Portal and press enter (or ctrl-j nav), that will call nav with the row, the key (`user_id`), and the value, e.g., 9 and that in turn will cause next.jdbc to do select * from user where id = 9 and then Portal displays that and you can continue to navigate into column values across the database.

🙌 1
seancorfield22:04:09

In addition, if you require next.jdbc.datafy then a lot of raw JDBC data types become navigable -- you can call .getMetadata on a Connection and datafy (`ctrl-j datafy`) the Java object to get a "bean" view of the metadata, which contains some additional keys like :all-tables which you can nav into and next.jdbc will fetch all the table data from the schema and display that in Portal.

🙌 1
Carlo23:04:20

Amazing, thank you very much for taking the time to write this out @U04V70XH6! I'm going to try this out in the next few days 🥳