mirror of https://github.com/logseq/logseq
enhance: consistently create sanitized property ident
for new properties. Also made build-new-property easier to use by making property name optionalpull/11196/head
parent
3793cf6d81
commit
009e5b6d62
|
@ -275,9 +275,9 @@
|
|||
(when (= (closed-value-name e) value-name)
|
||||
e))) values)))
|
||||
|
||||
|
||||
;; TODO: db ident should obey clojure's rules for keywords
|
||||
(defn get-db-ident-from-name
|
||||
(defn user-property-ident-from-name
|
||||
"Makes a user property :db/ident from a name by sanitizing the given name"
|
||||
[property-name]
|
||||
(let [n (-> (string/lower-case property-name)
|
||||
(string/replace #"^:\s*" "")
|
||||
|
@ -285,8 +285,8 @@
|
|||
(string/replace " " "-")
|
||||
(string/replace "#" "")
|
||||
(string/trim))]
|
||||
(when-not (string/blank? n)
|
||||
(keyword "user.property" n))))
|
||||
(assert (seq n) "name is not empty")
|
||||
(keyword "user.property" n)))
|
||||
|
||||
(defn get-class-ordered-properties
|
||||
[class-entity]
|
||||
|
|
|
@ -63,7 +63,7 @@
|
|||
(: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 db-ident prop-name property-schema)
|
||||
property-tx (merge (sqlite-util/build-new-property db-ident property-schema {:original-name prop-name})
|
||||
property-attributes)]
|
||||
(into [property-tx page-tx]
|
||||
(when-not closed-value-page-uuids? closed-value-blocks-tx))))
|
||||
|
|
|
@ -28,8 +28,8 @@
|
|||
{})
|
||||
[(sqlite-util/build-new-property
|
||||
db-ident
|
||||
prop-name
|
||||
schema)])]
|
||||
schema
|
||||
{:original-name prop-name})])]
|
||||
(update blocks 0 default-db/mark-block-as-built-in)))
|
||||
built-in-properties)))
|
||||
|
||||
|
|
|
@ -68,31 +68,32 @@
|
|||
(def property-ref-types #{:page :block :date :entity})
|
||||
|
||||
(defn build-new-property
|
||||
"Build a standard new property so that it is is consistent across contexts"
|
||||
[db-ident prop-name prop-schema]
|
||||
(assert (keyword? db-ident))
|
||||
(let [db-ident' (if (or (db-property/property? db-ident)
|
||||
(db-property/db-attribute-properties db-ident))
|
||||
db-ident
|
||||
(keyword "user.property" (name db-ident)))]
|
||||
(block-with-timestamps
|
||||
(cond->
|
||||
{:db/ident db-ident'
|
||||
:block/type "property"
|
||||
:block/journal? false
|
||||
:block/format :markdown
|
||||
:block/schema (merge {:type :default} prop-schema)
|
||||
:block/name (common-util/page-name-sanity-lc (name prop-name))
|
||||
:block/uuid (d/squuid)
|
||||
:block/original-name (name prop-name)}
|
||||
(= :many (:cardinality prop-schema))
|
||||
(assoc :db/cardinality :db.cardinality/many)
|
||||
(not= :many (:cardinality prop-schema))
|
||||
(assoc :db/cardinality :db.cardinality/one)
|
||||
(contains? property-ref-types (:type prop-schema))
|
||||
(assoc :db/valueType :db.type/ref)
|
||||
true
|
||||
(assoc :db/index true)))))
|
||||
"Build a standard new property so that it is is consistent across contexts. Takes
|
||||
an optional map with following keys:
|
||||
* :original-name - Case sensitive property name. Defaults to deriving this from db-ident"
|
||||
([db-ident prop-schema] (build-new-property db-ident prop-schema {}))
|
||||
([db-ident prop-schema {:keys [original-name]}]
|
||||
(assert (keyword? db-ident))
|
||||
(let [db-ident' (if (qualified-keyword? db-ident)
|
||||
db-ident
|
||||
(db-property/user-property-ident-from-name (name db-ident)))
|
||||
prop-name (or original-name (name db-ident'))]
|
||||
(block-with-timestamps
|
||||
(cond->
|
||||
{:db/ident db-ident'
|
||||
:block/type "property"
|
||||
:block/journal? false
|
||||
:block/format :markdown
|
||||
:block/schema (merge {:type :default} prop-schema)
|
||||
:block/name (common-util/page-name-sanity-lc (name prop-name))
|
||||
:block/uuid (d/squuid)
|
||||
:block/original-name (name prop-name)
|
||||
:db/index true
|
||||
:db/cardinality (if (= :many (:cardinality prop-schema))
|
||||
:db.cardinality/many
|
||||
:db.cardinality/one)}
|
||||
(contains? property-ref-types (:type prop-schema))
|
||||
(assoc :db/valueType :db.type/ref))))))
|
||||
|
||||
|
||||
(defn build-new-class
|
||||
|
|
|
@ -19,8 +19,8 @@
|
|||
|
||||
(deftest has-page-property-rule
|
||||
(let [conn (new-db-conn)
|
||||
_ (d/transact! conn [(sqlite-util/build-new-property :user.property/foo "foo" {})
|
||||
(sqlite-util/build-new-property :user.property/foo2 "foo2" {})
|
||||
_ (d/transact! conn [(sqlite-util/build-new-property :user.property/foo {})
|
||||
(sqlite-util/build-new-property :user.property/foo2 {})
|
||||
(sqlite-util/build-new-page "Page")
|
||||
{:block/original-name "Page" :user.property/foo "bar"}])]
|
||||
(is (= ["Page"]
|
||||
|
@ -41,10 +41,10 @@
|
|||
|
||||
(deftest page-property-rule
|
||||
(let [conn (new-db-conn)
|
||||
_ (d/transact! conn [(sqlite-util/build-new-property :user.property/foo "foo" {})
|
||||
(sqlite-util/build-new-property :user.property/foo2 "foo2" {})
|
||||
(sqlite-util/build-new-property :user.property/number-many "number-many" {:type :number :cardinality :many})
|
||||
(sqlite-util/build-new-property :user.property/page-many "page-many" {:type :page :cardinality :many})
|
||||
_ (d/transact! conn [(sqlite-util/build-new-property :user.property/foo {})
|
||||
(sqlite-util/build-new-property :user.property/foo2 {})
|
||||
(sqlite-util/build-new-property :user.property/number-many {:type :number :cardinality :many})
|
||||
(sqlite-util/build-new-property :user.property/page-many {:type :page :cardinality :many})
|
||||
(sqlite-util/build-new-page "Page")
|
||||
(sqlite-util/build-new-page "Page A")
|
||||
{:block/original-name "Page" :user.property/foo "bar"}
|
||||
|
|
|
@ -68,7 +68,7 @@
|
|||
(->> properties
|
||||
(map
|
||||
(fn [[prop-name val]]
|
||||
[(db-property/get-db-ident-from-name (name prop-name))
|
||||
[(db-property/user-property-ident-from-name (name prop-name))
|
||||
;; set indicates a :many value
|
||||
(if (set? val)
|
||||
(set (map #(translate-property-value % uuid-maps) val))
|
||||
|
@ -105,7 +105,7 @@
|
|||
(defn- build-property-refs [properties property-db-ids]
|
||||
(mapv
|
||||
(fn [prop-name]
|
||||
(db-property/get-db-ident-from-name (name prop-name)))
|
||||
(db-property/user-property-ident-from-name (name prop-name)))
|
||||
(keys properties)))
|
||||
|
||||
(def current-db-id (atom 0))
|
||||
|
@ -168,7 +168,7 @@
|
|||
(mapcat
|
||||
(fn [[prop-name]]
|
||||
(if (get-in properties [prop-name :closed-values])
|
||||
(let [db-ident (db-property/get-db-ident-from-name (name prop-name))]
|
||||
(let [db-ident (db-property/user-property-ident-from-name (name prop-name))]
|
||||
(db-property-util/build-closed-values
|
||||
db-ident
|
||||
prop-name
|
||||
|
@ -178,14 +178,13 @@
|
|||
:property-attributes
|
||||
{:db/id (or (property-db-ids (name prop-name))
|
||||
(throw (ex-info "No :db/id for property" {:property prop-name})))}}))
|
||||
[(let [db-ident (keyword "user.property" (name prop-name))]
|
||||
(merge
|
||||
(sqlite-util/build-new-property db-ident prop-name (get-in properties [prop-name :block/schema]))
|
||||
{:db/id (or (property-db-ids (name prop-name))
|
||||
(throw (ex-info "No :db/id for property" {:property prop-name})))}
|
||||
(when-let [props (not-empty (get-in properties [prop-name :properties]))]
|
||||
(assoc (->block-properties-tx props uuid-maps)
|
||||
:block/refs (build-property-refs props property-db-ids)))))]))
|
||||
[(merge
|
||||
(sqlite-util/build-new-property prop-name (get-in properties [prop-name :block/schema]))
|
||||
{:db/id (or (property-db-ids (name prop-name))
|
||||
(throw (ex-info "No :db/id for property" {:property prop-name})))}
|
||||
(when-let [props (not-empty (get-in properties [prop-name :properties]))]
|
||||
(assoc (->block-properties-tx props uuid-maps)
|
||||
:block/refs (build-property-refs props property-db-ids))))]))
|
||||
property-uuids))
|
||||
pages-and-blocks-tx
|
||||
(vec
|
||||
|
|
|
@ -108,7 +108,7 @@
|
|||
Two main ways to create a property are to set property-id to a qualified keyword
|
||||
or to set it to nil and pass :property-name as an option"
|
||||
[repo property-id schema {:keys [property-name properties]}]
|
||||
(let [db-ident (or property-id (db-property/get-db-ident-from-name property-name))]
|
||||
(let [db-ident (or property-id (db-property/user-property-ident-from-name property-name))]
|
||||
(assert (qualified-keyword? db-ident))
|
||||
(if-let [property (db/entity db-ident)]
|
||||
(let [tx-data (->>
|
||||
|
@ -128,7 +128,7 @@
|
|||
(name property-id))]
|
||||
(assert (some? k-name)
|
||||
(prn "property-id: " property-id ", property-name: " property-name))
|
||||
(db/transact! repo [(sqlite-util/build-new-property db-ident k-name schema)]
|
||||
(db/transact! repo [(sqlite-util/build-new-property db-ident schema {:original-name k-name})]
|
||||
{:outliner-op :new-property})))))
|
||||
|
||||
(defn validate-property-value
|
||||
|
@ -199,7 +199,7 @@
|
|||
[repo block-eid property-id v {:keys [property-name] :as opts}]
|
||||
(let [block-eid (->eid block-eid)
|
||||
property-id (if (string? property-id)
|
||||
(db-property/get-db-ident-from-name property-id)
|
||||
(db-property/user-property-ident-from-name property-id)
|
||||
property-id)
|
||||
_ (assert (keyword? property-id) "property-id should be a keyword")
|
||||
block (db/entity repo block-eid)
|
||||
|
@ -268,7 +268,7 @@
|
|||
(if (string? property-id)
|
||||
(if-let [ent (db/entity [:block/original-name property-id])]
|
||||
[(:db/ident ent) ent]
|
||||
[(db-property/get-db-ident-from-name property-id) nil])
|
||||
[(db-property/user-property-ident-from-name property-id) nil])
|
||||
[property-id (db/entity property-id)])
|
||||
property-type (get-in property [:block/schema :type])
|
||||
_ (upsert-property! repo db-ident
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
(name key)
|
||||
key)]
|
||||
(if (sqlite-util/db-based-graph? repo)
|
||||
(when-let [property (d/entity db (db-property/get-db-ident-from-name property-name))]
|
||||
(when-let [property (d/entity db (db-property/user-property-ident-from-name property-name))]
|
||||
(get coll (:block/uuid property)))
|
||||
(get coll key))))
|
||||
|
||||
|
|
Loading…
Reference in New Issue