Skip to content

Commit 9ca0ca1

Browse files
chore: cleanup schema mapping spread across the app
Now that schema is no longer a persisted concept there is no need to spread built-in-properties keyword mappings across the app. Doing so makes built-in-properties more rigid as it would be needlessly coupled to the frontend, exporter and migrations. With this cleanup it'll be easy to deprecate :schema from built-in-properties when we need to
1 parent 878f0f8 commit 9ca0ca1

File tree

8 files changed

+63
-45
lines changed

8 files changed

+63
-45
lines changed

deps/db/src/logseq/db/frontend/property.cljs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
(def ^:large-vars/data-var built-in-properties*
3939
"Map of built in properties for db graphs with their :db/ident as keys.
4040
Each property has a config map with the following keys:
41+
TODO: Move some of these keys to :properties since :schema is a deprecated concept
4142
* :schema - Property's schema. Required key. Has the following common keys:
4243
* :type - Property type
4344
* :cardinality - property cardinality. Default to one/single cardinality if not set
@@ -599,12 +600,23 @@
599600
"Property values that shouldn't be updated"
600601
#{:logseq.property/built-in?})
601602

603+
(def schema-properties-map
604+
"Maps schema unqualified keywords to their qualified keywords.
605+
The qualified keywords are all properties except for :db/cardinality
606+
which is a datascript attribute"
607+
{:cardinality :db/cardinality
608+
:type :logseq.property/type
609+
:hide? :logseq.property/hide?
610+
:public? :logseq.property/public?
611+
:ui-position :logseq.property/ui-position
612+
:view-context :logseq.property/view-context
613+
:classes :logseq.property/classes})
614+
602615
(def schema-properties
603616
"Properties that used to be in block/schema. Schema originally referred to just type and cardinality
604617
but expanded to include a property's core configuration because it was easy to add to the schema map.
605-
We should move some of these out since they are just like any other properties e.g. view-context"
606-
#{:db/cardinality :logseq.property/type :logseq.property/hide? :logseq.property/public?
607-
:logseq.property/view-context :logseq.property/ui-position :logseq.property/classes})
618+
We should move some of these out since they are just like any other properties e.g. :view-context"
619+
(set (vals schema-properties-map)))
608620

609621
(def logseq-property-namespaces
610622
#{"logseq.property" "logseq.property.tldraw" "logseq.property.pdf" "logseq.property.fsrs" "logseq.task"

deps/db/src/logseq/db/sqlite/create_graph.cljs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@
4444
(fn [[db-ident {:keys [attribute schema title closed-values properties] :as m}]]
4545
(let [db-ident (or attribute db-ident)
4646
prop-name (or title (name (:name m)))
47+
schema' (sqlite-util/schema->qualified-property-keyword schema)
4748
[property & others] (if closed-values
4849
(db-property-build/build-closed-values
4950
db-ident
5051
prop-name
51-
{:db/ident db-ident :schema schema :closed-values closed-values}
52+
{:db/ident db-ident :schema schema' :closed-values closed-values}
5253
{})
5354
[(sqlite-util/build-new-property
5455
db-ident
55-
schema
56+
schema'
5657
{:title prop-name})])
5758
pvalue-tx-m (->property-value-tx-m
5859
(merge property
@@ -86,7 +87,7 @@
8687
[db-ident]
8788
(sqlite-util/build-new-property
8889
db-ident
89-
(get-in db-property/built-in-properties [db-ident :schema])
90+
(sqlite-util/schema->qualified-property-keyword (get-in db-property/built-in-properties [db-ident :schema]))
9091
{:title (get-in db-property/built-in-properties [db-ident :title])}))
9192

9293
(defn- build-initial-properties

deps/db/src/logseq/db/sqlite/util.cljs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,9 @@
7070
[prop-schema]
7171
(reduce-kv
7272
(fn [r k v]
73-
(if (qualified-keyword? k)
74-
(assoc r k v)
75-
(cond
76-
(= k :cardinality)
77-
(assoc r :db/cardinality v)
78-
(= k :classes)
79-
(assoc r :logseq.property/classes v)
80-
(= k :position)
81-
(assoc r :logseq.property/ui-position v)
82-
:else
83-
(assoc r (keyword "logseq.property" k) v))))
73+
(if-let [new-k (and (simple-keyword? k) (db-property/schema-properties-map k))]
74+
(assoc r new-k v)
75+
(assoc r k v)))
8476
{}
8577
prop-schema))
8678

@@ -90,13 +82,12 @@
9082
* :title - Case sensitive property name. Defaults to deriving this from db-ident
9183
* :block-uuid - :block/uuid for property"
9284
([db-ident prop-schema] (build-new-property db-ident prop-schema {}))
93-
([db-ident prop-schema' {:keys [title block-uuid ref-type? properties]}]
85+
([db-ident prop-schema {:keys [title block-uuid ref-type? properties]}]
9486
(assert (keyword? db-ident))
9587
(let [db-ident' (if (qualified-keyword? db-ident)
9688
db-ident
9789
(db-property/create-user-property-ident-from-name (name db-ident)))
9890
prop-name (or title (name db-ident'))
99-
prop-schema (schema->qualified-property-keyword prop-schema')
10091
prop-type (get prop-schema :logseq.property/type :default)]
10192
(merge
10293
(dissoc prop-schema :db/cardinality)

deps/graph-parser/src/logseq/graph_parser/exporter.cljs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@
370370

371371
(defn- infer-property-schema-and-get-property-change
372372
"Infers a property's schema from the given _user_ property value and adds new ones to
373-
the property-schemas atom. If a property's :type changes, returns a map of
373+
the property-schemas atom. If a property's :logseq.property/type changes, returns a map of
374374
the schema attribute changed and how it changed e.g. `{:type {:from :default :to :url}}`"
375375
[db prop-val prop prop-val-text refs {:keys [property-schemas all-idents]} macros]
376376
;; Explicitly fail an unexpected case rather than cause silent downstream failures
@@ -390,15 +390,15 @@
390390
:else
391391
(db-property-type/infer-property-type-from-value
392392
(macro-util/expand-value-if-macro prop-val macros)))
393-
prev-type (get-in @property-schemas [prop :type])]
393+
prev-type (get-in @property-schemas [prop :logseq.property/type])]
394394
;; Create new property
395395
(when-not (get @property-schemas prop)
396396
(create-property-ident db all-idents prop)
397-
(let [schema (cond-> {:type prop-type}
397+
(let [schema (cond-> {:logseq.property/type prop-type}
398398
(#{:node :date} prop-type)
399399
;; Assume :many for now as detecting that detecting property values across files are consistent
400400
;; isn't possible yet
401-
(assoc :cardinality :many))]
401+
(assoc :db/cardinality :many))]
402402
(swap! property-schemas assoc prop schema)))
403403
(when (and prev-type (not= prev-type prop-type))
404404
{:type {:from prev-type :to prop-type}})))
@@ -555,7 +555,7 @@
555555
;; Change to :node as dates can be pages but pages can't be dates
556556
(= {:from :date :to :node} type-change)
557557
(do
558-
(swap! property-schemas assoc-in [prop :type] :node)
558+
(swap! property-schemas assoc-in [prop :logseq.property/type] :node)
559559
(update-page-or-date-values page-names-to-uuids val))
560560

561561
;; Unlike the other property changes, this one changes all the previous values of a property
@@ -568,9 +568,9 @@
568568
(swap! ignored-properties conj {:property prop :value val :schema (get property-changes prop)})
569569
nil)
570570
(do
571-
(swap! upstream-properties assoc prop {:schema {:type :default}
571+
(swap! upstream-properties assoc prop {:schema {:logseq.property/type :default}
572572
:from-type (:from type-change)})
573-
(swap! property-schemas assoc prop {:type :default})
573+
(swap! property-schemas assoc prop {:logseq.property/type :default})
574574
(get properties-text-values prop)))
575575

576576
:else
@@ -590,7 +590,7 @@
590590
[prop val'])
591591
[prop
592592
(if (set? val)
593-
(if (= :default (:type (get @property-schemas prop)))
593+
(if (= :default (:logseq.property/type (get @property-schemas prop)))
594594
(get properties-text-values prop)
595595
(update-page-or-date-values page-names-to-uuids val))
596596
val)])))
@@ -609,12 +609,11 @@
609609
(let [property-map {:db/ident k
610610
:logseq.property/type built-in-type}]
611611
[property-map v]))
612-
(when (db-property-type/value-ref-property-types (:type (get-schema-fn k)))
613-
(let [schema (get-schema-fn k)
614-
property-map (merge
612+
(when (db-property-type/value-ref-property-types (:logseq.property/type (get-schema-fn k)))
613+
(let [property-map (merge
615614
{:db/ident (get-ident all-idents k)
616615
:original-property-id k}
617-
(sqlite-util/schema->qualified-property-keyword schema))]
616+
(get-schema-fn k))]
618617
[property-map v])))))
619618
(db-property-build/build-property-values-tx-m new-block)))
620619

@@ -1095,11 +1094,9 @@
10951094
(fn [[prop {:keys [schema from-type]}]]
10961095
(let [prop-ident (get-ident all-idents prop)
10971096
upstream-tx
1098-
(when (= :default (:type schema))
1097+
(when (= :default (:logseq.property/type schema))
10991098
(build-upstream-properties-tx-for-default db prop prop-ident from-type block-properties-text-values))
1100-
property-pages-tx [(merge
1101-
{:db/ident prop-ident}
1102-
(sqlite-util/schema->qualified-property-keyword schema))]]
1099+
property-pages-tx [(merge {:db/ident prop-ident} schema)]]
11031100
;; If we handle cardinality changes we would need to return these separately
11041101
;; as property-pages would need to be transacted separately
11051102
(concat property-pages-tx upstream-tx)))
@@ -1117,7 +1114,7 @@
11171114
:ignored-properties (atom [])
11181115
;; Vec of maps with keys :path and :reason
11191116
:ignored-files (atom [])
1120-
;; Map of property names (keyword) and their current schemas (map).
1117+
;; Map of property names (keyword) and their current schemas (map of qualified properties).
11211118
;; Used for adding schemas to properties and detecting changes across a property's usage
11221119
:property-schemas (atom {})
11231120
;; Map of property or class names (keyword) to db-ident keywords

deps/outliner/src/logseq/outliner/property.cljs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@
158158
(throw (ex-info (str e)
159159
{:type :notification
160160
:payload {:message "Property failed to create. Please try a different property name."
161-
:type :error}})))))
162-
schema (sqlite-util/schema->qualified-property-keyword schema)]
161+
:type :error}})))))]
163162
(assert (qualified-keyword? db-ident))
164163
(if-let [property (and (qualified-keyword? property-id) (d/entity db db-ident))]
165164
(update-property conn db-ident property schema opts)

deps/outliner/test/logseq/outliner/property_test.cljs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
(deftest upsert-property!
1010
(testing "Creates a property"
1111
(let [conn (db-test/create-conn-with-blocks [])
12-
_ (outliner-property/upsert-property! conn nil {:type :number} {:property-name "num"})]
12+
_ (outliner-property/upsert-property! conn nil {:logseq.property/type :number} {:property-name "num"})]
1313
(is (= :number
1414
(:logseq.property/type (d/entity @conn :user.property/num)))
1515
"Creates property with property-name")))
@@ -19,19 +19,19 @@
1919
old-updated-at (:block/updated-at (d/entity @conn :user.property/num))]
2020

2121
(testing "and change its cardinality"
22-
(outliner-property/upsert-property! conn :user.property/num {:cardinality :many} {})
22+
(outliner-property/upsert-property! conn :user.property/num {:db/cardinality :many} {})
2323
(is (db-property/many? (d/entity @conn :user.property/num)))
2424
(is (> (:block/updated-at (d/entity @conn :user.property/num))
2525
old-updated-at)))
2626

2727
(testing "and change its type from a ref to a non-ref type"
28-
(outliner-property/upsert-property! conn :user.property/num {:type :checkbox} {})
28+
(outliner-property/upsert-property! conn :user.property/num {:logseq.property/type :checkbox} {})
2929
(is (= :checkbox (:logseq.property/type (d/entity @conn :user.property/num))))
3030
(is (= nil (:db/valueType (d/entity @conn :user.property/num)))))))
3131

3232
(testing "Multiple properties that generate the same initial :db/ident"
3333
(let [conn (db-test/create-conn-with-blocks [])]
34-
(outliner-property/upsert-property! conn nil {:type :default} {:property-name "p1"})
34+
(outliner-property/upsert-property! conn nil {:logseq.property/type :default} {:property-name "p1"})
3535
(outliner-property/upsert-property! conn nil {} {:property-name "p1"})
3636
(outliner-property/upsert-property! conn nil {} {:property-name "p1"})
3737

src/main/frontend/components/property/config.cljs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@
555555
(p/do!
556556
(db-property-handler/upsert-property!
557557
(:db/ident property)
558-
{:type (keyword v)}
558+
{:logseq.property/type (keyword v)}
559559
{})
560560
(set-sub-open! false)
561561
(restore-root-highlight-item! id))))
@@ -660,7 +660,7 @@
660660
:on-toggle-checked-change
661661
(fn []
662662
(let [update-cardinality-fn #(db-property-handler/upsert-property! (:db/ident property)
663-
{:cardinality (if many? :one :many)}
663+
{:db/cardinality (if many? :one :many)}
664664
{})]
665665
;; Only show dialog for existing values as it can be reversed for unused properties
666666
(if (and (seq values) (not many?))

src/main/frontend/worker/db/migrate.cljs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,24 @@
534534
[:db/retract e :logseq.user/avatar]])
535535
db-ids))))))
536536

537+
(defn- schema->qualified-property-keyword
538+
[prop-schema]
539+
(reduce-kv
540+
(fn [r k v]
541+
(if (qualified-keyword? k)
542+
(assoc r k v)
543+
(cond
544+
(= k :cardinality)
545+
(assoc r :db/cardinality v)
546+
(= k :classes)
547+
(assoc r :logseq.property/classes v)
548+
(= k :position)
549+
(assoc r :logseq.property/ui-position v)
550+
:else
551+
(assoc r (keyword "logseq.property" k) v))))
552+
{}
553+
prop-schema))
554+
537555
(defn- remove-block-schema
538556
[conn _search-db]
539557
(let [db @conn
@@ -546,7 +564,7 @@
546564
tx-data (mapcat (fn [eid]
547565
(let [entity (d/entity db eid)
548566
schema (:block/schema entity)
549-
schema-properties (sqlite-util/schema->qualified-property-keyword schema)
567+
schema-properties (schema->qualified-property-keyword schema)
550568
hidden-page? (contains? #{common-config/favorites-page-name common-config/views-page-name}
551569
(:block/title entity))
552570
m (assoc schema-properties :db/id eid)

0 commit comments

Comments
 (0)