ExAws.Dynamo (ExAws.Dynamo v4.0.1) View Source
Operations on the AWS DynamoDB service.
NOTE: When Mix.env in [:test, :dev], Dynamo clients will run by default against DynamoDB local.
Basic usage
defmodule User do
@derive [ExAws.Dynamo.Encodable]
defstruct [:email, :name, :age, :admin]
end
alias ExAws.Dynamo
# Create a provisioned users table with a primary key of email [String]
# and 1 unit of read and write capacity
Dynamo.create_table("Users", "email", %{email: :string}, 1, 1)
|> ExAws.request!
user = %User{email: "bubba@foo.com", name: "Bubba", age: 23, admin: false}
# Save the user
Dynamo.put_item("Users", user) |> ExAws.request!
# Retrieve the user by email and decode it as a User struct.
result = Dynamo.get_item("Users", %{email: user.email})
|> ExAws.request!
|> Dynamo.decode_item(as: User)
assert user == result
General notes
All options are handled as underscored atoms instead of camelcased binaries as specified
in the Dynamo API, e.g. IndexName
would be :index_name
. Anywhere in the API that requires
Dynamo type annotation ({"S":"mystring"}
) is handled for you automatically. For example,
ExAws.Dynamo.scan("Users", expression_attribute_values: [api_key: "foo"])
transforms into a query of
%{"ExpressionAttributeValues" => %{api_key: %{"S" => "foo"}}, "TableName" => "Users"}
Consult the function documentation to see precisely which options are handled this way.
If you wish to avoid this kind of automatic behaviour, you are free to specify the types yourself. For example,
ExAws.Dynamo.scan("Users", expression_attribute_values: [api_key: %{"B" => "Treated as binary"}])
becomes
%{"ExpressionAttributeValues" => %{api_key: %{"B" => "Treated as binary"}}, "TableName" => "Users"}
Alternatively, if what's being encoded is a struct, you're always free to implement ExAws.Dynamo.Encodable for that struct.
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Operations.html
Link to this section Summary
Functions
Batch-get up to 100 items (16 MB total max)
Put or delete up to 25 items (16 MB total max)
Create table
Create table with secondary indices
Decode an item returned from Dynamo. This will handle items wrapped in the ordinary
get_item
response map of %{"Item" => item}
.
Delete item in table
Delete Table
Describe table
Describe time to live
Get item from table
List tables
Put item in table
Query Table
Scan table
A synchronous operation that retrieves multiple items from one or more tables (but not from indexes) in a single account and region
A synchronous write operation that groups up to 25 action requests
Update item in table
Update Table
Update time to live
Link to this section Types
Specs
batch_get_item_opts() :: [ {:return_consumed_capacity, return_consumed_capacity_vals()} ]
Specs
batch_write_item_opts() :: [ return_consumed_capacity: return_consumed_capacity_vals(), return_item_collection_metrics: return_item_collection_metrics_vals() ]
Specs
delete_item_opts() :: [ condition_expression: binary(), expression_attribute_names: expression_attribute_names_vals(), expression_attribute_values: expression_attribute_values_vals(), return_consumed_capacity: return_consumed_capacity_vals(), return_item_collection_metrics: return_item_collection_metrics_vals(), return_values: return_values_vals() ]
Specs
dynamo_billing_types() :: :pay_per_request | :provisioned
Specs
dynamo_type_names() :: :blob | :boolean | :blob_set | :list | :map | :number_set | :null | :number | :string | :string_set
Specs
Specs
Specs
expression_attribute_values_vals() :: [{atom(), ExAws.Dynamo.Encodable.t()}] | %{required(atom()) => ExAws.Dynamo.Encodable.t()}
Specs
get_item() :: [ consistent_read: boolean(), keys: [primary_key()], expression_attribute_names: expression_attribute_names_vals(), projection_expression: binary() ]
Specs
get_item_opts() :: [ consistent_read: boolean(), expression_attribute_names: expression_attribute_names_vals(), projection_expression: binary(), return_consumed_capacity: return_consumed_capacity_vals() ]
Specs
key_definitions() :: [{atom() | binary(), dynamo_type_names()}, ...]
Specs
Specs
Specs
put_item_opts() :: [ condition_expression: binary(), expression_attribute_names: expression_attribute_names_vals(), expression_attribute_values: expression_attribute_values_vals(), return_consumed_capacity: return_consumed_capacity_vals(), return_item_collection_metrics: return_item_collection_metrics_vals(), return_values: return_values_vals() ]
Specs
query_opts() :: [ consistent_read: boolean(), exclusive_start_key: exclusive_start_key_vals(), expression_attribute_names: expression_attribute_names_vals(), expression_attribute_values: expression_attribute_values_vals(), filter_expression: binary(), index_name: binary(), key_condition_expression: binary(), limit: pos_integer(), projection_expression: binary(), return_consumed_capacity: return_consumed_capacity_vals(), scan_index_forward: boolean(), select: select_vals() ]
Specs
return_consumed_capacity_vals() :: :none | :total | :indexes
Specs
return_item_collection_metrics_vals() :: :size | :none
Specs
return_values_on_condition_check_failure_vals() :: :all_old | :none
Specs
return_values_vals() :: :none | :all_old | :updated_old | :all_new | :updated_new
Specs
scan_opts() :: [ exclusive_start_key: exclusive_start_key_vals(), expression_attribute_names: expression_attribute_names_vals(), expression_attribute_values: expression_attribute_values_vals(), filter_expression: binary(), index_name: binary(), limit: pos_integer(), projection_expression: binary(), return_consumed_capacity: return_consumed_capacity_vals(), segment: non_neg_integer(), select: select_vals(), total_segments: pos_integer() ]
Specs
select_vals() :: :all_attributes | :all_projected_attributes | :specific_attributes | :count
Specs
table_name() :: binary()
Specs
transact_get_item() :: {table_name :: binary(), primary_key :: primary_key()} | {table_name :: binary(), primary_key :: primary_key(), transact_get_item_opts()}
Specs
transact_get_item_opts() :: [ expression_attribute_names: expression_attribute_names_vals(), projection_expression: binary() ]
Specs
transact_get_items_opts() :: [ {:return_consumed_capacity, return_consumed_capacity_vals()} ]
Specs
transact_standard_item_opts() :: [ condition_expression: binary(), expression_attribute_names: expression_attribute_names_vals(), expression_attribute_values: expression_attribute_values_vals(), return_values_on_condition_check_failure: return_values_on_condition_check_failure_vals() ]
Specs
transact_update_item_opts() :: [ condition_expression: binary(), expression_attribute_names: expression_attribute_names_vals(), expression_attribute_values: expression_attribute_values_vals(), return_values_on_condition_check_failure: return_values_on_condition_check_failure_vals(), update_expression: binary() ]
Specs
transact_write_item() :: {:condition_check, {table_name :: binary(), key :: primary_key(), transact_standard_item_opts()}} | {:delete, {table_name :: binary(), key :: primary_key(), transact_standard_item_opts()}} | {:put, {table_name :: binary(), item :: map(), transact_standard_item_opts()}} | {:update, {table_name :: binary(), key :: primary_key(), transact_update_item_opts()}}
Specs
transact_write_items_opts() :: [ client_request_token: binary(), return_consumed_capacity: return_consumed_capacity_vals(), return_item_collection_metrics: return_item_collection_metrics_vals() ]
Specs
update_item_opts() :: [ condition_expression: binary(), expression_attribute_names: expression_attribute_names_vals(), expression_attribute_values: expression_attribute_values_vals(), return_consumed_capacity: return_consumed_capacity_vals(), return_item_collection_metrics: return_item_collection_metrics_vals(), return_values: return_values_vals(), update_expression: binary() ]
Specs
write_item() :: [ [{:delete_request, [{:key, primary_key()}]}] | [{:put_request, [{:item, map()}]}] ]
Link to this section Functions
Specs
batch_get_item( %{required(table_name()) => get_item()}, opts :: batch_get_item_opts() ) :: ExAws.Operation.JSON.t()
Batch-get up to 100 items (16 MB total max)
Map of table names to request parameter maps. https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html
Parameters with keys that are automatically annotated with dynamo types are:
[:keys]
Dynamo.batch_get_item(%{
"Users" => [
consistent_read: true,
keys: [
[api_key: "key1"],
[api_key: "api_key2"]
],
expression_attribute_names: %{"#api_key" => "api_key"},
projection_expression: "#api_key"
],
"Subscriptions" => %{
keys: [
%{id: "id1"}
]
}
})
As you see, you're largely free to use either keyword args or maps in the body. A map is required for the argument itself because the table names are most often binaries, and I refuse to inflict proplists on anyone.
Specs
batch_write_item( %{required(table_name()) => [write_item()]}, opts :: batch_write_item_opts() ) :: ExAws.Operation.JSON.t()
Put or delete up to 25 items (16 MB total max)
Map of table names to request parameter maps. https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
Parameters with keys that are automatically annotated with Dynamo types are:
[:keys]
create_table(name, primary_key, key_definitions, read_capacity, write_capacity, billing_mode \\ :provisioned)
View SourceSpecs
create_table( table_name :: binary(), key_schema :: binary() | atom() | key_schema(), key_definitions :: key_definitions(), read_capacity :: pos_integer(), write_capacity :: pos_integer(), billing_mode :: dynamo_billing_types() ) :: ExAws.Operation.JSON.t()
Create table
key_schema
can be a simple binary or atom indicating a simple hash key.
billing_mode
may be either :provisioned
(default) or :pay_per_request
.
If you are creating a :pay-per-request
table, you will still need to provide values for read and write capacities,
although they will be ignored - you may consider providing nil
in those cases.
create_table(name, key_schema, key_definitions, read_capacity, write_capacity, global_indexes, local_indexes, billing_mode \\ :provisioned)
View SourceSpecs
create_table( table_name :: binary(), key_schema :: key_schema(), key_definitions :: key_definitions(), read_capacity :: pos_integer(), write_capacity :: pos_integer(), global_indexes :: [Map.t()], local_indexes :: [Map.t()], billing_mode :: dynamo_billing_types() ) :: ExAws.ExAws.Operation.JSON.t()
Create table with secondary indices
Each index should follow the format outlined here: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html
For convenience, the keys in each index map are allowed to be atoms. e.g:
"KeySchema"
in the aws docs can be key_schema:
Note that both the global_indexes
and local_indexes
arguments expect a list of such indices.
billing_mode
may be either :provisioned
(default) or :pay_per_request
.
If you are creating a :pay-per-request
table, you will still need to provide values for read and write capacities,
although they will be ignored - you may consider providing nil
in those cases.
Examples
secondary_index = [%{
index_name: "my-global-index",
key_schema: [%{
attribute_name: "email",
key_type: "HASH",
}],
provisioned_throughput: %{
read_capacity_units: 1,
write_capacity_units: 1,
},
projection: %{
projection_type: "KEYS_ONLY",
}
}]
create_table("TestUsers", [id: :hash], %{id: :string, email: :string}, 1, 1, secondary_index, [])
Specs
decode_item(Map.t(), [{:as, atom()}]) :: Map.t()
Decode an item returned from Dynamo. This will handle items wrapped in the ordinary
get_item
response map of %{"Item" => item}
.
Example
Dynamo.get_item("users", %{id: "asdf"})
|> ExAws.request!
|> Dynamo.decode_item(as: User)
Specs
delete_item( table_name :: table_name(), primary_key :: primary_key(), opts :: delete_item_opts() ) :: ExAws.Operation.JSON.t()
Delete item in table
Specs
delete_table(table :: binary()) :: ExAws.Operation.JSON.t()
Delete Table
Specs
describe_table(name :: binary()) :: ExAws.Operation.JSON.t()
Describe table
Specs
describe_time_to_live(table :: binary()) :: ExAws.Operation.JSON.t()
Describe time to live
Specs
get_item( table_name :: table_name(), primary_key :: primary_key(), opts :: get_item_opts() ) :: ExAws.Operation.JSON.t()
Get item from table
Specs
list_tables() :: ExAws.Operation.JSON.t()
List tables
Specs
put_item(table_name :: table_name(), record :: map(), opts :: put_item_opts()) :: ExAws.Operation.JSON.t()
Put item in table
Specs
query(table_name :: table_name(), opts :: query_opts()) :: ExAws.Operation.JSON.t()
Query Table
Please read https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html
Dynamo.query("Users",
limit: 1,
expression_attribute_values: [desired_api_key: "adminkey"],
key_condition_expression: "api_key = :desired_api_key")
Parameters with keys that are automatically annotated with dynamo types are:
[:exclusive_start_key, :expression_attribute_names]
Specs
scan(table_name :: table_name(), opts :: scan_opts()) :: ExAws.Operation.JSON.t()
Scan table
Please read https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html
Dynamo.scan("Users"
limit: 1,
expression_attribute_values: [desired_api_key: "adminkey"],
expression_attribute_names: %{"#asdf" => "api_key"},
filter_expression: "#asdf = :desired_api_key")
Generally speaking, you won't need to use :expression_attribute_names
. It exists
to alias a column name if one of the columns you want to search against is a reserved Dynamo word,
like Percentile
. In this case, it's totally unnecessary as api_key
is not a reserved word.
Parameters with keys that are automatically annotated with Dynamo types are:
[:exclusive_start_key, :expression_attribute_names]
Specs
transact_get_items(items :: [transact_get_item()], transact_get_items_opts()) :: ExAws.Operation.JSON.t()
A synchronous operation that retrieves multiple items from one or more tables (but not from indexes) in a single account and region
Specs
transact_write_items( items :: [transact_write_item()], transact_write_items_opts() ) :: ExAws.Operation.JSON.t()
A synchronous write operation that groups up to 25 action requests
Specs
update_item( table_name :: table_name(), primary_key :: primary_key(), opts :: update_item_opts() ) :: ExAws.Operation.JSON.t()
Update item in table
For update_args format see https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html
Specs
update_table(name :: binary(), attributes :: Keyword.t() | Map.t()) :: ExAws.Operation.JSON.t()
Update Table
Specs
update_time_to_live( table :: binary(), ttl_attribute :: binary(), enabled :: boolean() ) :: ExAws.Operation.JSON.t()
Update time to live