MongoDBx::Tiny - Simple Mongo ORM for Perl
Version 0.04
# -------------------- package My::Data; use MongoDBx::Tiny; CONNECT_INFO host => 'localhost', port => 27017; DATABASE_NAME 'my_data'; # -------------------- package My::Data::Foo; use MongoDBx::Tiny::Document; COLLECTION_NAME 'foo'; ESSENTIAL qw/code/; FIELD 'code', INT, LENGTH(10), DEFAULT('0'), REQUIRED; FIELD 'name', STR, LENGTH(30), DEFAULT('noname'); # -------------------- package main; my $tiny = My::Data->new; $tiny->insert(foo => { code => 123, name => "foo_123"}) or die $!; my $foo = $tiny->single(foo => { code => 123}); $foo->name('foo_321'); $foo->update; $foo->remove;
A list of functions that can be exported.
CONNECT_INFO host => 'localhost', port => 27017;
DATABASE_NAME 'my_data';
LOAD_PLUGIN 'One'; # MongoDBx::Tiny::Plugin::One LOAD_PLUGIN 'Two'; LOAD_PLUGIN '+Class::Name';
my $tiny = My::Data->new(); # or you can specify connect_info, database_name. my $tiny = My::Data->new({ connect_info => [ host => 'localhost', port => 27017 ], database_name => 'my_data', });
returns MongoDB::Connection. you can override how to get connection object. sub get_connection { my $class = shift; return MongoDB::Connection->new(@{$class->CONNECT_INFO}) if !$ENV{PLACK_ENV}; my $key = 'some_key'; if (in_scope_container() and my $con = scope_container($key)) { return $con; } else { my $con = MongoDB::Connection->new(@{$class->CONNECT_INFO}); scope_container($key, $con) if in_scope_container(); return $con; } }
alias to installed value my $connect_info = $tiny->connect_info; my $database_name = $tiny->database_name;
alias to connection and database object. my $connection = $tiny->connection; # MongoDB::Connection object my $database = $tiny->database; # MongoDB::Database object
override if you want. MongoDBx::Tiny::Cursor MongoDBx::Tiny::Validator MongoDBx::Tiny::GridFS
returns MongoDB::Collection $collection = $tiny->collection('collection_name')
just (re)connect & disconnect
$document_object = $tiny->insert('collection_name' => $document);
returns MongoDBx::Tiny::Document object. $document_object = $tiny->single('collection_name' => $MongoDB_oid_object); $tiny->single('collection_name' => $oid_text); $query = { field => $val }; $tiny->single('collection_name' => $query);
returns MongoDBx::Tiny::Cursor object. $query = { field => $val }; $cursor = $tiny->search('collection_name' => $query); while (my $object = $cursor->next) { # warn $object->id; } # list context @object = $tiny->search('collection_name' => $query);
$tiny->update('collection_name',$query,$document);
$tiny->remove('collection_name',$query);
$count_num = $tiny->count('collection_name',$query);
$document_object = $tiny->document_to_object('collection_name',$document);
$validator = $tiny->validate('collecion_name',$document,$opt); my $validator = $tiny->validate( 'foo', { code => 123, name => "foo_123"}, { state => 'insert' } ); my $foo1 = $tiny->insert( 'foo', $validator->document, { state => 'insert', no_validate => 1 } ); # erros: [{ field => 'field1', code => 'errorcode', message => 'message1' },,,] my @erros = $validator->erros; my @fields = $validator->errors('field'); my @error_code = $validator->errors('code'); my @error_message = $validator->errors('message');
returns MongoDBx::Tiny::GridFS $gridfs = $tiny->gridfs(); $gridfs = $tiny->gridfs({database => $mongo_databse_object }); $gridfs = $tiny->gridfs({fields => 'other_filename' }); my $gridfs = $tiny->gridfs; $gridfs->put('/tmp/foo.txt', {"filename" => 'foo.txt' }); my $foo_txt = $gridfs->get({ filename => 'foo.txt' })->slurp; $gridfs->put('/tmp/bar.txt','bar.txt'); my $bar_txt = $gridfs->get('bar.txt')->slurp;
$document_class_name = $tiny->document_class('collecion_name');
# -------------------- package MyDB; use MongoDBx::Tiny; LOAD_PLUGIN('PluginName'); LOAD_PLUGIN('+Class::Name'); # -------------------- package MongoDBx::Tiny::Plugin::PluginName; use strict; use warnings; use utf8; our @EXPORT = qw/function_for_plugin/; sub function_for_plugin {} # -------------------- $tiny->function_for_plugin;
[EXPERIMENTAL] $tiny->process('collecion_name','some',@args); $tiny->process('foo','some',$validator,$arg); # just call Data::Foo::process_some # sub process_foo { my ($class,$tiny,$validator,$arg) = @_; }
[EXPERIMENTAL] $tiny->set_indexes('collection_name');
[EXPERIMENTAL] # drop indexes without "_id"; $tiny->unset_indexes('collection_name');
https://github.com/naoto43/mongodbx-tiny/
Naoto ISHIKAWA, <toona at seesaa.co.jp>
<toona at seesaa.co.jp>
Copyright 2013 Naoto ISHIKAWA.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install MongoDBx::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MongoDBx::Tiny
CPAN shell
perl -MCPAN -e shell install MongoDBx::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.