-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relationships? #3
Comments
I would do it like this: (require '[odoyle.rules :as o])
(def rules
(o/ruleset
{::todo
[:what
[id ::text text]
:then-finally
(->> (o/query-all o/*session* ::todo)
(reduce #(assoc %1 (:id %2) %2) {})
(o/insert! ::todos ::by-id))]
::todo-with-sub-todos
[:what
[id ::text text]
[id ::sub-todos sub-todos]]
::update-sub-todos
[:what
[id ::sub-todo-ids sub-todo-ids]
[::todos ::by-id id->todo]
:then
(->> (mapv id->todo sub-todo-ids)
(o/insert! id ::sub-todos))]}))
(def initial-session
(reduce o/add-rule (o/->session) rules))
(-> initial-session
(o/insert 1 {::text "do this"
::sub-todo-ids [2]})
(o/insert 2 ::text "but first do this!")
o/fire-rules
(o/query-all ::todo-with-sub-todos)
println)
;; [{:id 1, :text "do this", :sub-todos [{:id 2, :text "but first do this!"}]}] I'm saving all the todos as a derived fact (i added With rules it kind of turns "querying" inside out but it accomplishes the same thing :D |
Very interesting! This is probably too granular for users who want to express relationships all over their programs. Do you think it would make sense performance-wise to build an in-memory graph-db on top of odoyle-rules? Having performant rules on top of a graph-db that can derive facts on itself and cause side effects is highly exciting! |
I've been thinking about it recently actually. The I'd like to figure out how to update that map incrementally, but i haven't come up with a good approach yet. That would allow it to possibly behave like a normal database, maintaining all sorts of indexes of facts and only recomputing when necessary. That would be really neat. |
FYI for 0.6.0 i wrote a longer write-up about this topic, which shows how to do this recursively (sub-todos with their own sub-todos). |
From looking at Datomic it appears you can have a one-to-many relationship by specifying |
Right, in odoyle the triplets must have a unique id+attribute combo; that is a strict requirement. You are technically still in triplet land; you're just inserting data structures derived from the original triplets as new triplets. You won't be able to avoid resorting to Clojure; in fact, using it more often is one of the goals. See the very end of the writeup I mentioned above. |
If |
How would one query relationships? For example what when todos have sub-todos:
This can be normalized and added to the session one by one:
But how does one query it??
The text was updated successfully, but these errors were encountered: