Skip to content

Commit

Permalink
Merge pull request #58 from CargoSense/secondary-indices
Browse files Browse the repository at this point in the history
Secondary indices
  • Loading branch information
benwilson512 committed Aug 15, 2015
2 parents cf9ee58 + d49b15e commit d01144b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
45 changes: 32 additions & 13 deletions lib/ex_aws/dynamo/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,35 +151,54 @@ defmodule ExAws.Dynamo.Client do
"""
defcallback create_table(
table_name :: binary,
key_schema :: binary | atom,
key_schema :: binary | atom | key_schema,
key_definitions :: key_definitions,
read_capacity :: pos_integer,
write_capacity :: pos_integer) :: ExAws.Request.response_t

@doc """
Create table
@doc """
Create table
key_schema allows specifying hash and / or range keys IE
```
[api_key: :hash, something_rangy: :range]
```
"""
key_schema allows specifying hash and / or range keys IE
```
[api_key: :hash, something_rangy: :range]
```
"""
defcallback create_table(
table_name :: binary,
key_schema :: [key_schema],
key_schema :: key_schema,
key_definitions :: key_definitions,
read_capacity :: pos_integer,
write_capacity :: pos_integer) :: ExAws.Request.response_t

@doc "Create table with indices"
@doc """
Create table with secondary indices
Each index should follow the format outlined here: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html
For convenience, the keys in each index map are allowed to be atoms. IE:
`"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.
Examples
secondary_index = [%{
index_name: "my-global-index",
key_schema: [%{
attribute_name: "email",
attribute_type: "string",
}]
}]
create_table("TestUsers", [id: :hash], %{id: :string}, 1, 1, secondary_index, [])
"""
defcallback create_table(
table_name :: binary,
key_schema :: [key_schema],
key_schema :: key_schema,
key_definitions :: key_definitions,
read_capacity :: pos_integer,
write_capacity :: pos_integer,
global_indexes :: Keyword.t,
local_indexes :: Keyword.t) :: ExAws.Request.response_t
global_indexes :: [Map.t],
local_indexes :: [Map.t]) :: ExAws.Request.response_t

@doc "Describe table"
defcallback describe_table(name :: binary) :: ExAws.Request.response_t
Expand Down
9 changes: 5 additions & 4 deletions lib/ex_aws/dynamo/impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ defmodule ExAws.Dynamo.Impl do
}
}
data = %{
"GlobalSecondaryIndexes" => global_indexes |> camelize_keys(deep: true),
"LocalSecondaryIndexes" => local_indexes |> camelize_keys(deep: true)
"GlobalSecondaryIndexes" => global_indexes |> Enum.map(&camelize_keys(&1, deep: true)),
"LocalSecondaryIndexes" => local_indexes |> Enum.map(&camelize_keys(&1, deep: true))
} |> Enum.reduce(data, fn
({_, indices = %{}}, data) when map_size(indices) == 0 -> data
{name, indices}, data -> Map.put(data, name, Enum.into(indices, %{}))
{_, []}, data -> data
{name, indices}, data ->
Map.put(data, name, indices)
end)

request(client, :create_table, data)
Expand Down
4 changes: 4 additions & 0 deletions lib/ex_aws/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ defmodule ExAws.Utils do
end
end)
end

def camelize_keys([%{} | _] = opts, deep: deep) do
Enum.map(opts, &camelize_keys(&1, deep: deep))
end

def camelize_keys(opts, depth) do
try do
Expand Down
2 changes: 1 addition & 1 deletion test/lib/ex_aws/dynamo/impl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Test.ClientData.Dynamo do

def config_root, do: Application.get_all_env(:ex_aws)

def request(client, _data, _action), do: client
def request(client, _action, _data), do: client
end

defmodule ExAws.Dynamo.ImplTest do
Expand Down
19 changes: 19 additions & 0 deletions test/lib/ex_aws/dynamo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ defmodule ExAws.DynamoTest do
assert Dynamo.create_table("Users", [email: :hash, age: :range], [email: :string, age: :number], 1, 1) == expected
end

test "create_table with secondary indexes" do
expected = %{"AttributeDefinitions" => [%{"AttributeName" => :id, "AttributeType" => "S"}],
"GlobalSecondaryIndexes" => [%{"IndexName" => "my-global-index", "KeySchema" =>
[%{"AttributeName" => "email", "AttributeType" => "string"}]}],
"KeySchema" => [%{"AttributeName" => :id, "KeyType" => "HASH"}],
"LocalSecondaryIndexes" => [%{"IndexName" => "my-global-index", "KeySchema" =>
[%{"AttributeName" => "email", "AttributeType" => "string"}]}],
"ProvisionedThroughput" => %{"ReadCapacityUnits" => 1, "WriteCapacityUnits" => 1}, "TableName" => "TestUsers"}

secondary_index = [%{
index_name: "my-global-index",
key_schema: [%{
attribute_name: "email",
attribute_type: "string",
}]
}]
assert Dynamo.create_table("TestUsers", [id: :hash], %{id: :string}, 1, 1, secondary_index, secondary_index) == expected
end

test "#scan" do
expected = %{"ExclusiveStartKey" => %{api_key: %{"S" => "api_key"}}, "ExpressionAttributeNames" => %{api_key: "#api_key"},
"ExpressionAttributeValues" => %{":api_key" => %{"S" => "asdfasdfasdf"}, ":name" => %{"S" => "bubba"}},
Expand Down

0 comments on commit d01144b

Please sign in to comment.