feat: dynamic db attributes

pull/11196/head
Tienson Qin 2024-03-29 20:33:18 +08:00
parent 9d5008d0a4
commit 81b09cdfc8
5 changed files with 45 additions and 19 deletions

View File

@ -117,12 +117,10 @@
(defn get-structured-blocks
[db]
(let [special-pages (map #(d/pull db '[*] %) #{:block/tags})
structured-blocks (->> (d/datoms db :avet :block/type)
(->> (d/datoms db :avet :block/type)
(keep (fn [e]
(when (contains? #{"closed value" "property" "class"} (:v e))
(d/pull db '[*] (:e e))))))]
(concat special-pages structured-blocks)))
(d/pull db '[*] (:e e)))))))
(defn get-favorites
"Favorites page and its blocks"
@ -154,7 +152,12 @@
(defn get-initial-data
"Returns current database schema and initial data"
[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?
(let [e (d/entity db :logseq.kv/graph-uuid)
id (:graph/uuid e)]
@ -166,9 +169,10 @@
latest-journals (get-latest-journals db 3)
all-files (get-all-files db)
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
:initial-data (concat idents favorites latest-journals all-files home-page-data structured-blocks)}))
:initial-data data}))
(defn restore-initial-data
"Given initial sqlite data and schema, returns a datascript connection"

View File

@ -44,7 +44,9 @@
(defn build-db-initial-data
[config-content]
(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)
:file/path (str "logseq/" "config.edn")
:file/content config-content

View File

@ -63,6 +63,8 @@
(assoc :block/created-at updated-at))]
block))
(def property-ref-types #{:page :block :date :object})
(defn build-new-property
"Build a standard new property so that it is is consistent across contexts"
[prop-name prop-schema & {:keys [db-ident]}]
@ -77,7 +79,7 @@
(assoc :db/cardinality :db.cardinality/many)
(not= :many (:cardinality prop-schema))
(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)
true
(assoc :db/index true))))

View File

@ -19,7 +19,11 @@
data (persist-db/<fetch-init-data repo)
_ (assert (some? data) "No data found when reloading db")
{: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)
_ (swap! db-conn/conns assoc db-name conn)
end-time (t/now)]

View File

@ -91,19 +91,33 @@
;; :default
(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!
[repo property-id schema {:keys [property-name]}]
(let [db-ident (or property-id (db-property/get-db-ident-from-name property-name))
property (db/entity db-ident)
k-name (or (:block/original-name property) (name property-name))]
(if property
(db/transact! repo [(cond->
(outliner-core/block-with-updated-at
(let [tx-data (->>
(conj
[(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})
:block/schema schema})]
(update-schema property schema))
(remove nil?))]
(db/transact! repo tx-data {:outliner-op :save-block}))
(db/transact! repo [(sqlite-util/build-new-property k-name schema {:db-ident db-ident})]
{:outliner-op :new-property}))))