A friendly turtle to harness the power of DynamoDB.
Leiningen and Boot
[ctorrisi/franklin "0.0.1-alpha5"]
deps
{:deps {ctorrisi/franklin {:mvn/version "0.0.1-alpha5"}}}
Use aws-api for legacy operations and operations not related to queries or persistence.
All operations are centred around being executed on a single table (table-centric).
(ns franklin.example (:require [franklin.core :as f]))
In order to demonstrate these operations, assume the following table named user_location
exists with a String
typed partition key named user_name
and a Number
typed sort key named time_stamp
.
The minimum to create a table context for a table named user_location
.
(def user-location-ctx (f/make-table-context "user_location"))
The definition above is fine if your program is using a single table with default AWS connection/credential options.
make-client
is a convenience function that wraps cognitect.aws.client.api/client
with {:api :dynamodb}
.
(def ddb-client (f/make-client {:region "ap-southeast-2"}))
(def ctx (f/make-table-context "user_location" {:client ddb-client}))
(def other-ctx (f/make-table-context "user_details" {:client ddb-client}))
(f/put-item ctx {:item {:user_name "corey"
:time_stamp 1564641545000
:latitude -37.813629
:longitude 144.963058}})
(f/update-item ctx {:key {:user_name "corey"
:time_stamp 1564641545000}
:update-expr "set latitude = :lat"
:expr-attr-vals {:lat -37.809010}})
(f/get-item ctx {:key {:user_name "corey"
:time_stamp 1564641545000}})
=> {:Item {:user_name "corey"
:time_stamp 1564641545000
:latitude -37.80901
:longitude 144.963058}}
(f/query ctx {:partition-key "corey"
:sort-key 1564641545000})
=> {:Items [{:time_stamp 1564641545000 :user_name "corey" :latitude -37.813629 :longitude 144.963058}]
:Count 1
:ScannedCount 1}
(f/query ctx {:partition-key "corey"
:sort-key {:between [1564641544999 1564641545001]}
:projections [:latitude "longitude"]})
=> {:Items [{:latitude -37.813629 :longitude 144.963058}]
:Count 1
:ScannedCount 1}
Supports all of the comparison operators available in DynamoDB's Query Key Condition Expression.
{:sort-key {:= x}}
{:sort-key x} ; equals is implicit, same as above.
{:sort-key {:< x}}
{:sort-key {:<= x}}
{:sort-key {:> x}}
{:sort-key {:>= x}}
{:sort-key {:between [x y]}}
{:sort-key {:begins_with x}}
; also supports a lower-case string variant for the comparison operator, i.e.
{:sort-key {"=" x}}
{:sort-key {"between" [x y]}}
(f/scan ctx)
=> {:Items [{:user_name "corey" :latitude -37.80901 :longitude 144.963058 :time_stamp 1564641545000}]
:Count 1
:ScannedCount 1}
To delete an item, assoc
the :delete?
key with a truthy value in the item's map.
(f/batch-write-item ctx {:items [{:user_name "alice"
:time_stamp 1564641565850}
{:user_name "bob"
:time_stamp 1564641575140}
{:user_name "corey"
:time_stamp 1564641545000
:delete? true}
{:user_name "corey"
:time_stamp 100}
{:user_name "corey"
:time_stamp 200}]})
(f/scan ctx)
=> {:Items [{:time_stamp 1564641575140, :user_name "bob"}
{:time_stamp 1564641565850, :user_name "alice"}
{:time_stamp 100, :user_name "corey"}
{:time_stamp 200, :user_name "corey"}]
:Count 3,
:ScannedCount 3}
(f/batch-get-item ctx {:keys [{:user_name "alice"
:time_stamp 1564641565850}
{:user_name "bob"
:time_stamp 1564641575140}]})
=> {:Responses {:user_location [{:time_stamp 1564641565850, :user_name "alice"}
{:time_stamp 1564641575140, :user_name "bob"}]}
:UnprocessedKeys {}}
(f/query-first-item ctx {:partition-key "corey"})
=> {:time_stamp 100 :user_name "corey"}
(f/query-last-item ctx {:partition-key "corey"})
=> {:time_stamp 200 :user_name "corey"}
Thanks to:
Distributed under the Eclipse Public License - v 2.0
Copyright © 2019 Corey Torrisi
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close