aws-dynamodb.cr

Crystal client for AWS DynamoDB

aws-dynamodb

Crystal client for AWS DynamoDB.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      aws-dynamodb:
        github: veelenga/aws-dynamodb.cr
    
  2. Run shards install

Usage

Initialize client

require "aws/dynamodb"

client = Aws::DynamoDB::Client.new(
  region: ENV["AWS_REGION"],
  aws_access_key_id: ENV["AWS_ACCESS_KEY_ID"],
  aws_secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
  endpoint: ENV["DYNAMODB_URL"]
)

Create table

client.create_table(
  TableName: "Movies",
  AttributeDefinitions: [
    {
      AttributeName: "Year",
      AttributeType: "N"
    },
    {
      AttributeName: "Name",
      AttributeType: "S"
    }
  ],
  KeySchema: [
    {
      AttributeName: "Name",
      KeyType:       "HASH"
    },
    {
      AttributeName: "Year",
      KeyType:       "RANGE"
    }
  ],
  ProvisionedThroughput: {
    ReadCapacityUnits:  5,
    WriteCapacityUnits: 5
  }
)

Put Item

client.put_item(
  TableName: "Movies",
  Item: {
    Year: { N: 2008 },
    Name: { S: "The Dark Knight" }
  }
)

Get Item

response = client.get_item(
  TableName: "Movies",
  Key: {
    Year: { N: 2008 },
    Name: { S: "The Dark Knight" }
  }
)
response["Item"].try &.["Name"].s #=> "The Dark Knight"

Development

  1. Setting Up DynamoDB Local. Alternatively it can be running in a container:
$ docker pull amazon/dynamodb-local
$ docker run -p 8000:8000 amazon/dynamodb-local
  1. Pass credentials + DB URL and run the examples:
$ AWS_REGION=..\
  AWS_ACCESS_KEY_ID=...\
  AWS_SECRET_ACCESS_KEY=...\
  DYNAMODB_URL=http://localhost:8000\
  crystal examples/put_get_item.cr

Contributors

Repository

aws-dynamodb.cr

Owner
Statistic
  • 4
  • 0
  • 0
  • 0
  • 3
  • about 4 years ago
  • November 24, 2020
License

MIT License

Links
Synced at

Wed, 25 Dec 2024 19:11:30 GMT

Languages