MongooseX::JSMethod - Set a method to run MongoDB-server side.
Version 0.01
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
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:
jsmethod()
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:
sum()
> 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.
Fernando Correa de Oliveira, <fernandocorrea at gmail.com>
<fernandocorrea at gmail.com>
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.
bug-mongoosex-jsmethod at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc MongooseX::JSMethod
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=MongooseX-JSMethod
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/MongooseX-JSMethod
CPAN Ratings
http://cpanratings.perl.org/d/MongooseX-JSMethod
Search CPAN
http://search.cpan.org/dist/MongooseX-JSMethod/
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.
To install MongooseX::JSMethod, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MongooseX::JSMethod
CPAN shell
perl -MCPAN -e shell install MongooseX::JSMethod
For more information on module installation, please visit the detailed CPAN module installation guide.