datalevin

teodorlu 2024-09-13T12:02:53.547099Z

Hi! Is the entity API (`d/entity`) supported when running datalevin from a babashka pod?

Huahai 2024-11-01T18:08:23.153799Z

done

2024-09-17T23:19:36.230749Z

Maybe YMMV or things have changed, but my recollection of Datomic Free was that an entity's supply of attributes might be incomplete until d/touch. You could force an attribute by querying it (the entity being a somewhat magical Map) or calling d/touch. After all, an entity might involve jillions of datoms, and a program generally does not want to spend time fetching any that it does not directly need.

teodorlu 2024-11-03T09:52:05.327969Z

Amazing, thank you! Looking forward to try it 🙏

teodorlu 2024-09-13T12:03:04.477049Z

(comment
  (require '[babashka.fs :as fs]
           '[babashka.pods])
  (babashka.pods/load-pod 'huahaiy/datalevin "0.9.10")
  (require '[pod.huahaiy.datalevin :as d])
  (def db-path "/tmp/datalevin/example-pod-entity")
  (when (fs/exists? db-path)
    (fs/delete-tree db-path))
  (def schema {:account/username {:db/valueType :db.type/string
                                  :db/unique :db.unique/identity}
               :account/name {:db/valueType :db.type/string}})
  (def conn (d/get-conn db-path schema))
  (d/transact! conn [{:account/username "teodorlu"
                      :account/name "Teodor"}])

  ;; Query appears to work fine
  (d/q '[:find ?username ?name
         :where
         [?e :account/username ?username]
         [?e :account/name ?name]]
       (d/db conn))
  ;; => #{["teodorlu" "Teodor"]}

  ;; Entity isn't doing what I expect!
  (select-keys (d/entity (d/db conn) [:account/username "teodorlu"])
               [:account/username :account/name])
  ;; => {}

  (into {} (d/entity (d/db conn) [:account/username "teodorlu"]))
  ;; => {:db/id 1, :db-name #uuid "2934d6d6-2734-4959-b296-547ed76d996e"}

  ,)

teodorlu 2024-09-13T12:04:46.863189Z

Making "lazy entities" work when working with two processes (my script bababashka process and the datalevin process) may be a challenge?

teodorlu 2024-09-13T12:49:37.362509Z

Entity API appears to work as I'd expect when running on a JVM:

;; Datalevin Entity API from JVM
(comment
  (set! *print-namespace-maps* false)
  (require '[babashka.fs :as fs]
           '[datalevin.core :as d])
  (def db-path "example-jvm-entity")
  (when (fs/exists? db-path)
    (fs/delete-tree db-path))
  (def schema {:account/username {:db/valueType :db.type/string
                                  :db/unique :db.unique/identity}
               :account/name {:db/valueType :db.type/string}})
  (def conn (d/get-conn db-path schema))
  (d/transact! conn [{:account/username "teodorlu"
                      :account/name "Teodor"}])

  (select-keys (d/entity (d/db conn) [:account/username "teodorlu"])
               [:account/username :account/name])
  ;; => {:account/username "teodorlu", :account/name "Teodor"}

  (into {} (d/entity (d/db conn) [:account/username "teodorlu"]))
  ;; => {:account/username "teodorlu", :account/name "Teodor"}

  ,)

2024-09-13T20:49:18.197579Z

You don't need d/touch to retrieve all the datoms of the entity? (I don't know Datalevlin's entity API)

teodorlu 2024-09-14T12:16:28.369249Z

My expectation is that touch is not needed. Touch appears to not be needed on JVM datalevin (see above). I don't think touch is needed with Datomic either.

Huahai 2024-09-14T17:54:11.200739Z

You can file an issue. Basically, entity needs to be serialized over the wires. These things take work. PR welcome

👍 1
teodorlu 2024-10-09T14:31:53.873579Z

@phill very late reply here, but as far as I can tell from the https://docs.datomic.com/reference/entities.html, d/touch appears not to be needed:

teodorlu 2024-10-09T14:32:02.791099Z

Added an issue: https://github.com/juji-io/datalevin/issues/283

2024-10-09T16:04:53.125139Z

@teodorlu You can navigate without having done touch. But if you just print the entity, the printout may reflect only a seemingly-arbitrary subset of datoms. Calling entity does not induce complete retrieval of all datoms related to the entity.