Skip to content

Commit b602b9b

Browse files
committed
backend->frontend datoms after save
1 parent 51026dc commit b602b9b

File tree

6 files changed

+77
-30
lines changed

6 files changed

+77
-30
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ pom.xml.asc
1414
.cpcache/
1515
node_modules/
1616
.shadow-cljs/
17+
.clj-kondo/**
1718

18-
/public/js/*
19+
/public/js/*

src/clj/reitit_db_fun/datom.clj

+14-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,17 @@
6767
(defn resultset-into-datoms
6868
"Konwertuje wynik działania selecta na datomy"
6969
[resultset]
70-
(into [] #_(sorted-set) (mapcat entity->datoms) resultset))
70+
(into [] #_(sorted-set) (comp
71+
(mapcat entity->datoms)
72+
(filter last)) resultset))
73+
74+
75+
(comment
76+
77+
(-> [{:address/id 2000001, :article/title "Test-2"
78+
:article/body "Treść artykułu 2" :user/name "Ewa"
79+
:user/id 1000003, :article/author 1000003
80+
:user/address 2000001, :address/street "Wróbla"
81+
:article/id 3000009, :address/city "Puławy"}]
82+
resultset-into-datoms)
83+
)

src/clj/reitit_db_fun/main.clj

+25-10
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@
9191
(defn get-articles-handler [{:keys [model]}]
9292
{:body (reitit-db-fun.model/get-articles model)})
9393

94-
(defn update-article-handler [{:keys [model body-params]}]
95-
{:body (reitit-db-fun.model/update-article model body-params)})
94+
(defn update-article-handler [{:keys [model body-params sente-functions]}]
95+
(let [datoms (reitit-db-fun.model/update-article model body-params)]
96+
(sente-fn/send-datoms-to-all-clients sente-functions datoms)
97+
{:body "Updated"}))
9698

9799
(defn get-app-handler [{:keys [keys-to-wrap sente sente-functions]}]
98100
(ring/ring-handler
@@ -211,12 +213,13 @@
211213
(jdbc/execute! datasource)
212214
(mapv :user/id))
213215
app (:app/handler @main-system)]
214-
(doseq [idx (range 100)]
216+
(doseq [idx (range 2)]
215217
(app {:request-method :post
216218
:uri "/api/article"
217219
:body-params {:article/title (str "Test-" (inc idx))
218220
:article/body (str "Treść artykułu " (inc idx))
219221
:article/author (rand-nth authors)}})))
222+
220223
;; sprawdzam pojedynczy artykuł
221224
(-> ((:app/handler @main-system)
222225
{:request-method :get
@@ -227,17 +230,18 @@
227230
(-> ((:app/handler @main-system)
228231
{:request-method :post
229232
:uri "/api/article"
230-
:body-params {:article/id "3000001"
231-
:article/title "Title zmieniony ponownie"
233+
:body-params {:article/id "3000002"
234+
:article/title "Title zmieniony 2"
232235
:article/body "Treść po zmiania"
236+
:article/address "2000001"
233237
:article/author "pkoza"}})
234238
(update :body slurp))
235239

236240
;; wszystkie arty
237241
(-> ((:app/handler @main-system)
238242
{:request-method :get
239-
:uri "/api/articles"})
240-
:body
243+
:uri "/api/articles"}
244+
:body)
241245
slurp)
242246

243247
;; Baza SQL na Datomy!
@@ -248,22 +252,33 @@
248252
:from [:article :address :user]
249253
:where [:and
250254
[:= :article/author :user/id]
251-
[:= :user/address :address/id]]
255+
[:= :user/address :address/id]
256+
]
252257
:limit 20})]
253258
(->> (jdbc/execute! datasource query)
254259
datom/resultset-into-datoms)))
260+
261+
(let [datasource (:storage/sql @main-system)
262+
query (sql/format {:select [:article/*]
263+
:from [:article]
264+
:where [:= :article/id 3000015]
265+
:limit 20})]
266+
(->> (jdbc/execute! datasource query)
267+
datom/resultset-into-datoms))
268+
269+
270+
255271
;; Datascript test db
256272
(def test-db (db/init-db (mapv #(apply d/datom %) initial-datoms)))
257273

258274
(d/q '[:find (pull ?e [* {:article/author [:user/name {:user/address [*]}]}])
259275
:where [?e :article/id _]]
260276
test-db)
261-
277+
;; test pojedynczego arta
262278

263279
(do
264280
(stop-system main-system)
265281
(start-system main-system config))
266282

267283
(-main)
268-
269284
)

src/clj/reitit_db_fun/model_impl.clj

+28-15
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
[reitit-db-fun.model]
55
[next.jdbc :as jdbc]
66
[next.jdbc.sql :as jdbc-sql]
7-
[reitit-db-fun.uuid :as uuid]))
7+
[reitit-db-fun.uuid :as uuid]
8+
[honey.sql :as sql]
9+
[honey.sql.helpers :refer [select where from left-join]]
10+
[reitit-db-fun.datom :as datom]))
811

912
;; SQL
10-
(def article-query (sql/format {:select [:article/* :address/* :user/*]
11-
:from [:article :address :user]
12-
:where [:and
13-
[:= :article/id ]
14-
[:= :article/author :user/id]
15-
[:= :user/address :address/id]]}))
1613

14+
(def article-query-full (-> (select :article/* :address/* :user/*)
15+
(from :article)
16+
(left-join :user [:= :article/author :user/id])
17+
(left-join :address [:= :user/address :address/id])))
1718

19+
(def article-query (-> (select :article/*)
20+
(from :article)))
21+
22+
(comment (sql/format article-query))
1823

1924
(defrecord ArticleSQL [datasource]
2025
reitit-db-fun.model/IArticle
@@ -28,14 +33,22 @@
2833
(jdbc-sql/insert! datasource :article
2934
{:title title
3035
:author author
31-
:body body}))]
32-
(if id
33-
(jdbc-sql/query datasource ["select * from article where id = ?" id])
34-
(jdbc-sql/query datasource ["select * from article where rowid = ?"
35-
(get result (keyword "last_insert_rowid()"))]))
36-
;; na koniec pobieram resultset i przerabiam na datomy
37-
38-
))
36+
:body body}))
37+
id (or id (get result (keyword "last_insert_rowid()")))
38+
_ (println "id:" id)
39+
#_ (if id
40+
(jdbc-sql/query datasource ["select * from article where id = ?" id])
41+
(jdbc-sql/query datasource ["select * from article where rowid = ?"
42+
(get result (keyword "last_insert_rowid()"))]))
43+
44+
;; na koniec pobieram resultset i przerabiam na datomy
45+
query (-> article-query (where [:= :article/id id]))
46+
query-str (sql/format query)
47+
_ (println query-str)
48+
resultset (jdbc/execute! datasource query-str)
49+
_ (println resultset)]
50+
(datom/resultset-into-datoms resultset)))
51+
3952
(get-articles [_]
4053
(jdbc/execute! datasource ["select * from article;"]))
4154
(get-article [_ article-id]

src/clj/reitit_db_fun/msg_handlers.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
(let [article (second event)]
2525
(if (m/validate v/Article article)
2626
(do (log/info "Saving to DB!")
27-
(model/update-article model article)
28-
(sente-fn/send-datoms-to-all-clients sente-functions [[:db/add 3 :author/name "Backender" 536870913]]))
27+
(let [datoms (model/update-article model article)]
28+
(sente-fn/send-datoms-to-all-clients sente-functions datoms)))
2929
(log/error "Error validating:" article
3030
(-> v/Article
3131
(m/explain article)

src/cljs/reitit_db_fun/db.cljs

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
(ds/transact! conn initial-data)
1414
conn))
1515

16+
(defn- add-db-add
17+
"dodaje :db/add do datomów.... TODO TOFIX"
18+
[datoms]
19+
(into [] (map (partial concat [:db/add])) datoms))
20+
1621
(defn save-datoms! [datoms]
17-
(ds/transact! conn datoms))
22+
(ds/transact! conn (add-db-add datoms)))
1823

1924
(comment
2025

0 commit comments

Comments
 (0)