-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented auto mapping, default schemas attributes and default hydrators attributes #47
Conversation
@pedroluislopez thank you for the PR. I'll have to take a look into this in detail as some of it is quite a big change. I really like the idea of getting the fillable attributes from the Model, so that definitely gets the thumbs up. My quick question on this is does the auto-mapping also work if the API has a mixture of Eloquent and non-Eloquent models? It is essential that this package does not assume that all models are Eloquent because that is not the case in every single implementation we have. |
Also, my general thinking on API config is that it should be cacheable, i.e. it should be in a config file rather than automatically generated on the fly. This is for performance reasons: it seems costly to work this out on every single request when it can be cached into config. Would be interested to hear what you think about automapping vs cached config in terms of performance? |
@jstoone as someone else who uses this package a lot I'd be interested in hearing your thoughts if you've got a spare few minutes |
@lindyhopchris You're right, the solution with automapping would affect performance. Anyway, conceptually you receive a model you want its schema from, and this could be defined inside that model to later use reflection when asking for the schema to obtain it. This behavior would be also fast and it would be developed inside the class About the adapters, a similar behavior should be looked for but I'll need to evaluate it better. If you like, I can cancel this PR and I send you another one including only the feature about using fillable attribute in eloquent models for attributes in the schema and the hydrator. |
@pedroluislopez yes, definitely send me a PR for the fillable thing - really like that. Definitely let me know your thoughts when you've had a think about the Schema - maybe write an issue before spending the time writing code? I do have a concern about reading it from the Model class though, which is that if you have two APIs in your application - e.g. So I'm not totally convinced about reading it directly from the model class, but if your thoughts could cover that topic then i'd definitely be interested in what you come up with. |
@lindyhopchris |
@lindyhopchris |
The modern frameworks do implement automatic features in order to avoid complex configuration files that are modified, usually, by several developers.
In order to improve this package, we develop two features. First avoid modification of json-api.php configuration file to include mappings of schemas and adapters: these are included automatically (we call this automapping). The second feature uses fillable attributes from eloquent models to avoid repetition of these attributes declaration in the schema and hydrator classes (by default, if attributes are declared in schema/hydrator class, these are used).
To use automapping you have to follow these steps:
automapping: true
in their configuration. Example:The mapping model/schema is declared in each Laravel model. Laravel models should be in
app/Models
folder. Also, in the model you have to declare a constant:const JSON_API_SCHEMA = Schema::class;
.The new implementation search in
app/Models
folder, classes that declareJSON_API_SCHEMA
constant and generates automatic schema mappings for namespaces configured for this purpose, and also generateseloquent-adapter
map usingresourceType
in schema classes.Automapping don't overwrite declarations of mappings and adapters declared in
json-api.php
configuration file.Note: We have created a completely new class called
CloudCreativity\LaravelJsonApi\Repositories\EloquentSchemasRepository
becauseCloudCreativity\JsonApi\Repositories\SchemasRepository
have their attributes declared as private, and we can't inherit them.