This is a companion library to mattpolzin/JSONAPI. JSONAPI is a much more well established library; by comparison, this library is more of an experiment. Not a lot of work or testing has gone into this library yet but it might serve as a jumping off place for you.
This package has two modules.
JSONAPIResourceCache is aimed at supporting the use-case where your entity cache is a value-type. All you need to do is create a cache type and conform your resources to Materializable in order to get going. This approach is well suited to state-driven development.
JSONAPIResourceStore contains a reference-type entity store.
You can view an example using this strategy in Google Colab.
Without endorsing this degree of force unwrapping, here's a chunk of code in lieu of a properly fleshed out README.
import JSONAPIResourceCache
// Assuming existing definitions of `Resource1` and `Resource2` // `JSONAPI.ResourceObject` types, create a cache type.
structCache:Equatable,ResourceCache{varwidgets:ResourceHash<Resource1>varcogs:ResourceHash<Resource2>}
// extend our resource types to be Materializable
extensionResource1Description:Materializable{staticvarcachePath:WritableKeyPath<Cache,ResourceHash<Resource1>>{ \.widgets }}extensionResource2Description:Materializable{staticvarcachePath:WritableKeyPath<Cache,ResourceHash<Resource2>>{ \.cogs }}
// decode a `JSONAPI.Document` with a single primary resource of type `Resource1` // and included resources for the `relative1` relationship.
letdocument=...
// create a resource cache from the response (could be merged with
// an existing local cache pretty easily).
letresourceCache= document.resourceCache()!
// get the primary resource from the document as a ResourceObject
letprimaryResource= document.body.primaryResource!.value
// materialize the primary resource's `relative1` relationship from the
// cache.
letrelative=(primaryResource ~> \.relative1).materialized(from: resourceCache)This reference-type store is not my personal choice for how to manage resources, but it represents a viable option.
Without endorsing this degree of force unwrapping, here's a chunk of code in lieu of a properly fleshed out README.
import JSONAPIResourceStore
// decode a `JSONAPI.Document` with a signle primary resource of type `Resource1` // and included resources for the `relative1` relationship.
letdocument=...
// create a resource store
letresourceStore= document.resourceStore()!
// get the primary resource from the document as a ResourceObject
letprimaryResource= document.body.primaryResource!.value
// retrieve the primary resource from the store as a `StoredResource`
letresource=resourceStore[primaryResource.id]!
// retrieve the related resource by the name of the relationship
letrelative= resource.relative1