Naoto Ishikawa > MongoDBx-Tiny-0.03 > MongoDBx::Tiny

Download:
MongoDBx-Tiny-0.03.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.03   Source  

NAME ^

MongoDBx::Tiny - Simple Mongo ORM for Perl

VERSION ^

Version 0.03

SYNOPSIS ^

  # --------------------
  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;

EXPORT ^

A list of functions that can be exported.

CONNECT_INFO

      CONNECT_INFO  host => 'localhost', port => 27017;

DATABASE_NAME

      DATABASE_NAME 'my_data';

LOAD_PLUGIN

      LOAD_PLUGIN 'One'; # MongoDBx::Tiny::Plugin::One
      LOAD_PLUGIN 'Two';
      LOAD_PLUGIN '+Class::Name';

SUBROUTINES/METHODS ^

new

    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',
    });

get_connection

  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;
        }
    }

connect_info, database_name

  alias to installed value

    my $connect_info  = $tiny->connect_info;

    my $database_name = $tiny->database_name;

connection, database

  alias to connection and database object.

    my $connection = $tiny->connection; # MongoDB::Connection object

    my $database   = $tiny->database;   # MongoDB::Database object

cursor_class, validator_class, gridfs_class

  override if you want.

    MongoDBx::Tiny::Cursor
  
    MongoDBx::Tiny::Validator
  
    MongoDBx::Tiny::GridFS

collection

  returns MongoDB::Collection

    $collection = $tiny->collection('collection_name')

connect / disconnect

  just (re)connect & disconnect

insert,create

    $document_object = $tiny->insert('collection_name' => $document);

single

  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);

search

  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);

update

    $tiny->update('collection_name',$query,$document);

remove

    $tiny->remove('collection_name',$query);

count

    $count_num = $tiny->count('collection_name',$query);

document_to_object

    $document_object = $tiny->document_to_object('collection_name',$document);

validate

    $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');

gridfs

  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

    $document_class_name = $tiny->document_class('collecion_name');

load_plugin

    # --------------------
    
    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;

process

  [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) = @_;
      }

set_indexes

  [EXPERIMENTAL]

    $tiny->set_indexes('collection_name');

unset_indexes

  [EXPERIMENTAL]

    # drop indexes without "_id";
    $tiny->unset_indexes('collection_name');

SEE ALSO ^

MongoDBx::Tiny::Document
MongoDBx::Tiny::Attributes
MongoDBx::Tiny::Relation
MongoDBx::Tiny::Util
MongoDBx::Tiny::Cursor
MongoDBx::Tiny::Validator
MongoDBx::Tiny::GridFS
MongoDBx::Tiny::GridFS::File
MongoDBx::Tiny::Plugin::SingleByCache

SUPPORT ^

https://github.com/naoto43/mongodbx-tiny/

AUTHOR ^

Naoto ISHIKAWA, <toona at seesaa.co.jp>

LICENSE AND COPYRIGHT ^

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.

syntax highlighting: