|
|
@@ -1,12 +1,18 @@
|
|
|
(ns ^:node-only logseq.outliner.db-pipeline
|
|
|
- "This ns provides a datascript listener for DB graphs to add additional changes
|
|
|
- that the frontend also adds per transact.
|
|
|
- Missing features from frontend.worker.pipeline including:
|
|
|
+ "This ns provides a datascript listener for DB graphs and helper fns that
|
|
|
+ build on top of it. The listener adds additional changes that the frontend
|
|
|
+ also adds per transact. Missing features from frontend.worker.pipeline including:
|
|
|
* Deleted blocks don't update effected :block/tx-id
|
|
|
* Delete empty property parent"
|
|
|
- (:require [datascript.core :as d]
|
|
|
+ (:require [clojure.string :as string]
|
|
|
+ [datascript.core :as d]
|
|
|
+ [logseq.db.sqlite.create-graph :as sqlite-create-graph]
|
|
|
+ [logseq.db.sqlite.build :as sqlite-build]
|
|
|
+ [logseq.db.sqlite.db :as sqlite-db]
|
|
|
+ [logseq.outliner.datascript-report :as ds-report]
|
|
|
[logseq.outliner.pipeline :as outliner-pipeline]
|
|
|
- [logseq.outliner.datascript-report :as ds-report]))
|
|
|
+ ["fs" :as fs]
|
|
|
+ ["path" :as node-path]))
|
|
|
|
|
|
|
|
|
(defn- rebuild-block-refs
|
|
|
@@ -40,3 +46,41 @@
|
|
|
[conn]
|
|
|
(d/listen! conn :pipeline-updates (fn pipeline-updates [tx-report]
|
|
|
(invoke-hooks conn tx-report))))
|
|
|
+
|
|
|
+(defn- find-on-classpath [classpath rel-path]
|
|
|
+ (some (fn [dir]
|
|
|
+ (let [f (node-path/join dir rel-path)]
|
|
|
+ (when (fs/existsSync f) f)))
|
|
|
+ (string/split classpath #":")))
|
|
|
+
|
|
|
+(defn- setup-init-data
|
|
|
+ "Setup initial data same as frontend.handler.repo/create-db"
|
|
|
+ [conn {:keys [additional-config classpath]}]
|
|
|
+ (let [config-content
|
|
|
+ (cond-> (or (some-> (find-on-classpath classpath "templates/config.edn") fs/readFileSync str)
|
|
|
+ (do (println "Setting graph's config to empty since no templates/config.edn was found.")
|
|
|
+ "{}"))
|
|
|
+ additional-config
|
|
|
+ ;; TODO: Replace with rewrite-clj when it's available
|
|
|
+ (string/replace-first #"(:file/name-format :triple-lowbar)"
|
|
|
+ (str "$1 "
|
|
|
+ (string/replace-first (str additional-config) #"^\{(.*)\}$" "$1"))))]
|
|
|
+ (d/transact! conn (sqlite-create-graph/build-db-initial-data config-content))))
|
|
|
+
|
|
|
+(defn init-conn
|
|
|
+ "Create sqlite DB, initialize datascript connection and sync listener and then
|
|
|
+ transacts initial data. Takes the following options:
|
|
|
+ * :additional-config - Additional config map to merge into repo config.edn
|
|
|
+ * :classpath - A java classpath string i.e. paths delimited by ':'. Used to find default config.edn
|
|
|
+ that comes with Logseq"
|
|
|
+ [dir db-name & [opts]]
|
|
|
+ (fs/mkdirSync (node-path/join dir db-name) #js {:recursive true})
|
|
|
+ ;; Same order as frontend.db.conn/start!
|
|
|
+ (let [conn (sqlite-db/open-db! dir db-name)]
|
|
|
+ (add-listener conn)
|
|
|
+ (setup-init-data conn opts)
|
|
|
+ conn))
|
|
|
+
|
|
|
+(def build-blocks-tx
|
|
|
+ "An alias for build-blocks-tx to specify default options for this ns"
|
|
|
+ sqlite-build/build-blocks-tx)
|