add more db2 fns

new fns:
- get-snippets-by-tag
- list-snippets
- delete-snippet-by-slug
This commit is contained in:
Travis Shears 2026-03-10 13:48:32 +01:00
parent c34908ac8f
commit 7d497191cb
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469

View file

@ -95,10 +95,9 @@
(let [conn (get-conn) (let [conn (get-conn)
db (d/db conn) db (d/db conn)
query '[:find (pull ?e [*]) query '[:find (pull ?e [*])
:where :in $ ?slug
[?e :snippet/slug ?slug]] :where [?e :snippet/slug ?slug]]]
results (d/q {:query query :args [db slug]})] (ffirst (d/q query db slug))))
(ffirst results)))
;; update ;; update
(def update-schema (def update-schema
@ -130,81 +129,46 @@
(let [tx-data [(merge {:db/id eid} patch)]] (let [tx-data [(merge {:db/id eid} patch)]]
(d/transact conn {:tx-data tx-data})))) (d/transact conn {:tx-data tx-data}))))
;; (defn list-snippets (defn list-snippets
;; "List snippets with optional pagination. Default order is by pub-date descending." "List all the snippets"
;; [{:keys [skip limit]}] []
;; (let [conn (get-conn) (let [conn (get-conn)
;; db (d/db conn) db (d/db conn)
;; query '[:find (pull ?e [*]) query '[:find (pull ?e [*])
;; :where :where
;; [?e :snippet/title]] [?e :snippet/slug]]]
;; results (d/q {:query query :args [db]})] (->> (d/q query db)
;; ;; Convert results and sort by pub-date descending (map first))))
;; (->> results
;; (map first)
;; (sort-by #(-> % :snippet/pub-date .getTime) >)
;; (drop (or skip 0))
;; (cond-> limit (take limit))
;; vec)))
;; (defn get-snippet-by-slug [slug] (defn delete-snippet-by-slug
;; "Get a single snippet by its slug." "Soft delete a snippet (retract its entity)."
;; (let [conn (get-conn) [slug]
;; db (d/db conn) (t/log! {:level :info, :data {:slug slug}} "Retracting snippet")
;; query '[:find (pull ?e [*]) (let [conn (get-conn)
;; :where db (d/db conn)
;; [?e :snippet/slug ?slug]] eid (:db/id (get-snippet-by-slug slug))]
;; results (d/q {:query query :args [db slug]})] (when (nil? eid)
;; (first (first results)))) (throw (ex-info "Snippet not found" {:slug slug})))
(d/transact conn {:tx-data [[:db/retractEntity eid]]})))
;; (defn delete-snippet [id] (defn list-tags
;; "Soft delete a snippet (retract its entity)." "List all tags used in snippets with their counts."
;; (t/log! {:level :info, :data {:id id}} "Deleting snippet") []
;; (let [conn (get-conn) (let [conn (get-conn)
;; db (d/db conn) db (d/db conn)
;; query '[:find ?e . query '[:find ?tag (count ?e)
;; :where :where
;; [?e :snippet/uuid ?uuid]] [?e :snippet/tags ?tag]]]
;; eid (d/q {:query query :args [db id]})] (d/q query db)))
;; (when eid
;; (d/transact conn {:tx-data [[:db/retractEntity eid]]}))))
;; (defn erase-snippet [id] (defn get-snippets-by-tag
;; "Hard delete a snippet. In Datomic, this is the same as soft delete "Get all snippets that have a specific tag."
;; since all history is still available through time-travel queries." [tag]
;; (delete-snippet id)) (let [conn (get-conn)
db (d/db conn)
;; (defn patch-snippet [id patch] query '[:find (pull ?e [*])
;; "Update specific fields of a snippet." :in $ ?tag
;; (t/log! {:level :info, :data {:patch patch :id id}} "Patching snippet") :where
;; (let [conn (get-conn) [?e :snippet/tags ?tag]]
;; db (d/db conn) results (d/q query db tag)]
;; query '[:find ?e . (mapv first results)))
;; :where
;; [?e :snippet/uuid ?uuid]]
;; eid (d/q {:query query :args [db id]})]
;; (when eid
;; (let [tx-data (into [] (map (fn [[k v]]
;; (let [attr (keyword "snippet" (name k))]
;; [:db/add eid attr v]))
;; patch))]
;; (d/transact conn {:tx-data tx-data})))))
;; (defn list-tags []
;; "List all tags used in snippets with their counts."
;; (let [conn (get-conn)
;; db (d/db conn)
;; query '[:find ?tag (count ?e)
;; :where
;; [?e :snippet/tags ?tag]]]
;; (d/q {:query query :args [db]})))
;; (defn get-snippets-by-tag [tag]
;; "Get all snippets that have a specific tag."
;; (let [conn (get-conn)
;; db (d/db conn)
;; query '[:find (pull ?e [*])
;; :where
;; [?e :snippet/tags ?tag]]
;; results (d/q {:query query :args [db tag]})]
;; (mapv first results)))