Skip to content

Commit a082b99

Browse files
committed
list automatic ids in tempid report
Fix #246
1 parent 41bb33e commit a082b99

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/datascript/db.cljc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,10 +870,13 @@
870870
[x]
871871
(or (and (number? x) (neg? x)) (string? x)))
872872

873+
(defn- new-eid? [db eid]
874+
(and (> eid (:max-eid db))
875+
(< eid tx0))) ;; tx0 is max eid
876+
873877
(defn- advance-max-eid [db eid]
874878
(cond-> db
875-
(and (> eid (:max-eid db))
876-
(< eid tx0)) ;; do not trigger advance if transaction id was referenced
879+
(new-eid? db eid)
877880
(assoc :max-eid eid)))
878881

879882
(defn- allocate-eid
@@ -885,6 +888,9 @@
885888
(assoc-in [:tempids e] eid)
886889
(tempid? e)
887890
(assoc-in [:tempids e] eid)
891+
(and (not (tempid? e))
892+
(new-eid? (:db-after report) eid))
893+
(assoc-in [:tempids eid] eid)
888894
true
889895
(update-in [:db-after] advance-max-eid eid))))
890896

test/datascript/test/transact.cljc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@
277277
:where [?e :name ?n]
278278
[?e :friend ?fe]
279279
[?fe :name ?fn]]]
280-
(is (= (:tempids tx) { -1 2, -2 3, "B" 4, -3 5, :db/current-tx (+ d/tx0 1) }))
280+
(is (= (:tempids tx) { 1 1, -1 2, -2 3, "B" 4, -3 5, :db/current-tx (+ d/tx0 1) }))
281281
(is (= (d/q q @conn "Sergey") #{["Ivan"] ["Petr"]}))
282282
(is (= (d/q q @conn "Boris") #{["Oleg"]}))
283283
(is (= (d/q q @conn "Oleg") #{["Boris"]}))))
@@ -298,7 +298,7 @@
298298
[(+ d/tx0 1) :prop2 "prop2"]
299299
[2 :name "Y"]
300300
[2 :created-at (+ d/tx0 1)]}))
301-
(is (= (:tempids tx1) (assoc {-1 2, :db/current-tx (+ d/tx0 1)}
301+
(is (= (:tempids tx1) (assoc {1 1, -1 2, :db/current-tx (+ d/tx0 1)}
302302
tx-tempid (+ d/tx0 1))))
303303
(let [tx2 (d/transact! conn [[:db/add tx-tempid :prop3 "prop3"]])
304304
tx-id (get-in tx2 [:tempids tx-tempid])]

test/datascript/test/upsert.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
(is (= (touched tx 3)
114114
{:name "Igor" :age 36}))
115115
(is (= (tempids tx)
116-
{}))))
116+
{3 3}))))
117117

118118
(testing "upsert over intermediate db, tempids"
119119
(let [tx (d/with db [{:db/id -1 :name "Igor" :age 35}

0 commit comments

Comments
 (0)