mirror of https://github.com/logseq/logseq
feat: dynamic db attributes
parent
9d5008d0a4
commit
81b09cdfc8
|
@ -117,12 +117,10 @@
|
||||||
|
|
||||||
(defn get-structured-blocks
|
(defn get-structured-blocks
|
||||||
[db]
|
[db]
|
||||||
(let [special-pages (map #(d/pull db '[*] %) #{:block/tags})
|
(->> (d/datoms db :avet :block/type)
|
||||||
structured-blocks (->> (d/datoms db :avet :block/type)
|
|
||||||
(keep (fn [e]
|
(keep (fn [e]
|
||||||
(when (contains? #{"closed value" "property" "class"} (:v e))
|
(when (contains? #{"closed value" "property" "class"} (:v e))
|
||||||
(d/pull db '[*] (:e e))))))]
|
(d/pull db '[*] (:e e)))))))
|
||||||
(concat special-pages structured-blocks)))
|
|
||||||
|
|
||||||
(defn get-favorites
|
(defn get-favorites
|
||||||
"Favorites page and its blocks"
|
"Favorites page and its blocks"
|
||||||
|
@ -154,7 +152,12 @@
|
||||||
(defn get-initial-data
|
(defn get-initial-data
|
||||||
"Returns current database schema and initial data"
|
"Returns current database schema and initial data"
|
||||||
[db]
|
[db]
|
||||||
(let [schema (:schema db)
|
(let [schema (->> (:schema db)
|
||||||
|
(remove (fn [[k _v]]
|
||||||
|
(or (integer? k)
|
||||||
|
(and (keyword? k)
|
||||||
|
(string/starts-with? (namespace k) "logseq.")))))
|
||||||
|
(into {}))
|
||||||
idents (remove nil?
|
idents (remove nil?
|
||||||
(let [e (d/entity db :logseq.kv/graph-uuid)
|
(let [e (d/entity db :logseq.kv/graph-uuid)
|
||||||
id (:graph/uuid e)]
|
id (:graph/uuid e)]
|
||||||
|
@ -166,9 +169,10 @@
|
||||||
latest-journals (get-latest-journals db 3)
|
latest-journals (get-latest-journals db 3)
|
||||||
all-files (get-all-files db)
|
all-files (get-all-files db)
|
||||||
home-page-data (get-home-page db all-files)
|
home-page-data (get-home-page db all-files)
|
||||||
structured-blocks (get-structured-blocks db)]
|
structured-blocks (get-structured-blocks db)
|
||||||
|
data (concat idents favorites latest-journals all-files home-page-data structured-blocks)]
|
||||||
{:schema schema
|
{:schema schema
|
||||||
:initial-data (concat idents favorites latest-journals all-files home-page-data structured-blocks)}))
|
:initial-data data}))
|
||||||
|
|
||||||
(defn restore-initial-data
|
(defn restore-initial-data
|
||||||
"Given initial sqlite data and schema, returns a datascript connection"
|
"Given initial sqlite data and schema, returns a datascript connection"
|
||||||
|
|
|
@ -44,7 +44,9 @@
|
||||||
(defn build-db-initial-data
|
(defn build-db-initial-data
|
||||||
[config-content]
|
[config-content]
|
||||||
(let [initial-data [(kv :db/type "db")
|
(let [initial-data [(kv :db/type "db")
|
||||||
(kv :schema/version db-schema/version)]
|
(kv :schema/version db-schema/version)
|
||||||
|
;; empty property value
|
||||||
|
{:db/ident :property/empty-placeholder}]
|
||||||
initial-files [{:block/uuid (d/squuid)
|
initial-files [{:block/uuid (d/squuid)
|
||||||
:file/path (str "logseq/" "config.edn")
|
:file/path (str "logseq/" "config.edn")
|
||||||
:file/content config-content
|
:file/content config-content
|
||||||
|
|
|
@ -63,6 +63,8 @@
|
||||||
(assoc :block/created-at updated-at))]
|
(assoc :block/created-at updated-at))]
|
||||||
block))
|
block))
|
||||||
|
|
||||||
|
(def property-ref-types #{:page :block :date :object})
|
||||||
|
|
||||||
(defn build-new-property
|
(defn build-new-property
|
||||||
"Build a standard new property so that it is is consistent across contexts"
|
"Build a standard new property so that it is is consistent across contexts"
|
||||||
[prop-name prop-schema & {:keys [db-ident]}]
|
[prop-name prop-schema & {:keys [db-ident]}]
|
||||||
|
@ -77,7 +79,7 @@
|
||||||
(assoc :db/cardinality :db.cardinality/many)
|
(assoc :db/cardinality :db.cardinality/many)
|
||||||
(not= :many (:cardinality prop-schema))
|
(not= :many (:cardinality prop-schema))
|
||||||
(assoc :db/cardinality :db.cardinality/one)
|
(assoc :db/cardinality :db.cardinality/one)
|
||||||
(contains? #{:page :block :date :object} (:type prop-schema))
|
(contains? property-ref-types (:type prop-schema))
|
||||||
(assoc :db/valueType :db.type/ref)
|
(assoc :db/valueType :db.type/ref)
|
||||||
true
|
true
|
||||||
(assoc :db/index true))))
|
(assoc :db/index true))))
|
||||||
|
|
|
@ -19,7 +19,11 @@
|
||||||
data (persist-db/<fetch-init-data repo)
|
data (persist-db/<fetch-init-data repo)
|
||||||
_ (assert (some? data) "No data found when reloading db")
|
_ (assert (some? data) "No data found when reloading db")
|
||||||
{:keys [schema initial-data]} (dt/read-transit-str data)
|
{:keys [schema initial-data]} (dt/read-transit-str data)
|
||||||
conn (sqlite-common-db/restore-initial-data initial-data schema)
|
conn (try
|
||||||
|
(sqlite-common-db/restore-initial-data initial-data schema)
|
||||||
|
(catch :default e
|
||||||
|
(js/console.error e)
|
||||||
|
(throw e)))
|
||||||
db-name (db-conn/datascript-db repo)
|
db-name (db-conn/datascript-db repo)
|
||||||
_ (swap! db-conn/conns assoc db-name conn)
|
_ (swap! db-conn/conns assoc db-name conn)
|
||||||
end-time (t/now)]
|
end-time (t/now)]
|
||||||
|
|
|
@ -91,19 +91,33 @@
|
||||||
;; :default
|
;; :default
|
||||||
(if (util/uuid-string? v-str) (uuid v-str) v-str))))
|
(if (util/uuid-string? v-str) (uuid v-str) v-str))))
|
||||||
|
|
||||||
|
(defn update-schema
|
||||||
|
[property {:keys [type cardinality]}]
|
||||||
|
(let [ident (:db/ident property)
|
||||||
|
cardinality (if (= cardinality :many) :db.cardinality/many
|
||||||
|
(get property :db/cardinality :db.cardinality/one))
|
||||||
|
type-data (when (and type (sqlite-util/property-ref-types type)) ; type changes
|
||||||
|
{:db/ident ident
|
||||||
|
:db/valueType :db.type/ref
|
||||||
|
:db/cardinality cardinality})]
|
||||||
|
(or type-data
|
||||||
|
{:db/ident ident
|
||||||
|
:db/cardinality cardinality})))
|
||||||
|
|
||||||
(defn upsert-property!
|
(defn upsert-property!
|
||||||
[repo property-id schema {:keys [property-name]}]
|
[repo property-id schema {:keys [property-name]}]
|
||||||
(let [db-ident (or property-id (db-property/get-db-ident-from-name property-name))
|
(let [db-ident (or property-id (db-property/get-db-ident-from-name property-name))
|
||||||
property (db/entity db-ident)
|
property (db/entity db-ident)
|
||||||
k-name (or (:block/original-name property) (name property-name))]
|
k-name (or (:block/original-name property) (name property-name))]
|
||||||
(if property
|
(if property
|
||||||
(db/transact! repo [(cond->
|
(let [tx-data (->>
|
||||||
(outliner-core/block-with-updated-at
|
(conj
|
||||||
|
[(outliner-core/block-with-updated-at
|
||||||
{:db/ident db-ident
|
{:db/ident db-ident
|
||||||
:block/schema schema})
|
:block/schema schema})]
|
||||||
(= :many (:cardinality schema))
|
(update-schema property schema))
|
||||||
(assoc :db/cardinality :db.cardinality/many))]
|
(remove nil?))]
|
||||||
{:outliner-op :save-block})
|
(db/transact! repo tx-data {:outliner-op :save-block}))
|
||||||
(db/transact! repo [(sqlite-util/build-new-property k-name schema {:db-ident db-ident})]
|
(db/transact! repo [(sqlite-util/build-new-property k-name schema {:db-ident db-ident})]
|
||||||
{:outliner-op :new-property}))))
|
{:outliner-op :new-property}))))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue