Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexdiliberto/a355684aca36798c259ad17772e3f01b to your computer and use it in GitHub Desktop.
Save alexdiliberto/a355684aca36798c259ad17772e3f01b to your computer and use it in GitHub Desktop.
Ember Data, Type Pluralization, and Relationships

A huge "Thank You" to @runspired for all the help in the #ember-data Ember Community Discord!

// models/foo-bar.js
export default class FooBar extends Model {}

JSON API "type" === "modelName" === "foo-bar".

We derive this from the file path in your project.

Your API can respond with plural but ember-data expects the singularized-dasherized version to be given to the store after normalization. In most places we redundantly guard against receiving non-normalized types internally but we won't always.

...expanding on this understanding (of type)

// models/biz-baz.js
export default class BizBaz extends Model {
  @belongsTo('foo-bar', { inverse: null })
  myFooBarProp;
}

here we have

  • a model definition for the type biz-baz
  • it has a property that is a relationship myFooBarProp
  • the relationship is to another model definition of type foo-bar

we expect the payload to look like

{
   id: "1",
   type: "biz-baz",
   relationships: {
     myFooBarProp: {
       data: {
         type: 'foo-bar',
         id: '2' 
       }
     }
   }
}

now let's expand FooBar to also have this relationship

// models/foo-bar.js
export default class FooBar extends Model {
   @belongsTo("biz-baz", { inverse: "myFooBarProp" })
   myBizBazProp;
}

// models/biz-baz.js
export default class BizBaz extends Model {
  @belongsTo('foo-bar', { inverse: "myBizBazProp" })
  myFooBarProp;
}

here we have

  • a model definition for the type foo-bar
  • it has a property that is a relationship myBizBazProp
  • the relationship is to another model definition of type biz-baz
  • the property on the other model definition that points back is myFooBarProp

and

  • a model definition for the type biz-baz
  • it has a property that is a relationship myFooBarProp
  • the relationship is to another model definition of type foo-bar
  • the property on the other model definition that points back is myBizBazProp

We expect the payload to include resources like this

{
   id: "1",
   type: "biz-baz",
   relationships: {
     myFooBarProp: {
       data: {
         type: 'foo-bar',
         id: '2' 
       }
     }
   }
}

{
   id: "2",
   type: "foo-bar",
   relationships: {
     myBizBazProp: {
       data: {
         type: 'biz-baz',
         id: '1' 
       }
     }
   }
}

...and with comments for more details

{
   id: "2",
   // modelName, matches path on disk
   type: "foo-bar",
   relationships: {
     // property on the model
     myBizBazProp: {
       data: {
         // modelName of related model
         // matches path on disk
         type: 'biz-baz', 
         id: '1' 
       }
     }
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment