How can I list all user-created attributes?

China☆狼群 提交于 2019-12-02 04:56:02

问题


I tried to find all user-created attributes with the code below and it returns many other default attributes, like db/unique and fressian/tag.

I'd like to get a set without them, so I was wondering if there is a better way to get it than filtering out the attributes by their prefixes.

Thanks

(q {:find '[?ident]
    :where '[[:db.part/db :db.install/attribute ?p]
             [?p :db/ident ?ident]]} db)

or

(filter (partial instance? datomic.db.Attribute)
      (:elements (p/db)))

回答1:


One way to do it is to white/black list the namespaces you want to filter out or include. Note there's no difference between some of Datomic's built-in attributes and user attributes. You can freely create attributes in any of the system namespaces e.g. db.type but of course you're not supposed to do it.

Having said that, there's only a few namespaces used for system attributes so you could simply filter out those known namespaces. e.g.

(def system-ns #{"db" "db.type" "db.install" "db.part" 
                 "db.lang" "fressian" "db.unique" "db.excise" 
                 "db.cardinality" "db.fn"})

(d/q '[:find ?e ?ident
   :in $ ?system-ns
   :where
   [?e :db/ident ?ident]
   [(namespace ?ident) ?ns]
   [((comp not contains?) ?system-ns ?ns)]]
 (d/db conn) system-ns)



回答2:


This is how I ended up doing it:

(defn get-user-schema [db]
  (->> (d/q '[:find ?e
              :where
              [?e :db/ident ?ident]
              [(namespace ?ident) ?ns]
              (not (or [(contains? #{"db" "fressian"} ?ns)]
                       [(.startsWith ?ns "db.")]))]
            db)
       (map #(->> % first (d/entity db) d/touch (into {})))))


来源:https://stackoverflow.com/questions/18281499/how-can-i-list-all-user-created-attributes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!