Browse Source

Remove :block/uuid and :block/name for properties

Tienson Qin 1 year ago
parent
commit
8d31f94b24

+ 4 - 1
deps/db/src/logseq/db/frontend/class.cljs

@@ -4,7 +4,10 @@
 ;; TODO: disable name changes for those built-in page/class names and their properties names
 (def ^:large-vars/data-var built-in-classes
   {:task {:original-name "Task"
-          :schema {:properties ["status" "priority" "scheduled"  "deadline"]}}
+          :schema {:properties #{:logseq.property/status
+                                 :logseq.property/priority
+                                 :logseq.property/scheduled
+                                 :logseq.property/deadline}}}
    :card {:original-name "card"
           ;; :schema {:property []}
           }

+ 13 - 1
deps/db/src/logseq/db/frontend/property.cljs

@@ -3,7 +3,8 @@
   (:require [clojure.set :as set]
             [logseq.db.sqlite.util :as sqlite-util]
             [datascript.core :as d]
-            [logseq.common.util :as common-util]))
+            [logseq.common.util :as common-util]
+            [clojure.string :as string]))
 
 (def first-stage-properties
   #{:built-in? :created-from-property})
@@ -254,3 +255,14 @@
 (defn property?
   [k]
   (contains? #{:logseq.property :user.property} (keyword (namespace k))))
+
+(defn get-db-ident-from-name
+  [property-name]
+  (let [n (-> (string/lower-case property-name)
+              (string/replace #"^:" "")
+              (string/replace " " "_")
+              (string/trim))]
+    (when-not (string/blank? n)
+      (->
+       (str "user.property/" n)
+       keyword))))

+ 3 - 4
deps/db/src/logseq/db/frontend/property/util.cljs

@@ -58,8 +58,8 @@
 (defn build-closed-values
   "Builds all the tx needed for property with closed values including
    the hidden page and closed value blocks as needed"
-  [db prop-name property {:keys [icon-id db-ident translate-closed-page-value-fn property-attributes]
-                          :or {translate-closed-page-value-fn identity}}]
+  [prop-name property {:keys [db-ident translate-closed-page-value-fn property-attributes]
+                       :or {translate-closed-page-value-fn identity}}]
   (let [page-tx (build-property-hidden-page property)
         page-id [:block/uuid (:block/uuid page-tx)]
         closed-value-page-uuids? (contains? #{:page :date} (get-in property [:block/schema :type]))
@@ -69,13 +69,12 @@
           (map (fn [{:keys [db-ident value icon description uuid]}]
                  (build-closed-value-block
                   uuid value page-id property {:db-ident db-ident
-                                               :icon-id icon-id
                                                :icon icon
                                                :description description}))
                (:closed-values property)))
         property-schema (assoc (:block/schema property)
                                :values (mapv :block/uuid closed-value-blocks-tx))
-        property-tx (merge (sqlite-util/build-new-property prop-name property-schema (:block/uuid property)
+        property-tx (merge (sqlite-util/build-new-property prop-name property-schema
                                                            {:db-ident db-ident})
                            property-attributes)]
     (into [property-tx page-tx]

+ 7 - 2
deps/db/src/logseq/db/frontend/schema.cljs

@@ -83,8 +83,8 @@
    ;; whether blocks is a repeated block (usually a task)
    :block/repeated? {}
 
-   :block/created-at {}
-   :block/updated-at {}
+   :block/created-at {:db/index true}
+   :block/updated-at {:db/index true}
 
    ;; page additional attributes
    ;; page's name, lowercase
@@ -92,6 +92,11 @@
 
    ;; page's original name
    :block/original-name {:db/unique :db.unique/identity}
+
+   ;; class properties
+   :class/schema.properties {:db/valueType :db.type/ref
+                             :db/cardinality :db.cardinality/many}
+
    ;; whether page's is a journal
    :block/journal? {}
    :block/journal-day {}

+ 16 - 31
deps/db/src/logseq/db/sqlite/create_graph.cljs

@@ -11,10 +11,8 @@
             [logseq.db.frontend.default :as default-db]))
 
 (defn- build-initial-properties
-  [db]
-  (let [;; Some uuids need to be pre-defined since they are referenced by other properties
-        default-property-uuids {:icon (d/squuid)}
-        built-in-properties (->>
+  []
+  (let [built-in-properties (->>
                              (map (fn [[k v]]
                                     (assert (keyword? k))
                                     [k (assoc v
@@ -25,22 +23,14 @@
     (mapcat
      (fn [[k-keyword {:keys [schema original-name closed-values db-ident]}]]
        (let [k-name (name k-keyword)
-             id (if (contains? db-property/first-stage-properties k-keyword)
-                  (let [id (:block/uuid (d/entity db db-ident))]
-                    (assert (uuid? id) "First stage properties are not created yet")
-                    id)
-                  (d/squuid))
              blocks (if closed-values
                       (db-property-util/build-closed-values
-                       db
                        (or original-name k-name)
-                       {:block/schema schema :block/uuid id :closed-values closed-values}
-                       {:icon-id (get default-property-uuids :icon)
-                        :db-ident db-ident})
+                       {:block/schema schema :closed-values closed-values}
+                       {:db-ident db-ident})
                       [(sqlite-util/build-new-property
                         (or original-name k-name)
                         schema
-                        (get default-property-uuids k-keyword id)
                         {:db-ident db-ident})])]
          (update blocks 0 default-db/mark-block-as-built-in)))
      built-in-properties)))
@@ -53,13 +43,8 @@
    key value})
 
 (defn build-db-initial-data
-  [db* config-content]
-  (let [db (d/db-with db*
-                      (map (fn [p]
-                             {:db/ident (db-property/name->db-ident p)
-                              :block/name (name p)
-                              :block/uuid (random-uuid)}) db-property/first-stage-properties))
-        initial-data [(kv :db/type "db")
+  [config-content]
+  (let [initial-data [(kv :db/type "db")
                       (kv :schema/version db-schema/version)]
         initial-files [{:block/uuid (d/squuid)
                         :file/path (str "logseq/" "config.edn")
@@ -73,22 +58,22 @@
                         :file/path (str "logseq/" "custom.js")
                         :file/content ""
                         :file/last-modified-at (js/Date.)}]
+        default-properties (build-initial-properties)
         default-pages (->> (ldb/build-pages-tx (map default-db/page-title->block ["Contents"]))
                            (map default-db/mark-block-as-built-in))
-        default-properties (build-initial-properties db)
-        name->properties (zipmap
-                          (map :block/name default-properties)
-                          default-properties)
+        db-ident->properties (zipmap
+                              (map :db/ident default-properties)
+                              default-properties)
         default-classes (map
                          (fn [[k-keyword {:keys [schema original-name]}]]
                            (let [k-name (name k-keyword)]
                              (default-db/mark-block-as-built-in
                               (sqlite-util/build-new-class
                                (let [properties (mapv
-                                                 (fn [property-name]
-                                                   (let [id (:block/uuid (get name->properties property-name))]
-                                                     (assert id (str "Built-in property " property-name " is not defined yet"))
-                                                     id))
+                                                 (fn [db-ident]
+                                                   (let [property (get db-ident->properties db-ident)]
+                                                     (assert property (str "Built-in property " db-ident " is not defined yet"))
+                                                     db-ident))
                                                  (:properties schema))]
                                  (cond->
                                   {:block/original-name (or original-name k-name)
@@ -96,6 +81,6 @@
                                    :db/ident (keyword "logseq.class" k-name)
                                    :block/uuid (d/squuid)}
                                    (seq properties)
-                                   (assoc-in [:block/schema :properties] properties)))))))
+                                   (assoc :class/schema.properties properties)))))))
                          db-class/built-in-classes)]
-    (vec (concat initial-data initial-files default-pages default-classes default-properties))))
+    (vec (concat initial-data initial-files default-properties default-pages default-classes))))

+ 5 - 7
deps/db/src/logseq/db/sqlite/util.cljs

@@ -65,22 +65,20 @@
 
 (defn build-new-property
   "Build a standard new property so that it is is consistent across contexts"
-  [prop-name prop-schema prop-uuid & {:keys [db-ident]}]
+  [prop-name prop-schema & {:keys [db-ident]}]
   (block-with-timestamps
    (cond->
     {:block/type "property"
-     :block/journal? false
-     :block/format :markdown
-     :block/uuid prop-uuid
      :block/schema (merge {:type :default} prop-schema)
-     :block/original-name (name prop-name)
-     :block/name (common-util/page-name-sanity-lc (name prop-name))}
+     :block/original-name (name prop-name)}
      (and db-ident (keyword? db-ident))
      (assoc :db/ident db-ident)
      (= :many (:cardinality prop-schema))
      (assoc :db/index true
             :db/cardinality :db.cardinality/many)
-     (contains? #{:page :block} (:type prop-schema))
+     (not= :many (:cardinality prop-schema))
+     (assoc :db/cardinality :db.cardinality/one)
+     (contains? #{:page :block :date :object} (:type prop-schema))
      (assoc :db/index true
             :db/valueType :db.type/ref))))
 

+ 1 - 2
scripts/src/logseq/tasks/db_graph/create_graph.cljs

@@ -34,7 +34,7 @@
           (string/replace-first #"(:file/name-format :triple-lowbar)"
                                 (str "$1 "
                                      (string/replace-first (str additional-config) #"^\{(.*)\}$" "$1"))))]
-    (d/transact! conn (sqlite-create-graph/build-db-initial-data @conn config-content))))
+    (d/transact! conn (sqlite-create-graph/build-db-initial-data config-content))))
 
 (defn init-conn
   "Create sqlite DB, initialize datascript connection and sync listener and then
@@ -172,7 +172,6 @@
                             (fn [[prop-name uuid]]
                               (if (get-in properties [prop-name :closed-values])
                                 (db-property-util/build-closed-values
-                                 db
                                  prop-name
                                  (assoc (get properties prop-name) :block/uuid uuid)
                                  {:icon-id

+ 4 - 4
src/main/frontend/components/content.cljs

@@ -163,9 +163,9 @@
                                           [:p (t :context-menu/template-exists-warning)]
                                           :error)
                                          (p/do!
-                                           (property-handler/set-block-property! repo block-id :template title)
+                                           (property-handler/set-block-property! repo block-id :logseq.property/template title)
                                            (when (false? template-including-parent?)
-                                             (property-handler/set-block-property! repo block-id :template-including-parent false))
+                                             (property-handler/set-block-property! repo block-id :logseq.property/template-including-parent false))
                                            (state/hide-custom-context-menu!))))))))]
          [:hr.menu-separator]])
       (ui/menu-link
@@ -185,8 +185,8 @@
             heading (or (pu/lookup properties :heading)
                         false)]
         [:.menu-links-wrapper
-         (ui/menu-background-color #(property-handler/set-block-property! repo block-id :background-color %)
-                                   #(property-handler/remove-block-property! repo block-id :background-color))
+         (ui/menu-background-color #(property-handler/set-block-property! repo block-id :logseq.property/background-color %)
+                                   #(property-handler/remove-block-property! repo block-id :logseq.property/background-color))
 
          (ui/menu-heading heading
                           #(editor-handler/set-heading! block-id %)

+ 1 - 1
src/main/frontend/components/db_based/page.cljs

@@ -86,7 +86,7 @@
 
 (rum/defc tags
   [page]
-  (let [tags-property (pu/get-property :tags)]
+  (let [tags-property (db/entity :block/tags)]
     (pv/property-value page tags-property
                        (map :block/uuid (:block/tags page))
                        {:page-cp (fn [config page]

+ 2 - 2
src/main/frontend/components/property/value.cljs

@@ -146,7 +146,7 @@
                          :on-change (fn [page]
                                       (let [repo (state/get-current-repo)]
                                         (property-handler/set-block-property! repo (:block/uuid block)
-                                                                              (:block/name property)
+                                                                              (:db/ident property)
                                                                               (:db/id page))
                                         (exit-edit-property)))}))))
 
@@ -339,7 +339,7 @@
       (p/do!
        (when (not= new-value value)
          (property-handler/set-block-property! repo (:block/uuid block)
-                                               (:block/original-name property)
+                                               (:db/ident property)
                                                (string/trim new-value)))
        (exit-edit-property)))))
 

+ 8 - 9
src/main/frontend/components/query.cljs

@@ -157,10 +157,9 @@
                        collapsed?
                        (:block/collapsed? current-block)))
         built-in-collapsed? (and collapsed? built-in?)
-        properties (:block/properties current-block)
-        query-table-property (pu/lookup properties :query-table)
+        query-table? (:logseq.property/query-table current-block)
         table? (or table-view?
-                   query-table-property
+                   query-table?
                    (and (string? query) (string/ends-with? (string/trim query) "table")))
         view-fn (if (keyword? view) (get-in (state/sub-config) [:query/views view]) view)
         view-f (and view-fn (sci/eval-string (pr-str view-fn)))
@@ -211,13 +210,13 @@
                  (if table?
                    [:a.flex.ml-1.fade-link {:title "Switch to list view"
                                             :on-click (fn [] (property-handler/set-block-property! (state/get-current-repo) current-block-uuid
-                                                                                                  "query-table"
-                                                                                                  false))}
+                                                                                                   :logseq.property/query-table
+                                                                                                   false))}
                     (ui/icon "list" {:style {:font-size 20}})]
                    [:a.flex.ml-1.fade-link {:title "Switch to table view"
                                             :on-click (fn [] (property-handler/set-block-property! (state/get-current-repo) current-block-uuid
-                                                                                                  "query-table"
-                                                                                                  true))}
+                                                                                                   :logseq.property/query-table
+                                                                                                   true))}
                     (ui/icon "table" {:style {:font-size 20}})]))
 
                [:a.flex.ml-1.fade-link
@@ -232,8 +231,8 @@
                           (and query-time (> query-time 50)))
                   (query-refresh-button query-time {:full-text-search? full-text-search?
                                                     :on-pointer-down (fn [e]
-                                                                     (util/stop e)
-                                                                     (query-result/trigger-custom-query! config q *query-error))}))]])])
+                                                                       (util/stop e)
+                                                                       (query-result/trigger-custom-query! config q *query-error))}))]])])
 
          (when dsl-query? builder)
 

+ 2 - 2
src/main/frontend/components/query_table.cljs

@@ -94,8 +94,8 @@
     [:th.whitespace-nowrap
      [:a {:on-click (fn []
                       (p/do!
-                       (property-handler/set-block-property! repo block-id :query-sort-by (if (uuid? column) column (name column)))
-                       (property-handler/set-block-property! repo block-id :query-sort-desc (not sort-desc?))))}
+                        (property-handler/set-block-property! repo block-id :logseq.property/query-sort-by (if (uuid? column) column (name column)))
+                        (property-handler/set-block-property! repo block-id :logseq.property/query-sort-desc (not sort-desc?))))}
       [:div.flex.items-center
        [:span.mr-1 title]
        (when (= sort-by-column column)

+ 4 - 4
src/main/frontend/extensions/pdf/assets.cljs

@@ -129,10 +129,10 @@
 (defn update-hl-block!
   [highlight]
   (when-let [block (db-model/get-block-by-uuid (:id highlight))]
-    (doseq [[k v] {:hl-stamp (if (area-highlight? highlight)
-                               (get-in highlight [:content :image])
-                               (js/Date.now))
-                   :hl-color (get-in highlight [:properties :color])}]
+    (doseq [[k v] {:logseq.property/hl-stamp (if (area-highlight? highlight)
+                                               (get-in highlight [:content :image])
+                                               (js/Date.now))
+                   :logseq.property/hl-color (get-in highlight [:properties :color])}]
       (property-handler/set-block-property! (state/get-current-repo) (:block/uuid block) k v))))
 
 (defn unlink-hl-area-image$

+ 6 - 6
src/main/frontend/extensions/srs.cljs

@@ -47,12 +47,12 @@
 
 (def card-hash-tag "card")
 
-(def card-last-interval-property        :card-last-interval)
-(def card-repeats-property              :card-repeats)
-(def card-last-reviewed-property        :card-last-reviewed)
-(def card-next-schedule-property        :card-next-schedule)
-(def card-last-easiness-factor-property :card-ease-factor)
-(def card-last-score-property           :card-last-score)
+(def card-last-interval-property        :logseq.property/card-last-interval)
+(def card-repeats-property              :logseq.property/card-repeats)
+(def card-last-reviewed-property        :logseq.property/card-last-reviewed)
+(def card-next-schedule-property        :logseq.property/card-next-schedule)
+(def card-last-easiness-factor-property :logseq.property/card-ease-factor)
+(def card-last-score-property           :logseq.property/card-last-score)
 
 (def default-card-properties-map {card-last-interval-property -1
                                   card-repeats-property 0

+ 24 - 38
src/main/frontend/handler/db_based/property.cljs

@@ -98,31 +98,21 @@
       (if (util/uuid-string? v-str) (uuid v-str) v-str))))
 
 (defn upsert-property!
-  [repo k-name schema {:keys [property-uuid]}]
-  (let [property (db/entity [:block/name (common-util/page-name-sanity-lc k-name)])
-        k-name (name k-name)
-        property-uuid (or (:block/uuid property) property-uuid (db/new-block-id))]
+  [repo k-name schema {}]
+  (let [db-ident (db-property/get-db-ident-from-name k-name)
+        property (db/entity db-ident)
+        k-name (name k-name)]
     (if property
-      (do
-        (db/transact! repo [(cond->
-                             (outliner-core/block-with-updated-at
-                              {:block/schema schema
-                               :block/uuid property-uuid
-                               :block/type "property"})
-                              (= :many (:cardinality schema))
-                              (assoc :db/cardinality :db.cardinality/many))]
-                      {:outliner-op :save-block})
-        (:db/ident property))
-      (let [db-ident (->
-                      (str "user.property/"
-                           (-> (string/lower-case k-name)
-                               (string/replace #"^:" "")
-                               (string/replace " " "_")
-                               (string/trim)))
-                      keyword)]
-        (db/transact! repo [(sqlite-util/build-new-property k-name schema property-uuid {:db-ident db-ident})]
-                      {:outliner-op :new-property})
-        db-ident))))
+      (db/transact! repo [(cond->
+                           (outliner-core/block-with-updated-at
+                            {:db/ident db-ident
+                             :block/schema schema})
+                            (= :many (:cardinality schema))
+                            (assoc :db/cardinality :db.cardinality/many))]
+                    {:outliner-op :save-block})
+      (db/transact! repo [(sqlite-util/build-new-property k-name schema {:db-ident db-ident})]
+                    {:outliner-op :new-property}))
+    db-ident))
 
 (defn validate-property-value
   [schema value]
@@ -132,7 +122,8 @@
   [repo block-id k-name values _opts]
   (let [block (db/entity repo [:block/uuid block-id])
         k-name (name k-name)
-        property (db/pull repo '[*] [:block/name (common-util/page-name-sanity-lc k-name)])
+        db-ident (db-property/get-db-ident-from-name k-name)
+        property (db/entity db-ident)
         values (remove nil? values)
         property-uuid (or (:block/uuid property) (db/new-block-id))
         property-schema (:block/schema property)
@@ -199,7 +190,7 @@
   [repo block-id k-name v {:keys [old-value] :as opts}]
   (let [block (db/entity repo [:block/uuid block-id])
         k-name (name k-name)
-        property (db/pull repo '[*] [:block/name (common-util/page-name-sanity-lc k-name)])
+        property (db/entity (db-property/get-db-ident-from-name k-name))
         property-uuid (or (:block/uuid property) (db/new-block-id))
         property-schema (:block/schema property)
         {:keys [type cardinality]} property-schema
@@ -282,8 +273,7 @@
                 (and (not (ldb/built-in? (db/get-db) property)) (empty? property-values)))
         (let [tx-data (cond-> (merge {:block/uuid property-uuid} properties)
                         property-name (merge
-                                       {:block/original-name property-name
-                                        :block/name (common-util/page-name-sanity-lc property-name)})
+                                       {:block/original-name property-name})
                         property-schema (assoc :block/schema
                                                  ;; a property must have a :type when making schema changes
                                                (merge {:type :default}
@@ -297,7 +287,8 @@
   (when-let [class (db/entity repo [:block/uuid class-uuid])]
     (when (contains? (:block/type class) "class")
       (let [k-name (name k-name)
-            property (db/pull repo '[*] [:block/name (common-util/page-name-sanity-lc k-name)])
+            db-ident (db-property/get-db-ident-from-name k-name)
+            property (db/entity db-ident)
             property-uuid (or (:block/uuid property) (db/new-block-id))
             property-type (get-in property [:block/schema :type])
             {:keys [properties] :as class-schema} (:block/schema class)
@@ -423,20 +414,15 @@
     (when (not= property-id (:block/uuid block))
       (when-let [property (db/pull [:block/uuid property-id])]
         (let [schema (:block/schema property)
-              k-name (:block/name property)
+              db-ident (:db/ident property)
               property-id (:db/ident property)
-              tags-or-alias? (and (contains? #{"tags" "alias"} k-name)
+              tags-or-alias? (and (contains? #{:block/tags :block/alias} db-ident)
                                   (uuid? property-value))]
           (if tags-or-alias?
-            (let [property-value-id (:db/id (db/entity [:block/uuid property-value]))
-                  attribute (case k-name
-                              "alias"
-                              :block/alias
-                              "tags"
-                              :block/tags)]
+            (let [property-value-id (:db/id (db/entity [:block/uuid property-value]))]
               (when property-value-id
                 (db/transact! repo
-                              [[:db/retract (:db/id block) attribute property-value-id]]
+                              [[:db/retract (:db/id block) db-ident property-value-id]]
                               {:outliner-op :save-block})))
             (if (= :many (:cardinality schema))
               (db/transact! repo

+ 2 - 2
src/main/frontend/handler/editor.cljs

@@ -90,12 +90,12 @@
 (defn set-block-own-order-list-type!
   [block type]
   (when-let [uuid (:block/uuid block)]
-    (property-handler/set-block-property! (state/get-current-repo) uuid :logseq.order-list-type (name type))))
+    (property-handler/set-block-property! (state/get-current-repo) uuid :logseq.property/order-list-type (name type))))
 
 (defn remove-block-own-order-list-type!
   [block]
   (when-let [uuid (:block/uuid block)]
-    (property-handler/remove-block-property! (state/get-current-repo) uuid :logseq.order-list-type)))
+    (property-handler/remove-block-property! (state/get-current-repo) uuid :logseq.property/order-list-type)))
 
 (defn own-order-number-list?
   [block]

+ 0 - 1
src/main/frontend/handler/repo.cljs

@@ -193,7 +193,6 @@
            _ (state/add-repo! {:url full-graph-name})
            _ (when-not file-graph-import? (route-handler/redirect-to-home!))
            initial-data (sqlite-create-graph/build-db-initial-data
-                         (db/get-db)
                          (migrate-db-config config/config-default-content))
            _ (db/transact! full-graph-name initial-data)
            _ (repo-config-handler/set-repo-config-state! full-graph-name config/config-default-content)

+ 2 - 2
src/main/logseq/api.cljs

@@ -1021,7 +1021,7 @@
       (if (or (not block) (true? overwrite))
         (do (when-let [old-target block]
               (property-handler/remove-block-property! repo (:block/uuid old-target) :template))
-            (property-handler/set-block-property! repo target-uuid :template template-name))
+            (property-handler/set-block-property! repo target-uuid :logseq.property/template template-name))
         (throw (js/Error. "Template already exists!"))))))
 
 (defn ^:export remove_template
@@ -1049,4 +1049,4 @@
   []
   true)
 
-(def ^:export set_blocks_id #(editor-handler/set-blocks-id! (map uuid %)))
+(def ^:export set_blocks_id #(editor-handler/set-blocks-id! (map uuid %)))

+ 0 - 1
src/test/frontend/test/helper.cljs

@@ -244,7 +244,6 @@ This can be called in synchronous contexts as no async fns should be invoked"
     (start-test-db! start-opts)
     (when db-graph?
       (let [built-in-data (sqlite-create-graph/build-db-initial-data
-                           (db/get-db repo)
                            config/config-default-content)]
         (db/transact! repo built-in-data)))
     (when-let [init-f (:init-data start-opts)]