-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.clj
48 lines (43 loc) · 1.73 KB
/
schema.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(ns lambdaconnect-model.schema
(:require [lambdaconnect-model.tools :as t]))
(defn- ident-schema-from-entity [entity]
[{:db/ident (t/unique-datomic-identifier entity)
:db/valueType :db.type/boolean
:db/cardinality :db.cardinality/one
:db/doc "A marker datom allowing for entity identification. Automatically generated based on Core Data model file."}])
(defn- attributes-schema-from-entity [entity]
(->> entity
:attributes
vals
(filter #(not (t/special-attribs (:name %))))
(map
(fn [attribute]
(merge (if (= (:type attribute) :db.type/uuid) {:db/index true} {})
{:db/ident (t/datomic-name attribute)
:db/valueType (:type attribute)
:db/cardinality :db.cardinality/one
:db/doc "Core Data based attribute definition. Automatically generated based on Core Data model file."})))))
(defn- relationships-schema-from-entity [entity]
(->> entity
:datomic-relationships
vals
(map
(fn [relationship]
{:db/ident (t/datomic-name relationship)
:db/valueType :db.type/ref
:db/index true
:db/cardinality (if (:to-many relationship)
:db.cardinality/many
:db.cardinality/one)
:db/doc "Core Data based relationship definition. Automatically generated based on Core Data model file"}))))
(defn- schema-from-entity [entity]
(concat
(ident-schema-from-entity entity)
(attributes-schema-from-entity entity)
(relationships-schema-from-entity entity)))
(defn schema-from-entities
"Generates schema for CoreData model"
[entities]
(->> entities
vals
(t/mapcat schema-from-entity)))