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