Mandel - Async model layer for MongoDB objects using Mango
0.11
package MyModel;
use Mandel;
package MyModel::Cat;
use Mandel::Document;
use Types::Standard 'Str';
field name => ( isa => Str );
field 'type';
belongs_to person => 'MyModel::Person';
package MyModel::Person;
use Mandel::Document;
use Types::Standard 'Int';
field [qw( name )];
field age => ( isa => Int );
has_many cats => 'MyModel::Cat';
has_one favorite_cat => 'MyModel::Cat';
package main;
my $connection = MyModel->connect("mongodb://localhost/my_db");
my $persons = $connection->collection('person');
my $p1 = $persons->create({ name => 'Bruce', age => 30 });
$p1->save(sub {
my($p1, $err) = @_;
});
$persons->count(sub {
my($persons, $err, $n_persons) = @_;
});
$persons->all(sub {
my($persons, $err, $objs) = @_;
for my $p (@$objs) {
$p->age(25)->save(sub {});
}
});
$persons->search({ name => 'Bruce' })->single(sub {
my($persons, $err, $person) = @_;
$person->cats(sub {
my($person, $err, $cats) = @_;
$_->remove(sub {}) for @$cats;
});
$person->remove(sub {
my($person, $err) = @_;
});
});
THIS IS ALPHA SOFTWARE! THE API MAY BE CHANGED AT ANY TIME! PLEASE CONTACT ME IF YOU HAVE ANY COMMENTS OR FEEDBACK.
Mandel is an async object-document-mapper. It allows you to work with your MongoDB documents in Perl as objects.
This class binds it all together:
-
An object modelling a document.
-
A collection of Mandel documents.
-
A single MongoDB document with logic.
Mandel inherits all attributes from Mojo::Base and implements the following new ones.
The namespaces which will be searched when looking for Types. By default, the (sub)class name of this module.
Returns Mandel::Model.
An instance of Mango which acts as the database connection. If not provided.
Mandel inherits all methods from Mojo::Base and implements the following new ones.
$self = $class->connect(@connect_args);
$clone = $self->connect(@connect_args);
@connect_args
will be passed on to "new" in Mango, which again will be set as "storage".
Calling this on an object will return a clone, but with a fresh "storage" object.
@names = $self->all_document_names;
Returns a list of all the documents in the "namespaces".
$document_class = $self->class_for($name);
Given a document name, find the related class name, ensure that it is loaded (or else die) and return it.
$collection_obj = $self->collection($name);
Returns a Mango::Collection object.
$model = $self->model($name);
$self = $self->model($name => \%model_args);
$self = $self->model($name => $model_obj);
Define or returns a Mandel::Model object. Will die unless a model is registered by that name or "class_for" returns a class which has the model()
method defined.
$self->initialize(@names, \%args);
$self->initialize(\%args);
Takes a list of document names. Calls the "initialize" in Mango::Document method on any document given as input. @names
default to "all_document_names" unless specified.
%args
defaults to empty hash ref, unless specified as input.
The initialize()
method will be called like this:
$document_class->initialize($self, \%args);
See "SYNOPSIS".
http://github.com/jhthorsen/mandel
Jan Henning Thorsen - [email protected]
Joel Berger - [email protected]
Holger Rupprecht - [email protected]
This project is a fork of MangoModel, created by Joel Berger, [email protected]
.
Copyright (C) 2013 by Jan Henning Thorsen
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.