Fork me on GitHub
#datascript
<
2022-01-31
>
denik02:01:05

what’s the best way to find orphan entities, i.e. entities without existing `db.type/ref` relationships?

Niki17:01:02

Something like

(d/q '[:find ?e
       :where [?e ?a _]
              (not [?e :ref _])]
  db)
Although it will not be very efficient

denik19:01:30

:ref refers to actual :refs or attributes called :ref?

Niki10:02:07

attribute called :ref

Niki10:02:21

If you need all refs, than maybe something like

(d/q '[:find ?e
       :in $ ?ref
       :where [?e _ _]
              (not [?e ?ref _])]
  db refs)

Niki10:02:14

where refs come from

(:db.type/ref (:rschema db))

denik03:02:30

thank you! I solved it using avet which should be faster:

(def refs
    (into #{}
          (comp
            (keep (fn [[k v]]
                    (when (= (:db.type v) :db.type/ref)
                      k)))
            (mapcat (juxt identity sdu/reverse-ref)))
          (get-schema)))

  (into #{}
        (comp (map (comp entity :e))
              (remove #(some % refs)))
        (datoms :ave :ident))

👍 1