NAME
MongoDBx::Tiny::Document - document class
SYNOPSIS
package My::Data::Foo;
use strict;
use MongoDBx::Tiny::Document;
COLLECTION_NAME 'foo';
# FIELD NAME, sub{}, sub{}..
ESSENTIAL qw/code/; # like CDBI's Essential.
FIELD 'code', INT, LENGTH(10), DEFAULT('0'), REQUIRED;
FIELD 'name', STR, LENGTH(30), DEFAULT('noname');
# RELATION ACCESSOR, sub{}
RELATION 'bar', RELATION_DEFAULT('single','foo_id','id');
INDEX 'code',{ unique => 1 };
INDEX 'name';
INDEX [code => 1, name => -1];
sub process_some {
my ($class,$tiny,$validator) = @_;
$tiny->insert($class->collection_name,$validator->document);
}
package My::Data::Bar;
use strict;
use MongoDBx::Tiny::Document;
COLLECTION_NAME 'bar';
ESSENTIAL qw/foo_id code/;
FIELD 'foo_id', OID, DEFAULT(''), REQUIRED;
FIELD 'code', INT(10), DEFAULT('0'),REQUIRED;
FIELD 'name', VARCHAR(30), DEFAULT('noname'),&MY_ATTRIBUTE;
RELATION 'foo', RELATION_DEFAULT('single','id','foo_id');
TRIGGER 'before_insert', sub {
my ($document_class,$tiny,$document,$opt) = @_;
};
# before_update,after_update,before_remove,after_remove
TRIGGER 'after_insert', sub {
my ($document_class,$object,$opt) = @_;
};
QUERY_ATTRIBUTES {
# no support in update and delete
single => { del_flag => "off" },
search => { del_flag => "off" }
};
sub MY_ATTRIBUTE {
return {
name => 'MY_ATTRIBUTE',
callback => sub {
return 1;
}
};
}
EXPORT
A list of functions that can be exported.
COLLECTION_NAME
# define collection name.
COLLECTION_NAME 'collection_name';
ESSENTIAL
# define essential field always fetched.
ESSENTIAL qw/field1 field2 field3/;
FIELD
# define field name and validation.
FIELD 'field_name', CODE, CODE;
RELATION
RELATION 'relation_name', RELATION_NAME;
sub RELATION_NAME {
my $self = shift;
my $c_name = shift; # relation
my $tiny = $self->tiny;
# xxx
}
TRIGGER
[EXPERIMENTAL]
TRIGGER 'phase', CODE;
QUERY_ATTRIBUTES
[EXPERIMENTAL]
QUERY_ATTRIBUTES {
# no support in update and delete
single => { del_flag => "off" },
search => { del_flag => "off" }
};
TODO: no_query option for condition
INDEX
[EXPERIMENTAL]
INDEX 'field_1';
INDEX 'field_2',{ unique => 1,drop_dups => 1, safe => 1, background => 1, name => 'foo' };
INDEX [field_2 => 1, field_3 => -1];
# for manage index
$tiny->set_indexes('collection_name');
MongoDBx::Tiny::Attributes::EXPORT
perldoc MongoDBx::Tiny::Attributes
MongoDBx::Tiny::Relation::EXPORT
perldoc MongoDBx::Tiny::Relation
SUBROUTINES/METHODS
new
$document_object = $document_class->new($document,$tiny);
collection_name, essential, field, relation, trigger, query_attributes, indexes
alias to installed value
$collection_name = $document_object->collection_name;
$essential = $document_object->essential;# {_id => 1, field1 => 1, field2 => 1}
# MongoDBx::Tiny::Document::Field
$field = $document_object->field;
# MongoDBx::Tiny::Document::Relation
$relation = $document_object->relation;
$qa = $document_object->query_attributes;
$attr = $qa->{$condition}; # condition: single,search
$indexes = $document_object->indexes; # arrayref
id
returns document value "_id"
tiny
returns MongoDBx::Tiny object
attributes_hashref
alias to object_to_document
object_to_document
$document = $document_object->object_to_document;
collection
returns MongoDB::Collection
$collection = $document_object->collection('collection_name');
update
$document_object->field_name('val');
$document_object->update;
#
$document_object->update($document);
# only field_name will be changed
$document_object->update({ field_name => 'val'});
remove
$document_object->remove;
MongoDBx::Tiny::Document::Field
my $field = $document_object->field;
my $attr = $document_object->get('field_name');
$attr->{name};
$attr->{callback};
my @field_names = $field->list;
my @default_fields = $field->list('DEFAULT');
my @required_fields = $field->list('REQUIRED')
my @oid_fields = $field->list('OID');
MongoDBx::Tiny::Document::Relation;
my $relation = $document_object->relation;
my @relations = $relation->list;
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.