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