Skip to content

Commit

Permalink
Moved db-with to datascript.conn
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Oct 24, 2023
1 parent 296881c commit 16af9ad
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
36 changes: 22 additions & 14 deletions src/datascript/conn.cljc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(ns datascript.conn
(:require
[datascript.db :as db #?@(:cljs [:refer [FilteredDB]])]
[datascript.db :as db #?@(:cljs [:refer [DB FilteredDB]])]
[datascript.storage :as storage]
[me.tonsky.persistent-sorted-set :as set])
#?(:clj
(:import
[datascript.db FilteredDB])))
[datascript.db DB FilteredDB])))

(defn with
([db tx-data] (with db tx-data nil))
Expand All @@ -15,22 +15,29 @@
(throw (ex-info "Filtered DB cannot be modified" {:error :transaction/filtered}))
(db/transact-tx-data (db/->TxReport db db [] {} tx-meta) tx-data))))

(defn ^DB db-with
"Applies transaction to an immutable db value, returning new immutable db value. Same as `(:db-after (with db tx-data))`."
[db tx-data]
{:pre [(db/db? db)]}
(:db-after (with db tx-data)))

(defn conn? [conn]
(and #?(:clj (instance? clojure.lang.IDeref conn)
:cljs (satisfies? cljs.core/IDeref conn))
(and
#?(:clj (instance? clojure.lang.IDeref conn)
:cljs (satisfies? cljs.core/IDeref conn))
(db/db? @conn)))

(defn conn-from-db [db]
{:pre [(db/db? db)]}
(if-some [storage (storage/storage db)]
(do
(storage/store db)
(atom db
:meta {:listeners (atom {})
:tx-tail (atom [])
:db-last-stored (atom db)}))
(atom db
:meta {:listeners (atom {})})))
(let [storage (storage/storage db)
meta (cond-> {:listeners (atom {})
:clients (atom {})}
storage
(merge {:tx-tail (atom [])
:db-last-stored (atom db)}))]
(when storage
(storage/store db))
(atom db :meta meta)))

(defn conn-from-datoms
([datoms]
Expand Down Expand Up @@ -85,7 +92,8 @@
@*report))

(defn transact!
([conn tx-data] (transact! conn tx-data nil))
([conn tx-data]
(transact! conn tx-data nil))
([conn tx-data tx-meta]
{:pre [(conn? conn)]}
(let [report (-transact! conn tx-data tx-meta)]
Expand Down
6 changes: 2 additions & 4 deletions src/datascript/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,9 @@
"Same as [[transact!]], but applies to an immutable database value. Returns transaction report (see [[transact!]])."
conn/with)

(defn ^DB db-with
(def ^{:arglists '([db tx-data]) :tag DB} db-with
"Applies transaction to an immutable db value, returning new immutable db value. Same as `(:db-after (with db tx-data))`."
[db tx-data]
{:pre [(db/db? db)]}
(:db-after (with db tx-data)))
conn/db-with)

(defn ^DB with-schema
"Warning! No validation or conversion. Only change schema in a compatible way"
Expand Down

0 comments on commit 16af9ad

Please sign in to comment.