Why new record type is pluralized as per default?


#1

Hi everybody!

I am trying to understand this little detail about ember data and I hope that someone can help me to figure it out.

I have a model describing a command and I send “commands” to the backend by

var record = this.get('store').createRecord('command', {...});
record.save();

I create a single record per time, with a single record creation function, and I push only that record to the backend, then why the message payload that is sent to it has type commands? (pluralized)

Before to find the way to switch off this behaviour, I would like to understand it.

Thanks in advance


#2

I think the reason for it is that it’s a pretty standard convention to pluralize API endpoints for a resource like that, and therefore pluralizing is a sensible default. For example it would be very common to have a ‘user’ model and have the API endpoints be /users and /users/:id.

To customize it, put the following in your adapter:

  // don't pluralize, just return type as-is
  pathForType: function(type) {
    return type;
  },

You can customize it per-model if you use a per-model adapter, or application-wide with the application adapter.


#3

Thank you for the reply. I am actually implementing that method, but it does not affect the type of the record being created with createRecord, it only changes the “path” for that request. The type is probably customizable by some other means I did not yet search, but I dont understand why as per default, a record created from a model does not have his type called with the model’s name.

Do you experience the same behavior?


#4

@beddu this is a recommendation to keep things consistent across the jsonapi.org standard.


#5

I found this note in json:api

Note: This spec is agnostic about inflection 
rules, so the value of type can be either plural 
or singular. However, the same value should be 
used consistently throughout an implementation.

I think this is my answer, thank you!


#6

@rtablada Ok, I agree, the API specification sais that the type of a record can be plural or singular and it

should be used consistently throughout an implementation

but still, I am not yet sure that Ember Data treats it in such consistent way, here is my idea:

  1. We define a model, for example post
  2. The backend serves to us an API end-point called /posts
  3. We request one or more records of type post by store.findRecord('post', 1) or store.findAll('post')

When the type of my record becomes plural and why? I believe that if I ask for one record of type post and I receieve it correctly, the type of it should still be post and not posts, regardless of how the API endpoint is called.

There is something i still don’t understand, please enlight me before that my brain just blow up! :smiley:


#7

To control the behavior of how the records are “saved” (aka posted to the back-end) we must override the serialize method of the DS.JSONAPISerializer class related to a specific model or related to the entire application if we want our customization to be applied to every model.

At the moment, I solved my createRecord payload type attribute issue coding serializers/application.js in this way:

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
    serialize: function(snapshot/*, options*/)
    {
        var japipayload = {
            type: snapshot.modelName,
            attributes: snapshot._attributes
        };
    
        return japipayload;
    }
});

and finally I get the type of the newly created record equal to the model’s name.