datalevin

2024-11-22T18:29:47.412509Z

๐Ÿ‘‹ Doesnโ€™t datalevin support transacting nested maps and creating entities from refs that have db/isComponent? true or am I holding it wrong? Example in ๐Ÿงต

Huahai 2024-11-23T17:30:43.197429Z

Sure, please do

2024-11-22T18:31:38.827639Z

Datomic example first, same schema and data doesnโ€™t work as I expected in datalevin:

(require '[datomic.api :as d])

(def db-uri "datomic:")

(d/create-database db-uri)

(def conn (d/connect db-uri))

(def schema [{:db/ident :question/answers
              :db/cardinality :db.cardinality/many
              :db/isComponent true
              :db/valueType :db.type/ref}
             {:db/ident :question/key
              :db/cardinality :db.cardinality/one
              :db/valueType :db.type/keyword}
             {:db/ident :question/title
              :db/cardinality :db.cardinality/one
              :db/valueType :db.type/string
              :db/fulltext true}
             {:db/ident :answer/value
              :db/valueType :db.type/string
              :db/cardinality :db.cardinality/one}
             {:db/ident :answer/category
              :db/valueType :db.type/string
              :db/cardinality :db.cardinality/one}])

@(d/transact conn schema)

(d/q '[:find ?e
       :where
       [?e :db/ident :answer/category]]
     (d/db conn))

(def data [{:question/key :hmsacld
            :question/answers [{:answer/value "1"
                                :answer/category "strongly agree"}
                               {:answer/value "2"
                                :answer/category "agree"}
                               {:answer/value "3"
                                :answer/category "disagree"}
                               {:answer/value "4"
                                :answer/category "strongly disagree"}]
            :question/title "Datalog is awesome"}])

@(d/transact conn data)

(d/q '[:find ?t ?av ?ac
       :where
       [?e :question/key :hmsacld]
       [?e :question/title ?t]
       [?e :question/answers ?a]
       [?a :answer/value ?av]
       [?a :answer/category ?ac]]
     (d/db conn))
;;=> #{["Datalog is awesome" "1" "strongly agree"] ["Datalog is awesome" "3" "disagree"] ["Datalog is awesome" "2" "agree"] ["Datalog is awesome" "4" "strongly disagree"]}

(require '[datalevin.core :as dl])

(def dl-db-uri "/tmp/datalevin/hello")
(def dl-conn (dl/get-conn dl-db-uri))

@(dl/transact dl-conn schema)
@(dl/transact dl-conn data)

;; But it was transacted successfully
(dl/q '[:find ?t ?av ?ac
        :where
        [?e :question/key :hmsacld]
        [?e :question/title ?t]
        [?e :question/answers ?a]
        [?a :answer/value ?av]
        [?a :answer/category ?ac]]
      (dl/db dl-conn))
;;=> #{}

;; But something was transacted successfully
(dl/q '[:find ?t
        :where
        [?e :question/key :hmsacld]
        [?e :question/title ?t]
        ;; [?e :question/answers ?a]
        ;; [?a :answer/value ?av]
        ;; [?a :answer/category ?ac]
        ]
      (dl/db dl-conn))
;;=> #{["Datalog is awesome"]}
But Iโ€™m pretty sure itโ€™s supported because simongray wrote about it in another slack channel.

2024-11-22T18:32:39.142209Z

Absolutely no urgency here, just getting to know a bit of Datalog. And great job creating datalevin, thank you ๐Ÿ™

Huahai 2024-11-22T20:52:03.723729Z

Likely a query bug, could you file an issue? I will take a look

โœ… 1
2024-11-22T21:09:03.599129Z

Done, thanks!

๐Ÿ‘ 1
Huahai 2024-11-22T21:50:58.051989Z

A few things: Datalevin's schema is just a map of maps. There's also no need to transact them.

Huahai 2024-11-22T21:51:31.247169Z

So Datomic's handling of schema is very different from Datalevin.

Huahai 2024-11-22T21:52:06.017299Z

Also, you query contains a clause that matches nothing. :question/key doesn't exist in the data.

Huahai 2024-11-22T21:52:18.984429Z

In any case, it works fine after fixing these.

2024-11-23T05:04:29.734679Z

Oh thanks a lot for the education! The missing :question/key data happened when creating the issue, it was present in the initial test code, sorry for that!

2024-11-23T05:22:28.202259Z

Works now, thanks so much for your help!

2024-11-23T05:35:14.574599Z

I did try it before with the schema as a map of maps. But I tried to transact that schema, which errored because I was trying to transact a map. Then I just refactored it to a datomic like schema sequence and transacted that one. Would you be open to a very small PR mentioning this small difference in the Readme (last 4 lines):

;; Define an optional schema.
;; Note that pre-defined schema is optional, as Datalevin does schema-on-write.
;; However, attributes requiring special handling need to be defined in schema,
;; e.g. many cardinality, uniqueness constraint, reference type, and so on.
;; Differences to Datomic: 
;; - The schema must be a map of maps, not a vector of maps.
;; - It is not `transact`ed into the db but passed on when acquiring connections.
;; - use `update-schema` to update the schema of an open connection to a database.
Or feel free to just use it if you think itโ€™s useful or ignore if itโ€™s too noob.

2024-11-24T08:27:06.682129Z

Done https://github.com/juji-io/datalevin/pull/297

๐Ÿ™ 1
Huahai 2024-11-25T06:05:51.794199Z

Merged.

๐Ÿ™ 1
Jeremy 2024-11-22T20:53:17.406239Z

I keep getting the below error. Is there any reason why load and sync aren't excluded with refer-clojure?

WARNING: sync already refers to: #'clojure.core/sync in namespace: datalevin.core, being replaced by: #'datalevin.core/sync
WARNING: load already refers to: #'clojure.core/load in namespace: datalevin.core, being replaced by: #'datalevin.core/load
It's not a big deal; I'm just wondering. ty

Jeremy 2024-11-23T09:12:46.523729Z

Thanks. I've upgraded. I was previously on v0.9.11

๐Ÿ‘ 1
Huahai 2024-11-22T21:12:15.730689Z

They are excluded.

Huahai 2024-11-22T21:16:13.867199Z

maybe you are on an older version?