|
|
@@ -0,0 +1,100 @@
|
|
|
+(ns frontend.handler.db-based.import
|
|
|
+ (:require [clojure.edn :as edn]
|
|
|
+ [frontend.config :as config]
|
|
|
+ [frontend.db :as db]
|
|
|
+ [frontend.handler.notification :as notification]
|
|
|
+ [frontend.handler.repo :as repo-handler]
|
|
|
+ [frontend.persist-db :as persist-db]
|
|
|
+ [frontend.state :as state]
|
|
|
+ [logseq.db :as ldb]
|
|
|
+ [logseq.db.sqlite.export :as sqlite-export]
|
|
|
+ [logseq.db.sqlite.util :as sqlite-util]
|
|
|
+ [logseq.shui.ui :as shui]
|
|
|
+ [promesa.core :as p]))
|
|
|
+
|
|
|
+(defn import-from-sqlite-db!
|
|
|
+ [buffer bare-graph-name finished-ok-handler]
|
|
|
+ (let [graph (str config/db-version-prefix bare-graph-name)]
|
|
|
+ (->
|
|
|
+ (p/do!
|
|
|
+ (persist-db/<import-db graph buffer)
|
|
|
+ (state/add-repo! {:url graph})
|
|
|
+ (repo-handler/restore-and-setup-repo! graph {:import-type :sqlite-db})
|
|
|
+ (state/set-current-repo! graph)
|
|
|
+ (persist-db/<export-db graph {})
|
|
|
+ (db/transact! graph (sqlite-util/import-tx :sqlite-db))
|
|
|
+ (finished-ok-handler))
|
|
|
+ (p/catch
|
|
|
+ (fn [e]
|
|
|
+ (js/console.error e)
|
|
|
+ (notification/show!
|
|
|
+ (str (.-message e))
|
|
|
+ :error))))))
|
|
|
+
|
|
|
+(defn import-from-debug-transit!
|
|
|
+ [bare-graph-name raw finished-ok-handler]
|
|
|
+ (let [graph (str config/db-version-prefix bare-graph-name)
|
|
|
+ datoms (ldb/read-transit-str raw)]
|
|
|
+ (p/do!
|
|
|
+ (persist-db/<new graph {:import-type :debug-transit
|
|
|
+ :datoms datoms})
|
|
|
+ (state/add-repo! {:url graph})
|
|
|
+ (repo-handler/restore-and-setup-repo! graph {:import-type :debug-transit})
|
|
|
+ (db/transact! graph (sqlite-util/import-tx :debug-transit))
|
|
|
+ (state/set-current-repo! graph)
|
|
|
+ (finished-ok-handler))))
|
|
|
+
|
|
|
+(defn- build-and-transact-import-edn
|
|
|
+ "Similar to db-based.export/import-submit"
|
|
|
+ [export-map]
|
|
|
+ (let [{:keys [init-tx block-props-tx misc-tx error] :as _txs}
|
|
|
+ (try
|
|
|
+ (sqlite-export/build-import export-map (db/get-db) {})
|
|
|
+ (catch :default e
|
|
|
+ (js/console.error "Import EDN error: " e)
|
|
|
+ {:error "An unexpected error occurred during import. See the javascript console for details."}))]
|
|
|
+ ;; (cljs.pprint/pprint _txs)
|
|
|
+ (if error
|
|
|
+ (notification/show! error :error)
|
|
|
+ (let [tx-meta {::sqlite-export/imported-data? true}
|
|
|
+ repo (state/get-current-repo)]
|
|
|
+ (-> (p/do
|
|
|
+ (db/transact! repo init-tx tx-meta)
|
|
|
+ (when (seq block-props-tx)
|
|
|
+ (db/transact! repo block-props-tx tx-meta))
|
|
|
+ (when (seq misc-tx)
|
|
|
+ (db/transact! repo misc-tx tx-meta)))
|
|
|
+ (p/catch (fn [e]
|
|
|
+ (js/console.error "Import EDN error: " e)
|
|
|
+ (notification/show! "An unexpected error occurred during import. See the javascript console for details." :error))))))))
|
|
|
+
|
|
|
+(defn import-from-edn!
|
|
|
+ "Creates a new DB graph and imports sqlite.build EDN file"
|
|
|
+ [bare-graph-name file-body finished-ok-handler]
|
|
|
+ (let [graph (str config/db-version-prefix bare-graph-name)
|
|
|
+ finished-error-handler
|
|
|
+ #(do
|
|
|
+ (state/set-state! :graph/importing nil)
|
|
|
+ (shui/dialog-close-all!))
|
|
|
+ edn-data (try
|
|
|
+ (edn/read-string file-body)
|
|
|
+ (catch :default e
|
|
|
+ (js/console.error e)
|
|
|
+ (notification/show! "The given EDN file is not valid EDN. Please fix and try again."
|
|
|
+ :error)
|
|
|
+ (finished-error-handler)
|
|
|
+ nil))]
|
|
|
+ (when (some? edn-data)
|
|
|
+ (-> (p/do!
|
|
|
+ (persist-db/<new graph {:import-type :edn})
|
|
|
+ (state/add-repo! {:url graph})
|
|
|
+ (repo-handler/restore-and-setup-repo! graph {:import-type :edn})
|
|
|
+ (state/set-current-repo! graph)
|
|
|
+ (build-and-transact-import-edn edn-data)
|
|
|
+ (finished-ok-handler))
|
|
|
+ (p/catch
|
|
|
+ (fn [e]
|
|
|
+ (js/console.error e)
|
|
|
+ (notification/show! (str "Unexpected error: " (.-message e))
|
|
|
+ :error)
|
|
|
+ (finished-error-handler)))))))
|