Giter Club home page Giter Club logo

Comments (3)

tonsky avatar tonsky commented on August 27, 2024

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"}, ...]

from datascript.

tonsky avatar tonsky commented on August 27, 2024

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)))))

from datascript.

ThomasDeutsch avatar ThomasDeutsch commented on August 27, 2024

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!

from datascript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.