-
Notifications
You must be signed in to change notification settings - Fork 89
/
app.clj
76 lines (63 loc) · 2.67 KB
/
app.clj
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
(ns app
(:require [datomic.api :as d]
[clojure.tools.nrepl.server :refer [start-server stop-server]]
[clojure.tools.cli :refer [parse-opts]]
[mount :refer [defstate]]
[app.utils.datomic :refer [touch]]
[app.config :refer [app-config]]
[app.nyse :as nyse])
(:gen-class))
;; example on creating a network REPL
(defn- start-nrepl [{:keys [host port]}]
(start-server :bind host :port port))
;; nREPL is just another simple state
(defstate nrepl :start (start-nrepl (:nrepl app-config))
:stop (stop-server nrepl))
;; datomic schema
(defn create-schema [conn]
(let [schema [{:db/id #db/id [:db.part/db]
:db/ident :order/symbol
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/index true
:db.install/_attribute :db.part/db}
{:db/id #db/id [:db.part/db]
:db/ident :order/bid
:db/valueType :db.type/bigdec
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}
{:db/id #db/id [:db.part/db]
:db/ident :order/qty
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}
{:db/id #db/id [:db.part/db]
:db/ident :order/offer
:db/valueType :db.type/bigdec
:db/cardinality :db.cardinality/one
:db.install/_attribute :db.part/db}]]
@(d/transact conn schema)))
(defn add-order [ticker bid offer qty] ;; can take connection as param
@(d/transact nyse/conn [{:db/id (d/tempid :db.part/user)
:order/symbol ticker
:order/bid bid
:order/offer offer
:order/qty qty}]))
(defn find-orders [ticker] ;; can take connection as param
(let [orders (d/q '[:find ?e :in $ ?ticker
:where [?e :order/symbol ?ticker]]
(d/db nyse/conn) ticker)]
(touch nyse/conn orders)))
(defn create-nyse-schema []
(create-schema nyse/conn))
;; "any" regular function to pass arguments
(defn parse-args [args]
(let [opts [["-d" "--datomic-uri [datomic url]" "Datomic URL"
:default "datomic:mem://mount"]
["-h" "--help"]]]
(-> (parse-opts args opts)
:options)))
;; example of an app entry point with arguments
(defn -main [& args]
(mount/start-with-args
(parse-args args)))