Association

Associations represent relationships between your Models.

The hasMany and belongsTo helpers are how you actually define relationships:

import { Server, Model, hasMany, belongsTo }

new Server({
  models: {
    user: Model.extend({
      comments: hasMany()
    }),
    comments: Model.extend({
      user: belongsTo()
    })
  }
})

View the Relationships guide to learn more about setting up relationships.

Each usage of the helper registers an Association (either a HasMany association or BelongsTo association) with your server's Schema. You can access these associations using either the schema.associationsFor() method, or the associations property on individual model instances.

You can then introspect the associations to do things like dynamically build up your JSON response in your serializers.

Properties

foreignKey: String

Returns the name used for the association's foreign key.

let server = new Server({
  models: {
    user: Model,
    post: Model.extend({
      fineAuthor: belongsTo("user"),
      comments: hasMany()
    }),
    comment: Model
  }
});

let associations = server.associationsFor('post')

associations.fineAuthor.foreignKey // fineAuthorId
associations.comments.foreignKey // commentIds

isPolymorphic: Boolean

Returns a Boolean that's true if the association is polymorphic:

For example, given

new Server({
  models: {
    comment: Model.extend({
      commentable: belongsTo({ polymorphic: true })
    })
  }
})

then

server.schema.associationsFor('comment').commentable.isPolymorphic // true

Check out the guides on polymorphic associations to learn more.

type: String

Returns either the string "hasMany" or "belongsTo", based on the association type.

modelName: String

The modelName of the associated model.

For example, given this configuration

new Server({
  models: {
    user: Model,
    comment: Model.extend({
      user: belongsTo()
    })
  }
})

the association's modelName would be user.

Note that an association's modelName and the name can be different. This is because Mirage supports multiple relationships of the same type:

new Server({
  models: {
    user: Model,
    comment: Model.extend({
      author: belongsTo('user'),
      reviewer: belongsTo('user')
    })
  }
})

For both these relationships, the modelName is user, but the first association has a name of author while the second has a name of reviewer.

name: String

The name of the association, which comes from the property name that was used to define it.

For example, given this server definition

new Server({
  models: {
    user: Model,
    comment: Model.extend({
      author: belongsTo('user')
    })
  }
})

the association's name would be author.

The name is used by Mirage to define foreign keys on the model (comment.authorId in this case), among other things.

Methods

isReflexive(): Boolean

Returns a Boolean that's true if the association is self-referential, i.e. if a model has an association with itself.

For example, given

new Server({
  models: {
    user: Model.extend({
      friends: hasMany('user')
    })
  }
})

then

server.schema.associationsFor('user').friends.isReflexive // true