datascript

s-ol 2023-06-04T04:28:27.656349Z

Hey everyone, I'm pretty new to datomic/datascript but we've been getting along quite well over the last couple of days ;) The one thing that I'm still trying to figure out is how to get structured data into the DB easily. :find (pull) is great for getting the data into the shape I need, but how do I deal with the reverse? I find that in some contexts I need to generate a "subgraph" that goes 3-4 links deep, with one-to-many Connections at some levels. This structure is easy to build up using all the regular clojure tools, but when I'm ready to put it into the DB it gets messy: I need to reach deep into the data to lift some references out and turn others into lookup refs. How do you deal with this?

s-ol 2023-06-04T10:15:09.961929Z

On a somewhat related note, I'm getting this error when doing a transaction with nested (1 level) entities: Cannot store nil as a value at {:db/id -1, :moveset/moves nil} (but :moveset/moves is not nil but a listf of nested entities). Is this a bug or am I doing something wrong? (Repro in thread)

Niki 2023-06-05T13:02:28.680859Z

Actually it should work

Niki 2023-06-05T13:02:48.104789Z

what if you make moveset/moves a vector, not a list?

Niki 2023-06-05T13:03:01.639879Z

right now I have a feeling it interprets it as a function call

s-ol 2023-06-06T14:14:15.585399Z

ah, that might be the case. I didn't pay much attention to the collection types and that was the result of a (map …) somewhere

s-ol 2023-06-04T10:15:33.720879Z

(ns toast
  (:require [datascript.core :as d]
            [clojure.pprint :refer [pprint]]))


(def schema {:account/name            {:db/cardinality :db.cardinality/one
                                       :db/unique :db.unique/identity}
             :move/account            {:db/valueType :db.type/ref
                                       :db/cardinality :db.cardinality/one}
             :move/amount             {;:db/valueType :db.type/double
                                       :db/cardinality :db.cardinality/one}

             :moveset/moves           {:db/valueType :db.type/ref
                                       :db/cardinality :db.cardinality/many
                                       :db/isComponent true}
             :moveset/virtual         {;:db/valueType :db.type/boolean
                                       :db/cardinality :db.cardinality/one}
             :moveset/imported-tx     {:db/valueType :db.type/ref
                                       :db/cardinality :db.cardinality/one}

             :tx/description          {;:db/valueType :db.type/string
                                        :db/cardinality :db.cardinality/one}
             :tx/value-time           {};:db/valueType :db.type/instant}
             :tx/movesets-pos         {:db/valueType :db.type/ref
                                       :db/cardinality :db.cardinality/many
                                       :db/unique :db.unique/identity}
             :tx/movesets-neg         {:db/valueType :db.type/ref
                                       :db/cardinality :db.cardinality/many
                                       :db/unique :db.unique/identity}})

(def !conn (d/create-conn schema))

(d/transact! !conn
   [{:account/name :test}
    {:account/name :toast}])

(d/transact! !conn
  [{:db/id -1,
    :moveset/moves
    (#:move{:account [:account/name :test], :delta -39.65M}
     #:move{:account [:account/name :toast], :delta 39.65M})}
   {:db/id -2,
    :moveset/virtual true,
    :moveset/moves
    (#:move{:account [:account/name :test], :delta 39.65M}
     #:move{:account [:account/name :toast], :delta -39.65M})}
   {:db/id -3,
    :tx/description "Addebito SDD",
    :tx/value-time "2023-05-05T00:00:00Z"}
   [:db/add -3 :tx/movesets-neg -1]
   [:db/add -3 :tx/movesets-pos -2]])

(pprint @!conn)