rtablada / adonis-jsonapi Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Adonis 4 has been released, although the documentation is still being updated. It would be nice to have this working with it.
Hi, i have an User model that has many Contact.
This is my JsonApiViews/User.js
file
const JsonApiView = require('adonis-jsonapi/src/JsonApiView')
class User extends JsonApiView {
get attributes() {
return ['first-name', 'last-name', 'email'];
}
contacts() {
return this.hasMany('App/Http/JsonApiViews/Contact');
}
}
module.exports = User
This is my JsonApiViews/Contact.js
file
const JsonApiView = require('adonis-jsonapi/src/JsonApiView')
class Contact extends JsonApiView {
get attributes() {
return ['first-name', 'last-name', 'email', 'phone'];
}
user() {
return this.belongsTo('App/Http/JsonApiViews/User');
}
}
module.exports = Contact
In my ContactCotroller.js
in the index method i have this:
* index (request, response) {
const contacts = yield Contact.all()
response.jsonApi('Contact', contacts)
}
And i'm getting this error:
{
"error": {
"type": "TypeError",
"message": "Cannot match against 'undefined' or 'null'.",
"file": "node_modules/adonis-jsonapi/src/JsonApiView.js",
"line": 5
}
}
I started getting that when i configured the relationship. When they were not related, everything worked fine. What i am missing?
I have a paginated query like this:
const contacts = yield Database
.from('contacts')
.where(conditions)
.orderBy(sort, dir)
.paginate(page, limit)
response.jsonApi('Contact', contacts.data)
this will return some metadata like
total: 406,
currentPage: 1,
perPage: 10,
lastPage: 41
i would like to access it for ui pagination purposes but it is not being sent from the JsonApiView.
It there a way to do it?
The setting:
I have two serializers: Product
and ProductShopVideo
.
// file JsonApiViews/V1/Product.js
class Product extends JsonApiView {
get attributes() {
return [
'title',
'sku',
// ... some more attrs here, doesn't really matter
'active'
];
}
productShopVideos() {
return this.hasMany('App/Http/JsonApiViews/V1/ProductShopVideo', {
included: true,
excludeRelation: 'product',
ref: '_id'
})
}
}
// file JsonApiViews/V1/Product.js
class ProductShopVideo extends JsonApiView {
get attributes() {
return [
"addedDate" ,
"deletedBy" ,
"deleted" ,
"addedBy" ,
"disabled"
];
}
product() {
return this.belongsTo('App/Http/JsonApiViews/V1/Product', {
ref: 'product'
});
}
}
The result I recieve using proper data and the Product serializer is:
{
"meta": {},
"included": [
{
"type": "productShopVideos",
"id": "5b432fd15c242f0458c15a9d",
"attributes": {}
}
],
"data": {
"type": "product",
"id": "5b432fd05c242f0458c15a9a__499",
"attributes": {},
"relationships": {
"product-shop-videos": {
"data": [
{
"type": "productShopVideos",
"id": "5b432fd15c242f0458c15a9d"
}
]
}
}
}
}
I removed attributes for both to make it a bit clear. As you can see, the type for ProductShopVideo
in included
is not relevant. It should be kebab-cased as well.
Hi, is there a way to get the request relationships? something similar to getAttributes
function?
Ran into cases when I need to exclude more than one relation using excludeRelation
. It would be good to have it as an array.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.