The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MongooseX::JSMethod - Set a method to run MongoDB-server side.

VERSION

Version 0.01

SYNOPSIS

If you want to create a recursive method to sum the value of a element and all its children (running on Mongo Server):

    package Test;
    use Moose;
    with 'Mongoose::Document';
    with 'MongooseX::JSMethod';
    
    has name     => (is => 'rw', isa => 'Str');
    has value    => (is => 'rw', isa => 'Int');
    has children => (is => 'rw', isa => 'Mongoose::Join[Test]', default => sub{Mongoose::Join->new(with_class => __PACKAGE__)});
                                                                                
    jsmethod(sum => << 'EOJS');                                                 
          var sum = this.value + 0;
          this.children.forEach(function(x){                                    
             sum += x.fetch().sum();
          });
          return sum;
    EOJS
    
    42

SUBROUTINES/METHODS

jsmethod

It is the reason of this Role. jsmethod() needs 2 parameter: The first is the name of the method and the second is a string with the javascript code of your method. It will create 2 methods, one for perl that will call then second and return what it returns. The other method is the javascript method, it is what the code you gave to jsmethod() is. It insert a new field on your document with the code. So if you do something like this with the Test class:

    use Test;
    Mongoose->db("my_database");
    $a = Test->new({name => "The answer", value => 42});
    $a->save;

On MongoDB, it will create the document:

    > db.test.findOne()
    {
        "_id" : ObjectId("4f9395a1d9507ff008000000"),
        "value" : NumberLong(42),
        "name" : "The answer",
        "children" : [ ],
        "sum" : function cf__78_anon() {
           var sum = this.value + 0;
           this.children.forEach(function (x) {sum += x.fetch().sum();});
           return sum;
        }
    }

If on your MongoDB shell you run the sum() method:

    > db.test.findOne().sum()
    42

Yes, it runs your method (returns 42). If you run the perl method it will do the same:

    use Test;
    Mongoose->db("my_database");
    $a = Test->find_one()->sum;
    print $a, $/

Prints 42.

AUTHOR

Fernando Correa de Oliveira, <fernandocorrea at gmail.com>

BUGS

Please report any bugs or feature requests to bug-mongoosex-jsmethod at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MongooseX-JSMethod. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc MongooseX::JSMethod

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2012 Fernando Correa de Oliveira.

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.