Skip to content

Commit 62deed7

Browse files
enhance(cli): add api mode for export-edn command
Also changes :graph from being a required arg to an option
1 parent 60a38f8 commit 62deed7

File tree

7 files changed

+71
-26
lines changed

7 files changed

+71
-26
lines changed

deps/cli/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,15 @@ $ logseq query '(task DOING)' -a my-token
116116
:uuid "68795144-e5f6-48e8-849d-79cd6473b952"}
117117
...
118118
119-
# Export DB graph as markdown
119+
# Export local graph as markdown
120120
$ logseq export yep
121121
Exported 41 pages to yep_markdown_1756128259.zip
122122
123-
# Export DB graph as EDN
124-
$ logseq export-edn woot -f woot.edn
123+
# Export current graph as EDN
124+
$ logseq export-edn -a my-token
125+
Exported 16 properties, 3 classes and 16 pages to yep_1763407592.edn
126+
# Export local graph as EDN to specified file
127+
$ logseq export-edn -g woot -f woot.edn
125128
Exported 16 properties, 1 classes and 36 pages to woot.edn
126129
127130
# Import into current graph with EDN

deps/cli/src/logseq/cli.cljs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@
9191
:args->opts [:graph] :require [:graph]
9292
:spec cli-spec/export}
9393
{:cmds ["export-edn"] :desc "Export DB graph as EDN"
94-
:description "Export a graph to EDN like the in-app graph EDN export. See https://github.com/logseq/docs/blob/master/db-version.md#edn-data-export for more about this export type."
94+
:description "Export a local graph to EDN or the current in-app graph if --api-server-token is given. See https://github.com/logseq/docs/blob/master/db-version.md#edn-data-export for more about this export type."
9595
:fn (lazy-load-fn 'logseq.cli.commands.export-edn/export)
96-
:args->opts [:graph] :require [:graph]
9796
:spec cli-spec/export-edn}
9897
{:cmds ["append"] :desc "Appends text to current page"
9998
:fn (lazy-load-fn 'logseq.cli.commands.append/append)

deps/cli/src/logseq/cli/commands/export_edn.cljs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,44 @@
22
"Export edn command"
33
(:require ["fs" :as fs]
44
[clojure.pprint :as pprint]
5+
[logseq.cli.util :as cli-util]
6+
[logseq.common.util :as common-util]
57
[logseq.db.common.sqlite-cli :as sqlite-cli]
68
[logseq.db.sqlite.export :as sqlite-export]
7-
[logseq.common.util :as common-util]
8-
[logseq.cli.util :as cli-util]))
9+
[logseq.db.sqlite.util :as sqlite-util]
10+
[promesa.core :as p]))
11+
12+
(defn- write-export-edn-map [export-map {:keys [graph file]}]
13+
(let [file' (or file (str graph "_" (quot (common-util/time-ms) 1000) ".edn"))]
14+
(println (str "Exported " (cli-util/summarize-build-edn export-map) " to " file'))
15+
(fs/writeFileSync file' (with-out-str (pprint/pprint export-map)))))
916

10-
(defn export [{{:keys [graph] :as options} :opts}]
17+
(defn- build-export-options [options]
18+
(cond-> {:export-type (:export-type options)}
19+
(= :graph (:export-type options))
20+
(assoc :graph-options (dissoc options :file :export-type :graph))))
21+
22+
(defn- local-export [{{:keys [graph] :as options} :opts}]
23+
(when-not graph
24+
(cli-util/error "Command missing required option 'graph'"))
1125
(if (fs/existsSync (cli-util/get-graph-path graph))
1226
(let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
13-
export-map (sqlite-export/build-export @conn
14-
(cond-> {:export-type (:export-type options)}
15-
(= :graph (:export-type options))
16-
(assoc :graph-options (dissoc options :file :export-type :graph))))
17-
file (or (:file options) (str graph "_" (quot (common-util/time-ms) 1000) ".edn"))]
18-
(println (str "Exported " (cli-util/summarize-build-edn export-map) " to " file))
19-
(fs/writeFileSync file
20-
(with-out-str (pprint/pprint export-map))))
21-
(cli-util/error "Graph" (pr-str graph) "does not exist")))
27+
export-map (sqlite-export/build-export @conn (build-export-options options))]
28+
(write-export-edn-map export-map options))
29+
(cli-util/error "Graph" (pr-str graph) "does not exist")))
30+
31+
(defn- api-export
32+
[{{:keys [api-server-token] :as options} :opts}]
33+
(let [opts (build-export-options options)]
34+
(-> (p/let [resp (cli-util/api-fetch api-server-token "logseq.cli.export_edn" [(clj->js opts)])]
35+
(if (= 200 (.-status resp))
36+
(p/let [body (.json resp)
37+
export-map (sqlite-util/transit-read (aget body "export-body"))]
38+
(write-export-edn-map export-map (assoc options :graph (.-graph body))))
39+
(cli-util/api-handle-error-response resp)))
40+
(p/catch cli-util/command-catch-handler))))
41+
42+
(defn export [{{:keys [api-server-token]} :opts :as m}]
43+
(if api-server-token
44+
(api-export m)
45+
(local-export m)))

deps/cli/src/logseq/cli/commands/import_edn.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
(p/catch cli-util/command-catch-handler)))
2121

2222
(defn- local-import [{:keys [graph]} import-map]
23-
(if (and graph (fs/existsSync (cli-util/get-graph-path graph)))
23+
(if (fs/existsSync (cli-util/get-graph-path graph))
2424
(let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
2525
{:keys [init-tx block-props-tx misc-tx]}
2626
(sqlite-export/build-import import-map @conn {})

deps/cli/src/logseq/cli/spec.cljs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
:desc "File to save export"}})
88

99
(def export-edn
10-
{:include-timestamps? {:alias :T
10+
{:api-server-token {:alias :a
11+
:desc "API server token to export current graph"}
12+
:graph {:alias :g
13+
:desc "Local graph to export"}
14+
:include-timestamps? {:alias :T
1115
:desc "Include timestamps in export"}
1216
:file {:alias :f
1317
:desc "File to save export"}
@@ -27,24 +31,24 @@
2731

2832
(def import-edn
2933
{:api-server-token {:alias :a
30-
:desc "API server token to query current graph"}
34+
:desc "API server token to import into current graph"}
3135
:graph {:alias :g
3236
:desc "Local graph to import into"}
3337
:file {:alias :f
3438
:require true
3539
:desc "EDN File to import"}})
3640

3741
(def query
38-
{:graphs {:alias :g
42+
{:api-server-token {:alias :a
43+
:desc "API server token to query current graph"}
44+
:graphs {:alias :g
3945
:coerce []
4046
:desc "Additional graphs to local query"}
4147
:properties-readable {:alias :p
4248
:coerce :boolean
4349
:desc "Make properties on local, entity queries show property values instead of ids"}
4450
:title-query {:alias :t
45-
:desc "Invoke local query on :block/title"}
46-
:api-server-token {:alias :a
47-
:desc "API server token to query current graph"}})
51+
:desc "Invoke local query on :block/title"}})
4852

4953
(def search
5054
{:api-server-token {:alias :a

src/main/logseq/api.cljs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
(def ^:export get_page_data cli-based-api/get-page-data)
215215
(def ^:export upsert_nodes cli-based-api/upsert-nodes)
216216
(def ^:export import_edn cli-based-api/import-edn)
217+
(def ^:export export_edn cli-based-api/export-edn)
217218

218219
;; file based graph APIs
219220
(def ^:export get_current_graph_templates file-based-api/get_current_graph_templates)

src/main/logseq/api/db_based/cli.cljs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
[frontend.modules.outliner.ui :as ui-outliner-tx]
66
[frontend.state :as state]
77
[logseq.cli.common.mcp.tools :as cli-common-mcp-tools]
8+
[logseq.db.sqlite.util :as sqlite-util]
89
[promesa.core :as p]
9-
[logseq.db.sqlite.util :as sqlite-util]))
10+
[clojure.string :as string]
11+
[logseq.common.config :as common-config]))
1012

1113
(defn list-tags
1214
[options]
@@ -59,4 +61,16 @@
5961
{:outliner-op :batch-import-edn}
6062
(outliner-op/batch-import-edn! edn-data {}))]
6163
(when error (throw (ex-info error {})))
62-
(ui-handler/re-render-root!)))
64+
(ui-handler/re-render-root!)))
65+
66+
(defn export-edn
67+
"Given sqlite.export options, exports the current graph as a json map with the
68+
:export-body key containing a transit string of the export EDN"
69+
[options*]
70+
(p/let [options (-> (js->clj options* :keywordize-keys true)
71+
(update :export-type (fnil keyword :graph)))
72+
result (state/<invoke-db-worker :thread-api/export-edn (state/get-current-repo) options)]
73+
(when (= :export-edn-error result)
74+
(throw (ex-info "Export EDN error" {})))
75+
{:export-body (sqlite-util/transit-write result)
76+
:graph (string/replace-first (state/get-current-repo) common-config/db-version-prefix "")}))

0 commit comments

Comments
 (0)