Skip to content

Instantly share code, notes, and snippets.

@toritori0318
Created June 22, 2012 16:49
Show Gist options
  • Select an option

  • Save toritori0318/2973936 to your computer and use it in GitHub Desktop.

Select an option

Save toritori0318/2973936 to your computer and use it in GitHub Desktop.
dynamodb crud script(App::Rad)
accessKeyId=xxxxxxxxxxxxxxxxxxxxxxx
secretAccessKey=yyyyyyyyyyyyyyyyyyyyyy
host=dynamodb.ap-northeast-1.amazonaws.com
use strict;
use warnings;
use 5.10.0;
use Data::Dumper;
use Data::Validator;
use Net::Amazon::DynamoDB;
use App::Rad;
App::Rad->run();
sub setup {
my $c = shift;
$c->register_commands( {
create => 'ユーザ作成',
list => 'ユーザリスト',
update => 'ユーザ更新',
delete => 'ユーザ削除',
});
# setup dynamodb
$c->load_config( 'config');
$c->stash->{dynamo} = Net::Amazon::DynamoDB->new(
access_key => $c->config->{accessKeyId},
secret_key => $c->config->{secretAccessKey},
host => $c->config->{host},
tables => {
'user' => {
hash_key => 'id',
attributes => {
id => 'S',
name => 'S',
}
},
},
);
}
sub create {
create_or_update(@_);
return $c->cmd . "is succeed";
}
sub update {
create_or_update(@_);
return $c->cmd . "is succeed";
}
sub list {
my $c = shift;
# list
my $list = $c->stash->{dynamo}->scan_items(
'user', {}, {},
);
print Dumper $list;
return $c->cmd . "is succeed";
}
sub create_or_update {
my $c = shift;
my $args = eval {
state $rule = Data::Validator->new(
id => { isa => 'Str'},
name => { isa => 'Str'},
);
$rule->validate($c->options);
};
if($@) {
$c->execute('usage') or return undef;
}
# create
$c->stash->{dynamo}->update_item(
'user',
{ name => $args->{id} },
{ id => $args->{id}, },
);
}
sub delete {
my $c = shift;
my $args = eval {
state $rule = Data::Validator->new(
id => { isa => 'Str'},
);
$rule->validate($c->options);
};
if($@) {
$c->execute('usage') or return undef;
}
# delete
$c->stash->{dynamo}->delete_item(
'user',
{ id => $c->options->{id} },
{},
);
return $c->cmd . "is succeed";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment