Skip to content

Commit

Permalink
Merge pull request logseq#11606 from logseq/feat/property-default-value
Browse files Browse the repository at this point in the history
feat: property default value support
  • Loading branch information
tiensonqin authored Nov 26, 2024
2 parents 9aff2d0 + 188af6f commit 47f33c9
Show file tree
Hide file tree
Showing 30 changed files with 708 additions and 302 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ jobs:
- name: Run some ClojureScript tests against DB version
run: DB_GRAPH=1 node static/tests.js -r frontend.db.query-dsl-test

- name: Run ClojureScript query tests against DB version with basic query type
run: DB_GRAPH=1 DB_QUERY_TYPE=basic node static/tests.js -r frontend.db.query-dsl-test

- name: Run ClojureScript tests
run: node static/tests.js -e fix-me

Expand Down
3 changes: 2 additions & 1 deletion deps/db/.carve/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
logseq.db.frontend.schema
logseq.db.frontend.validate
logseq.db.test.helper
logseq.db]
logseq.db
logseq.db.frontend.property.type]
:report {:format :ignore}}
12 changes: 9 additions & 3 deletions deps/db/bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@
(concat (mapcat val rules/rules)
;; TODO: Update linter to handle false positive on ?str-val for :property
(rules/extract-rules (dissoc rules/query-dsl-rules :property))
;; TODO: Update linter to handle false positive on :task, :priority, :property and :private-property
(rules/extract-rules (dissoc rules/db-query-dsl-rules :task :priority :property :private-property)))))}}
;; TODO: Update linter to handle false positive on :task, :priority, :*property* rules
(rules/extract-rules (dissoc rules/db-query-dsl-rules
:task :priority
:property :simple-query-property :private-property
:property-scalar-default-value
:property-missing-value
:has-property-or-default-value)))))}}

:tasks/config
{:large-vars
{:max-lines-count 50}}}
{:max-lines-count 50
:metadata-exceptions #{:large-vars/doc-var}}}}
77 changes: 46 additions & 31 deletions deps/db/src/logseq/db/frontend/entity_plus.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,65 @@
result)]
(or result' default-value))))))

(defn- lookup-kv-with-default-value
[db ^Entity e k default-value]
(or
;; from kv
(get (.-kv e) k)
;; from db
(let [result (lookup-entity e k default-value)]
(if (some? result)
result
;; property default value
(when (qualified-keyword? k)
(when-let [property (d/entity db k)]
(let [schema (lookup-entity property :block/schema nil)]
(if (= :checkbox (:type schema))
(lookup-entity property :logseq.property/scalar-default-value nil)
(lookup-entity property :logseq.property/default-value nil)))))))))

(defn lookup-kv-then-entity
([e k] (lookup-kv-then-entity e k nil))
([^Entity e k default-value]
(try
(when k
(case k
:block/raw-title
(let [db (.-db e)]
(let [db (.-db e)]
(case k
:block/raw-title
(if (and (db-based-graph? db) (= "journal" (:block/type e)))
(get-journal-title db e)
(lookup-entity e :block/title default-value)))
(lookup-entity e :block/title default-value))

:block/properties
(let [db (.-db e)]
:block/properties
(if (db-based-graph? db)
(lookup-entity e :block/properties
(->> (into {} e)
(filter (fn [[k _]] (db-property/property? k)))
(into {})))
(lookup-entity e :block/properties nil)))

;; cache :block/title
:block/title
(or (:block.temp/cached-title @(.-cache e))
(let [title (get-block-title e k default-value)]
(vreset! (.-cache e) (assoc @(.-cache e)
:block.temp/cached-title title))
title))

:block/_parent
(->> (lookup-entity e k default-value)
(remove (fn [e] (or (:logseq.property/created-from-property e)
(:block/closed-value-property e))))
seq)

:block/_raw-parent
(lookup-entity e :block/_parent default-value)

:property/closed-values
(->> (lookup-entity e :block/_closed-value-property default-value)
(sort-by :block/order))

(or (get (.-kv e) k)
(lookup-entity e k default-value))))
(lookup-entity e :block/properties nil))

;; cache :block/title
:block/title
(or (:block.temp/cached-title @(.-cache e))
(let [title (get-block-title e k default-value)]
(vreset! (.-cache e) (assoc @(.-cache e)
:block.temp/cached-title title))
title))

:block/_parent
(->> (lookup-entity e k default-value)
(remove (fn [e] (or (:logseq.property/created-from-property e)
(:block/closed-value-property e))))
seq)

:block/_raw-parent
(lookup-entity e :block/_parent default-value)

:property/closed-values
(->> (lookup-entity e :block/_closed-value-property default-value)
(sort-by :block/order))

(lookup-kv-with-default-value db e k default-value))))
(catch :default e
(js/console.error e)))))

Expand Down
14 changes: 10 additions & 4 deletions deps/db/src/logseq/db/frontend/property.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,16 @@
:public? true
:view-context :page}
:queryable? true}
;; :logseq.property/default-value {:title "Default value"
;; :schema {:type :any
;; :public? true
;; :view-context :property}}
:logseq.property/default-value {:title "Default value"
:schema {:type :entity
:public? false
:hide? true
:view-context :property}}
:logseq.property/scalar-default-value {:title "Non ref type default value"
:schema {:type :any
:public? false
:hide? true
:view-context :property}}
:logseq.property.class/properties {:title "Tag Properties"
:schema {:type :property
:cardinality :many
Expand Down
21 changes: 13 additions & 8 deletions deps/db/src/logseq/db/frontend/property/build.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@
[logseq.db.frontend.property.type :as db-property-type]))

(defn- closed-value-new-block
[block-id value property]
[block-id block-type value property]
(let [property-id (:db/ident property)]
(merge {:block/type "closed value"
:block/format :markdown
:block/uuid block-id
:block/page property-id
:block/closed-value-property property-id
:logseq.property/created-from-property property-id
:logseq.property/created-from-property (if (= property-id :logseq.property/default-value)
[:block/uuid block-id]
property-id)
:block/parent property-id}
(if (db-property-type/original-value-ref-property-types (get-in property [:block/schema :type]))
(if (db-property-type/property-value-content? block-type property)
{:property.value/content value}
{:block/title value}))))

(defn build-closed-value-block
"Builds a closed value block to be transacted"
[block-uuid block-value property {:keys [db-ident icon]}]
[block-uuid block-type block-value property {:keys [db-ident icon]}]
(assert block-uuid (uuid? block-uuid))
(cond->
(closed-value-new-block block-uuid block-value property)
(closed-value-new-block block-uuid block-type block-value property)
(and db-ident (keyword? db-ident))
(assoc :db/ident db-ident)

Expand All @@ -36,10 +38,11 @@

(defn closed-values->blocks
[property]
(map (fn [{uuid' :uuid :keys [db-ident value icon]}]
(map (fn [{uuid' :uuid :keys [db-ident value icon schema]}]
(cond->
(build-closed-value-block
uuid'
(:type schema)
value
property
{:db-ident db-ident :icon icon})
Expand Down Expand Up @@ -71,9 +74,11 @@
;; page block
(:db/id block))
:block/parent (:db/id block)
:logseq.property/created-from-property (or (:db/id property) {:db/ident (:db/ident property)})
:logseq.property/created-from-property (if (= (:db/ident property) :logseq.property/default-value)
(:db/id block)
(or (:db/id property) {:db/ident (:db/ident property)}))
:block/order (db-order/gen-key)}
(if (db-property-type/original-value-ref-property-types (get-in property [:block/schema :type]))
(if (db-property-type/property-value-content? (get-in block [:block/schema :type]) property)
{:property.value/content value}
{:block/title value}))
sqlite-util/block-with-timestamps))
Expand Down
16 changes: 16 additions & 0 deletions deps/db/src/logseq/db/frontend/property/type.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
"Valid property types that can change cardinality"
#{:default :number :url :date :node})

(def default-value-ref-property-types
"Valid ref property :type for default value support"
#{:default :number :checkbox})

(def text-ref-property-types
"Valid ref property :types that support text"
#{:default :url :entity})

(assert (set/subset? cardinality-property-types (set user-built-in-property-types))
"All closed value types are valid property types")

Expand Down Expand Up @@ -188,3 +196,11 @@
(url? val) :url
(contains? #{true false} val) :checkbox
:else :default))

(defn property-value-content?
"Whether property value should be stored in :property.value/content"
[block-type property]
(or
(original-value-ref-property-types (get-in property [:block/schema :type]))
(and (= (:db/ident property) :logseq.property/default-value)
(original-value-ref-property-types block-type))))
Loading

0 comments on commit 47f33c9

Please sign in to comment.