npm miragejs 0.1.35

latest releases: 0.1.48, 0.1.47, 0.1.46...
4 years ago

🚀 Enhancements

  • #328 Make serializer.schema public

    A reference to the schema instance. Useful to reference registered schema information.

  • #325 Relationship introspection

    This is a new feature that allows you to introspect a model's associations.

    Given

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

    you can now introspect the post's associations, either through schema or an actual instance of the Post model:

    server.schema.associationsFor('post')
    // or
    post.associations

    Both methods will return the same data structure:

    {
      user: BelongsToAssociation,
      comments: HasManyAssociation
    }

    and you can use the new properties exposed on the Association class to get the type, name, modelName, and foreign key:

    let userAssociation = post.associations.user
    
    userAssociation.type // "belongsTo"
    userAssociation.name // "user"
    userAssociation.modelName // "user"
    userAssociation.foreignKey // "userId"

    This feature can be useful if you need to build up something dynamically based on a model's relationships.

  • #327 Pass the primary resource to serializer.include()

    This is an update to the Serializer.include method that lets you use the primary resource being serialized to determine what the included associations should be.

    For example, when combined with the previous feature, you can use it to configure your Serializer to include all of a model's relationships by default:

    new Server({
      serializers: {
        application: Serializer.extend({
          include(request, resource) {
            let associations = this.schema.associationsFor(resource.modelName);
    
            return Object.keys(associations)
          }
        })
      }
    })

🏠 Internal

  • Dependency updates

Don't miss a new miragejs release

NewReleases is sending notifications on new releases.