Infomercial
In the time-honored tradition of infomercials everywhere, these snippets gloss over a lot of details to reveal the truest, purest essence of a product. If you want to go all FDA on me and validate the claims, check out the full source.
The code below will shout at show you how you can generate and insert data for a forum's database. Here's an entity relationship diagram for the database:
One thing the diagram doesn't capture is that, for the like type, there's a uniqueness constraint on post-id and created-by-id. Also, every instance of created-by-id and updated-by-id refers to a user, but including that in the diagram would just clutter it.
(You will learn later how to declare the entity schema with relationship and other constraints to Specmonstah. You can also have a look at the (def schema ...) in the infomercial source code.)

Insert entity hierarchy in dependency order, with correct foreign keys

Posts have a foreign key referencing a Topic, Topics reference a Topic Category, and all of these reference a User. The snippet below shows that you can specify that you want one Post created, and Specmonstah will ensure that the other entities are created and inserted in dependency order:
1
(insert {:post [[1]]})
2
@mock-db
3
; =>
4
[[:user {:id 1 :username "K7X5r6UVs9Mm2Eks"}]
5
[:topic-category {:id 2 :created-by-id 1 :updated-by-id 1}]
6
[:topic {:id 5
7
:topic-category-id 2
8
:title "ejJ2B88UZo2NK2sMuU4"
9
:created-by-id 1
10
:updated-by-id 1}]
11
[:post {:id 9 :topic-id 5 :created-by-id 1 :updated-by-id 1}]]
Copied!
The insert function is an example of code you might write to manage the relationship between specmonstah-generated data and your own database. In this case, insert simulates inserting records in a db by conjing entities on an mock-db atom. The maps were generated using clojure.spec. Notice that all the foreign keys line up.

Specify different users

In the previous example, all entities referenced the same User. In this one, the Topic's created-by-id will reference a new user:
1
(insert {:topic [[:t0 {:refs {:created-by-id :custom-user}}]]
2
:post [[1]]})
3
@mock-db
4
; =>
5
[[:user {:id 1 :username "gMKGTwBnOvB0xt"}]
6
[:topic-category {:id 2 :created-by-id 1 :updated-by-id 1}]
7
[:user {:id 5 :username "2jK0TXCU2UcBM89"}]
8
[:topic {:id 6
9
:topic-category-id 2
10
:title "cmo2Vg8DQByz302c"
11
:created-by-id 5
12
:updated-by-id 1}]
13
[:post {:id 10 :topic-id 6 :created-by-id 1 :updated-by-id 1}]]
Copied!
Two users, one with :id 1 and another with :id 5. The topic's :created-by-id attribute is 5, and all other User references are 1.

Multiple entities

What if you want to insert 2 or 3 or more posts?
1
(insert {:post [[3]]})
2
@mock-db
3
; =>
4
[[:user {:id 1 :username "yB96fd"}]
5
[:topic-category {:id 2 :created-by-id 1 :updated-by-id 1}]
6
[:topic {:id 5
7
:topic-category-id 2
8
:title "KEh29Ru7aVVg2"
9
:created-by-id 1
10
:updated-by-id 1}]
11
[:post {:id 9 :topic-id 5 :created-by-id 1 :updated-by-id 1}]
12
[:post {:id 13 :topic-id 5 :created-by-id 1 :updated-by-id 1}]
13
[:post {:id 17 :topic-id 5 :created-by-id 1 :updated-by-id 1}]]
Copied!
Just say "I want 3 posts" and Specmonstah delivers.

Uniqueness constraints

You can't have two Likes that reference the same Post and User; in other words, a User can't Like the same Post twice. Specmonstah will automatically generate unique Users if you specify multiple Likes:
1
(insert {:like [[3]]})
2
@mock-db
3
; =>
4
[[:user {:id 1 :username "T2TD3pAB79X5"}]
5
[:user {:id 2 :username "ziJ9GnvNMOHcaUz"}]
6
[:topic-category {:id 3 :created-by-id 2 :updated-by-id 2}]
7
[:topic {:id 6
8
:topic-category-id 3
9
:title "4juV71q9Ih9eE1"
10
:created-by-id 2
11
:updated-by-id 2}]
12
[:post {:id 10 :topic-id 6 :created-by-id 2 :updated-by-id 2}]
13
[:like {:id 14 :post-id 10 :created-by-id 1}]
14
[:like {:id 17 :post-id 10 :created-by-id 2}]
15
[:user {:id 20 :username "b73Ts5BoO"}]
16
[:like {:id 21 :post-id 10 :created-by-id 20}]]
Copied!
Three Likes, Three different Users, and we're not violating the uniqueness constraint. With just one line of code. I think this feature is particularly cool.

Polymorphic relations

Whereas foreign keys in RDBMSs must reference records in a specific table, some databases like Datomic have reference types attributes that can reference any entity at all. You might want to use this in your forum so that users can like either Topics or Posts. Specmonstah handles this use case.
There are two snippets below. In the first, you say you want to create three :polymorphic-likes with {:ref-types {:liked-id :post}}. Specmonstah generates 3 likes that refer to a post. The second snippet includes {:ref-types {:liked-id :topic}}, so the likes refer to a topic. Polymorphic references compose with uniqueness constraints, so three users are created, just like in the previous snippet.
1
(insert {:polymorphic-like [[3 {:ref-types {:liked-id :post}}]]})
2
@mock-db
3
[[:user {:id 1 :username "gI3q3Y6HR1uwc"}]
4
[:user {:id 2 :username "klKs7"}]
5
[:topic-category {:id 3 :created-by-id 2 :updated-by-id 2}]
6
[:topic {:id 6
7
:topic-category-id 3
8
:title "RF6g"
9
:created-by-id 2
10
:updated-by-id 2}]
11
[:post {:id 10 :topic-id 6 :created-by-id 2 :updated-by-id 2}]
12
[:polymorphic-like {:id 14 :liked-id 10 :created-by-id 1}]
13
[:polymorphic-like {:id 17 :liked-id 10 :created-by-id 2}]
14
[:user {:id 20 :username "Gcf"}]
15
[:polymorphic-like {:id 21 :liked-id 10 :created-by-id 20}]]
16
17
18
(insert {:polymorphic-like [[3 {:ref-types {:liked-id :topic}}]]})
19
@mock-db
20
[[:user {:id 1 :username "5Z382YCNrJB"}]
21
[:topic-category {:id 2 :created-by-id 1 :updated-by-id 1}]
22
[:topic {:id 5
23
:topic-category-id 2
24
:title "i3"
25
:created-by-id 1
26
:updated-by-id 1}]
27
[:user {:id 9 :username "dJtC"}]
28
[:polymorphic-like {:id 10 :liked-id 5 :created-by-id 9}]
29
[:polymorphic-like {:id 13 :liked-id 5 :created-by-id 1}]
30
[:user {:id 16 :username "8ZS"}]
31
[:polymorphic-like {:id 17 :liked-id 5 :created-by-id 16}]]
Copied!

Visualization

Sometimes you want to inspect all the work that Specmonstah is doing for you. One way to do that is to produce an image of the entities Specmonstah produces, and their relationships:
1
(lio/view (:data (sm/add-ents {:schema schema} {:like [[2]]})))
Copied!
This shows that that two Likes were generated (l0 and l1). The Likes are applied to a Post (p0), and so forth.
And that brings the infomercial to a close. If you're ready to learn how you, too, can accomplish these amazing feats, read on!
Last modified 2yr ago