jsonapi-datastore 0.1.2

Creator: bradpython12

Last updated:

Add to Cart

Description:

jsonapidatastore 0.1.2

# python-jsonapi-datastoreClient-side [JSON API](http://jsonapi.org) data handling made easy. This is a re-write of Lucas Hosseini's [JavaScript jsonapi-datastore](https://github.com/beauby/jsonapi-datastore/) for Python. The serialization is not included yet. If you need it feel free to open a PR or ask for it.## DescriptionThe [JSONAPI](http://jsonapi.org) standard is great for exchanging data (which is its purpose), but the format is not ideal to work directly within an application.python-jsonapi-datastore is a framework-agnostic library that takes away the burden of handling [JSONAPI](http://jsonapi.org) data on the client side.What it does:- read JSONAPI payloads,- rebuild the underlying data graph,- allows you to query models and access their relationships directly,- create new models.What it does not do:- make requests to your API. You design your endpoints URLs, the way you handle authentication, caching, etc. is totally up to you.## InstallingInstall python-jsonapi-datastore with `pip` as usual: pip install jsonapi-datastoreThis library has no 3rd-party dependencies.## Parsing dataJust call the `.sync()` method of your store.```pythonstore = JsonApiDataStore()store.sync(data)```This parses the data and incorporates it in the store, taking care of already existing records (by updating them) and relationships.## Parsing with meta dataIf you have meta data in your payload use the `.syncWithMeta` method of your store.```pythonstore = JsonApiDataStore()store.syncWithMeta(data)```This does everything that `.sync()` does, but returns an object with data and meta split.## Retrieving modelsJust call the `.find(type, id)` method of your store.```pythonarticle = store.find('article', 123)```or call the `.findAll(type)` method of your store to get all the models of that type.```pythonarticles = store.findAll('article')```All the attributes *and* relationships are accessible through the model as object properties.```pythonprint(article.author.name)```In case a related resource has not been fetched yet (either as a primary resource or as an included resource), the corresponding property on the model will contain only the `type` and `id` (and the `._placeHolder` property will be set to `true`). However, the models are *updated in place*, so you can fetch a related resource later, and your data will remain consistent.## Examples```python# Create a store:store = JsonApiDataStore()# Then, given the following payload, containing two `articles`, with a related `user` who is the author of both:payload = { 'data': [{ 'type': 'article', 'id': 1337, 'attributes': { 'title': 'Cool article' }, 'relationships': { 'author': { 'data': { 'type': 'user', 'id': 1 } } } }, { 'type': 'article', 'id': 300, 'attributes': { 'title': 'Even cooler article' }, 'relationships': { 'author': { 'data': { 'type': 'user', 'id': 1 } } } }]}# we can sync it:articles = store.sync(payload)# which will return the list of synced articles.# Later, we can retrieve one of those:article = store.find('article', 1337)# If the author resource has not been synced yet, we can only access its id and its type:print(article.author)# { id: 1, _type: 'article' }# If we do sync the author resource later:authorPayload = { 'data': { 'type': 'user', 'id': 1, 'attributes': { 'name': 'Lucas' } }}store.sync(authorPayload)# we can then access the author's name through our old `article` reference:print(article.author.name)# 'Lucas'```## What's missingCurrently, the store does not handle `links` attributes or resource-level or relationship-level meta.## ContributingPull-requests welcome!

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.