Infomercial
Last updated
Last updated
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.)
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:
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.
In the previous example, all entities referenced the same User. In this one, the Topic's created-by-id
will reference a new user:
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
.
What if you want to insert 2 or 3 or more posts?
Just say "I want 3 posts" and Specmonstah delivers.
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:
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.
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-like
s 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.
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:
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!