Skip to content
Deus Eks edited this page Oct 26, 2019 · 9 revisions

Which is fastest, transact! or db-with ?

There are no performance differences, since transact! uses db-with internally.

How is equality treated when comparing entities?

Entity equality only checks if the entity ids (eid) is the same of two given entities. It does not looks if they're from the same database or even database version. This is efficient since otherwise an entire walk on the database could happen (which could be extremely expensive)

How do you handle de-referencing entities in a cardinality/many ?

Datascript does not provide any kind of automated handling of retracting datoms that need to be removed from a cardinality/many.

General approach for removing such datoms

Calling transact! with something like :

[[:db/retract 63 :event/contacts 56]] 

works and does retract that datom 56 for the entity id 63. This is the single datom removal. The :event/contacts keyword refers to the entity attribute that is defined as cardinality/many. The entity id in the :db/retract keyword is the 'parent' entity id that contains the list.

If you want to submit multiple retractions just group each retract statement in the outer vector:

[[:db/retract 63 :event/contacts 56][:db/retract 63 :event/contacts 55]] 

This will remove two entities :db/id 55 and :db/id 56 from the cardinality/many called :event/contacts You must use vectors within a vector even in the case of a single retract statement.

See this issue 324 for more information on a real use case.

Using Non :db/id attributes in your retracts

You can also have non :db/id attributes in the retract statement but they need to marked as :b/unique so as not to get an error: Lookup ref attribute should be marked as :db/unique: [:contact/email "janet.lansdale@google.com"]

[[:db/retract 63 :event/contacts [:contact/email "janet.lansdale@google.com"]] [:db/retract 63 :event/contacts [:contact/email "nina@gmail.com"]]]

Mark Bastian has some fantastic examples including retractions at the datascript/playground

Clone this wiki locally