Fork me on GitHub
#datascript
<
2020-02-19
>
ts150314:02:19

Hello guys. Can someone point me to the documentation or example of how to work properly with nested entities in datascript? for example, I have an entity parent which has a children property and this property is a reference with cardinality many It’s simple to add the new references but it’s not clear how to delete references (without deleting child entities itself) I will appreciate for any help.

cjsauer23:02:43

Hello @UF2GC1JRM 👋 This is relatively straightforward using retractions:

(d/transact conn {:tx-data [[:db/retract parent :children child1]])
This would retract the fact that child1 is a child of parent. You would first need to query for the entity ids to obtain both parent and child1, and then use them in the transaction. If you wanted to remove multiple parent/child relationships in one go:
(d/transact conn {:tx-data [[:db/retract parent :children child1]
                            [:db/retract parent :children child2]
                            [:db/retract parent :children child3]]})

cjsauer23:02:01

Here is a potentially helpful reference page: https://github.com/tonsky/datascript/wiki/FAQ Keep in mind that the Datomic API docs are usually helpful as well, with a few differences. Cross referencing the Datomic documentation with the datascript wiki is a good strategy.

ts150307:02:43

Hi @U6GFE9HS7. Thanks, will give it a try

unbalanced15:02:37

@UF2GC1JRM

(def schema 
  {:thing/parent {:db/unique :db.unique/identity}
  :thing/child {:db/valueType :db.type/ref
               :db/cardinality :db.cardinality/many
               :db/isComponent true}}) 
The key part is the :db/isComponent true

unbalanced15:02:50

Will usually improve your experience with parent/child stuff

ts150316:02:27

Yes, it could help but in my case from domain modeling perspective the child entity it's not the component of the parent entity

cjsauer21:02:31

Right. :db/isComponent will cause the child to be retracted along with the parent, which from your problem description is not what you want.

unbalanced23:02:41

To answer your specific question, to delete a reference from :thing/parent to :thing/child you can simply assert

(d/trasnsact conn 
[[:db/retract [:thing/parent parent-id] :thing/children [:thing/child child-id]])
Not sure if that helps

Daniel Hines22:02:42

Can you define rules in the :where clause? I.e, as opposed to passing them into :in?

cjsauer15:02:49

Not that I know of…could you give an example of what you’re after?

Daniel Hines15:02:37

I'm using Roam Research, a web app for note taking with built in Datascript queries of your notes. It supports the where clause, but not in. I tried this:

[:find ?content
  :where
    [?e :node/title ?title]
    [(re-find #"February 21st" ?title)]
    [(parent ?a ?b)
      [?a :block/children ?b]]
    (parent ?e ?x)
    [?x :block/string ?content] 
]
The idea is to make a rule called parent that I can use in another rule called ancestor , allowing me to do recursive queries. But I get the following error:
Cannot parse binding, expected (bind-scalar | bind-tuple | bind-coll | bind-rel)