offline-agency / laravel-mongo-auto-sync
This package provides a better support for MongoDB relationships in Laravel Projects.
Fund package maintenance!
offline-agency
Installs: 466
Dependents: 0
Suggesters: 0
Security: 0
Stars: 15
Watchers: 4
Forks: 0
Open Issues: 16
Requires
- php: >=7.4
- ext-json: *
- ext-mongodb: *
- illuminate/container: ^5.0|^6.0|^7.0|^8.0
- illuminate/database: ^5.0|^6.0|^7.0|^8.0
- illuminate/events: ^5.0|^6.0|^7.0|^8.0
- illuminate/support: ^5.0|^6.0|^7.0|^8.0
- jenssegers/mongodb: ^3.6|^3.7|^3.8
Requires (Dev)
- orchestra/testbench: ^3.0|^4.0|^5.0|^6.0
- phpunit/phpcov: ^7.0|^8.0
- phpunit/phpunit: ^5.0|^6.0|^7.0|^8.0|^9.0
- dev-master
- 3.1.1
- 3.1.0
- 3.0.0
- 2.0
- 2.0-alpha.2
- 2.0-alpha.1
- 1.20
- 1.19
- 1.18
- 1.17
- 1.16
- 1.15
- 1.14
- 1.13
- 1.12
- 1.11
- 1.10
- 1.9
- 1.8
- 1.7
- 1.6
- 1.5
- 1.4
- 1.3
- 1.2
- 1.1
- 1.0
- dev-dependabot/composer/league/commonmark-2.6.0
- dev-dependabot/composer/laravel/framework-8.83.28
- dev-dependabot/composer/symfony/process-5.4.46
- dev-dependabot/composer/symfony/http-foundation-5.4.46
- dev-edit-readme-file
- dev-feat-upgrade-laravel-mongo-4.0
- dev-feat-add-support-php82
- dev-feat-add-phpstan
- dev-fix-sync-error
- dev-analysis-x023GO
- dev-develop
- dev-add-exception-on-updateRelationshipsWithSync
This package is auto-updated.
Last update: 2024-12-09 22:31:56 UTC
README
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
PHP Version Compatibility
- Version 1: PHP 7.1, 7.2, 7.3
- Version 2: PHP 7.4
- Version 3: PHP 7.4-8.1
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.