08: visiting functions
You now have most of the pieces you need to generate and insert fixture data into a test database. Now you just need to... actually insert the data! To insert data you must visit each ent in the ent db with a visiting function, and that's what you'll learn to do in this section and the next.
Earlier I wrote,
Visiting ent nodes is kind of like mapping: when you call map on a seq, you apply a mapping function to each element, creating a new seq from the mapping function's return values. When you visit ents, you apply a visiting function to each ent. The visiting function's return value is stored as an attribute on the ent (remember that ents are implemented as graph nodes, and nodes can have attributes).
You've actually already seen a visiting function at work. In the last couple sections you learned how to use spec to generate a value for each ent. The generated value was stored under the :spec-gen attribute; that's because the sg/ent-db-spec-gen you called actually applies a visiting function to the ent db it generates. Let's create our own visiting function so you can see how this works:
1
(ns reifyhealth.specmonstah-tutorial.08
2
(:require [reifyhealth.specmonstah.core :as sm]))
3
4
(def schema
5
{:user {:prefix :u}
6
:topic {:prefix :t
7
:relations {:owner-id [:user :id]}}
8
:post {:prefix :p
9
:relations {:topic-id [:topic :id]}}})
10
11
(defn announce
12
[db {:keys [ent-name]}]
13
(str "announcing... " ent-name "!"))
14
15
(defn ex-01
16
[]
17
(-> (sm/add-ents {:schema schema} {:post [[1]]})
18
(sm/visit-ents :announce announce)
19
(get-in [:data :attrs])))
20
21
(ex-01)
22
;; =>
23
{:post {:type :ent-type},
24
:p0 {:type :ent,
25
:index 0,
26
:ent-type :post,
27
:query-term [1],
28
:loom.attr/edge-attrs {:t0 {:relation-attrs #{:topic-id}}},
29
:announce "announcing... :p0!"},
30
:topic {:type :ent-type},
31
:t0 {:type :ent,
32
:index 0,
33
:ent-type :topic,
34
:query-term [:_],
35
:loom.attr/edge-attrs {:u0 {:relation-attrs #{:owner-id}}},
36
:announce "announcing... :t0!"},
37
:user {:type :ent-type},
38
:u0 {:type :ent,
39
:index 0,
40
:ent-type :user,
41
:query-term [:_],
42
:announce "announcing... :u0!"}}
Copied!
(ex-01) creates an ent db, applies a visiting function, and then looks up the :attrs key in the graph associated with the ent db's :data key. The :attrs key is where loom stores each node's attributes. We can see that each ent (:u0, :t0, :tl0) has an attribute with the key :announce and the value of "announcing... :ent-name!". Let's walk through this. You call the function ex-01, whose body is:
1
(-> (sm/add-ents {:schema schema} {:todo [[1]]})
2
(sm/visit-ents :announce announce)
3
(get-in [:data :attrs]))
Copied!
sm/add-ents builds the ent db and passes it to sm/visit-ents. sm/visit-ents takes three arguments: the ent db, a visit key (:announce), and a visiting function. Then, internally, sm/visits-ents iterates overs each ent in the ent db, passing the ent's name to the visiting function along with the db and visit key, using the return value to assign an attribute to the ent.
So in the above example, the ents are :u0, :t0, and :p0, and sm/visit-ents iterates over them in that order. For :u0, it passes the following arguments to the visiting function announce:
    1.
    the ent db
    2.
    a map that includes the key :ent-name whose value is :u0
The function announce uses the :ent-name passed in the second argument to return the string "announcing... :u0!", and that gets associated with the :announce key under the ent's attributes.
So, it's great and all that the announce function returns its little string. But how would we use visiting functions to actually insert records into a database? The answer is that the map passed as the second argument to visiting functions contains a lot more information than just the :ent-name of the current ent. It also includes the :ent-type (:user, :topic, :post) and, when you're using the clojure.spec visiting function, the :spec-gen value. The next section will show you how to combine those values to insert data in a database.
Before you continue to the next section, though, try implementing your own visiting function. Can you write a visiting function that calls swap on atom, inserting a vector of [:ent-type :ent-name] for every ent?
Last modified 2yr ago
Copy link