Skip to main content
# python-jsonapi-datastore

Client-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.

## Description

The [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.

## Installing

Install python-jsonapi-datastore with `pip` as usual:

pip install jsonapi-datastore

This library has no 3rd-party dependencies.

## Parsing data

Just call the `.sync()` method of your store.
```python
store = 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 data

If you have meta data in your payload use the `.syncWithMeta` method of your store.
```python
store = JsonApiDataStore()
store.syncWithMeta(data)
```
This does everything that `.sync()` does, but returns an object with data and meta split.

## Retrieving models

Just call the `.find(type, id)` method of your store.
```python
article = store.find('article', 123)
```
or call the `.findAll(type)` method of your store to get all the models of that type.
```python
articles = store.findAll('article')
```
All the attributes *and* relationships are accessible through the model as object properties.
```python
print(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 missing

Currently, the store does not handle `links` attributes or resource-level or relationship-level meta.

## Contributing

Pull-requests welcome!

Release files for jsonapi-datastore 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonapi-datastore 0.1.2
File Size Uploaded
jsonapi-datastore-0.1.2.tar.gz 5.1 kB Details

Release files / jsonapi-datastore-0.1.2.tar.gz

Download URL jsonapi-datastore-0.1.2.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d4d91a345796ff464049dddc084803e8d2304b5e7501dc010a2d1fed6e28540b
BLAKE2b-256 checksum
How to use checksums
abb52511a8fce368e834b652dcb626c11414552418b62f19dd07b66dfb978315
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page