-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamo_db.cljs
102 lines (78 loc) · 3.02 KB
/
dynamo_db.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(ns serverless.aws.dynamo-db
(:require [goog.object :as gobj]
[serverless.aws.sdk :refer [AWS js-call]]
[serverless.core :refer [def-]])
(:refer-clojure :exclude [get update]))
(def- DynamoDB (gobj/get AWS "DynamoDB"))
(def- DocumentClient (gobj/get DynamoDB "DocumentClient"))
(def- Converter (gobj/get DynamoDB "Converter"))
(def unmarshall
(let [unmarshall (gobj/get Converter "unmarshall")]
(fn [item] (-> item clj->js unmarshall (js->clj :keywordize-keys true)))))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Streams (assumes batch size = 1)
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def- record (comp first :Records))
(defn- event-name? [type record]
(= (:eventName record) type))
(def insert? (comp (partial event-name? "INSERT") record))
(def modify? (comp (partial event-name? "MODIFY") record))
(def remove? (comp (partial event-name? "REMOVE") record))
(def old-image (comp unmarshall :OldImage :dynamodb record))
(def new-image (comp unmarshall :NewImage :dynamodb record))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Document Clients
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def- default-client
(new DocumentClient))
(defn document-client [table-name]
(new DocumentClient (clj->js {:params {:TableName table-name}})))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Set Code
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn create-set
([values] (create-set default-client values))
([client values]
(js-invoke client "createSet" (clj->js values))))
(defn set->clj-set [s]
(set (gobj/get s "values")))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Transaction Actions
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- transaction-action [key table-name options]
{key (merge {:TableName table-name} options)})
(def condition-check-action (partial transaction-action :ConditionCheck))
(def delete-action (partial transaction-action :Delete))
(def put-action (partial transaction-action :Put))
(def update-action (partial transaction-action :Update))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Regular Actions
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn delete [client params]
(js-call client "delete" params))
(defn get [client params]
(js-call client "get" params))
(defn put [client params]
(js-call client "put" params))
(defn query [client params]
(js-call client "query" params))
(defn transact-write [client actions]
(js-call client "transactWrite" {:TransactItems actions}))
(defn update [client params]
(js-call client "update" params))
;; Used to generate dependencies for interceptors
(defn table-name->deps [table-name]
(let [client (document-client table-name)]
#:dynamo-db
{;; Transaction methods
:condition-check-action (partial condition-check-action table-name)
:delete-action (partial delete-action table-name)
:put-action (partial put-action table-name)
:update-action (partial update-action table-name)
;; Regular methods
:delete (partial delete client)
:get (partial get client)
:put (partial put client)
:query (partial query client)
:transact-write (partial transact-write client)
:update (partial update client)}))