Skip to content

Commit

Permalink
feat: adds output schema for db adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeldelboni committed Aug 15, 2023
1 parent b6c8d5c commit 41a3637
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
30 changes: 20 additions & 10 deletions src/codes/clj/docs/backend/adapters.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns codes.clj.docs.backend.adapters
(:require [codes.clj.docs.backend.schemas.types :as schemas.types])
(:require [codes.clj.docs.backend.schemas.db :as schemas.db]
[codes.clj.docs.backend.schemas.types :as schemas.types])
(:import [java.time ZoneId]
[java.time.format DateTimeFormatter]))

Expand All @@ -18,33 +19,37 @@
(date->localdatetime (ZoneId/of "UTC"))
(.format (DateTimeFormatter/ofPattern str-format))))

; TODO: schema & test
; TODO: input schema & test
(defn db->author
{:malli/schema [:=> [:cat :any] schemas.db/Author]}
[{:keys [author-id login account-source avatar-url created-at]}]
{:author/author-id author-id
:author/login login
:author/account-source account-source
:author/avatar-url avatar-url
:author/created-at created-at})

; TODO: schema & test
; TODO: input schema & test
(defn db->note
{:malli/schema [:=> [:cat :any] schemas.db/Note]}
[{:keys [id definition-id body created] :as note}]
{:note/note-id id
:note/definition-id definition-id
:note/body body
:note/created-at created
:note/author (db->author note)})

; TODO: schema & test
; TODO: input schema & test
(defn db->notes
{:malli/schema [:=> [:cat :any] [:sequential schemas.db/Note]]}
[db-rows]
(->> db-rows
(filter #(= (:type %) "note"))
(map db->note)))

; TODO: schema & test
; TODO: input schema & test
(defn db->example
{:malli/schema [:=> [:cat :any [:sequential schemas.db/Author]] schemas.db/Example]}
[{:keys [id definition-id body created] :as example}
editors]
{:example/example-id id
Expand All @@ -54,8 +59,9 @@
:example/author (db->author example)
:example/editors editors})

; TODO: schema & test
; TODO: input schema & test
(defn db->examples
{:malli/schema [:=> [:cat :any] [:sequential schemas.db/Example]]}
[db-rows]
(->> db-rows
(filter #(= (:type %) "example"))
Expand All @@ -66,24 +72,28 @@
example (last sorted-examples)]
(db->example example editors))))))

; TODO: schema & test
; TODO: input schema & test
(defn db->see-also
{:malli/schema [:=> [:cat :any] schemas.db/SeeAlso]}
[{:keys [id definition-id body created] :as see-also}]
{:see-also/see-also-id id
:see-also/definition-id definition-id
:see-also/definition-id-to body
:see-also/created-at created
:see-also/author (db->author see-also)})

; TODO: schema & test
; TODO: input schema & test
(defn db->see-alsos
{:malli/schema [:=> [:cat :any] [:sequential schemas.db/SeeAlso]]}
[db-rows]
(->> db-rows
(filter #(= (:type %) "see-also"))
(map db->see-also)))

; TODO: schema
(defn db->definitions [db-rows]
; TODO: input schema
(defn db->definitions
{:malli/schema [:=> [:cat :any] [:sequential schemas.db/Definition]]}
[db-rows]
(->> db-rows
(group-by :definition-id)
(map (fn [[definition-id items]]
Expand Down
4 changes: 2 additions & 2 deletions src/codes/clj/docs/backend/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@
(execute! db)
first))

; TODO schema
(defn get-all
(defn get-by-definition
{:malli/schema [:=> [:cat :string schemas.types/DatabaseComponent] [:sequential schemas.db/Definition]]}
[definition-id db]
(->> (-> (sql.helpers/union-all
(-> (sql.helpers/select
Expand Down
7 changes: 7 additions & 0 deletions src/codes/clj/docs/backend/schemas/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,10 @@
:note/definition-id
:note/created-at
:note/updated-at]))

(def Definition
[:map
[:definition/definition-id :string]
[:definition/notes [:sequential Note]]
[:definition/examples [:sequential Example]]
[:definition/see-alsos [:sequential SeeAlso]]])
12 changes: 6 additions & 6 deletions test/integration/codes/clj/docs/backend/db_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
:see-also/definition-id "clojure.core/disj"
:see-also/definition-id-to "clojure.core/dissoc"
:see-also/created-at inst?}]}]
(db/get-all "clojure.core/disj" database))))
(db/get-by-definition "clojure.core/disj" database))))

(defflow note-db-test
{:init (util/start-system! create-and-start-components!)
Expand All @@ -134,7 +134,7 @@
:note/definition-id "clojure.core/disj"
:note/body "my note about this function."
:note/created-at inst?}]}]
(db/get-all "clojure.core/disj" database)))
(db/get-by-definition "clojure.core/disj" database)))

(update-note {:note/note-id (:note/note-id note)
:note/author-id author-id
Expand All @@ -149,7 +149,7 @@
:note/created-at inst?
;todo: :note/updated-at inst?
}]}]
(db/get-all "clojure.core/disj" database))))
(db/get-by-definition "clojure.core/disj" database))))

(defflow example-db-test
{:init (util/start-system! create-and-start-components!)
Expand All @@ -175,7 +175,7 @@
(flow "check transaction was inserted in db"
(match? [{:definition/examples [example-full-1
example-full-2]}]
(db/get-all "clojure.core/disj" database)))
(db/get-by-definition "clojure.core/disj" database)))

(update-example {:example/example-id (:example/example-id example-1)
:example/author-id author-id
Expand All @@ -188,7 +188,7 @@
(flow "check transaction was inserted in db"
(match? [{:definition/examples [(assoc example-full-1 :example/body "my example about this function. edit 2")
example-full-2]}]
(db/get-all "clojure.core/disj" database))))
(db/get-by-definition "clojure.core/disj" database))))

(defflow all-db-test
{:init (util/start-system! create-and-start-components!)
Expand Down Expand Up @@ -274,4 +274,4 @@
:account-source "github"
:avatar-url "https://my.pic.com/me.jpg"
:created-at inst?}}]}])
(db/get-all "clojure.core/disj" database))))
(db/get-by-definition "clojure.core/disj" database))))

0 comments on commit 41a3637

Please sign in to comment.