datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
lambdam 2020-12-01T08:51:28.074700Z

Humm, I have another question that comes then. When I take a subset of information from Datomic, I can have relations between entities that cannot be represented with tree style imbricated data structures (JSON style), like in this kind of relations: https://lambdam.com/images/2020-08-er-diagram.svg Would a solution be to walk through the whole data and replace the db ids by lookup refs, like so:

:book/categories [17592186048456 17592186048457 17592186048458]
;; to
:book/categories [[:entity/id #uuid "6743167e-e1b1-4cd6-9d37-7e7b8db5dd53"] [:entity/id #uuid "052d8b26-e175-43b0-be23-d660deb5376b"] [:entity/id #uuid "42cf0a5c-f3c1-4216-8d12-365b7aede4ce"]]
I didn't try it yet but I don't know if it is a well known case that has a well known solution.

lambdam 2020-12-01T10:31:14.075200Z

Well, this works:

(def ds-schema
  {:entity/id {;; :db/valueType :db.type/uuid
               :db/unique :db.unique/identity}
   :user/friends {:db/valueType :db.type/ref
                  :db/cardinality :db.cardinality/many}})

(def conn* (ds/create-conn ds-schema))

(def uid1 (java.util.UUID/randomUUID))
(def uid2 (java.util.UUID/randomUUID))

(ds/transact! conn*
              [{:entity/id uid1
                :user/name "Foo"}
               {:entity/id uid2
                :user/name "Bar"
                :user/friends [[:entity/id uid1]]}])

(ds/pull @conn*
         '[* {:user/friends [*]}]
         [:entity/id uid2])

lambdam 2020-12-01T10:33:59.076400Z

But that means stripping always all :db/id fields when data goes back and forth between front and back :thinking_face: ?!

pithyless 2020-12-01T11:24:03.078Z

If you're trying to do a 1:1 sync from Datomic to DataScript, you may want to check out https://github.com/metasoarous/datsync - and either use it directly, or at least study how it solves these problems.

lambdam 2020-12-01T12:28:51.080600Z

Thanks @pithyless, That is an overcomplicated lib for my needs. The Datomic and the Datascript are on the same server. I didn't choose a classical webapp architecture for my current project. I'll take a look at the id translation part of the code of Datsync. I saw this project few years ago but forgot about it. Thanks again.