snippets/dev/repl.clj
2025-06-04 10:47:56 +02:00

86 lines
3.6 KiB
Clojure

(ns repl
(:require [snippets :as main]
[com.biffweb :as biff :refer [q]]
[clojure.edn :as edn]
[clojure.java.io :as io]))
;; REPL-driven development
;; ----------------------------------------------------------------------------------------
;; If you're new to REPL-driven development, Biff makes it easy to get started: whenever
;; you save a file, your changes will be evaluated. Biff is structured so that in most
;; cases, that's all you'll need to do for your changes to take effect. (See main/refresh
;; below for more details.)
;;
;; The `clj -M:dev dev` command also starts an nREPL server on port 7888, so if you're
;; already familiar with REPL-driven development, you can connect to that with your editor.
;;
;; If you're used to jacking in with your editor first and then starting your app via the
;; REPL, you will need to instead connect your editor to the nREPL server that `clj -M:dev
;; dev` starts. e.g. if you use emacs, instead of running `cider-jack-in`, you would run
;; `cider-connect`. See "Connecting to a Running nREPL Server:"
;; https://docs.cider.mx/cider/basics/up_and_running.html#connect-to-a-running-nrepl-server
;; ----------------------------------------------------------------------------------------
;; This function should only be used from the REPL. Regular application code
;; should receive the system map from the parent Biff component. For example,
;; the use-jetty component merges the system map into incoming Ring requests.
(defn get-context []
(biff/merge-context @main/system))
(defn add-fixtures []
(biff/submit-tx (get-context)
(-> (io/resource "fixtures.edn")
slurp
edn/read-string)))
(defn check-config []
(let [prod-config (biff/use-aero-config {:biff.config/profile "prod"})
dev-config (biff/use-aero-config {:biff.config/profile "dev"})
;; Add keys for any other secrets you've added to resources/config.edn
secret-keys [:biff.middleware/cookie-secret
:biff/jwt-secret
:mailersend/api-key
:recaptcha/secret-key
; ...
]
get-secrets (fn [{:keys [biff/secret] :as config}]
(into {}
(map (fn [k]
[k (secret k)]))
secret-keys))]
{:prod-config prod-config
:dev-config dev-config
:prod-secrets (get-secrets prod-config)
:dev-secrets (get-secrets dev-config)}))
(comment
;; Call this function if you make a change to main/initial-system,
;; main/components, :tasks, :queues, config.env, or deps.edn.
(main/refresh)
;; Call this in dev if you'd like to add some seed data to your database. If
;; you edit the seed data (in resources/fixtures.edn), you can reset the
;; database by running `rm -r storage/xtdb` (DON'T run that in prod),
;; restarting your app, and calling add-fixtures again.
(add-fixtures)
;; Query the database
(let [{:keys [biff/db] :as ctx} (get-context)]
(q db
'{:find (pull user [*])
:where [[user :user/email]]}))
;; Update an existing user's email address
(let [{:keys [biff/db] :as ctx} (get-context)
user-id (biff/lookup-id db :user/email "hello@example.com")]
(biff/submit-tx ctx
[{:db/doc-type :user
:xt/id user-id
:db/op :update
:user/email "new.address@example.com"}]))
(sort (keys (get-context)))
;; Check the terminal for output.
(biff/submit-job (get-context) :echo {:foo "bar"})
(deref (biff/submit-job-for-result (get-context) :echo {:foo "bar"})))