r/Clojure • u/SimonGray • Aug 26 '20
Domain modeling with Datalog
https://youtu.be/oo-7mN9WXTw1
Aug 27 '20
Are there food visual repls for datalog, the way the presenter portrayed it with the tables, which I found intuitive
3
u/SimonGray Aug 27 '20 edited Aug 27 '20
You can use REBL for it (it's a just a vector of tuples) or you can do it yourself using reagent or something similar, e.g.
(let [rows [[11 :user/name "richhickey"] [22 :user/name "tonsky"] [33 :user/name "pithyless"]]] [:table [:thead [:tr [:th "e"] [:th "a"] [:th "v"]]] [:tbody (for [row rows] [:tr (for [column row] [:td column])])]])produces
e a v 11 :user/name richhickey 22 :user/name tonsky 33 :user/name pithyless
2
u/olymk2 Aug 26 '20
yeah I liked that one, I wish there was more info on datalog beyond the basics, especially around things like aggregates.
Been struggling trying to do counts, it works until there are no entities in which case I don't get 0.
Also I wonder if there is a way to flatten data via the pull interface, currently when I reference related data it comes out as a sub map I often find I then need to flatten the data.
2
u/TheLastSock Aug 26 '20
Been struggling trying to do counts, it works until there are no entities in which case I don't get 0.
Can you clarify? One possible interpretation is that you think datomics implantation of the aggregate count doesn't trim m return 0 if given a query with no matches.
But it's possible you mean your building your own implementation and are struggling to get the count function correct.
As to flattening the data. The query API returns a set of results. That set is flat. So you could just use the query API.
2
u/olymk2 Aug 27 '20
(d/q '[:find ?e ?i ?n ?slug (count ?eid2) :keys eid id product/name product/slug ?prices :in $ ?product-id :where [?e :product/id ?i] [?eid2 :price/product ?prices] [?e :product/name ?n] [?e :product/slug ?slug]] u/conn product-id)This is what I was trying, I get why its not working as it needs an entity to exist to count it, the issue is I want the rest of the results even if they don't have prices associated with the entity.
2
u/ribelo Aug 27 '20
I don't know what you want to get, but you will definitely have a cartesian product in the returned result, because there is no connection between the
?eand the?eid2. Another problem is that one of the arguments is also?product-id, which is not used anywhere in the code.1
u/olymk2 Aug 27 '20
With the flattening I think my issue is I am using pull inside the query so I can wild card the entities with * I guess I could list every entity in the query.
1
10
u/SimonGray Aug 26 '20
I've been trying to find good datalog tutorials/talks that go a little beyond the basics. This one was excellent. Suggestions are welcome.