Reputation: 2565
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)))
Upvotes: 3
Views: 760
Reputation: 183429
Show user-defined attributes:
(clojure.pprint/print-table (->> (d/pull db '{:eid 0 :selector [{:db.install/attribute [*]}]})
:db.install/attribute
(remove (fn [m] (or (clojure.string/starts-with? (namespace (:db/ident m)) "db") (clojure.string/starts-with? (namespace (:db/ident m)) "fressian"))))
(map #(update % :db/valueType :db/ident))
(map #(update % :db/cardinality :db/ident))
(sort-by :db/ident)))
(Adapted from get-schema
function here: Datomicion-starter - GitHub)
Output:
| :db/id | :db/ident | :db/valueType | :db/cardinality |
|--------+--------------+------------------+----------------------|
| 74 | :inv/color | :db.type/keyword | :db.cardinality/one |
| 80 | :inv/count | :db.type/long | :db.cardinality/one |
| 75 | :inv/size | :db.type/keyword | :db.cardinality/one |
| 73 | :inv/sku | :db.type/string | :db.cardinality/one |
| 76 | :inv/type | :db.type/keyword | :db.cardinality/one |
| 79 | :item/count | :db.type/long | :db.cardinality/one |
| 78 | :item/id | :db.type/ref | :db.cardinality/one |
| 77 | :order/items | :db.type/ref | :db.cardinality/many |
Upvotes: 0
Reputation: 625
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 {})))))
Upvotes: 1
Reputation: 2413
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)
Upvotes: 6