-
Notifications
You must be signed in to change notification settings - Fork 3
/
query.cljs
71 lines (60 loc) · 1.65 KB
/
query.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(ns query
"Script that queries a local graph with given query"
(:require ["fs" :as fs]
["path" :as path]
["os" :as os]
[datascript.transit :as dt]
[datascript.core :as d]
[clojure.pprint :as pprint]
[clojure.edn :as edn]
[nbb.core :as nbb])
(:refer-clojure :exclude [exists?]))
;; fs utils
(defn expand-home
[path]
(path/join (os/homedir) path))
(defn entries
[dir]
(fs/readdirSync dir))
(defn slurp
[file]
(fs/readFileSync file))
(defn exists?
[file]
(fs/existsSync file))
(defn directory?
[file]
(.isDirectory (fs/lstatSync file)))
;; graph utils
(defn get-graph-paths
[]
(let [dir (expand-home ".logseq/graphs")]
(->> (entries dir)
(filter #(re-find #".transit$" %))
(map #(str dir "/" %)))))
(defn full-path->graph
[path]
(second (re-find #"\+\+([^\+]+).transit$" path)))
(defn get-graph-path
[graph]
(some #(when (= graph (full-path->graph %)) %)
(get-graph-paths)))
(defn- get-graph-db
[graph]
(when-let [file (or (get-graph-path graph)
;; graph is a path
graph)]
(when (exists? file)
(-> file slurp dt/read-transit-str))))
(defn -main
[args]
(if-not (= 2 (count args))
(println "Usage: $0 GRAPH QUERY")
(let [[graph-name query] args
db (or (get-graph-db graph-name)
(throw (ex-info "No graph found" {:graph graph-name})))
query' (edn/read-string query)
results (map first (d/q query' db))]
(pprint/pprint results))))
(when (= nbb/*file* (:file (meta #'-main)))
(-main *command-line-args*))