-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmigrate_sparkeats.cljs
79 lines (57 loc) · 1.87 KB
/
migrate_sparkeats.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
72
73
74
75
76
77
78
79
(ns migrate-sparkeats
{:clj-kondo/config '{:lint-as {promesa.core/let clojure.core/let}}}
(:require
["fs" :as fs]
[promesa.core :as p]
["mysql2/promise" :as mysql]))
;; utilities
(defn select-all [tablename]
(str "SELECT * FROM " tablename))
(defn stringify [rows]
(js/JSON.stringify (clj->js rows)))
(defn base64->image [file-buffer]
(js/Buffer.from (.toString file-buffer "utf-8") "base64"))
(defn js->clj-with-keys [js]
(js->clj js :keywordize-keys true))
(defn has-image? [row]
(not (nil? (:file (js->clj-with-keys row)))))
;; images
(defn write-image-file
":fd is the image filename."
[tablename row]
(let [path (str "data/" tablename)]
(when (not (fs/existsSync path)) (fs/mkdirSync path))
(fs/writeFileSync (str path "/" (:fd row))
(base64->image (:file row)))))
(defn write-image-files [tablename rows]
(->> rows
(map #(js->clj-with-keys %))
(run! #(write-image-file tablename %))))
;; json
(defn write-json-file [tablename rows]
(fs/writeFileSync
(str "data/" tablename ".json")
(stringify rows)))
;; mysql
(def database #js {:host "localhost"
:user "root"
:database "sparkeats"})
(defn migrate-sparkeats [tablenames]
(when (not (fs/existsSync "data")) (fs/mkdirSync "data"))
(p/let [connection (mysql/createConnection database)]
(doseq [tablename tablenames]
(p/let [[rows] (.execute connection (select-all tablename))]
(write-json-file tablename rows)
(when (has-image? (first rows))
(write-image-files tablename rows))))
(.end connection)))
;; entry
(defn -main [& tablenames]
(println "Migrating sparkeats.")
(migrate-sparkeats tablenames))
;; develop
(defn scratch [])
(comment
(-main "place" "placeImage" "review" "reviewImage")
(scratch)
(fs/rmdirSync (str "data/placeImage") #js {:recursive true}))