Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter whole entities with d/filter #63

Closed
ThomasDeutsch opened this issue Feb 26, 2015 · 3 comments
Closed

filter whole entities with d/filter #63

ThomasDeutsch opened this issue Feb 26, 2015 · 3 comments

Comments

@ThomasDeutsch
Copy link

I open this issue since i can not find a documentation/test on how to accomplish this.

How can i filter an entity based on one attribute value?

For example, i would like to set a filter that will return entities where :name=="Ivan"

[{:db/id 1
 :name  "Petr"
 :email "petya@spb.ru"
 :aka   ["I" "Great"]
 :password "<SECRET>"}
 {:db/id 2
  :name  "Ivan"
  :aka   ["Terrible" "IV"]
  :password "<PROTECTED>"}]

(defn only-evans [udb datom] .....)

(d/filter db only-evans) 

will only return
[[{:db/id 1
:name "Petr"
:email "petya@spb.ru"
:aka ["I" "Great"]
:password ""}]

@ThomasDeutsch ThomasDeutsch changed the title Remove entities with d/filter filter whole entities with d/filter Feb 26, 2015
@tonsky
Copy link
Owner

tonsky commented Feb 26, 2015

What is your source? Db? Datoms? Entities?

(d/q '[:find [?e ...]
       :in $ ?n
       :where [?e :name n]]
      db "Ivan")
;; => [1, 2, 3]

(filter (fn [datom]
          (and (= (.-a datom) :name)
               (= (.-v datom) "Ivan")))
        datoms)
;; => [#datascript.core/Datom{:e 1, :a :name, :v "Ivan"}, ...]

(filter (fn [entity]
          (= (:name entity) "Ivan"))
        entities)
;; => [{:db/id 1, :name "Ivan"}, {:db/id 2, :name "Ivan"}, ...]

@tonsky
Copy link
Owner

tonsky commented Feb 26, 2015

Oh, you’re asking about datascript/filter. Try this:

(d/filter db
  (fn [db datom]
    (and (= (.-a datom) :name)
         (= (.-v datom) "Ivan"))))

This will only keep datoms which are about :name ([* :name *]), so essentially querying such db will get you just

{:db/id 2, :name "Ivan"}

without any other properties of entity.

You probably want to keep any attributes of entity if that entity has a :name attribute equal to "Ivan". Then

(d/filter db
  (fn [db datom]
    (let [eid    (.-e datom)
          entity (d/entity db eid)]
      (= "Ivan" (:name entity)))))

@ThomasDeutsch
Copy link
Author

I am rewriting the datascript todo app.
For the filtering, i would like to pass a filtered db to my todo-list ( like you described in the video ).

The last example is just what i needed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants