Swiftpack.co - Package - mattpolzin/JSONAPI-ResourceStorage

JSONAPI-ResourceStorage

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.

JSONAPIResourceCache

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.
struct Cache: Equatable, ResourceCache {
    var widgets: ResourceHash<Resource1>
    var cogs: ResourceHash<Resource2>
}

// extend our resource types to be Materializable
extension Resource1Description: Materializable {
    static var cachePath: WritableKeyPath<Cache, ResourceHash<Resource1>> { \.widgets }
}

extension Resource2Description: Materializable {
    static var cachePath: WritableKeyPath<Cache, ResourceHash<Resource2>> { \.cogs }
}

// decode a `JSONAPI.Document` with a single primary resource of type `Resource1` 
// and included resources for the `relative1` relationship.
let document = ...

// create a resource cache from the response (could be merged with
// an existing local cache pretty easily).
let resourceCache = document.resourceCache()!

// get the primary resource from the document as a ResourceObject
let primaryResource = document.body.primaryResource!.value

// materialize the primary resource's `relative1` relationship from the
// cache.
let relative = (primaryResource ~> \.relative1).materialized(from: resourceCache) 

JSONAPIResourceStore

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.
let document = ...

// create a resource store
let resourceStore = document.resourceStore()!

// get the primary resource from the document as a ResourceObject
let primaryResource = document.body.primaryResource!.value

// retrieve the primary resource from the store as a `StoredResource`
let resource = resourceStore[primaryResource.id]!

// retrieve the related resource by the name of the relationship
let relative = resource.relative1

Github

link
Stars: 0

Dependencies

Used By

Total: 0

Releases

Specify platform minimums - 2020-06-28 00:50:28

Platform minimums inherited by dependance on JSONAPI are now explicit in this package's manifest.

Update to stable release versions of JSONAPI v4 - 2020-06-01 03:56:58

Rename `materialize()` to `materialized()` - 2020-05-31 21:53:24

Update to latest JSON release candidate. - 2020-05-30 02:33:17

Add non-optional constructors for SuccessDocument. - 2020-05-27 06:06:17

Initial pre-release - 2020-05-26 20:45:35