core icon indicating copy to clipboard operation
core copied to clipboard

CamelCase Serializer

Open sebasgarcep opened this issue 9 years ago • 28 comments

I have an use case right now where I need to autoload certain relationships and transform all fields to camelcase before sending them to the API consumer. I'm already autoloading the relationships using .addGlobalScope and it works fine, but I don't understand how to serialize my data as camelcase.

For example, when making an API call I get:

{
  "id": 1,
  "name": "John",
  "last_name": "Doe",
  "posts": [{
    "id": 3,
    "title": "Hey!",
    "created_at": "..."
  }]
}

When I want:

{
  "id": 1,
  "name": "John",
  "lastName": "Doe",
  "posts": [{
    "id": 3,
    "title": "Hey!",
    "createdAt": "..."
  }]
}

What is the most general way to achieve this result?

sebasgarcep avatar Nov 06 '17 02:11 sebasgarcep

Rolling out a serializer is the only way do it. Lemme share some code for that.

'use strict'

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

class JsonSerializer extends VanillaSerializer {
  _getRowJSON (modelInstance) {
    const json = _.transform(modelInstance.toObject(), (result, value, key) => {
      result[_.kebabCase(key)] = value
      return result
    }, {})

    this._attachRelations(modelInstance, json)
    this._attachMeta(modelInstance, json)
    return json
  }
}

module.exports = JsonSerializer

Save it inside app/Models/Serializers/JsonSerializer.js

And use this as the serializer for your models.

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

The result[_.kebabCase(key)] = value is the place where you do the actual transformation of keys to camel case

thetutlage avatar Nov 06 '17 08:11 thetutlage

Closing since the answer was given and there's no answer from issue reporter.

RomainLanz avatar Nov 10 '17 18:11 RomainLanz

@thetutlage your solution works when returning data to the client, but how do you do the same transformation on your model, for example:

const User = use('App/Models/User')
const user = await User.find(1)

// Would like to access fields like this:
user.userName

// Instead of:
user.user_name

Also, would be nice to do the inverse.

const user = new User()

user.userName = 'test' // Transform it to 'user_name'
await user.save()

waldoibarra avatar Dec 12 '17 23:12 waldoibarra

We write everything with JS: mobile, desktop, web, server. Using snake case on properties is uncomfortable when everything else in our stack is camel case. So I believe what @walbertoibarra pointing out is a valid concern in many use cases.

sebasgarcep avatar Dec 13 '17 00:12 sebasgarcep

How about naming fields as camelCase within SQL?

It is going to be much pain in transforming fields on fly before and after every SQL query for Lucid.

thetutlage avatar Dec 13 '17 05:12 thetutlage

@thetutlage I think many things in Lucid kind of expect snake case structure to work out of the box. I don't know how much of a pain in the ass it would be to use everything in camel case inside SQL.

sebasgarcep avatar Dec 13 '17 05:12 sebasgarcep

I don't think Lucid should enforce casing for SQL. If there are hardcoded strings like that, I am happy to fix them

thetutlage avatar Dec 13 '17 05:12 thetutlage

@thetutlage the thing is there is a problem using camelcase in column due to some operating system case sensitivity, you can look it up on the net

chbybnwr avatar Jan 05 '18 06:01 chbybnwr

Yes, then you should rely on snake_case itself. I don't see any point of adding layers of converting keys to different cases before and after saving them to the DB.

Every weird layer has it's own complexity and I want to stay away from it.

thetutlage avatar Jan 05 '18 07:01 thetutlage

@thetutlage I think my question is relevant to this thread: It's not even about converting different cases, but different names - Is there equivalent to columnName of Sails.Js attributes in Adonis/Lucid?

I couldn't find a proper workaround for this issue: I can use computed properties as an alias for the field (getter) + hook beforeCreate/beforeUpdate for a setter (I haven't tried it yet, so I'm not sure it will work) As I wrote, this is not a proper workaround and make it too complicated for maintenance.

Thanks!

gweizi avatar Feb 07 '18 16:02 gweizi

@gweizi I think @thetutlage's solution is the most appropiate. Just name your fields with camelCase. I haven't tried it but it should work. The only thing that may require some extra work may be relationships as they expect a particular structure for foreign key fields by default, but you can provide your own.

sebasgarcep avatar Feb 07 '18 21:02 sebasgarcep

@sebasgarcep Probably I didn't explain my issue that well. I give an example to make it more clear. Assume I have an exist database with a table called foo, this table has a column called "bad_name_666". I would like to create a model with a field called good_name that refer to the db column "bad_name_666". Is there any option to do that? or I must change the db scheme (in this case the column name bad_name_666 to good_name)?

Thanks again!

gweizi avatar Feb 08 '18 09:02 gweizi

No there is no way to do that, since it adds a complexity layer on top of your database schema and also I have no plans to introduce anything like this.

The attributes should be 100% same as the database schema, since it's easier to reason about them

thetutlage avatar Feb 08 '18 09:02 thetutlage

Objection.js, which is built top on knex.js, has a tranform fields function. Adonis Lucid is also built on knex.js, so you can use this feature from objection.js

  1. Install objection
npm i objection
  1. Add in top of config/database.js
const { knexSnakeCaseMappers } = require("objection");
  1. Add mappers to your connection. For example, change this
  pg: {
    client: 'pg',
    connection: {
      host: Env.get('DB_HOST', 'localhost'),
      port: Env.get('DB_PORT', ''),
      user: Env.get('DB_USER', 'root'),
      password: Env.get('DB_PASSWORD', ''),
      database: Env.get('DB_DATABASE', 'adonis')
    },
    debug: Env.get('DB_DEBUG', false)
  }

To this

  pg: ({
    ...{
      client: 'pg',
      connection: {
        host: Env.get('DB_HOST', 'localhost'),
        port: Env.get('DB_PORT', ''),
        user: Env.get('DB_USER', 'root'),
        password: Env.get('DB_PASSWORD', ''),
        database: Env.get('DB_DATABASE', 'adonis')
      },
      debug: Env.get('DB_DEBUG', false)
    },
    ...knexSnakeCaseMappers()
  })

That's all, now you can use .where('createdAt', ...) instead of .where('created_at', ...) and SomeModel.createdAt instead of SomeModel.created_at. This conversion also works for SomeModel.toJSON()

xxxwww avatar Oct 21 '18 06:10 xxxwww

Thanks @xxxwww , great solution ~

ngloom avatar Feb 21 '19 02:02 ngloom

I was originally using @xxxwww's solution, but found it causes Lucid's child population to fail silently. I haven't delved into it, but my guess is it is not assigning the ids correctly due to the property name mismatch.

let user = await User.query().where('email', email).with('role').fetch() and

let user = await User.find(1)
user.load('role')

both fail silently, leaving role null. I've had to revert back to a custom serializer.

skrenek avatar Feb 28 '19 15:02 skrenek

@skrenek looks like the conversion works for the foreign key in relationships too. Therefore, you need to explicitly specify the key if it is stored in a database in a format other than camelCase

xxxwww avatar Mar 01 '19 18:03 xxxwww

Can you elaborate on what you mean by specifying the key? I thought the point of the workaround was to allow db columns to be specified in snake case and properties in code in camel case.

skrenek avatar Mar 04 '19 15:03 skrenek

Any update on this guys? Since my whole table's name are in camelCase. Please help with answer how can i change the default behavior of table.timestamps() which create table name in snack_case. I want it to be in camelCase.

prakashpie avatar Jun 13 '19 12:06 prakashpie

@xxxwww per @skrenek could you elaborate on what you meant by specifying the key?

Secular12 avatar Aug 01 '19 21:08 Secular12