refactor(db): move fns from base to utils

This commit is contained in:
defclass
2020-12-17 15:24:50 +08:00
committed by Tienson Qin
parent 6ad34f5b3f
commit ba9eaf2dc6
7 changed files with 154 additions and 143 deletions

View File

@@ -24,24 +24,43 @@
remove-files-db!
get-conn
get-files-conn
me-tx
remove-conn!]
[frontend.db.utils
date->int db->json db->string get-max-tx-id get-tx-id
group-by-page me-tx seq-flatten sort-by-pos
string->db with-repo]
group-by-page seq-flatten sort-by-pos
string->db with-repo
entity pull pull-many transact! get-key-value]
[frontend.db.model
add-properties! block-and-children-transform blocks-count blocks-count-cache clean-export! cloned? delete-blocks delete-file! delete-file-blocks! delete-file-pages! delete-file-tx delete-files delete-pages-by-files filter-only-public-pages-and-blocks get-alias-page get-all-block-contents get-all-tagged-pages get-all-tags get-all-templates get-block-and-children get-block-and-children-no-cache get-block-by-uuid get-block-children get-block-children-ids get-block-content get-block-file get-block-immediate-children get-block-page get-block-page-end-pos get-block-parent get-block-parents get-block-referenced-blocks get-block-refs-count get-blocks-by-priority get-blocks-contents get-collapsed-blocks get-config get-custom-css get-date-scheduled-or-deadlines get-db-type get-empty-pages get-file get-file-after-blocks get-file-after-blocks-meta get-file-blocks get-file-contents get-file-last-modified-at get-file-no-sub get-file-page get-file-page-id get-file-pages get-files get-files-blocks get-files-full get-files-that-referenced-page get-journals-length get-latest-journals get-marker-blocks get-matched-blocks get-page get-page-alias get-page-alias-names get-page-blocks get-page-blocks-count get-page-blocks-no-cache get-page-file get-page-format get-page-name get-page-properties get-page-properties-content get-page-referenced-blocks get-page-referenced-pages get-page-unlinked-references get-pages get-pages-relation get-pages-that-mentioned-page get-pages-with-modified-at get-public-pages get-tag-pages journal-page? local-native-fs? mark-repo-as-cloned! page-alias-set page-blocks-transform pull-block rebuild-page-blocks-children reset-config! set-file-last-modified-at! sub-key-value template-exists? transact-files-db! with-block-refs-count]
add-properties! block-and-children-transform blocks-count blocks-count-cache clean-export! cloned? delete-blocks
delete-file! delete-file-blocks! delete-file-pages! delete-file-tx delete-files delete-pages-by-files
filter-only-public-pages-and-blocks get-alias-page get-all-block-contents get-all-tagged-pages get-all-tags
get-all-templates get-block-and-children get-block-and-children-no-cache get-block-by-uuid get-block-children
get-block-children-ids get-block-content get-block-file get-block-immediate-children get-block-page
get-block-page-end-pos get-block-parent get-block-parents get-block-referenced-blocks get-block-refs-count
get-blocks-by-priority get-blocks-contents get-collapsed-blocks get-config get-custom-css
get-date-scheduled-or-deadlines get-db-type get-empty-pages get-file get-file-after-blocks get-file-after-blocks-meta
get-file-blocks get-file-contents get-file-last-modified-at get-file-no-sub get-file-page get-file-page-id
get-file-pages get-files get-files-blocks get-files-full get-files-that-referenced-page get-journals-length
get-latest-journals get-marker-blocks get-matched-blocks get-page get-page-alias get-page-alias-names get-page-blocks
get-page-blocks-count get-page-blocks-no-cache get-page-file get-page-format get-page-name get-page-properties
get-page-properties-content get-page-referenced-blocks get-page-referenced-pages get-page-unlinked-references
get-pages get-pages-relation get-pages-that-mentioned-page get-pages-with-modified-at get-public-pages get-tag-pages
journal-page? local-native-fs? mark-repo-as-cloned! page-alias-set page-blocks-transform pull-block
rebuild-page-blocks-children reset-config! set-file-last-modified-at! sub-key-value template-exists?
transact-files-db! with-block-refs-count]
[frontend.db.react
get-current-marker get-current-page get-current-priority get-handler-keys set-file-content! set-key-value transact-react! remove-key! remove-q! remove-query-component! add-q! add-query-component! clear-query-state! clear-query-state-without-refs-and-embeds! get-block-blocks-cache-atom get-page-blocks-cache-atom kv q query-state query-components query-entity-in-component remove-custom-query! set-new-result!]
get-current-marker get-current-page get-current-priority get-handler-keys set-file-content! set-key-value
transact-react! remove-key! remove-q! remove-query-component! add-q! add-query-component! clear-query-state!
clear-query-state-without-refs-and-embeds! get-block-blocks-cache-atom get-page-blocks-cache-atom kv q
query-state query-components query-entity-in-component remove-custom-query! set-new-result!]
[frontend.db.query-custom
custom-query custom-query-result-transform]
[frontend.db.base
entity pull pull-many transact! get-key-value])
custom-query custom-query-result-transform])
;; persisting DBs between page reloads
(defn persist! [repo]

View File

@@ -1,59 +0,0 @@
(ns frontend.db.base
"Base query utils are required by model.cljs and react.cljs"
(:require [frontend.state :as state]
[frontend.db.conn :as conn]
[datascript.core :as d]
[frontend.config :as config]
[frontend.util :as util]))
(defn entity
([id-or-lookup-ref]
(entity (state/get-current-repo) id-or-lookup-ref))
([repo id-or-lookup-ref]
(when-let [db (conn/get-conn repo)]
(d/entity db id-or-lookup-ref))))
(defn pull
([eid]
(pull (state/get-current-repo) '[*] eid))
([selector eid]
(pull (state/get-current-repo) selector eid))
([repo selector eid]
(when-let [conn (conn/get-conn repo)]
(try
(d/pull conn
selector
eid)
(catch js/Error e
nil)))))
(defn pull-many
([eids]
(pull-many '[*] eids))
([selector eids]
(pull-many (state/get-current-repo) selector eids))
([repo selector eids]
(when-let [conn (conn/get-conn repo)]
(try
(d/pull-many conn selector eids)
(catch js/Error e
(js/console.error e))))))
(defn transact!
([tx-data]
(transact! (state/get-current-repo) tx-data))
([repo-url tx-data]
(when-not config/publishing?
(let [tx-data (->> (util/remove-nils tx-data)
(remove nil?))]
(when (seq tx-data)
(when-let [conn (conn/get-conn repo-url false)]
(d/transact! conn (vec tx-data))))))))
(defn get-key-value
([key]
(get-key-value (state/get-current-repo) key))
([repo-url key]
(when-let [db (conn/get-conn repo-url)]
(some-> (d/entity db key)
key))))

View File

@@ -6,8 +6,7 @@
[frontend.state :as state]
[frontend.config :as config]
[frontend.idb :as idb]
[datascript.core :as d]
[frontend.db.utils :as db-utils]))
[datascript.core :as d]))
(defonce conns (atom {}))
@@ -64,6 +63,12 @@
(swap! conns dissoc (datascript-db repo))
(swap! conns dissoc (datascript-files-db repo)))
(defn me-tx
[db {:keys [name email avatar]}]
(util/remove-nils {:me/name name
:me/email email
:me/avatar avatar}))
(defn start!
([me repo]
(start! me repo {}))
@@ -78,6 +83,6 @@
db-type
(assoc :db/type db-type))])
(when me
(d/transact! db-conn [(db-utils/me-tx (d/db db-conn) me)]))
(d/transact! db-conn [(me-tx (d/db db-conn) me)]))
(when listen-handler (listen-handler repo)))))

View File

@@ -1,12 +1,11 @@
(ns frontend.db.debug
(:require [medley.core :as medley]
[frontend.db.model :as model]
[frontend.db.base :as base]))
[frontend.db.utils :as db-utils]))
;; shortcut for query a block with string ref
(defn qb
[string-id]
(base/pull [:block/uuid (medley/uuid string-id)]))
(db-utils/pull [:block/uuid (medley/uuid string-id)]))
(comment
(defn debug!

View File

@@ -17,15 +17,14 @@
[cljs-time.core :as t]
[cljs-time.coerce :as tc]
[frontend.util :as util :refer [react] :refer-macros [profile]]
[frontend.db-schema :as db-schema]
[frontend.db.base :as base]))
[frontend.db-schema :as db-schema]))
;; TODO: extract to specific models and move data transform logic to the
;; correponding handlers.
(defn transact-files-db!
([tx-data]
(base/transact! (state/get-current-repo) tx-data))
(db-utils/transact! (state/get-current-repo) tx-data))
([repo-url tx-data]
(when-not config/publishing?
(let [tx-data (->> (util/remove-nils tx-data)
@@ -50,7 +49,7 @@
(when (conn/get-conn repo)
(->
(react/q repo [:blocks id] {}
'[:find (base/pull ?block [*])
'[:find (db-utils/pull ?block [*])
:in $ ?id
:where
[?block :block/uuid ?id]]
@@ -151,7 +150,7 @@
[repo page-name]
(let [alias-ids (get-page-alias repo page-name)]
(when (seq alias-ids)
(->> (base/pull-many repo '[:page/name] alias-ids)
(->> (db-utils/pull-many repo '[:page/name] alias-ids)
(map :page/name)
distinct))))
@@ -197,7 +196,7 @@
(when end-pos
(let [pred (fn [db meta]
(>= (:start-pos meta) end-pos))]
(-> (d/q '[:find (base/pull ?block [*])
(-> (d/q '[:find (db-utils/pull ?block [*])
:in $ ?file-id ?pred
:where
[?block :block/file ?file-id]
@@ -217,7 +216,7 @@
ks (if content-level?
'[:block/uuid :block/meta :block/content :block/level]
'[:block/uuid :block/meta])
blocks (base/pull-many repo-url ks eids)]
blocks (db-utils/pull-many repo-url ks eids)]
(->> (filter (fn [{:block/keys [meta]}]
(>= (:start-pos meta) end-pos)) blocks)
db-utils/sort-by-pos))))
@@ -282,7 +281,7 @@
(when-let [conn (conn/get-files-conn repo)]
(->>
(d/q
'[:find (base/pull ?file [*])
'[:find (db-utils/pull ?file [*])
:where
[?file :file/path]]
@conn)
@@ -311,17 +310,17 @@
(defn get-block-by-uuid
[uuid]
(base/entity [:block/uuid uuid]))
(db-utils/entity [:block/uuid uuid]))
(defn get-page-format
[page-name]
(when-let [file (:page/file (base/entity [:page/name page-name]))]
(when-let [path (:file/path (base/entity (:db/id file)))]
(when-let [file (:page/file (db-utils/entity [:page/name page-name]))]
(when-let [path (:file/path (db-utils/entity (:db/id file)))]
(format/get-format path))))
(defn page-alias-set
[repo-url page]
(when-let [page-id (:db/id (base/entity [:page/name page]))]
(when-let [page-id (:db/id (db-utils/entity [:page/name page]))]
(let [aliases (get-page-alias repo-url page)
aliases (if (seq aliases)
(set
@@ -360,7 +359,7 @@
(defn sort-blocks
[blocks]
(let [pages-ids (map (comp :db/id :block/page) blocks)
pages (base/pull-many '[:db/id :page/last-modified-at :page/name :page/original-name] pages-ids)
pages (db-utils/pull-many '[:db/id :page/last-modified-at :page/name :page/original-name] pages-ids)
pages-map (reduce (fn [acc p] (assoc acc (:db/id p) p)) {} pages)
blocks (map
(fn [block]
@@ -375,7 +374,7 @@
(some->>
(react/q repo-url [:marker/blocks marker]
{:use-cache? true}
'[:find (base/pull ?h [*])
'[:find (db-utils/pull ?h [*])
:in $ ?marker
:where
[?h :block/marker ?m]
@@ -391,7 +390,7 @@
(defn get-page-properties
[page]
(when-let [page (base/entity [:page/name page])]
(when-let [page (db-utils/entity [:page/name page])]
(:page/properties page)))
(defn add-properties!
@@ -443,8 +442,8 @@
:or {use-cache? true
pull-keys '[*]}}]
(let [page (string/lower-case page)
page-id (or (:db/id (base/entity repo-url [:page/name page]))
(:db/id (base/entity repo-url [:page/original-name page])))
page-id (or (:db/id (db-utils/entity repo-url [:page/name page]))
(:db/id (db-utils/entity repo-url [:page/original-name page])))
db (conn/get-conn repo-url)]
(when page-id
(some->
@@ -454,7 +453,7 @@
:query-fn (fn [db]
(let [datoms (d/datoms db :avet :block/page page-id)
block-eids (mapv :e datoms)]
(base/pull-many repo-url pull-keys block-eids)))}
(db-utils/pull-many repo-url pull-keys block-eids)))}
nil)
react)))))
@@ -466,13 +465,13 @@
([repo-url page {:keys [pull-keys]
:or {pull-keys '[*]}}]
(let [page (string/lower-case page)
page-id (or (:db/id (base/entity repo-url [:page/name page]))
(:db/id (base/entity repo-url [:page/original-name page])))
page-id (or (:db/id (db-utils/entity repo-url [:page/name page]))
(:db/id (db-utils/entity repo-url [:page/original-name page])))
db (conn/get-conn repo-url)]
(when page-id
(let [datoms (d/datoms db :avet :block/page page-id)
block-eids (mapv :e datoms)]
(some->> (base/pull-many repo-url pull-keys block-eids)
(some->> (db-utils/pull-many repo-url pull-keys block-eids)
(page-blocks-transform repo-url)))))))
(defn get-page-blocks-count
@@ -500,18 +499,18 @@
(defn get-block-page
[repo block-id]
(when-let [block (base/entity repo [:block/uuid block-id])]
(base/entity repo (:db/id (:block/page block)))))
(when-let [block (db-utils/entity repo [:block/uuid block-id])]
(db-utils/entity repo (:db/id (:block/page block)))))
(defn get-block-page-end-pos
[repo page-name]
(or
(when-let [page-id (:db/id (base/entity repo [:page/name (string/lower-case page-name)]))]
(when-let [page-id (:db/id (db-utils/entity repo [:page/name (string/lower-case page-name)]))]
(when-let [db (conn/get-conn repo)]
(let [block-eids (->> (d/datoms db :avet :block/page page-id)
(mapv :e))]
(when (seq block-eids)
(let [blocks (base/pull-many repo '[:block/meta] block-eids)]
(let [blocks (db-utils/pull-many repo '[:block/meta] block-eids)]
(-> (last (db-utils/sort-by-pos blocks))
(get-in [:block/meta :end-pos])))))))
;; TODO: need more thoughts
@@ -522,7 +521,7 @@
(let [priority (string/capitalize priority)]
(when (conn/get-conn repo)
(->> (react/q repo [:priority/blocks priority] {}
'[:find (base/pull ?h [*])
'[:find (db-utils/pull ?h [*])
:in $ ?priority
:where
[?h :block/priority ?priority]]
@@ -568,7 +567,7 @@
(defn get-block-children-ids
[repo block-uuid]
(when-let [conn (conn/get-conn repo)]
(let [eid (:db/id (base/entity repo [:block/uuid block-uuid]))]
(let [eid (:db/id (db-utils/entity repo [:block/uuid block-uuid]))]
(->> (d/q
'[:find ?e1
:in $ ?e2 %
@@ -586,28 +585,28 @@
(defn get-block-immediate-children
[repo block-uuid]
(when-let [conn (conn/get-conn repo)]
(let [ids (->> (:block/children (base/entity repo [:block/uuid block-uuid]))
(let [ids (->> (:block/children (db-utils/entity repo [:block/uuid block-uuid]))
(map :db/id))]
(when (seq ids)
(base/pull-many repo '[*] ids)))))
(db-utils/pull-many repo '[*] ids)))))
(defn get-block-children
[repo block-uuid]
(when-let [conn (conn/get-conn repo)]
(let [ids (get-block-children-ids repo block-uuid)]
(when (seq ids)
(base/pull-many repo '[*] ids)))))
(db-utils/pull-many repo '[*] ids)))))
(defn get-block-and-children
([repo block-uuid]
(get-block-and-children repo block-uuid true))
([repo block-uuid use-cache?]
(let [block (base/entity repo [:block/uuid block-uuid])
(let [block (db-utils/entity repo [:block/uuid block-uuid])
page (:db/id (:block/page block))
pos (:start-pos (:block/meta block))
level (:block/level block)
pred (fn []
(let [block (base/entity repo [:block/uuid block-uuid])
(let [block (db-utils/entity repo [:block/uuid block-uuid])
pos (:start-pos (:block/meta block))]
(fn [data meta]
(>= (:start-pos meta) pos))))]
@@ -616,7 +615,7 @@
:transform-fn #(block-and-children-transform % repo block-uuid level)
:inputs-fn (fn []
[page (pred)])}
'[:find (base/pull ?block [*])
'[:find (db-utils/pull ?block [*])
:in $ ?page ?pred
:where
[?block :block/page ?page]
@@ -627,14 +626,14 @@
;; TODO: performance
(defn get-block-and-children-no-cache
[repo block-uuid]
(let [block (base/entity repo [:block/uuid block-uuid])
(let [block (db-utils/entity repo [:block/uuid block-uuid])
page (:db/id (:block/page block))
pos (:start-pos (:block/meta block))
level (:block/level block)
pred (fn [data meta]
(>= (:start-pos meta) pos))]
(-> (d/q
'[:find (base/pull ?block [*])
'[:find (db-utils/pull ?block [*])
:in $ ?page ?pred
:where
[?block :block/page ?page]
@@ -672,15 +671,15 @@
(defn get-page-file
[page-name]
(some-> (base/entity [:page/name page-name])
(some-> (db-utils/entity [:page/name page-name])
:page/file))
(defn get-block-file
[block-id]
(let [page-id (some-> (base/entity [:block/uuid block-id])
(let [page-id (some-> (db-utils/entity [:block/uuid block-id])
:block/page
:db/id)]
(:page/file (base/entity page-id))))
(:page/file (db-utils/entity page-id))))
(defn get-file-page-id
[file-path]
@@ -700,8 +699,8 @@
(defn get-page
[page-name]
(if (util/uuid-string? page-name)
(base/entity [:block/uuid (uuid page-name)])
(base/entity [:page/name page-name])))
(db-utils/entity [:block/uuid (uuid page-name)])
(db-utils/entity [:page/name page-name])))
(defn get-page-name
[file ast]
@@ -783,7 +782,7 @@
[repo page]
(when (conn/get-conn repo)
(let [pages (page-alias-set repo page)
page-id (:db/id (base/entity [:page/name page]))
page-id (:db/id (db-utils/entity [:page/name page]))
ref-pages (->> (react/q repo [:page/ref-pages page-id] {:use-cache? false}
'[:find ?ref-page-name
:in $ ?pages
@@ -837,7 +836,7 @@
(defn get-pages-that-mentioned-page
[repo page]
(when (conn/get-conn repo)
(let [page-id (:db/id (base/entity [:page/name page]))
(let [page-id (:db/id (db-utils/entity [:page/name page]))
pages (page-alias-set repo page)
mentioned-pages (->> (react/q repo [:page/mentioned-pages page-id] {:use-cache? false}
'[:find ?mentioned-page-name
@@ -857,10 +856,10 @@
[page]
(when-let [repo (state/get-current-repo)]
(when (conn/get-conn repo)
(let [page-id (:db/id (base/entity [:page/name page]))
(let [page-id (:db/id (db-utils/entity [:page/name page]))
pages (page-alias-set repo page)]
(->> (react/q repo [:page/refed-blocks page-id] {}
'[:find (base/pull ?block [*])
'[:find (db-utils/pull ?block [*])
:in $ ?pages
:where
[?block :block/ref-pages ?ref-page]
@@ -880,7 +879,7 @@
(when-let [repo (state/get-current-repo)]
(when-let [conn (conn/get-conn repo)]
(->> (react/q repo [:custom :scheduled-deadline journal-title] {}
'[:find (base/pull ?block [*])
'[:find (db-utils/pull ?block [*])
:in $ ?day
:where
(or
@@ -914,11 +913,11 @@
[page]
(when-let [repo (state/get-current-repo)]
(when-let [conn (conn/get-conn repo)]
(let [page-id (:db/id (base/entity [:page/name page]))
(let [page-id (:db/id (db-utils/entity [:page/name page]))
pages (page-alias-set repo page)
pattern (re-pattern (str "(?i)" page))]
(->> (d/q
'[:find (base/pull ?block [*])
'[:find (db-utils/pull ?block [*])
:in $ ?pattern
:where
[?block :block/content ?content]
@@ -941,7 +940,7 @@
(when-let [repo (state/get-current-repo)]
(when (conn/get-conn repo)
(->> (react/q repo [:block/refed-blocks block-uuid] {}
'[:find (base/pull ?ref-block [*])
'[:find (db-utils/pull ?ref-block [*])
:in $ ?block-uuid
:where
[?block :block/uuid ?block-uuid]
@@ -967,7 +966,7 @@
pred)
(take limit)
db-utils/seq-flatten
(base/pull-many '[:block/uuid
(db-utils/pull-many '[:block/uuid
:block/content
:block/properties
:block/format
@@ -977,16 +976,16 @@
(defn get-blocks-contents
[repo block-uuids]
(let [db (conn/get-conn repo)]
(base/pull-many repo '[:block/content]
(db-utils/pull-many repo '[:block/content]
(mapv (fn [id] [:block/uuid id]) block-uuids))))
(defn journal-page?
[page-name]
(:page/journal? (base/entity [:page/name page-name])))
(:page/journal? (db-utils/entity [:page/name page-name])))
(defn mark-repo-as-cloned!
[repo-url]
(base/transact!
(db-utils/transact!
[{:repo/url repo-url
:repo/cloned? true}]))
@@ -1021,7 +1020,7 @@
(defn get-db-type
[repo]
(base/get-key-value repo :db/type))
(db-utils/get-key-value repo :db/type))
(defn local-native-fs?
[repo]
@@ -1140,7 +1139,7 @@
(->> (d/datoms conn :avet :block/uuid)
(map :v)
(map (fn [id]
(let [e (base/entity [:block/uuid id])]
(let [e (db-utils/entity [:block/uuid id])]
{:db/id (:db/id e)
:block/uuid id
:block/content (:block/content e)
@@ -1159,7 +1158,7 @@
(defn filter-only-public-pages-and-blocks
[db]
(let [public-pages (get-public-pages db)
contents-id (:db/id (base/entity [:page/name "contents"]))]
contents-id (:db/id (db-utils/entity [:page/name "contents"]))]
(when (seq public-pages)
(let [public-pages (set (conj public-pages contents-id))
page-or-block? #(contains? #{"page" "block" "me" "recent" "file"} %)
@@ -1206,7 +1205,7 @@
(defn delete-file!
[repo-url file-path]
(base/transact! repo-url (delete-file-tx repo-url file-path)))
(db-utils/transact! repo-url (delete-file-tx repo-url file-path)))
(defn delete-pages-by-files
[files]

View File

@@ -5,14 +5,14 @@
solution.
"
(:require [frontend.db.conn :as conn]
[frontend.db.base :as base]
[frontend.state :as state]
[frontend.date :as date]
[frontend.util :as util :refer-macros [profile] :refer [react]]
[clojure.string :as string]
[frontend.config :as config]
[datascript.core :as d]
[lambdaisland.glogi :as log]))
[lambdaisland.glogi :as log]
[frontend.db.utils :as db-utils]))
;; Query atom of map of Key ([repo q inputs]) -> atom
;; TODO: replace with LRUCache, only keep the latest 20 or 50 items?
@@ -39,7 +39,7 @@
(defn remove-key!
[repo-url key]
(base/transact! repo-url [[:db.fn/retractEntity [:db/ident key]]])
(db-utils/transact! repo-url [[:db.fn/retractEntity [:db/ident key]]])
(set-new-result! [repo-url :kv key] nil))
(defn clear-query-state!
@@ -106,7 +106,7 @@
(defn query-entity-in-component
([id-or-lookup-ref]
(base/entity (state/get-current-repo) id-or-lookup-ref))
(db-utils/entity (state/get-current-repo) id-or-lookup-ref))
([repo id-or-lookup-ref]
(let [k [:entity id-or-lookup-ref]
result-atom (:result (get @query-state k))]
@@ -178,7 +178,7 @@
(date/journal-name))]
(when page
(let [page-name (util/url-decode (string/lower-case page))]
(base/entity (if tag?
(db-utils/entity (if tag?
[:tag/name page-name]
[:page/name page-name]))))))
@@ -245,7 +245,7 @@
(apply concat
(for [{:block/keys [ref-pages]} blocks]
(map (fn [page]
(when-let [page (base/entity [:page/name (:page/name page)])]
(when-let [page (db-utils/entity [:page/name (:page/name page)])]
[:page/refed-blocks (:db/id page)]))
ref-pages)))
@@ -316,7 +316,7 @@
(apply d/q query db inputs))
(keyword? query)
(base/get-key-value repo-url query)
(db-utils/get-key-value repo-url query)
(seq inputs)
(apply d/q query db inputs)

View File

@@ -5,7 +5,9 @@
[clojure.string :as string]
[datascript.transit :as dt]
[frontend.util :as util]
[frontend.date :as date]))
[frontend.date :as date]
[frontend.db.conn :as conn]
[frontend.config :as config]))
;; transit serialization
@@ -21,12 +23,6 @@
(defn string->db [s]
(dt/read-transit-str s))
(defn me-tx
[db {:keys [name email avatar]}]
(util/remove-nils {:me/name name
:me/email email
:me/avatar avatar}))
(defn seq-flatten [col]
(flatten (seq col)))
@@ -59,3 +55,55 @@
(map (fn [block]
(assoc block :block/repo repo))
blocks))
(defn entity
([id-or-lookup-ref]
(entity (state/get-current-repo) id-or-lookup-ref))
([repo id-or-lookup-ref]
(when-let [db (conn/get-conn repo)]
(d/entity db id-or-lookup-ref))))
(defn pull
([eid]
(pull (state/get-current-repo) '[*] eid))
([selector eid]
(pull (state/get-current-repo) selector eid))
([repo selector eid]
(when-let [conn (conn/get-conn repo)]
(try
(d/pull conn
selector
eid)
(catch js/Error e
nil)))))
(defn pull-many
([eids]
(pull-many '[*] eids))
([selector eids]
(pull-many (state/get-current-repo) selector eids))
([repo selector eids]
(when-let [conn (conn/get-conn repo)]
(try
(d/pull-many conn selector eids)
(catch js/Error e
(js/console.error e))))))
(defn transact!
([tx-data]
(transact! (state/get-current-repo) tx-data))
([repo-url tx-data]
(when-not config/publishing?
(let [tx-data (->> (util/remove-nils tx-data)
(remove nil?))]
(when (seq tx-data)
(when-let [conn (conn/get-conn repo-url false)]
(d/transact! conn (vec tx-data))))))))
(defn get-key-value
([key]
(get-key-value (state/get-current-repo) key))
([repo-url key]
(when-let [db (conn/get-conn repo-url)]
(some-> (d/entity db key)
key))))