Reputation: 1168
I've been doing a research to figure out a way to implement Standard JSONAPI (https://jsonapi.org) but I didn't find a source of truth or a convincing way to implement this.
to represent this in a real example, we have the following response
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
// ... this article's attributes
},
"relationships": {
// ... this article's relationships
}
}
}
so the expected entities we have are article and maybe author. those entities are stored in Database (SQL or NoSQL) and any tool is used to query the data (Mongoose, Knex, typeorm..etc)
the expected returned result from a database is (select 1st from articles)
{
"name":"article1",
"id":"1"
}
and the question is where is the best place to process the response from DB and transfer it into JSONAPI standard? is it Model layer? Controller? view or add a presentation layer?
I've been thinking about a way to implement this by using a template engine (pug, Handlebars..etc) but is this a good idea? since the template engine target is HTML and not JSON?
in case it is a good idea to use a template engine to render JSON, is there any available JSON template engine to generate JSON?
Any advice or a guide or a well-written example can be looked at?
Thanks.
Upvotes: 4
Views: 1590
Reputation: 720
You can use: https://github.com/ringcentral/nestjs-json-api https://www.npmjs.com/package/json-api-nestjs
They have some nice examples that you can use and check how they are doing
Upvotes: 2
Reputation: 4877
I did it in the controllers of my NestJS app, using jsonapi-serializer. I created a service for JSON API serialisation. That seemed like the best separation of concerns to me. The controller gets the data from the service, then calls the serialisation service, then passes the response back to the caller.
Upvotes: 4