datascript

denik 2022-01-31T02:14:05.325099Z

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

Niki 2022-02-01T10:35:07.368719Z

attribute called :ref

Niki 2022-02-01T10:36:21.585759Z

If you need all refs, than maybe something like

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

Niki 2022-02-01T10:37:14.352829Z

where refs come from

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

denik 2022-02-02T03:29:30.072669Z

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
Niki 2022-01-31T17:53:02.988329Z

Something like

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

denik 2022-01-31T19:12:30.320279Z

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