diff --git a/deps/db/src/logseq/db/frontend/property/build.cljs b/deps/db/src/logseq/db/frontend/property/build.cljs index 156e7c88ec..7e0147a6ef 100644 --- a/deps/db/src/logseq/db/frontend/property/build.cljs +++ b/deps/db/src/logseq/db/frontend/property/build.cljs @@ -80,20 +80,23 @@ {:block/content value})) sqlite-util/block-with-timestamps)) -;; TODO: Add support for types besides :default when needed by getting property types -;; and passing them to build-property-value-block (defn build-property-values-tx-m - "Builds a map of property names to their property value blocks to be transacted, given a block - and a properties map with raw property values" + "Builds a map of property names to their property value blocks to be + transacted, given a block and a properties map with raw property values. The + properties map can have keys that are db-idents or they can be maps. If a map, + it should have :original-property-id and :db/ident keys. See + ->property-value-tx-m for such an example" [block properties] ;; Build :db/id out of uuid if block doesn't have one for tx purposes (let [block' (if (:db/id block) block (assoc block :db/id [:block/uuid (:block/uuid block)]))] (->> properties (map (fn [[k v]] - [k - (if (set? v) - (set (map #(build-property-value-block block' {:db/ident k} %) v)) - (build-property-value-block block' {:db/ident k} v))])) + (let [property-map (if (map? k) k {:db/ident k})] + (assert (:db/ident property-map) "Key in map must have a :db/ident") + [(or (:original-property-id property-map) (:db/ident property-map)) + (if (set? v) + (set (map #(build-property-value-block block' property-map %) v)) + (build-property-value-block block' property-map v))]))) (into {})))) (defn build-properties-with-ref-values diff --git a/deps/db/src/logseq/db/sqlite/build.cljs b/deps/db/src/logseq/db/sqlite/build.cljs index 0ed84589d7..159fe7d2e6 100644 --- a/deps/db/src/logseq/db/sqlite/build.cljs +++ b/deps/db/src/logseq/db/sqlite/build.cljs @@ -75,24 +75,27 @@ "Provides the next temp :db/id to use in a create-graph transact!" #(swap! current-db-id dec)) -;; TODO: Use build-property-values-tx-m (defn- ->property-value-tx-m "Given a new block and its properties, creates a map of properties which have values of property value tx. This map is used for both creating the new property values and then adding them to a block" [new-block properties properties-config all-idents] (->> properties - (map (fn [[k v]] - (when (and (db-property-type/value-ref-property-types (get-in properties-config [k :block/schema :type])) - ;; TODO: Support translate-property-value without this hack - (not (vector? v))) - (let [property-map {:db/ident (get-ident all-idents k) - :block/schema {:type (get-in properties-config [k :block/schema :type])}}] - [k (if (set? v) - (->> v - (map #(db-property-build/build-property-value-block new-block property-map %)) - set) - (db-property-build/build-property-value-block new-block property-map v))])))) - (into {}))) + (keep (fn [[k v]] + (if-let [built-in-type (get-in db-property/built-in-properties [k :schema :type])] + (when (and (db-property-type/value-ref-property-types built-in-type) + ;; closed values are referenced by their :db/ident so no need to create values + (not (get-in db-property/built-in-properties [k :closed-values]))) + (let [property-map {:db/ident k + :block/schema {:type built-in-type}}] + [property-map v])) + (when (and (db-property-type/value-ref-property-types (get-in properties-config [k :block/schema :type])) + ;; TODO: Support translate-property-value without this hack + (not (vector? v))) + (let [property-map {:db/ident (get-ident all-idents k) + :original-property-id k + :block/schema {:type (get-in properties-config [k :block/schema :type])}}] + [property-map v]))))) + (db-property-build/build-property-values-tx-m new-block))) (defn- extract-content-refs "Extracts basic refs from :block/content like `[[foo]]`. Adding more ref support would diff --git a/deps/db/test/logseq/db/sqlite/build_test.cljs b/deps/db/test/logseq/db/sqlite/build_test.cljs index 7eecf5eafc..1414ee0e08 100644 --- a/deps/db/test/logseq/db/sqlite/build_test.cljs +++ b/deps/db/test/logseq/db/sqlite/build_test.cljs @@ -25,8 +25,8 @@ :where [?b :block/original-name "Jayson Tatum"]] @conn))) "Person class is created and correctly associated to a page"))) - -(deftest build-properties + +(deftest build-properties-user (let [conn (d/create-conn db-schema/schema-for-db-based-graph) _ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}")) _ (sqlite-build/create-blocks @@ -37,7 +37,7 @@ (is (= "Clutch defense" (->> @conn (d/q '[:find [(pull ?b [*]) ...] - :where [?b :block/content "Jrue Holiday"]]) + :where [?b :block/content "Jrue Holiday"]]) first :user.property/description (db-property/ref->property-value-contents @conn))) @@ -46,8 +46,36 @@ (is (= "Awesome selfless basketball" (->> @conn (d/q '[:find [(pull ?b [*]) ...] - :where [?b :block/original-name "Jayson Tatum"]]) + :where [?b :block/original-name "Jayson Tatum"]]) first :user.property/description (db-property/ref->property-value-contents @conn))) - "description property is created and correctly associated to a page"))) \ No newline at end of file + "description property is created and correctly associated to a page"))) + +(deftest build-properties-built-in + (let [conn (d/create-conn db-schema/schema-for-db-based-graph) + _ (d/transact! conn (sqlite-create-graph/build-db-initial-data "{}")) + _ (sqlite-build/create-blocks + conn + [{:page {:block/original-name "page1"} + :blocks [{:block/content "some todo" + :build/properties {:logseq.task/status :logseq.task/status.doing}} + {:block/content "some slide" + :build/properties {:logseq.property/background-image "https://placekitten.com/200/300"}}]}])] + (is (= "Doing" + (->> @conn + (d/q '[:find [(pull ?b [*]) ...] + :where [?b :block/content "some todo"]]) + first + :logseq.task/status + (db-property/ref->property-value-contents @conn))) + "built-in property with closed value is created and correctly associated to a block") + + (is (= "https://placekitten.com/200/300" + (->> @conn + (d/q '[:find [(pull ?b [*]) ...] + :where [?b :block/content "some slide"]]) + first + :logseq.property/background-image + (db-property/ref->property-value-contents @conn))) + "built-in :default property is created and correctly associated to a block"))) \ No newline at end of file diff --git a/src/main/frontend/worker/handler/page/db_based/page.cljs b/src/main/frontend/worker/handler/page/db_based/page.cljs index de75b2666d..eca91a36a5 100644 --- a/src/main/frontend/worker/handler/page/db_based/page.cljs +++ b/src/main/frontend/worker/handler/page/db_based/page.cljs @@ -26,6 +26,8 @@ page' (->> properties (keep (fn [[k v]] + ;; TODO: Pass in property type in order to support property + ;; types other than :default (when (db-property-util/built-in-has-ref-value? k) [k v]))) (into {})))]