Skip to content

0.5.0: Entity inheritance

Compare
Choose a tag to compare
@junglebarry junglebarry released this 20 Aug 16:28
b3b178d

Entity definitions can now be split across multiple types:

Add the ability to extend subclasses of JsonapiEntity, whilst drawing annotated properties from the prototype chain.

Prior to this release, if you attempt to create a common superclass that extends JsonapiEntity, then extend with child @entity classes, the only properties to be deserialised are those declared in the subclass.

For example, the @entity class Cat:

https://github.com/junglebarry/jsonapi-transformers/blob/59f82faa73b2bd238f0c7eb3b36a3f64f1e14c87/spec/test-data/cat.ts#L1-L21

extends the (abstract) Animal:

https://github.com/junglebarry/jsonapi-transformers/blob/59f82faa73b2bd238f0c7eb3b36a3f64f1e14c87/spec/test-data/animal.ts#L1-L14

so you would expect a deserialised Cat to contain all the Animal fields and the Cat fields.

This release adds the ability to pull inherited JSON:API fields from the parent type, by making attribute, link, meta, and relationship traverse up the prototype chain.

However, this release does not attempt to allow full OO extensions: it is not permitted to have an @entity subtype of a class that is, itself, and @entity (due to the odd prototype chain).