
MongoX - DSL sugar for MongoDB

version 0.001

# quick bootstrap, add connection and switch to db:'test'
use MongoX { host => 'mongodb://127.0.0.1',db => 'test' };
# common way
use MongoX;
#register default connection;
add_connection host => '127.0.0.1';
# switch to default connection;
use_connection;
# use database 'test'
use_db 'test';
#add/register another connection with id "remote2"
add_connection host => '192.168.1.1',id => 'remote2';
# switch to this connection
use_connection 'remote2';
#get a collection object (from the db in current context)
my $foo = get_collection 'foo';
# use 'foo' as default context collection
use_collection 'foo';
# use context's db/collection
say 'total rows:',context_collection->count();
my $id = context_collection->insert({ name => 'Pan', home => 'Beijing' });
my $gridfs = context_db->get_gridfs;
...

MongoX is a light wrapper to MongoDB driver, it provide a versy simple but handy DSL syntax. It also will provide some usefull helpers like builtin mongoshell, you can quick work with MongoDB.

my $db = context_db;
Return current MongoDB::Database object in context;
my $con = context_connection;
Return current MongoDB::Connection object in context.
my $col = context_collection;
Return current MongoDB::Collection object in context, you can replace the object with "use_collection".

# create a default connection
use_connection;
# use another connection with id:'con2'
use_connection 'con2';
Switch to given connection, set the context connection to this connection.
use_db 'foo';
Switch to the database, set the context database to this database;
use_collection 'user'
Set 'user' collection as context collection.
add_connection id => 'default', host => 'mongodb://127.0.0.1:27017'
Register a connnection with the id, if omit, will add as default connection. All options exclude 'id' will direct pass to MongoDB::Connection.
boot host => 'mongodb://127.0.0.1',db => 'test' # same as: add_connection host => 'mongodb://127.0.0.1', id => 'default'; use_connection; use_db 'test';
Boot is equivalent to call add_connection,use_connection,use_db.

MongoX takes a set of options for the class construction at compile time as a HASH parameter to the "use" line.
As a convenience, you can pass the default connection parameters and default database, then when MongoX import, it will apply these options to "add_connection" and "use_db", so the following code:
use MongoX { host => 'mongodb://127.0.0.1',db => 'test' };
is equivalent to:
use MongoX;
add_connection host => 'mongodb://127.0.0.1';
use_connection;
use_db 'test';

Pan Fan(nightsailer) <nightsailer at gmail dot com>

This software is copyright (c) 2010 by Pan Fan(nightsailer).
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.