Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Field Names Using Snake Case #4906

Closed
nabati opened this issue Nov 22, 2015 · 1 comment
Closed

JSON Field Names Using Snake Case #4906

nabati opened this issue Nov 22, 2015 · 1 comment

Comments

@nabati
Copy link

nabati commented Nov 22, 2015

Is it possible to define a model which uses camelCase for the attributes, but underscore/snake case when converted to json, or maybe even a field name that will be used for JSON only?

Currently;

var User = seq.define('user', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true
    },
    shortLivedToken: {
        type: Sequelize.STRING,
        field: 'short_lived_token'
    },
    longLivedToken: {
        type: Sequelize.STRING,
        field: 'long_lived_token'
    }
}, {
    underscored: true,
    underscoredAll: true,
});

Turns into when calling response.json(user)

{
  "id": null,
  "shortLivedToken": "abcde",
  "longLivedToken": "fghji",
  "updated_at": "2015-11-21T18:32:20.181Z",
  "created_at": "2015-11-21T18:32:20.181Z"
}

And I want it to be

{
  "id": null,
  "short_lived_token": "abcde",
  "long_lived_token": "fghij",
  "updated_at": "2015-11-21T18:32:20.181Z",
  "created_at": "2015-11-21T18:32:20.181Z"
}

Any ideas?

@mickhansen
Copy link
Contributor

Not possible. The attribute names are used for getting values, toJSON uses methods for getting values.
Overwrite toJSON or don't use camel cased attribute names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants