datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
refset 2020-02-19T00:03:47.054Z

Hi 🙂 I work on Crux and was playing with Crux->DataScript tx replication a couple of weeks ago: https://github.com/crux-labs/crux-datascript/blob/master/test/crux_datascript/core_test.clj It's just an experiment for now but it would be great to hear feedback should you have any.

lilactown 2020-02-19T00:11:26.054400Z

awesome! I’ll take a look

2🙏
ts1503 2020-02-19T14:07:19.059300Z

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.

unbalanced 2020-02-20T15:06:37.064900Z

@sergey.tkachenko

(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

unbalanced 2020-02-20T15:08:50.065100Z

Will usually improve your experience with parent/child stuff

ts1503 2020-02-20T16:08:27.065300Z

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

cjsauer 2020-02-20T21:38:31.066600Z

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

unbalanced 2020-02-20T23:23:41.066800Z

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

2020-02-19T22:39:42.060200Z

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

cjsauer 2020-02-20T15:01:49.064700Z

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

2020-02-21T15:05:37.071400Z

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)

cjsauer 2020-02-19T23:37:43.060300Z

Hello @sergey.tkachenko 👋 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]]})

cjsauer 2020-02-19T23:40:01.060500Z

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.