|
|
|
@ -1,11 +1,14 @@
|
|
|
|
|
(ns wanijo.instance.domain
|
|
|
|
|
(:require [clojure.spec.alpha :as spec]
|
|
|
|
|
[wanijo.framework.neo4j :as neo4j]
|
|
|
|
|
[clojure.pprint :as pprint]))
|
|
|
|
|
[wanijo.schema.domain :as domain-schema]
|
|
|
|
|
[wanijo.attribute.domain :as domain-attr]))
|
|
|
|
|
|
|
|
|
|
(spec/def ::created-at ::neo4j/date-str)
|
|
|
|
|
(spec/def ::created_at ::neo4j/date-str)
|
|
|
|
|
(spec/def ::updated-at ::neo4j/date-str)
|
|
|
|
|
(spec/def ::name (spec/and (complement empty?) string?))
|
|
|
|
|
(spec/def ::updated_at ::neo4j/date-str)
|
|
|
|
|
(spec/def ::name string?)
|
|
|
|
|
(spec/def ::value
|
|
|
|
|
(spec/or :string string?
|
|
|
|
|
:bool boolean?
|
|
|
|
@ -13,6 +16,38 @@
|
|
|
|
|
:number number?))
|
|
|
|
|
(spec/def ::instance_uuid ::neo4j/uuid)
|
|
|
|
|
(spec/def ::attribute_uuid ::neo4j/uuid)
|
|
|
|
|
(spec/def ::instance
|
|
|
|
|
(spec/keys :req-un [::neo4j/uuid
|
|
|
|
|
::created_at
|
|
|
|
|
::updated_at
|
|
|
|
|
::name]))
|
|
|
|
|
(spec/def ::properties
|
|
|
|
|
(spec/coll-of
|
|
|
|
|
(spec/keys :req-un [::neo4j/uuid
|
|
|
|
|
::created_at
|
|
|
|
|
::updated_at
|
|
|
|
|
::domain-attr/attribute])))
|
|
|
|
|
(spec/def ::link
|
|
|
|
|
(spec/keys :req-un [::neo4j/uuid
|
|
|
|
|
::created_at
|
|
|
|
|
::name]))
|
|
|
|
|
(spec/def ::target ::instance)
|
|
|
|
|
(spec/def ::source ::instance)
|
|
|
|
|
(spec/def ::links-out
|
|
|
|
|
(spec/coll-of
|
|
|
|
|
(spec/keys :req-un [::link
|
|
|
|
|
::target
|
|
|
|
|
::domain-schema/schema])))
|
|
|
|
|
(spec/def ::links-in
|
|
|
|
|
(spec/coll-of
|
|
|
|
|
(spec/keys :req-un [::link
|
|
|
|
|
::source
|
|
|
|
|
::domain-schema/schema])))
|
|
|
|
|
(spec/def ::tags
|
|
|
|
|
(spec/coll-of
|
|
|
|
|
(spec/keys :req-un [::neo4j/uuid
|
|
|
|
|
::name
|
|
|
|
|
::created-at])))
|
|
|
|
|
|
|
|
|
|
(neo4j/defquery findy-by-schema
|
|
|
|
|
"MATCH (i:instance)-[:of]->(s:schema)
|
|
|
|
@ -70,7 +105,13 @@
|
|
|
|
|
-[:of]->(s:schema)
|
|
|
|
|
RETURN i, s")
|
|
|
|
|
|
|
|
|
|
(spec/def ::instance-with-schema
|
|
|
|
|
(spec/and ::instance
|
|
|
|
|
(spec/keys :req-un
|
|
|
|
|
[::domain-schema/schema])))
|
|
|
|
|
|
|
|
|
|
(defn find-by-uuid! [uuid]
|
|
|
|
|
{:post [(spec/assert ::instance-with-schema %)]}
|
|
|
|
|
(->> (neo4j/exec-query! find-by-uuid
|
|
|
|
|
{:uuid uuid})
|
|
|
|
|
(map #(assoc (:i %)
|
|
|
|
@ -96,7 +137,6 @@
|
|
|
|
|
"MATCH (i:instance {uuid:{uuid}})
|
|
|
|
|
SET i.name = {name},
|
|
|
|
|
i.updated_at = {updated_at}")
|
|
|
|
|
|
|
|
|
|
(neo4j/defquery edit-property
|
|
|
|
|
"MATCH (i:instance {uuid:{instance_uuid}}),
|
|
|
|
|
(a:attribute {uuid:{attribute_uuid}})
|
|
|
|
@ -107,7 +147,6 @@
|
|
|
|
|
p.value = {value}
|
|
|
|
|
ON MATCH SET p.updated_at = {now},
|
|
|
|
|
p.value = {value}")
|
|
|
|
|
|
|
|
|
|
(spec/def ::now ::neo4j/date-str)
|
|
|
|
|
(spec/def ::prop-tuple-values
|
|
|
|
|
(spec/keys :req-un [::neo4j/uuid
|
|
|
|
@ -118,7 +157,6 @@
|
|
|
|
|
(spec/def ::prop-tuple
|
|
|
|
|
(spec/coll-of
|
|
|
|
|
(spec/tuple fn? ::prop-tuple-values)))
|
|
|
|
|
|
|
|
|
|
(defn instance->prop-tuples [instance]
|
|
|
|
|
{:post [(spec/assert ::prop-tuple %)]}
|
|
|
|
|
(map (fn [prop]
|
|
|
|
@ -133,7 +171,6 @@
|
|
|
|
|
:instance_uuid (:uuid instance)
|
|
|
|
|
:attribute_uuid (-> prop :attribute :uuid)}]))
|
|
|
|
|
(:properties instance)))
|
|
|
|
|
|
|
|
|
|
(defn edit! [instance]
|
|
|
|
|
(let [prop-tuples (instance->prop-tuples instance)]
|
|
|
|
|
(apply neo4j/exec-queries!
|
|
|
|
@ -151,7 +188,6 @@
|
|
|
|
|
(p:property)-[pc:of]->(i),
|
|
|
|
|
(p)-[pac:of]->(a:attribute)
|
|
|
|
|
DELETE pac, pc, cb, ic, p, i")
|
|
|
|
|
|
|
|
|
|
(defn delete! [uuid]
|
|
|
|
|
(neo4j/exec-query! delete {:uuid uuid}))
|
|
|
|
|
|
|
|
|
@ -163,7 +199,6 @@
|
|
|
|
|
SET l.created_at = {created_at},
|
|
|
|
|
l.name = {name}
|
|
|
|
|
CREATE (i)<-[:link_from]-(l)-[:link_to]->(t)")
|
|
|
|
|
|
|
|
|
|
(defn create-link! [link]
|
|
|
|
|
(let [tuples (map (fn [target-uuid]
|
|
|
|
|
[create-link
|
|
|
|
@ -183,8 +218,8 @@
|
|
|
|
|
(t)-[:of]->(s:schema)
|
|
|
|
|
RETURN i, l, t, s
|
|
|
|
|
ORDER BY s.name, t.name, t.created_at")
|
|
|
|
|
|
|
|
|
|
(defn outgoing-links! [uuid]
|
|
|
|
|
{:post [(spec/assert ::links-out %)]}
|
|
|
|
|
(map (fn [row]
|
|
|
|
|
{:link (:l row)
|
|
|
|
|
:target (:t row)
|
|
|
|
@ -199,8 +234,8 @@
|
|
|
|
|
(source)-[:of]->(schema:schema)
|
|
|
|
|
RETURN i, l, source, schema
|
|
|
|
|
ORDER BY schema.name, source.name, source.created_at")
|
|
|
|
|
|
|
|
|
|
(defn incoming-links! [uuid]
|
|
|
|
|
{:post [(spec/assert ::links-in %)]}
|
|
|
|
|
(map (fn [row]
|
|
|
|
|
{:link (:l row)
|
|
|
|
|
:source (:source row)
|
|
|
|
@ -212,7 +247,6 @@
|
|
|
|
|
"MATCH (l:link {uuid:{uuid}}),
|
|
|
|
|
(l)-[r]-()
|
|
|
|
|
DELETE r, l")
|
|
|
|
|
|
|
|
|
|
(defn delete-link! [uuid]
|
|
|
|
|
(neo4j/exec-query! delete-link
|
|
|
|
|
{:uuid uuid}))
|
|
|
|
@ -221,12 +255,23 @@
|
|
|
|
|
"MATCH (i:instance {uuid:{uuid}})-[:has]->(t:tag)
|
|
|
|
|
RETURN t
|
|
|
|
|
ORDER BY t.name")
|
|
|
|
|
|
|
|
|
|
(defn tags! [uuid]
|
|
|
|
|
{:post [(spec/assert ::tags %)]}
|
|
|
|
|
(neo4j/exec-query! tags
|
|
|
|
|
{:uuid uuid}))
|
|
|
|
|
|
|
|
|
|
(spec/def ::full-instance
|
|
|
|
|
(spec/and ::instance-with-schema
|
|
|
|
|
(spec/keys ::req-un [::neo4j/uuid
|
|
|
|
|
::created-at
|
|
|
|
|
::updated-at
|
|
|
|
|
::name
|
|
|
|
|
::properties
|
|
|
|
|
::links-out
|
|
|
|
|
::links-in
|
|
|
|
|
::tags])))
|
|
|
|
|
(defn full-instance-by-uuid! [uuid]
|
|
|
|
|
{:post [(spec/assert ::full-instance %)]}
|
|
|
|
|
(assoc (find-by-uuid! uuid)
|
|
|
|
|
:properties
|
|
|
|
|
(find-properties! uuid)
|
|
|
|
@ -241,7 +286,6 @@
|
|
|
|
|
"MATCH (u:user {uuid:{user_uuid}}),
|
|
|
|
|
(i:instance {uuid:{uuid}})
|
|
|
|
|
RETURN EXISTS((i)-[:starred_by]->(u)) AS starred")
|
|
|
|
|
|
|
|
|
|
(defn is-starred! [uuid user-uuid]
|
|
|
|
|
(-> (neo4j/exec-query! is-starred
|
|
|
|
|
{:user_uuid user-uuid
|
|
|
|
@ -255,7 +299,6 @@
|
|
|
|
|
MERGE (i)-[s:starred_by]->(u)
|
|
|
|
|
ON CREATE
|
|
|
|
|
SET s.created_at = {now}")
|
|
|
|
|
|
|
|
|
|
(defn mark-starred! [uuid user-uuid]
|
|
|
|
|
(neo4j/exec-query! mark-starred
|
|
|
|
|
{:uuid uuid
|
|
|
|
@ -267,7 +310,6 @@
|
|
|
|
|
-[s:starred_by]->
|
|
|
|
|
(:user {uuid:{user_uuid}})
|
|
|
|
|
DELETE s")
|
|
|
|
|
|
|
|
|
|
(defn remove-starred! [uuid user-uuid]
|
|
|
|
|
(neo4j/exec-query! remove-starred
|
|
|
|
|
{:uuid uuid
|
|
|
|
@ -279,7 +321,6 @@
|
|
|
|
|
(i:instance)
|
|
|
|
|
RETURN i, s
|
|
|
|
|
ORDER BY s.created_at DESC")
|
|
|
|
|
|
|
|
|
|
(defn starred-by-user! [user-uuid]
|
|
|
|
|
(map #(assoc (:i %)
|
|
|
|
|
:starred_at (-> % :s :created_at))
|
|
|
|
|