diff --git a/src/main/frontend/db/conn.cljs b/src/main/frontend/db/conn.cljs index e107e10326..5831b6e272 100644 --- a/src/main/frontend/db/conn.cljs +++ b/src/main/frontend/db/conn.cljs @@ -9,7 +9,8 @@ [logseq.graph-parser.text :as text] [logseq.db :as ldb] [logseq.db.frontend.schema :as db-schema] - [logseq.graph-parser.util :as gp-util])) + [logseq.graph-parser.util :as gp-util] + [datascript.core :as d])) (defonce conns (atom {})) @@ -84,6 +85,12 @@ [repo] (swap! conns dissoc (datascript-db repo))) +(defn kv + [key value] + {:db/id -1 + :db/ident key + key value}) + (defn start! ([repo] (start! repo {})) @@ -91,6 +98,9 @@ (let [db-name (datascript-db repo) db-conn (ldb/start-conn :schema (get-schema repo) :create-default-pages? false)] (swap! conns assoc db-name db-conn) + (when db-graph? + (d/transact! db-conn [(kv :db/type "db")]) + (d/transact! db-conn [(kv :schema/version db-schema/version)])) (when listen-handler (listen-handler repo)) (ldb/create-default-pages! db-conn {:db-graph? db-graph?})))) diff --git a/src/main/frontend/db/model.cljs b/src/main/frontend/db/model.cljs index b1c0047277..84b81c6069 100644 --- a/src/main/frontend/db/model.cljs +++ b/src/main/frontend/db/model.cljs @@ -79,7 +79,7 @@ (defn get-tag-blocks [repo tag-name] - (d/q '[:find ?b + (d/q '[:find [?b ...] :in $ ?tag :where [?e :block/name ?tag] @@ -1237,38 +1237,38 @@ independent of format as format specific heading characters are stripped" "Returns all property values of a given property for use in a simple query. Property values that are references are displayed as page references" [repo property] - (->> (d/q - '[:find ?prop-type ?v - :in $ ?prop-name - :where - [?b :block/properties ?bp] - [?prop-b :block/name ?prop-name] - [?prop-b :block/uuid ?prop-uuid] - [?prop-b :block/schema ?prop-schema] - [(get ?prop-schema :type) ?prop-type] - [(get ?bp ?prop-uuid) ?v]] - (conn/get-db repo) - (name property)) - (map (fn [[prop-type v]] [prop-type (if (coll? v) v [v])])) - (mapcat (fn [[prop-type vals]] - (case prop-type - :enum - (map #(:block/content (db-utils/entity repo [:block/uuid %])) vals) - :default + (let [property-name (if (keyword? property) + (name property) + (util/page-name-sanity-lc property))] + (->> (d/q + '[:find ?prop-type ?v + :in $ ?prop-name + :where + [?b :block/properties ?bp] + [?prop-b :block/name ?prop-name] + [?prop-b :block/uuid ?prop-uuid] + [?prop-b :block/schema ?prop-schema] + [(get ?prop-schema :type) ?prop-type] + [(get ?bp ?prop-uuid) ?v]] + (conn/get-db repo) + property-name) + (map (fn [[prop-type v]] [prop-type (if (coll? v) v [v])])) + (mapcat (fn [[prop-type vals]] + (case prop-type + :default ;; Remove multi-block properties as there isn't a supported approach to query them yet - (map str (remove uuid? vals)) - (:page :date) - (map #(page-ref/->page-ref (:block/original-name (db-utils/entity repo [:block/uuid %]))) - vals) - :number - vals + (map str (remove uuid? vals)) + (:page :date) + (map #(page-ref/->page-ref (:block/original-name (db-utils/entity repo [:block/uuid %]))) + vals) + :number + vals ;; Checkboxes returned as strings as builder doesn't display boolean values correctly - (map str vals)))) + (map str vals)))) ;; Remove blanks as they match on everything - (remove string/blank?) - (distinct) - (sort))) - + (remove string/blank?) + (distinct) + (sort)))) (defn get-block-property-values "Get blocks which have this property." @@ -1287,15 +1287,15 @@ independent of format as format specific heading characters are stripped" "Get classes which have given property as a class property" [property-uuid] (d/q - '[:find ?b - :in $ ?property-uuid - :where - [?b :block/schema ?schema] - [(get ?schema :properties) ?schema-properties*] - [(set ?schema-properties*) ?schema-properties] - [(contains? ?schema-properties ?property-uuid)]] - (conn/get-db) - property-uuid)) + '[:find [?b ...] + :in $ ?property-uuid + :where + [?b :block/schema ?schema] + [(get ?schema :properties) ?schema-properties*] + [(set ?schema-properties*) ?schema-properties] + [(contains? ?schema-properties ?property-uuid)]] + (conn/get-db) + property-uuid)) (defn get-template-by-name [name] @@ -1574,17 +1574,16 @@ independent of format as format specific heading characters are stripped" (conn/get-db repo))) (defn get-namespace-children - [repo-url eid] + [repo eid] (->> - (d/q '[:find ?children + (d/q '[:find [?children ...] :in $ ?parent % :where (namespace ?parent ?children)] - (conn/get-db repo-url) + (conn/get-db repo) eid (:namespace rules/rules)) - db-utils/seq-flatten - (set))) + distinct)) (defn get-class-objects [repo class-id] diff --git a/src/main/frontend/db/react.cljs b/src/main/frontend/db/react.cljs index eac1d363c4..d574c6ec79 100644 --- a/src/main/frontend/db/react.cljs +++ b/src/main/frontend/db/react.cljs @@ -62,11 +62,7 @@ (when-let [result-atom (get-in @query-state [k :result])] (reset! result-atom new-result))) -(defn kv - [key value] - {:db/id -1 - :db/ident key - key value}) +(def kv conn/kv) (defn remove-key! [repo-url key] @@ -383,4 +379,4 @@ (defn db-graph? "Whether the current graph is db-only" [] - (= "db" (sub-key-value :db/type))) + (= "db" (:db/type (db-utils/entity :db/type)))) diff --git a/src/main/frontend/db/restore.cljs b/src/main/frontend/db/restore.cljs index 27180759ba..172f3aad16 100644 --- a/src/main/frontend/db/restore.cljs +++ b/src/main/frontend/db/restore.cljs @@ -133,10 +133,6 @@ ;; TODO: Store schema in sqlite ;; (db-migrate/migrate attached-db) - (d/transact! conn [(react/kv :db/type "db") - {:schema/version db-schema/version}] - {:skip-persist? true}) - (js/setTimeout (fn [] (p/let [other-data (persist-db/