offline-agency/laravel-mongo-auto-sync

This package provides a better support for MongoDB relationships in Laravel Projects.

3.1.0 2023-01-17 11:01 UTC

README

Latest Stable Version Total Downloads Build Status MIT Licensed Quality Score StyleCI codecov

This package provides a better support for MongoDB relationships in Laravel Projects. At low level all CRUD operations has been handled by jenssegers/laravel-mongodb

Installation

composer require offline-agency/laravel-mongo-auto-sync

Prerequisites

Make sure you have the MongoDB PHP driver installed. You can find installation instructions at http://php.net/manual/en/mongodb.installation.php

Package version Compatibility

This package Laravel Laravel MongoDB
1.x 5.8.x 3.5.x
1.x 6.x 3.6.x
2.x 5.8.x 3.5.x
2.x 6.x 3.6.x
2.x 7.x 3.7.x
2.x 8.x 3.8.x
2.x 9.x 3.9.x
3.x 5.8.x 3.5.x
3.x 6.x 3.6.x
3.x 7.x 3.7.x
3.x 8.x 3.8.x
3.x 9.x 3.9.x

PHP Version Compatibility

  • Version 1: PHP 7.1, 7.2, 7.3
  • Version 2: PHP 7.4+
  • Version 3: PHP 7.4+

Features

  • Sync changes between collection with relationships after CRUD operations
    • EmbedsOne & EmbedsMany

Example without our package

//create a new Article with title "Game of Thrones" with Category "TV Series"
//assign data to $article       
$article->save();
/*
Article::class {
  'title' => 'Game of Thrones',
  'category' => Category::class {
      'name' => 'TV Series'
   }
}
*/

//Retrieve 'TV Series' category
$category = Category::where('name', 'TV Series')->first();
/*
  Category::class {
      'name' => 'Game of Thrones',
      'articles' => null
  }
*/ 

The sub document article has not been updated with the new article. So you will need some extra code to write in order to see the new article it in the category page. The number of sync depends on the number of the relationships and on the number of the entry in every single EmbedsMany relationships.

Total updates = ∑ (entry in all EmbedsMany relationships) + ∑ (EmbedsOne relationships)

As you can see the lines of extra code can rapidly increase, and you will write many redundant code.

Example with our package

//create a new Article with title "Game of Thrones" with Category "TV Series"
$article->storeWithSync($request);
/*
Article::class {
  'title' => 'Game of Thrones',
  'category' => Category::class {
      'name' => 'TV Series'
  }
}
 */
//Retrieve 'TV Series' category
$category = Category::where('name', 'TV Series')->first();   
/*
Category::class {
  'name' => 'Game of Thrones',
  'articles' => Article::class {
      'title' => 'Game of Thrones'
  }
}
*/ 

The sub document article has been updated with the new article, with no need of extra code 🎉

You can see the new article on the category page because the package synchronizes the information for you by reading the Model Setup.

These example can be applied for all write operations on the database.

  • Referenced sub documents [TO DO]
  • Handle sub document as Model in order to exploit Laravel ORM support during write operation (without sync feature) [TO BE TEST]
  • Handle referenced sub document as Model in order to exploit Laravel ORM support during write operation (without sync feature) [TO DO]
  • Advance cast field support

Use cases

  • Blog: see demo here
  • Ecommerce
  • API System for mobile application o for generated static site
  • Any projects that require fast read operations and (slow) write operations that can be run on background

Documentation

You can find the documentation here

Testing

Run this command inside your project's route

docker-compose up

Now run the tests with:

composer test

Roadmap 🚀

  • Refactor target synchronization to Observer pattern, so all this operation can be run on background using Laravel Queue System. This will also speed up all the operations in the collection that is primary involved in write operations.
  • Command Analyse Database: This command will analyse the database in order to find some relationship error. Ex: An article with a category associated that is not present on the Category's sub document.
  • Refactor save() method in order to handle CRUD operation on relationship also without sync.
  • Support for referenced relationships.
  • Better support for all field types.
  • DestroyWithSync() without delete sub documents on other collections.
  • Add more tests.
  • Nested relationships.
  • Benchmark MongoDB vs Mysql (write and read operation).
  • Fix typo errors.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email support@offlineagency.com instead of using the issue tracker.

Credits

About us

Offline Agency is a web design agency based in Padua, Italy. You'll find an overview of our projects on our website.

License

The MIT License (MIT). Please see License File for more information.