All Projects → nilportugues → eloquent-mongodb-repository

nilportugues / eloquent-mongodb-repository

Licence: MIT License
Eloquent MongoDB Repository Implementation

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to eloquent-mongodb-repository

sql-repository
[PHP 7] SQL Repository implementation
Stars: ✭ 37 (+105.56%)
Mutual labels:  composer, crud, filter, paginate, pageable
Laravel Database Encryption
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Stars: ✭ 238 (+1222.22%)
Mutual labels:  composer, eloquent, laravel5
Lara Eye
Filter your Query\Builder using a structured query language
Stars: ✭ 39 (+116.67%)
Mutual labels:  composer, eloquent, filter
repository
[PHP 7] Implementation and definition of a base Repository in Domain land.
Stars: ✭ 26 (+44.44%)
Mutual labels:  crud, filter, pageable
Larrock Core
Core components for LarrockCMS
Stars: ✭ 46 (+155.56%)
Mutual labels:  composer, crud, laravel5
Filebase
A Simple but Powerful Flat File Database Storage.
Stars: ✭ 235 (+1205.56%)
Mutual labels:  composer, crud, storage
Eloquent Filter
This simple package helps you filter Eloquent data using query filters.
Stars: ✭ 24 (+33.33%)
Mutual labels:  eloquent, orm, filter
Datagrid
Datagrid for Laravel v5
Stars: ✭ 44 (+144.44%)
Mutual labels:  composer, filter
Silexstarter
Starter app based on Silex framework with mvc and modular arch, scaffold generator, and admin panel
Stars: ✭ 11 (-38.89%)
Mutual labels:  composer, crud
Phero
这个是一个独立的orm组件可以使用在任何系统中,提供灵活的orm操作,注解形式的数据库和类的映射。This is a database query tool library. swoole mysql pool.
Stars: ✭ 100 (+455.56%)
Mutual labels:  composer, orm
Sleepingowladmin
🦉 Administrative interface builder for Laravel (Laravel admin)
Stars: ✭ 671 (+3627.78%)
Mutual labels:  composer, crud
Laravel Ecommerce
Laravel open source e-commerce system.
Stars: ✭ 163 (+805.56%)
Mutual labels:  composer, laravel5
Laravel Zero
A PHP framework for console artisans
Stars: ✭ 2,821 (+15572.22%)
Mutual labels:  composer, illuminate
Easyhttp
A Laravel HTTP-client to make HTTP request easier and log requests and responses
Stars: ✭ 20 (+11.11%)
Mutual labels:  composer, laravel5
romans
A Simple PHP Roman Numerals Library
Stars: ✭ 40 (+122.22%)
Mutual labels:  composer, filter
laravel-searchzy
Package que te permite buscar y filtrar registros de Eloquent Models en Laravel de una manera simple y sencilla.
Stars: ✭ 15 (-16.67%)
Mutual labels:  eloquent, filter
MercadoLivreProductsCrawler
PHP Console Crawler to Download Products from a Store on MercadoLivre.com.br
Stars: ✭ 18 (+0%)
Mutual labels:  eloquent, illuminate
Coastercms
The repository for Coaster CMS (coastercms.org), a full featured, Laravel based Content Management System
Stars: ✭ 380 (+2011.11%)
Mutual labels:  composer, laravel5
Laravel Wallet
Easy work with virtual wallet
Stars: ✭ 401 (+2127.78%)
Mutual labels:  composer, eloquent
eloquent-filter
Library to form search criteria through expressions in the query string
Stars: ✭ 23 (+27.78%)
Mutual labels:  eloquent, filter

Eloquent MongoDB Repository

PHP7 Tested Build Status Scrutinizer Code Quality SensioLabsInsight Latest Stable Version Total Downloads License Donate

Eloquent MongoDB Repository using nilportugues/repository as foundation, using jenssegers/mongodb.

Installation

Use Composer to install the package:

$ composer require nilportugues/eloquent-mongodb-repository

Why? Drivers and Multiple Implementations!

Using this implementation you can switch it out to test your code without setting up databases.

Doesn't sound handy? Let's think of yet another use case you'll love using this. Functional tests and Unitary tests.

No database connection will be needed, nor fakes. Using an InMemoryRepository or FileSystemRepository implementation will make those a breeze to code. And once the tests finish, all data may be destroyed with no worries at all.

Available drivers:

Also, if you feel like changing the repository implementation, no logic changes would be needed, as there are a set of drivers for you to use out of the box:

Usage

To set up Eloquent you don't need Laravel or Lumen frameworks at all. This is how you use Eloquent in any project.

<?php
use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule();
$capsule->getDatabaseManager()->extend('mongodb', function($config) {
    return new \Jenssegers\Mongodb\Connection($config);
});

$capsule->addConnection([
        'driver' => 'mongodb',
        'host' => 'localhost',
        'port' => 27017,
        'database' => 'default',
        'username' => '',
        'password' => '',
        'options' => [
            'db' => 'admin'
        ]
    ],
    'default'
);
$capsule->bootEloquent();
$capsule->setAsGlobal();

Now that Eloquent is running, we can use the Repository.

A note about MongoDB Models

The best way to ensure maximum compatibility with other Repository implementations is to ignore the MongoDB's ObjectId field: _id and declare one such as id. This means, not using MongoDB's ObjectId to fetch elements.

This also means that you'll need an Adapter build from MongoDB object the expected Business objects. More on that further down, or check the /example directory.

One Repository for One Eloquent Model

A well defined repository returns one kind of objects that belong to one Business model.

<?php
use NilPortugues\Foundation\Infrastructure\Model\Repository\EloquentMongoDB\EloquentRepository;

class UserRepository extends EloquentRepository 
{
    /**
     * {@inheritdoc}
     */
    protected function modelClassName()
    {
        return User::class;
    }
}

To be faithful to the repository pattern, using Eloquent Models internally is OK, but Business objects should be returned.

Therefore, you should translate Eloquent to Business representations and the other way round. This is represented by $userAdapter in the example below.

The fully implementation should be along the lines:

<?php
use NilPortugues\Foundation\Infrastructure\Model\Repository\EloquentMongoDB\EloquentRepository;

class UserRepository extends EloquentRepository 
{
    protected $userAdapter;
    
    /**
     * @param $userAdapter
     */
    public function __construct($userAdapter)
    {
        $this->userAdapter = $userAdapter; 
    }
    
    /**
     * {@inheritdoc}
     */
    protected function modelClassName()
    {
        return User::class;
    }
    
    /**
     * {@inheritdoc}
     */    
    public function find(Identity $id, Fields $fields = null)
    {
        $eloquentModel = parent::find($id, $fields);   
        
        return $this->userAdapter->fromEloquent($eloquentModel);
    }
    
    /**
     * {@inheritdoc}
     */    
    public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)
    {
        $eloquentModelArray = parent::findBy($filter, $sort, $fields);   
        
        return $this->fromEloquentArray($eloquentModelArray);
    }       
    
    /**
     * {@inheritdoc}
     */
    public function findAll(Pageable $pageable = null)
    {
        $page = parent::findAll($pageable);
        
        return new Page(
            $this->fromEloquentArray($page->content()),
            $page->totalElements(),
            $page->pageNumber(),
            $page->totalPages(),
            $page->sortings(),
            $page->filters(),
            $page->fields()
        );
    } 

    /**
     * {@inheritdoc}
     */
    public function add(Identity $value)
    {
        $value = $this->userAdapter->toEloquent($value);

        return parent::add($value);
    }

    /**
     * {@inheritdoc}
     */
    public function addAll(array $values)
    {
        $eloquent = [];
        foreach ($values as $value) {
            $eloquent[] = $this->userAdapter->toEloquent($value);
        }

        parent::addAll($eloquent);
    }
    
   /**
    * @param array $eloquentModelArray
    * @return array
    */
   protected function fromEloquentArray(array $eloquentModelArray)
   {
        $results = [];
        foreach ($eloquentModelArray as $eloquentModel) {
            //This is required to handle findAll returning array, not objects.
            $eloquentModel = (object) $eloquentModel;
            
            $results[] = $this->userAdapter->fromEloquent($eloquentModel);
        }
        
        return $results;
   } 
}

A sample implementation can be found in the /example directory.

One EloquentRepository for All Eloquent Models

While this is not the recommended way, as a repository should only return one kind of Business objects, this works well with Laravel projects.

While the amount of code is less than the previous example, bare in mind that your code will be coupled with Eloquent.

<?php
use NilPortugues\Foundation\Infrastructure\Model\Repository\EloquentMongoDB\EloquentRepository;

class EloquentMongoDBRepository extends EloquentRepository
{
    /**
     * @var string
     */
    protected $modelClass;
    
    /**
     * @param string $modelClass
     */
    public function __construct($modelClass)
    {
        $this->modelClass = (string) $modelClass;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function modelClassName()
    {
        return $this->modelClass;
    }
}

Filtering data

Filtering is as simple as using the Filter object. For instance, lets retrieve how many users are named Ken.

<?php
use NilPortugues\Foundation\Domain\Model\Repository\Filter;

$repository = new UserRepository();

$filter = new Filter();
$filter->must()->contain('name', 'Ken');

echo $repository->count($filter);

Notice how the key name matches the database column name in the users table.

Available options

Filter allow you to use must(), mustNot() and should() methods to set up a fine-grained search. These provide a fluent interface with the following methods available:

  • public function notEmpty($filterName)
  • public function hasEmpty($filterName)
  • public function startsWith($filterName, $value)
  • public function endsWith($filterName, $value)
  • public function equal($filterName, $value)
  • public function notEqual($filterName, $value)
  • public function includeGroup($filterName, array $value)
  • public function notIncludeGroup($filterName, array $value)
  • public function range($filterName, $firstValue, $secondValue)
  • public function notRange($filterName, $firstValue, $secondValue)
  • public function notContain($filterName, $value)
  • public function contain($filterName, $value)
  • public function beGreaterThanOrEqual($filterName, $value)
  • public function beGreaterThan($filterName, $value)
  • public function beLessThanOrEqual($filterName, $value)
  • public function beLessThan($filterName, $value)

Sorting data

Sorting is straight forward. Create an instance of Sort and pass in the column names and ordering.

<?php
use NilPortugues\Foundation\Domain\Model\Repository\Sort;

$repository = new UserRepository();

$filter = null; //all records
$sort = new Sort(['name', 'id'], new Order('ASC', 'DESC'));
$fields = null; //all columns

$results = $repository->findBy($filter, $sort, $fields);

Fields data

Create a Fields object to fetch only selected columns. If no Fields object is passed, all columns are selected by default.

<?php
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields;

$repository = new UserRepository();

$filter = null; //all records
$sort = null; //existing order
$fields = new Fields(['name', 'id']);

$results = $repository->findBy($filter, $sort, $fields);

Fetching data

Repository allows you to fetch data from the database by using the following methods:

  • public function findAll(Pageable $pageable = null)
  • public function find(Identity $id, Fields $fields = null)
  • public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)

Quality

To run the PHPUnit tests at the command line, go to the tests directory and issue phpunit.

This library attempts to comply with PSR-1, PSR-2, PSR-4.

If you notice compliance oversights, please send a patch via Pull Request.

Contribute

Contributions to the package are always welcome!

Support

Get in touch with me using one of the following means:

Authors

License

The code base is licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].