Fork me on GitHub
#reveal
<
2022-09-21
>
bmd02:09:00

I’m trying to play with the db explorer and have tried various maps, but none of them give the db explorer menu item. From the next.jdbc docs, I’m guessing only :java-url, :user, and :password are needed. Do I need to add some metadata to it or something?

{:dbtype "postgres"
 :jdbcUrl "jdbc:"
 :host ""
 :port 5432
 :user "myuser"
 :password "mypw"}

vlaaad05:09:35

Do you have next.jdbc on the classpath?

vlaaad05:09:59

Or an old clojure jdbc?

bmd12:09:19

It’s next.jdbc

vlaaad12:09:36

Hmm, can you try this code in the REPL and report the results?

(require '[clojure.string :as str] 'next.jdbc)
(import '[clojure.lang Associative])
(let [get-ds (resolve 'next.jdbc.protocols/get-datasource)
      get-conn (resolve 'next.jdbc.protocols/get-connection)
      sourceable (resolve 'next.jdbc.protocols/Sourceable)
      x {:dbtype "postgres"
         :jdbcUrl "jdbc:"
         :host ""
         :port 5432
         :user "myuser"
         :password "mypw"}]
  (if (or (and (string? x) (str/starts-with? x "jdbc:"))
          (and (instance? Associative x)
               (or (contains? x :jdbcUrl)
                   (and (contains? x :dbtype) (contains? x :dbname))))
          (contains? (meta x) 'next.jdbc.protocols/get-datasource)
          (->> sourceable
               deref
               :impls
               keys
               (remove #{Associative String})
               (some #(instance? % x))))
    (get-conn (get-ds x) {})
    :doesn't-work))
does it return something? does it throw?

bmd12:09:03

When evaling that and not tapping it to reveal, I get {:fx/type #function[vlaaad.reveal.view/observable-view], :ref #atom[nil 0x739d0f14], :fn #function[permiso.source.sphere.db.bmd-db/eval64902/fn--64905]} If I cider-tap-last-sexp, it causes a stackoverflowerror. If it helps, we pass a map w/ these keys to #’hikari-cp.core/make-datasource

(:adapter
 :metric-registry
 :application-name
 :password
 :username
 :max-lifetime
 :minimum-idle
 :connection-timeout
 :leak-detection-threshold
 :url
 :read-only
 :maximum-pool-size
 :idle-timeout)

vlaaad13:09:11

hmm, not sure what’s your REPL setup, but it’s impossible to return a map there, it should either throw, return :doesn’t-work or return connection?

vlaaad13:09:41

can you share stackoverflow error stack trace?

bmd13:09:31

doh, i’m sorry. I copy/pasted and then had to do other stuff, and then i eval’d the wrong code when i came back. When I eval the code (w/o tapping it to reveal), i get FATAL: password authentication failed for user "spheredb"

vlaaad13:09:56

This is the reason reveal does not suggest db explorer, you need valid credentials

vlaaad13:09:28

I assume you replaced x with other map that has “spheredb” user?

bmd13:09:14

i had replaced the x map w/ the proper values when i got that error. But now when i replaced it w/ what we pass to hikari, i get :doesn’t-work

vlaaad13:09:11

can you share what you pass to hikari (with redacted sensitive details like password/user/url)?

bmd13:09:47

{:adapter "postgresql"
 :application-name "myapp"
 :password "mypw"
 :username "myuser"
 :max-lifetime 60000
 :minimum-idle 4
 :connection-timeout 30000
 :leak-detection-threshold 10000
 :url "jdbc:"
 :read-only true
 :maximum-pool-size 10
 :idle-timeout 60000}

vlaaad13:09:40

Is this format supported by next.jdbc? When I implemented next.jdbc support, I mimicked what next.jdbc expects from maps to be treated as data source description, namely, it needed to have either :jdbcUrl key, or both :dbtype and :dbname keys

vlaaad13:09:23

btw, if you create hikari datasource, as in javax.sql.DataSource instance, you can use db explorer on that

bmd13:09:33

when i tap the hikari object, it doesn’t have a db-explorer option, but it’s type is com.zaxxer.hikari.HikariDataSource

bmd13:09:16

and not sure if next.jdbc consumes the map or not, as i’m new on this project

bmd13:09:41

when i call (next.jdbc/get-connection on the datasource (type com.zaxxer.hikari.HikariDataSource) i get a com.zaxxer.hikari.pool.HikariProxyConnection object, which i use in a next.jdbc/execute! call

vlaaad15:09:55

com.zaxxer.hikari.HikariDataSource implements javax.sql.DataSource… If you open a reveal context menu on datasource instance, will it show db explorer action?

bmd16:09:20

doesn’t seem to. I tried the menu from each of the 4 components of the datasource. I’ll work on getting a straight next.jdbc object into reveal tonight and report how it goes (it’ll be middle of the night for you). Thanks for your time and help!

vlaaad16:09:53

Great! Just a note — if action isn't available, it probably means that an attempt to evaluate it threw an exception, and reveal drops all action evaluation exceptions as "unavailable actions" — https://github.com/vlaaad/reveal/blob/master/src/vlaaad/reveal/action.clj#L53

vlaaad16:09:59

You can disable this and see if there are exceptions

vlaaad16:09:27

To disable, re-evaluate it to something else in the REPL, e.g.:

(in-ns 'vlaaad.reveal.action)
(defn collect [annotated-value]
  (let [{:keys [value annotation]} annotated-value
        actions (->> @*registry
                     (keep (fn [[id check]]
                             (try
                               (when-let [f (check value annotation)]
                                 (let [label (name id)]
                                   {:id id
                                    :label label
                                    :form (stream/horizontal
                                            (stream/raw-string "(" {:fill :util})
                                            (stream/raw-string label {:fill :symbol})
                                            stream/separator
                                            (stream/stream value annotation)
                                            (stream/raw-string ")" {:fill :util}))
                                    :invoke f}))
                               (catch Exception e (.printStackTrace e))))))                  ;; <- I only changed this line
        freqs (->> actions (map :label) frequencies)]
    (->> actions
         (sort-by (juxt :label :id))
         (map #(cond-> % (< 1 (freqs (:label %))) (assoc :label (str (symbol (:id %))))))
         (into []))))

bmd18:09:42

Got it to pretty much the smallest possible code:

(def next-source-map
  (let [d    (datasource-map)]
    {:dbtype    "postgres"
     :dbname    dbname
     :host      host
     :jdbc-url  (:url d)
     :user      (:username d)
     :password  (:password d)
     :read-only true}))

(def next-source (next.jdbc/get-datasource next-source-map))

(with-open [connection (jdbc/get-connection next-source)]
  (jdbc/execute! connection [sql-query-str]))
The above code does run the sql-query. I hadn’t snapped that there are two version of reveal, so I switched to the latest reveal-pro on maven 1.3.351 and restarted my repl, and eval’d next-source-map, the datasource (created from get-datasource), and the connection (from get-connection). None of these had a db-explorer menu item. Though it’d be nice to have this working, it isn’t critical. We can drop it for now and I’ll revisit once I have more time to devote to learning reveal better from your reveal/extend github page. Again thanks for your time thanks3

vlaaad19:09:37

Btw it's :jdbcUrl, not :jdbc-url

bmd19:09:45

I’ve been using

(evil-define-key 'normal cider-mode (kbd ", e q") 'cider-tap-last-sexp)
to have a keystroke to easily tap, but it just being listed as a key binding in spacemacs. Is there a better way to work w/ reveal? (I sadly don’t know elisp very well)

vlaaad19:09:47

There is nrepl middleware that you can add to the middleware list that will open renewal window with evaluation results automatically shown

bmd19:09:20

i’ve been wanting that, and have tried, but obviously have been doing something wrong. In ~/.lein/profiles.clj I have this under user:

:dependencies [[com.bhauman/rebel-readline "0.1.4"]
                       ; [vlaaad/reveal "1.3.276"]
                       [dev.vlaaad/reveal-pro "1.3.351"]
                       ]
and this at the top level of the map (parallel to :user)
:repl {:dependencies [[vlaaad/reveal-pro "1.3.351"]]
        :jvm-opts ["-Dvlaaad.reveal.prefs={:theme,:light}"]}
        :global-vars {*print-length* 100}
        :repl-options {:nrepl-middleware [vlaaad.reveal.nrepl/middleware]}
        }
and then for extra measure added this to the root level of my project in .nrepl.edn
{:middleware [vlaaad.reveal.nrepl/middleware]}
I then lein repl from a terminal and jack into it from emacs. I still need to add-tap to a reveal/ui

vlaaad20:09:11

Why do you still need to tap? Is there a window shown? Does it have repl output?

bmd20:09:41

No window appears until i add-tap. There is a background java app that starts when the repl does, iirc (have done enough permutations not 100% positive when it starts, but 99.9% certain it’s before add-tap). But no window pops up when the java app does