adonis-jsonapi
Advanced tools
Comparing version
{ | ||
"name": "adonis-jsonapi", | ||
"version": "0.8.1", | ||
"version": "0.8.2", | ||
"description": "A JSON API configuration for adonis-framework", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -210,3 +210,3 @@ # Adonis JSON API | ||
} | ||
get primaryKey() { | ||
@@ -220,2 +220,27 @@ return 'slug'; | ||
### Custom Primary Keys for Relationships | ||
For relations primary keys can be customized using `ref`. | ||
For instance in our AuthorView, we can say that our related books use the `isbn` as a primary key: | ||
```js | ||
const JsonApiView = require('adonis-jsonapi/src/JsonApiView'); | ||
class Author extends JsonApiView { | ||
get attributes() { | ||
return ['first-name', 'last-name']; | ||
} | ||
books() { | ||
return this.hasMany('App/Http/JsonApiViews/Book', { | ||
ref: 'isbn', | ||
included: true, | ||
excludeRelation: 'author' | ||
}); | ||
} | ||
} | ||
module.exports = Author; | ||
``` | ||
## Error Handling | ||
@@ -222,0 +247,0 @@ |
@@ -5,4 +5,5 @@ const i = require('inflect'); | ||
class JsonApiRelation { | ||
constructor(serializer, { included = false, excludeRelation }) { | ||
constructor(serializer, { ref = 'id', included = false, excludeRelation }) { | ||
this.serializer = serializer; | ||
this.ref = ref; | ||
this.included = included; | ||
@@ -16,7 +17,7 @@ this.excludeRelation = excludeRelation; | ||
if (this.included) { | ||
return Object.assign({}, { ref: 'id', included: this.included }, | ||
return Object.assign({}, { ref: this.ref, included: this.included }, | ||
serializer.build({ excludeRelation: this.excludeRelation })); | ||
} | ||
return { ref: 'id', included: this.included, type: serializer.type }; | ||
return { ref: this.ref, included: this.included, type: serializer.type }; | ||
} | ||
@@ -23,0 +24,0 @@ } |
17891
3.45%224
0.45%277
9.92%