Serialization for JSON API

I’ve never built a custom serializer, so I feel like I’m shooting in the dark a little while working on this, but from the research, I believe this is where my next step is since I need a different JSON API to be sent from my Adonis.js backend to my Ember.js app.

Here is what I have currently without using a custom Serializer.

{
data: { 
    attributes: [{
           0: { id: 221, name: "kara"}
             }]
         }
}

Here is what I’m wanting:

{
data: [{
     0: { 
         attributes: { name: "kara", region: "Texas" },
         id: "221",
         type: "aegroupdbview1ms"
       }
   }]
}

Current Controller:
‘use strict’

const aegroupdbview1m = use('App/Models/aegroupdbview1m')

class aegroupdbview1mController {
  async index ({ request, response, params: { id }}) {
    try {
      const Pid = await aegroupdbview1m.query().fetch()
      response.status(201).json({
        message: "Here are all your AEs",
        data: {
          attributes: Pid,
        }
      })
    } catch (e) {
      response.status(500).json({
        message: 'Warning! We were not able to retrieve your data.',
        details: {e}
      })
    } finally {

    }
  }

}

module.exports = aegroupdbview1mController

Current Model

'use strict'

const Model = use('Model')

class aegroupdbview1m extends Model {


}

module.exports = aegroupdbview1m

Now, this is where I need help…

New Controller

'use strict'

const aegroupdbview1m = use('App/Models/aegroupdbview1m')

class aegroupdbview1mController {
  async index ({ request, response, params: { id }}) {
    try {
      const Pid = await aegroupdbview1m.query().fetch()
      Pid.toJSON()
    } catch (e) {
      response.status(500).json({
        message: 'Warning! We were not able to retrieve your data.',
        details: {e}
      })
    } finally {

    }
  }

}

module.exports = aegroupdbview1mController

New Model

'use strict'

const Model = use('Model')

class aegroupdbview1m extends Model {
  
  static get Serializer () {
    return use('App/Models/Serializers/JsonSerializer')
  }

}

module.exports = aegroupdbview1m

JsonSerializer
‘use strict’

const _ = use('lodash')
const VanillaSerializer = require('@adonisjs/lucid/src/Lucid/Serializers/Vanilla')


class JsonSerializer extends VanillaSerializer {
  constructor (rows, pages = null, isOne = false) {
    this.rows = rows
    this.pages = pages
    this.isOne = isOne
  }

  toJSON () {
		if (this.isOne) {
			const data = this._getRowJSON(this.rows[0])
			return this._serialize(data)
		}

		const data = this.rows.map(this._getRowJSON.bind(this))
		const serializedData = this._serialize(data)

		if (this.pages) {
			return _.merge({}, this.pages, { serializedData })
		}

		return serializedData
	}
}

module.exports = JsonSerializer

start/hooks.js
hooks.js
const { ioc } = require(’@adonisjs/fold’)

ioc.bind(‘App/Models/Serializers/JsonSerializer’, () => {
return require(’./JsonSerializer’)
})

this is the current error I’m running into.

@ShaunWillis

Would a presenter class help? Have a look here:

You can kind of use a presenter as a way of massaging your controller data on it’s way to the view.