move logic from instance.db to instance.domain

neo4j-4
Josha von Gizycki 5 years ago
parent 61dbceb364
commit 4aa3a14ba2

@ -1,33 +1,9 @@
(ns wanijo.instance.db (ns wanijo.instance.db
(:require [clojure.spec.alpha :as spec] (:require [clojure.spec.alpha :as spec]
[wanijo.specs :as specs]
[wanijo.infrastructure.neo4j :as neo4j] [wanijo.infrastructure.neo4j :as neo4j]
[wanijo.schema.db :as domain-schema] [wanijo.instance.domain :as domain-instance]
[wanijo.attribute.domain :as domain-attr] [wanijo.tag.db :as db-tag]))
[wanijo.tag.db :as db-tag]
[wanijo.link.db :as db-link]))
(spec/def ::instance
(spec/keys :req-un [::neo4j/uuid
::specs/created_at
::specs/updated_at
::specs/name]))
(spec/def ::properties
(spec/coll-of
(spec/keys :req-un [::neo4j/uuid
::specs/created_at
::specs/updated_at
::domain-attr/attribute])))
(spec/def ::target ::instance)
(spec/def ::source ::instance)
(spec/def ::tags
(spec/coll-of ::db-tag/tag))
(spec/def ::contains-schema
(spec/keys :req-un [::domain-schema/schema]))
(spec/def ::instances-with-tags
(spec/coll-of (spec/merge ::instance
(spec/keys :req-un [::tags]))))
(neo4j/defquery findy-by-schema (neo4j/defquery findy-by-schema
"MATCH (i:instance)-[:of]->(s:schema) "MATCH (i:instance)-[:of]->(s:schema)
WHERE s.uuid = {uuid} WHERE s.uuid = {uuid}
@ -35,7 +11,7 @@
(i)-[:tagged_with]->(t:tag) (i)-[:tagged_with]->(t:tag)
RETURN i, t") RETURN i, t")
(defn find-by-schema! [schema-uuid] (defn find-by-schema! [schema-uuid]
{:post [(spec/assert ::instances-with-tags %)]} {:post [(spec/assert ::domain-instance/instances-with-tags %)]}
(->> (neo4j/exec-query! findy-by-schema (->> (neo4j/exec-query! findy-by-schema
{:uuid schema-uuid}) {:uuid schema-uuid})
(group-by :i) (group-by :i)
@ -84,13 +60,11 @@
(concat [instance-tuple] (concat [instance-tuple]
prop-tuples)))) prop-tuples))))
(spec/def ::instance-with-schema
(spec/merge ::instance ::contains-schema))
(neo4j/defquery find-by-uuid (neo4j/defquery find-by-uuid
"MATCH (i:instance {uuid:{uuid}})-[:of]->(s:schema) "MATCH (i:instance {uuid:{uuid}})-[:of]->(s:schema)
RETURN i, s") RETURN i, s")
(defn find-by-uuid! [uuid] (defn find-by-uuid! [uuid]
{:post [(spec/assert ::instance-with-schema %)]} {:post [(spec/assert ::domain-instance/instance-with-schema %)]}
(->> (neo4j/exec-query! find-by-uuid (->> (neo4j/exec-query! find-by-uuid
{:uuid uuid}) {:uuid uuid})
(map #(assoc (:i %) (map #(assoc (:i %)
@ -111,15 +85,6 @@
(neo4j/exec-query! find-properties (neo4j/exec-query! find-properties
{:uuid uuid}))) {:uuid uuid})))
(spec/def ::prop-tuple-values
(spec/keys :req-un [::neo4j/uuid
::specs/now
::value
::specs/instance_uuid
::specs/attribute_uuid]))
(spec/def ::prop-tuple
(spec/coll-of
(spec/tuple fn? ::prop-tuple-values)))
(neo4j/defquery edit-instance (neo4j/defquery edit-instance
"MATCH (i:instance {uuid:{uuid}}) "MATCH (i:instance {uuid:{uuid}})
SET i.name = {name}, SET i.name = {name},
@ -135,7 +100,7 @@
ON MATCH SET p.updated_at = {now}, ON MATCH SET p.updated_at = {now},
p.value = {value}") p.value = {value}")
(defn instance->prop-tuples [instance] (defn instance->prop-tuples [instance]
{:post [(spec/assert ::prop-tuple %)]} {:post [(spec/assert ::domain-instance/prop-tuple %)]}
(map (fn [prop] (map (fn [prop]
(let [prop-uuid (:uuid prop) (let [prop-uuid (:uuid prop)
uuid (if (empty? prop-uuid) uuid (if (empty? prop-uuid)
@ -192,12 +157,6 @@
#(-> % direction :name) #(-> % direction :name)
#(-> % direction :created_at))))) #(-> % direction :created_at)))))
(spec/def ::links-out
(spec/coll-of
(spec/keys :req-un [::db-link/link
::target
::domain-schema/schema
::tags])))
(neo4j/defquery outgoing-links (neo4j/defquery outgoing-links
"MATCH (inst:instance {uuid:{uuid}}), "MATCH (inst:instance {uuid:{uuid}}),
(inst)<-[:link_from]-(link:link), (inst)<-[:link_from]-(link:link),
@ -207,15 +166,9 @@
(target)-[:tagged_with]->(tag:tag) (target)-[:tagged_with]->(tag:tag)
RETURN link, target, schema, tag") RETURN link, target, schema, tag")
(defn outgoing-links! [uuid] (defn outgoing-links! [uuid]
{:post [(spec/assert ::links-out %)]} {:post [(spec/assert ::domain-instance/links-out %)]}
(group-by-link-and-assoc-tags uuid outgoing-links :target)) (group-by-link-and-assoc-tags uuid outgoing-links :target))
(spec/def ::links-in
(spec/coll-of
(spec/keys :req-un [::db-link/link
::source
::domain-schema/schema
::tags])))
(neo4j/defquery incoming-links (neo4j/defquery incoming-links
"MATCH (inst:instance {uuid:{uuid}}), "MATCH (inst:instance {uuid:{uuid}}),
(inst)<-[:link_to]-(link:link), (inst)<-[:link_to]-(link:link),
@ -226,19 +179,11 @@
RETURN link, source, schema, tag RETURN link, source, schema, tag
ORDER BY schema.name, source.name, source.created_at") ORDER BY schema.name, source.name, source.created_at")
(defn incoming-links! [uuid] (defn incoming-links! [uuid]
{:post [(spec/assert ::links-in %)]} {:post [(spec/assert ::domain-instance/links-in %)]}
(group-by-link-and-assoc-tags uuid incoming-links :source)) (group-by-link-and-assoc-tags uuid incoming-links :source))
(spec/def ::contains-full-information
(spec/keys :req-un [::properties
::links-out
::links-in
::tags]))
(spec/def ::full-instance
(spec/merge ::instance-with-schema
::contains-full-information))
(defn full-instance-by-uuid! [uuid] (defn full-instance-by-uuid! [uuid]
{:post [(spec/assert ::full-instance %)]} {:post [(spec/assert ::domain-instance/full-instance %)]}
(assoc (find-by-uuid! uuid) (assoc (find-by-uuid! uuid)
:properties :properties
(find-properties! uuid) (find-properties! uuid)

@ -0,0 +1,65 @@
(ns wanijo.instance.domain
(:require [wanijo.specs :as specs]
[wanijo.attribute.domain :as domain-attr]
[wanijo.schema.db :as db-schema]
[wanijo.tag.db :as db-tag]
[wanijo.link.db :as db-link]
[clojure.spec.alpha :as spec]))
(spec/def ::instance
(spec/keys :req-un [::specs/uuid
::specs/created_at
::specs/updated_at
::specs/name]))
(spec/def ::properties
(spec/coll-of
(spec/keys :req-un [::specs/uuid
::specs/created_at
::specs/updated_at
::domain-attr/attribute])))
(spec/def ::target ::instance)
(spec/def ::source ::instance)
(spec/def ::tags
(spec/coll-of ::db-tag/tag))
(spec/def ::contains-schema
(spec/keys :req-un [::db-schema/schema]))
(spec/def ::instances-with-tags
(spec/coll-of (spec/merge ::instance
(spec/keys :req-un [::tags]))))
(spec/def ::instance-with-schema
(spec/merge ::instance ::contains-schema))
(spec/def ::prop-tuple-values
(spec/keys :req-un [::specs/uuid
::specs/now
::value
::specs/instance_uuid
::specs/attribute_uuid]))
(spec/def ::prop-tuple
(spec/coll-of
(spec/tuple fn? ::prop-tuple-values)))
(spec/def ::links-out
(spec/coll-of
(spec/keys :req-un [::db-link/link
::target
::db-schema/schema
::tags])))
(spec/def ::links-in
(spec/coll-of
(spec/keys :req-un [::db-link/link
::source
::db-schema/schema
::tags])))
(spec/def ::contains-full-information
(spec/keys :req-un [::properties
::links-out
::links-in
::tags]))
(spec/def ::full-instance
(spec/merge ::instance-with-schema
::contains-full-information))

@ -1,6 +1,5 @@
(ns wanijo.instance.forms (ns wanijo.instance.forms
(:require [wanijo.specs :as specs] (:require [wanijo.specs :as specs]))
[wanijo.instance.db :as domain]))
(def form (def form
{:fields {:name {:label "Name" {:fields {:name {:label "Name"

@ -6,7 +6,6 @@
[markdown.core :as md] [markdown.core :as md]
[formulare.core :as form] [formulare.core :as form]
[wanijo.tag.view :as view-tag] [wanijo.tag.view :as view-tag]
[wanijo.instance.db :as domain]
[wanijo.visualisation.viz :as viz] [wanijo.visualisation.viz :as viz]
[wanijo.infrastructure [wanijo.infrastructure
[view :as view] [view :as view]

@ -6,7 +6,7 @@
[wanijo.schema.db :as domain-schema] [wanijo.schema.db :as domain-schema]
[wanijo.instance [wanijo.instance
[view :as view-instance] [view :as view-instance]
[db :as domain-instance]] [db :as db-instance]]
[wanijo.tag [wanijo.tag
[db :as domain] [db :as domain]
[forms :as forms]])) [forms :as forms]]))
@ -22,7 +22,7 @@
(resp/redirect (path :instance-show (resp/redirect (path :instance-show
{:uuid instance-uuid}))) {:uuid instance-uuid})))
(view-instance/show! (view-instance/show!
(domain-instance/full-instance-by-uuid! instance-uuid) (db-instance/full-instance-by-uuid! instance-uuid)
(domain-schema/accessible-schemas! user-uuid) (domain-schema/accessible-schemas! user-uuid)
req)))) req))))

@ -1,7 +1,8 @@
(ns wanijo.visualisation.db (ns wanijo.visualisation.db
(:require [clojure.spec.alpha :as spec] (:require [clojure.spec.alpha :as spec]
[wanijo.infrastructure.neo4j :as neo4j] [wanijo.infrastructure.neo4j :as neo4j]
[wanijo.instance.db :as domain-instance])) [wanijo.instance.db :as db-instance]
[wanijo.instance.domain :as domain-instance]))
(spec/def ::link-name (spec/def ::link-name
(spec/or :not-give nil? (spec/or :not-give nil?
@ -34,7 +35,7 @@
:links-out :links-out
(fn [links-in] (fn [links-in]
(map #(assoc (map #(assoc
(domain-instance/full-instance-by-uuid! (db-instance/full-instance-by-uuid!
(-> % :target :uuid)) (-> % :target :uuid))
:link-name :link-name
(-> % :label :name)) (-> % :label :name))
@ -42,9 +43,9 @@
(defn search! [term] (defn search! [term]
{:post [(spec/assert (spec/coll-of ::with-enriched-out-links) %)]} {:post [(spec/assert (spec/coll-of ::with-enriched-out-links) %)]}
(->> (neo4j/exec-query! search {:term (str ".*" term ".*")}) (->> (neo4j/exec-query! search {:term (str ".*" term ".*")})
(map #(domain-instance/full-instance-by-uuid! (-> % :i :uuid))) (map #(db-instance/full-instance-by-uuid! (-> % :i :uuid)))
(map enrich-links))) (map enrich-links)))
(defn instance! [uuid] (defn instance! [uuid]
{:post [(spec/assert ::with-enriched-out-links %)]} {:post [(spec/assert ::with-enriched-out-links %)]}
(enrich-links (domain-instance/full-instance-by-uuid! uuid))) (enrich-links (db-instance/full-instance-by-uuid! uuid)))

@ -1,10 +1,10 @@
(ns wanijo.visualisation.routes (ns wanijo.visualisation.routes
(:require [compojure.core :refer [defroutes wrap-routes (:require [compojure.core :refer [defroutes wrap-routes
GET POST DELETE]] GET POST DELETE]]
[ring.util.response :as resp] [wanijo.infrastructure
[wanijo.infrastructure.routing :refer [register! path]] [routing :refer [register! path]]
[wanijo.infrastructure.view :as view] [view :as view]]
[wanijo.instance.db :as domain-instance] [wanijo.instance.db :as db-instance]
[wanijo.schema.db :as domain-schema] [wanijo.schema.db :as domain-schema]
[wanijo.visualisation [wanijo.visualisation
[db :as vis-db] [db :as vis-db]
@ -35,7 +35,7 @@
(defroutes routes (defroutes routes
(GET (register! :vis-explore "/vis/explore/:instance-uuid") (GET (register! :vis-explore "/vis/explore/:instance-uuid")
[instance-uuid :as req] [instance-uuid :as req]
(vis-view/index (domain-instance/find-by-uuid! instance-uuid) (vis-view/index (db-instance/find-by-uuid! instance-uuid)
req)) req))
(GET (register! :vis-all-instances "/vis/all-instances") (GET (register! :vis-all-instances "/vis/all-instances")
[:as req] [:as req]

Loading…
Cancel
Save