| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Mojolicious::Plugin::Mongodbv2; | ||
| 4 | $Mojolicious::Plugin::Mongodbv2::VERSION = '1.01'; | ||
| 5 | use Mojo::Base 'Mojolicious::Plugin'; | ||
| 6 | use MongoDB; | ||
| 7 | |||
| 8 | sub register { | ||
| 9 | my ($self, $app, $conf) = @_; | ||
| 10 | $conf = $conf || {}; | ||
| 11 | |||
| 12 | $conf->{helper} ||= 'db'; | ||
| 13 | |||
| 14 | $app->attr('mongodb_connection' => sub { | ||
| 15 | my $m = Mojolicious::Plugin::Mongodbv2::Client->new(conf => $conf); | ||
| 16 | $m->init(); | ||
| 17 | return $m->_client; | ||
| 18 | }); | ||
| 19 | |||
| 20 | $app->helper($conf->{helper} => sub { | ||
| 21 | my $s = shift; | ||
| 22 | my $m = $s->app->mongodb_connection; | ||
| 23 | return $m->db($m->db_name); | ||
| 24 | }); | ||
| 25 | } | ||
| 26 | |||
| 27 | package Mojolicious::Plugin::Mongodbv2::Client; | ||
| 28 | $Mojolicious::Plugin::Mongodbv2::Client::VERSION = '1.01'; | ||
| 29 | use Mojo::Base -base; | ||
| 30 | |||
| 31 | has 'conf' => sub { {host => 'mongodb://localhost/mongodbv2' } }; | ||
| 32 | has '_client'; | ||
| 33 | |||
| 34 | sub init { | ||
| 35 | my $s = shift; | ||
| 36 | $s->_client(MongoDB::MongoClient->new($s->conf)); | ||
| 37 | } | ||
| 38 | |||
| 39 | 1; | ||
| 40 | |||
| 41 | =pod | ||
| 42 | |||
| 43 | =head1 NAME | ||
| 44 | |||
| 45 | Mojolicious::Plugin::Mongodbv2 - MongoDB v2 driver in Mojolicious | ||
| 46 | |||
| 47 | =for html <p><img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/EmilianoBruni/mojolicious-plugin-mongodbv2?style=plastic"> <a href="https://travis-ci.com/EmilianoBruni/mojolicious-plugin-mongodbv2"><img alt="Travis tests" src="https://img.shields.io/travis/com/EmilianoBruni/mojolicious-plugin-mongodbv2?label=Travis%20tests&style=plastic"></a></p> | ||
| 48 | |||
| 49 | =head1 VERSION | ||
| 50 | |||
| 51 | version 1.01 | ||
| 52 | |||
| 53 | =head1 SYNOPSIS | ||
| 54 | |||
| 55 | plugin 'mongodbv2', { | ||
| 56 | host => 'mongodb://localhost/mongodbv2', | ||
| 57 | helper => 'db', | ||
| 58 | } | ||
| 59 | |||
| 60 | =head1 DESCRIPTION | ||
| 61 | |||
| 62 | Provides helper to easy use MongoDB v2 driver in Mojolicious application. | ||
| 63 | |||
| 64 | =encoding UTF-8 | ||
| 65 | |||
| 66 | =head1 CONFIGURATION OPTIONS | ||
| 67 | |||
| 68 | host (optional) MongoDB URI Connection | ||
| 69 | (default: 'mongodb://localhost/mongodbv2') | ||
| 70 | helper (optional) The name to give to the easy-access helper | ||
| 71 | (default: 'db') | ||
| 72 | |||
| 73 | All other options passed to the plugin are used to connect to MongoDB. | ||
| 74 | |||
| 75 | =head1 HELPERS/ATTRIBUTES | ||
| 76 | |||
| 77 | =head2 mongodb_connection | ||
| 78 | |||
| 79 | This plugin helper will return the MongoDB::MongoClient object | ||
| 80 | |||
| 81 | =head2 db | ||
| 82 | |||
| 83 | This is the name of the default easy-access helper. | ||
| 84 | |||
| 85 | =head1 BUGS/CONTRIBUTING | ||
| 86 | |||
| 87 | Please report any bugs through the web interface at L<https://github.com/EmilianoBruni/mojolicious-plugin-mongodbv2/issues> | ||
| 88 | If you want to contribute changes or otherwise involve yourself in development, feel free to fork the Git repository from | ||
| 89 | L<https://github.com/EmilianoBruni/mojolicious-plugin-mongodbv2/>. | ||
| 90 | |||
| 91 | =head1 SUPPORT | ||
| 92 | |||
| 93 | You can find this documentation with the perldoc command too. | ||
| 94 | |||
| 95 | perldoc Mojolicious::Plugin::Mongodbv2 | ||
| 96 | |||
| 97 | =head1 AUTHOR | ||
| 98 | |||
| 99 | Emiliano Bruni <info@ebruni.it> | ||
| 100 | |||
| 101 | =head1 COPYRIGHT AND LICENSE | ||
| 102 | |||
| 103 | This software is copyright (c) 2021 by Emiliano Bruni. | ||
| 104 | |||
| 105 | This is free software; you can redistribute it and/or modify it under | ||
| 106 | the same terms as the Perl 5 programming language system itself. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | __END__ |