You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 16, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: docs/data_layer.rst
+115-1Lines changed: 115 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,4 +5,118 @@ Data layer
5
5
6
6
.. currentmodule:: flask_rest_jsonapi
7
7
8
-
Coming soon. If you want to learn let's see tests or api reference.
8
+
|The data layer is a CRUD interface between resource manager and data. It is a very flexible system to use any ORM or data storage. You can even create a data layer that use multiple ORMs and data storage to manage your own objects. The data layer implements a CRUD interface for objects and relationships. It also manage pagination, filtering and sorting.
9
+
|
10
+
|Flask-REST-JSONAPI got a full featured data layer that use the popular ORM `SQLAlchemy <https://www.sqlalchemy.org/>`_.
11
+
12
+
.. note::
13
+
14
+
The default data layer used by a resource manager is the SQLAlchemy one. So if you want to use it, you don't have to specify the class of the data layer in the resource manager
15
+
16
+
To configure the data layer you have to set his required parameters in the resource manager.
17
+
18
+
Example:
19
+
20
+
.. code-block:: python
21
+
22
+
from flask_rest_jsonapi import ResourceList
23
+
from your_project.schemas import PersonSchema
24
+
from your_project.models import Person
25
+
26
+
classPersonList(ResourceList):
27
+
schema = PersonSchema
28
+
data_layer = {'session': db.session,
29
+
'model': Person}
30
+
31
+
You can also plug additional methods to your data layer in the resource manager. There is 2 kind of additional methods:
32
+
* query: the "query" additional method takes view_kwargs as parameter and return an alternative query to retrieve the collection of objects in the get method of the ResourceList manager.
33
+
* pre / post process methods: all CRUD and relationship(s) operations have a pre / post process methods. Thanks to it you can make additional work before and after each operations of the data layer. Parameters of each pre / post process methods are available in the `flask_rest_jsonapi.data_layers.base.Base <https://github.com/miLibris/flask-rest-jsonapi/blob/master/flask_rest_jsonapi/data_layers/base.py>`_ base class.
34
+
35
+
Example:
36
+
37
+
.. code-block:: python
38
+
39
+
from sqlalchemy.orm.exc import NoResultFound
40
+
from flask_rest_jsonapi import ResourceList
41
+
from flask_rest_jsonapi.exceptions import ObjectNotFound
person =self.session.query(Person).filter_by(id=view_kwargs['id']).one()
59
+
data['person_id'] = person.id
60
+
61
+
schema = ComputerSchema
62
+
data_layer = {'session': db.session,
63
+
'model': Computer,
64
+
'methods': {'query': query,
65
+
'before_create_object': before_create_object}}
66
+
67
+
.. note::
68
+
69
+
You don't have to declare additonals data layer methods in the resource manager. You can declare them in a dedicated module or in the declaration of the model.
70
+
71
+
Example:
72
+
73
+
.. code-block:: python
74
+
75
+
from sqlalchemy.orm.exc import NoResultFound
76
+
from flask_rest_jsonapi import ResourceList
77
+
from flask_rest_jsonapi.exceptions import ObjectNotFound
78
+
from your_project.models import Computer, Person
79
+
from your_project.additional_methods.computer import before_create_object
80
+
81
+
classComputerList(ResourceList):
82
+
schema = ComputerSchema
83
+
data_layer = {'session': db.session,
84
+
'model': Computer,
85
+
'methods': {'query': Computer.query,
86
+
'before_create_object': before_create_object}}
87
+
88
+
SQLAlchemy
89
+
----------
90
+
91
+
Required parameters:
92
+
93
+
:session: the session used by the data layer
94
+
:model: the model used by the data layer
95
+
96
+
Optional parameters:
97
+
98
+
:id_field: the field used as identifier field instead of the primary key of the model
99
+
:url_field: the name of the parameter in the route to get value to filter with. Instead "id" is used.
100
+
101
+
Custom data layer
102
+
-----------------
103
+
104
+
Like I said previously you can create and use your own data layer. A custom data layer must inherit from `flask_rest_jsonapi.data_layers.base.Base <https://github.com/miLibris/flask-rest-jsonapi/blob/master/flask_rest_jsonapi/data_layers/base.py>`_. You can see the full scope of possibilities of a data layer in this base class.
105
+
106
+
Usage example:
107
+
108
+
.. code-block:: python
109
+
110
+
from flask_rest_jsonapi import ResourceList
111
+
from your_project.schemas import PersonSchema
112
+
from your_project_data_layers import MyCustomDataLayer
113
+
114
+
classPersonList(ResourceList):
115
+
schema = PersonSchema
116
+
data_layer = {'class': MyCustomDataLayer,
117
+
'param_1': value_1,
118
+
'param_2': value_2}
119
+
120
+
.. note::
121
+
122
+
All items except "class" in the data_layer dict of the resource manager will be plugged as instance attributes of the data layer. It is easier to use in the data layer.
Copy file name to clipboardExpand all lines: docs/errors.rst
+74-1Lines changed: 74 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,4 +5,77 @@ Errors
5
5
6
6
.. currentmodule:: flask_rest_jsonapi
7
7
8
-
Coming soon. If you want to learn let's see tests or api reference.
8
+
JSONAPI 1.0 specification recommand to return errors like that:
9
+
10
+
.. sourcecode:: http
11
+
12
+
HTTP/1.1 422 Unprocessable Entity
13
+
Content-Type: application/vnd.api+json
14
+
15
+
{
16
+
"errors": [
17
+
{
18
+
"status": "422",
19
+
"source": {
20
+
"pointer":"/data/attributes/first-name"
21
+
},
22
+
"title": "Invalid Attribute",
23
+
"detail": "First name must contain at least three characters."
24
+
}
25
+
],
26
+
"jsonapi": {
27
+
"version": "1.0"
28
+
}
29
+
}
30
+
31
+
The "source" field gives information about the error if it is located in data provided or in querystring parameter.
32
+
33
+
The previous example displays error located in data provided instead of this next example displays error located in querystring parameter "include":
34
+
35
+
.. sourcecode:: http
36
+
37
+
HTTP/1.1 400 Bad Request
38
+
Content-Type: application/vnd.api+json
39
+
40
+
{
41
+
"errors": [
42
+
{
43
+
"status": "400",
44
+
"source": {
45
+
"parameter": "include"
46
+
},
47
+
"title": "BadRequest",
48
+
"detail": "Include parameter is invalid"
49
+
}
50
+
],
51
+
"jsonapi": {
52
+
"version": "1.0"
53
+
}
54
+
}
55
+
56
+
Flask-REST-JSONAPI provides two kind of helpers to achieve error displaying:
57
+
58
+
|* **the errors module**: you can import jsonapi_errors from the errors module to create the structure of a list of errors according to JSONAPI 1.0 specification
59
+
|
60
+
|* **the exceptions module**: you can import lot of exceptions from this module that helps you to raise exceptions that will be well formatted according to JSONAPI 1.0 specification
61
+
62
+
When you create custom code for your api I recommand to use exceptions from exceptions module of Flask-REST-JSONAPI to raise errors because JsonApiException based exceptions are catched and rendered according to JSONAPI 1.0 specification.
63
+
64
+
Example:
65
+
66
+
.. code-block:: python
67
+
68
+
# all required imports are not displayed in this example
69
+
from flask_rest_jsonapi.exceptions import ObjectNotFound
Copy file name to clipboardExpand all lines: docs/filtering.rst
+10-4Lines changed: 10 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,11 +5,11 @@ Filtering
5
5
6
6
.. currentmodule:: flask_rest_jsonapi
7
7
8
-
Flask-REST-JSONAPI as a very flexible filtering system. The filtering system is completely related to the data layer used by the resource. I will explain the filtering interface for SQLAlchemy data layer but you can use the same interface to your filtering implementation of your custom data layer. The only requirement is that you have to use the filter querystring parameter to make filtering according to JSONAPI 1.0 specification.
8
+
Flask-REST-JSONAPI as a very flexible filtering system. The filtering system is completely related to the data layer used by the ResourceList manager. I will explain the filtering interface for SQLAlchemy data layer but you can use the same interface to your filtering implementation of your custom data layer. The only requirement is that you have to use the "filter" querystring parameter to make filtering according to JSONAPI 1.0 specification.
9
9
10
10
.. note::
11
11
12
-
Urls examples are not urlencoded for a better readability
12
+
Examples are not urlencoded for a better readability
13
13
14
14
SQLAlchemy
15
15
----------
@@ -27,7 +27,7 @@ In this example we want to retrieve persons which name is John. So we can see th
27
27
28
28
:name: the name of the field you want to filter on
29
29
:op: the operation you want to use (all sqlalchemy operations are available)
30
-
:val: the value that you want to compare. You can replace this by field if you want to compare against the value of an other field
30
+
:val: the value that you want to compare. You can replace this by "field" if you want to compare against the value of an other field
31
31
32
32
Example with field:
33
33
@@ -57,7 +57,7 @@ If you want to filter through relationships you can do that:
57
57
58
58
.. note ::
59
59
60
-
When you filter on relationships use "any" operator for to many relationships and "has" operator for to one relationships.
60
+
When you filter on relationships use "any" operator for "to many" relationships and "has" operator for "to one" relationships.
61
61
62
62
There is a shortcut to achieve the same filter:
63
63
@@ -106,6 +106,8 @@ You can also use boolean combinaison of operations:
106
106
] HTTP/1.1
107
107
Accept: application/vnd.api+json
108
108
109
+
Availables operators depend on field type
110
+
109
111
Common available operators:
110
112
111
113
* any: used to filter on to many relationships
@@ -128,3 +130,7 @@ Common available operators:
128
130
* notin_: check if field is not in a list of values
129
131
* notlike: check if field does not contains a string
130
132
* startswith: check if field starts with a string
133
+
134
+
.. note::
135
+
136
+
Availables operators depend on field type in your model
0 commit comments