Skip to content

Instantly share code, notes, and snippets.

@jshy
Created May 10, 2014 02:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jshy/fa209c35d54551a70060 to your computer and use it in GitHub Desktop.
Save jshy/fa209c35d54551a70060 to your computer and use it in GitHub Desktop.
MongoDB REST Interface in Perl (Mojolicious)
#!/usr/bin/env perl
use Mojolicious::Lite;
use MongoDB;
use MongoDB::OID;
my $mongo_port = shift || 27017;
helper 'mongo' => sub {
my ($self, $name) = @_;
my $host = 'localhost:' . $mongo_port;
my $conn = MongoDB::MongoClient->new(host => $host);
};
helper 'value2oid' => sub {
my ($self, $value) = @_;
MongoDB::OID->new($value);
};
# get database base collections
# has the side effect of creating the db if it does not exist and will return an empty array object.
any [qw(GET POST)] => '/:db' => sub {
my $self = shift;
my @collections = $self->mongo->get_database($self->param('db'))->collection_names;
$self->render(json => \@collections);
};
# delete database
del '/:db' => sub {
my $self = shift;
$self->mongo->get_database($self->param('db'))->drop;
$self->render(json => {});
};
# return items. If the collection does not exists, it will not be created till it has/had data
# get '/:db' will not show collections if there were never any collections with data.
get '/:db/:collection' => sub {
my $self = shift;
my @items = map {$_->{_id} = $_->{_id}->value; $_ } $self->mongo->get_database($self->param('db'))->get_collection($self->param('collection'))->find()->all;
$self->render(json => \@items);
};
# delete a collection
del '/:db/:collection' => sub {
my $self = shift;
$self->mongo->get_database($self->param('db'))->get_collection($self->param('collection'))->drop;
$self->render(json => {});
};
# create item
post '/:db/:collection' => sub {
my $self = shift;
my $params = $self->req->params->to_hash;
my $collection = $self->mongo->get_database($self->param('db'))->get_collection($self->param('collection'));
my $oid = $collection->insert($params);
my $item = $collection->find_one({ _id => $oid});
$item->{_id} = $item->{_id}->value;
$self->render(json => $item);
};
# update whole item
# the :id can be anything because the id is taken from the data. It like this to fit my Ember-models url format for PUTs
# if you don't like this behavior just remove the /:id portion.
put '/:db/:collection/:id' => sub {
my $self = shift;
my $params = $self->req->params->to_hash;
$params->{_id} = $self->value2oid($params->{_id}); # make the id a MongoDB::OID
my $collection = $self->mongo->get_database($self->param('db'))->get_collection($self->param('collection'));
$collection->save($params);
my $item = $collection->find_one({ _id => $params->{_id}});
$item->{_id} = $item->{_id}->value;
$self->render(json => $item);
};
# get record
get '/:db/:collection/:id' => sub {
my $self = shift;
my $oid = $self->value2oid($self->param('id'));
my $item = $self->mongo->get_database($self->param('db'))->get_collection($self->param('collection'))->find_one({ _id => $oid});
$item->{_id} = $item->{_id}->value;
$self->render(json => $item);
};
# delete record
del '/:db/:collection/:id' => sub {
my $self = shift;
$self->mongo->get_database($self->param('db'))->get_collection($self->param('collection'))->remove({ _id => $self->value2oid($self->param('id')) });
$self->render(json => {});
};
# get databases names
any '/' => sub {
my $self = shift;
my @names = $self->mongo->database_names;
$self->render( json => \@names );
};
app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment