towoju5 / smart-meta-manager
Simple smart meta manage for laravel/middey Technology
Requires
- php: ^7.4|^8.0
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-10-28 15:43:48 UTC
README
Nellalink SmartMetaManager is a powerful Laravel package designed to simplify the management of metadata for your models. It provides a flexible and efficient way to associate additional information with your model instances, allowing for dynamic and extensible data storage without altering your database schema.
Table of Contents
- Installation
- Configuration
- Usage
- MetaDataTrait
- SmartMetaManager Controller
- API Usage Examples
- Error Handling
- Best Practices
- Performance Considerations
- Security
- Extending Nellalink SmartMetaManager
- Troubleshooting
- Contributing
- Credits
- License
Installation
You can install the package via composer:
composer require towoju5/smart-meta-manager
After installation, publish the configuration file:
php artisan vendor:publish --tag=smart-meta-manager-config
Configuration
The configuration file config/meta_models.php
allows you to specify which models can have associated metadata and set the authentication guard:
return [ 'meta_data_models' => [ 'user' => App\Models\User::class, 'product' => App\Models\Product::class, // Add more models as needed ], 'auth_guard' => 'api', ];
Usage
Model Setup
To enable metadata functionality for a model, use the MetaDataTrait:
use Towoju5\SmartMetaManager\Trait\MetaDataTrait;
class User extends Model
{
use MetaDataTrait;
// ... other model code
}
README documentation on how to access the traits from the models:
Accessing Traits in Models
This guide explains how to use the MetaDataTrait in your models to manage metadata.
Setup
- Import the MetaDataTrait at the top of your model file:
use Towoju5\SmartMetaManager\Trait\MetaDataTrait;
- Use the trait in your model:
class YourModel extends Model
{
use MetaDataTrait;
// ...
}
Usage
Once the trait is included in your model, you can use the following methods:
Setting Metadata
$model->setMeta('key', 'value');
Getting Metadata
$value = $model->getMeta('key', 'default_value');
Deleting Metadata
$model->deleteMeta('key');
Getting All Metadata
$allMeta = $model->getAllMeta();
Searching Metadata
$results = $model->searchMeta('search_term');
Checking for Specific Metadata
$exists = $model->hasMetaKeyValue('key', 'value');
Example
$user = User::find(1);
$user->setMeta('preferences', json_encode(['theme' => 'dark']));
$preferences = json_decode($user->getMeta('preferences'), true);
This trait provides a flexible way to add metadata to your models without modifying the database schema for each new piece of information you want to store.
API Endpoints
Nellalink SmartMetaManager provides the following API endpoints:
- GET
/api/meta/{model}
- Retrieve all meta data for a model - POST
/api/meta/{model}
- Add new meta data - GET
/api/meta/{model}/search
- Search meta data - GET
/api/meta/user/all
- Retrieve all user meta data - GET
/api/meta/{model}/{key}
- Retrieve specific meta data - PUT
/api/meta/{model}/{key}
- Update specific meta data - DELETE
/api/meta/{model}/{key}
- Delete specific meta data - POST
/api/meta/{model}/check
- Check if a key-value pair exists
Authentication
All API endpoints require authentication using the guard specified in the config file. Ensure you're authenticated before making requests.
MetaDataTrait
The MetaDataTrait provides methods for interacting with metadata at the model level. Available methods include:
metaData()
setMeta($key, $value, $userId)
getMeta($key, $userId, $default = null)
deleteMeta($key, $userId)
getAllMetaForUser($userId)
searchMeta($userId, $search)
hasMetaKeyValue($key, $value, $userId)
SmartMetaManager Controller
The SmartMetaManager controller handles API requests for metadata operations. Controller methods include:
getModelMeta(Request $request, $model)
getAllUserMeta(Request $request)
searchMeta(Request $request, $model)
setMeta(Request $request, $model)
getMeta(Request $request, $model, $key)
updateMeta(Request $request, $model, $key)
deleteMeta(Request $request, $model, $key)
checkMetaKeyValue(Request $request, $model)
API Usage Examples
For detailed API usage examples, including requests and responses for various operations, please refer to the full documentation.
Error Handling
Nellalink SmartMetaManager uses consistent error responses for all API endpoints. Common error scenarios include invalid model names, non-existent keys, validation errors, and authentication failures.
Best Practices
- Use consistent and descriptive key names for your metadata
- Implement caching for frequently accessed metadata
- Use batch operations for setting or updating multiple meta values
- Regularly clean up orphaned or outdated metadata
Performance Considerations
- Ensure proper indexing on the metadata table
- Use eager loading to avoid N+1 query problems
- Implement chunking for operations on large datasets
- Consider using Laravel's queue system for time-consuming metadata operations
Security
- Implement proper authorization checks
- Validate and sanitize input data
- Avoid storing sensitive information as metadata
- Implement rate limiting on API endpoints
Extending Nellalink SmartMetaManager
You can extend the functionality of Nellalink SmartMetaManager by adding custom scopes, utilizing Laravel's model events, implementing custom validation rules, and creating custom middleware.
Troubleshooting
For common issues and their solutions, please refer to the Troubleshooting section in the full documentation.
Contributing
Contributions to Nellalink SmartMetaManager are welcome! Please follow the steps outlined in the Contributing section of the full documentation.
Credits
License
The MIT License (MIT). Please see License File for more information.