1
0
Эх сурвалжийг харах

fix: db/id enhancement failing non-tx validation contexts

Simplify addition of :db/id and add it without forcing adding :db/id
in all schema declarations
Gabriel Horner 1 жил өмнө
parent
commit
6b0b9c5e18

+ 7 - 10
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -139,17 +139,14 @@
   [datoms & {:keys [entity-fn]}]
   (let [ent-maps
         (reduce (fn [acc {:keys [a e v]}]
-                  (let [acc (if (get-in acc [e :db/id])
-                              acc
-                              (assoc-in acc [e :db/id] e))]
-                    (if (contains? db-schema/card-many-attributes a)
-                     (update acc e update a (fnil conj #{}) v)
+                  (if (contains? db-schema/card-many-attributes a)
+                    (update acc e update a (fnil conj #{}) v)
                     ;; If there's already a val, don't clobber it and automatically start collecting it as a :many
-                     (if-let [existing-val (get-in acc [e a])]
-                       (if (set? existing-val)
-                         (update acc e assoc a (conj existing-val v))
-                         (update acc e assoc a #{existing-val v}))
-                       (update acc e assoc a v)))))
+                    (if-let [existing-val (get-in acc [e a])]
+                      (if (set? existing-val)
+                        (update acc e assoc a (conj existing-val v))
+                        (update acc e assoc a #{existing-val v}))
+                      (update acc e assoc a v))))
                 {}
                 datoms)
         entity-fn' (or entity-fn

+ 9 - 4
deps/db/src/logseq/db/frontend/validate.cljs

@@ -21,12 +21,17 @@
   boolean indicating if db is valid"
   [{:keys [db-after tx-data tx-meta]} validate-options]
   (let [changed-ids (->> tx-data (map :e) distinct)
-        ent-maps* (-> (mapcat #(d/datoms db-after :eavt %) changed-ids)
-                      (db-malli-schema/datoms->entity-maps {:entity-fn #(d/entity db-after %)})
-                      vals)
+        tx-datoms (mapcat #(d/datoms db-after :eavt %) changed-ids)
+        ent-maps* (map (fn [[db-id m]]
+                         ;; Add :db/id for debugging
+                         (assoc m :db/id db-id))
+                       (db-malli-schema/datoms->entity-maps tx-datoms {:entity-fn #(d/entity db-after %)}))
         ent-maps (db-malli-schema/update-properties-in-ents db-after ent-maps*)
         db-schema (update-schema db-malli-schema/DB db-after validate-options)
-        invalid-ent-maps (remove #(m/validate db-schema [%]) ent-maps)]
+        invalid-ent-maps (remove
+                          ;; remove :db/id as it adds needless declarations to schema
+                          #(m/validate db-schema [(dissoc % :db/id)])
+                          ent-maps)]
     (js/console.log "changed eids:" changed-ids tx-meta)
     (if (seq invalid-ent-maps)
       (do