Skip to content

Commit 3425044

Browse files
ricellisemlaver
authored andcommitted
docs: 0.8.0 API changes migration guide
Signed-off-by: Esteban Laver <[email protected]>
1 parent 79f8285 commit 3425044

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

api-changes.md

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# `0.8.0`
2+
3+
For versions earlier than `0.8.0` the model classes for document types:
4+
* `Document`
5+
* `DesignDocument`
6+
* `ReplicationDocument`
7+
8+
were generated with standard Python convention names for all fields.
9+
By convention a leading `_` in Python implies internal use so leading `_` were
10+
not used for the reserved CouchDB names in the Python model classes and
11+
were added during serialization.
12+
13+
This representation of the `Document` model did not allow for members with the
14+
same names as the reserved (`_` prefixed) document metadata members.
15+
16+
This meant that members named any of the following were removed by the `Document`
17+
`from_dict` and `to_dict` functions:
18+
* `attachments`
19+
* `conflicts`
20+
* `deleted`
21+
* `deleted_conflicts`
22+
* `id`
23+
* `local_seq`
24+
* `rev`
25+
* `revisions`
26+
* `revs_info`
27+
28+
as described in [issue #490](https://github.com/IBM/cloudant-python-sdk/issues/490).
29+
30+
To resolve this problem, starting from version `0.8.0` model classes that accept
31+
user defined properties use the leading `_` CouchDB convention for
32+
CouchDB metadata property names instead of using the Python convention.
33+
This introduces breaking changes that require code updates for usages
34+
of the model types `Document`, `DesignDocument` and `ReplicationDocument`.
35+
36+
## Breaking changes
37+
38+
The kwarg or attribute names that changed are:
39+
| kwarg/attribute name (<`0.8.0`) | kwarg/attribute name (>=`0.8.0`) |
40+
| --- | --- |
41+
| `attachments`| `_attachments` |
42+
| `conflicts`| `_conflicts` |
43+
| `deleted`| `_deleted` |
44+
| `deleted_conflicts`| `_deleted_conflicts` |
45+
| `id`| `_id` |
46+
| `local_seq`| `_local_seq` |
47+
| `rev`| `_rev` |
48+
| `revisions`| `_revisions` |
49+
| `revs_info`| `_revs_info` |
50+
51+
_Note:_ Dictionary literals always used the `_` prefixed form of the
52+
name so there are no code changes in those cases.
53+
54+
### Writing
55+
56+
In the case of writing to the server the names are
57+
kwarg parameters used to initialize these classes:
58+
* `Document`
59+
* `DesignDocument`
60+
* `ReplicationDocument`
61+
62+
The functions that impacted by these changes are:
63+
1. Functions that accept `Document` in the `document` kwarg:
64+
* `post_document`
65+
* `put_document`
66+
* `put_local_document`
67+
1. Functions that accept `DesignDocument` in the `design_document` kwarg:
68+
* `put_design_document`
69+
1. Functions that accept `ReplicationDocument` in the `replication_document` kwarg:
70+
* `put_replication_document`
71+
1. Functions that accept `BulkDocs` in the `bulk_docs` kwarg. In this case the
72+
changes are in the elements of the `List[Document]` in the `docs` kwarg:
73+
* `post_bulk_docs`
74+
75+
#### Example class initialization
76+
77+
Before:
78+
```python
79+
# id is used in Document initializer
80+
my_doc = Document(
81+
id="small-appliances:1000042",
82+
type="product",
83+
productid="1000042",
84+
name="Fidget toy")
85+
86+
result = service.post_document(db='products', document=my_doc).get_result()
87+
```
88+
89+
After:
90+
```python
91+
# Now _id is used in Document initializer
92+
my_doc = Document(
93+
_id="small-appliances:1000042",
94+
type="product",
95+
productid="1000042",
96+
name="Fidget toy")
97+
98+
result = service.post_document(db='products', document=my_doc).get_result()
99+
```
100+
101+
#### Example dict literal
102+
103+
Before & After (no changes):
104+
```python
105+
# _id is used in dict literal
106+
my_doc = {
107+
'_id': 'small-appliances:1000042',
108+
'type': 'product',
109+
'productid': '1000042',
110+
'name': 'Fidget toy'
111+
}
112+
113+
result = service.post_document(db='products', document=my_doc).get_result()
114+
```
115+
116+
### Reading
117+
118+
In the case of reading from the server the `_` prefixed names were always used in the raw
119+
dictionaries returned from the `get_result` function. As such **no changes** are necessary
120+
to the key names to read the values from these result dicts. However, renames are necessary
121+
if the calling code uses the `from_dict` function to convert the result dict to a model class.
122+
123+
The functions impacted in that case are:
124+
1. Functions returning a `dict` that represents a `Document`:
125+
* `get_document`
126+
* `get_local_document`
127+
1. Functions returning a `dict` that represents a `DesignDocument`:
128+
* `get_design_document`
129+
1. Functions returning a `dict` that represents a `ReplicationDocument`:
130+
* `get_replication_document`
131+
1. Functions returning a `dict` containing a `Document` representation:
132+
* `post_bulk_get` (via `BulkGetResult` `results` > `BulkGetResultItem` `docs` >`BulkGetResultDocument` `ok`)
133+
1. Functions returning a `dict` potentially containing a `Document` representation (for example if using `include_docs`):
134+
* `post_all_docs` (via `AllDocsResult` `rows` > `DocsResultRow` `doc`)
135+
* `post_changes` (via `ChangesResult` `results` > `ChangesResultItem` `doc`)
136+
* `post_find` (via `FindResult` `docs`)
137+
* `post_partition_find`(via `FindResult` `docs`)
138+
* `post_search` (via `SearchResult` `rows` > `SearchResultRow` `doc` or `SearchResult` `groups` > `SearchResultProperties` `rows` > `SearchResultRow` `doc`)
139+
* `post_partition_search` (via `SearchResult` `rows` > `SearchResultRow` `doc` or `SearchResult` `groups` > `SearchResultProperties` `rows` > `SearchResultRow` `doc`)
140+
* `post_view` (via `ViewResult` `rows` > `ViewResultRow` `doc`)
141+
* `post_partition_view` (via `ViewResult` `rows` > `ViewResultRow` `doc`)
142+
143+
#### Example result
144+
145+
Before & after (no changes):
146+
```python
147+
result = service.get_document(
148+
db='products',
149+
doc_id='small-appliances:1000042'
150+
).get_result()
151+
152+
# _id is used to access document id in result dict
153+
print(result._id)
154+
# prints:
155+
# small-appliances:1000042
156+
```
157+
158+
#### Example `from_dict`
159+
160+
Before:
161+
```python
162+
result = service.get_document(
163+
db='products',
164+
doc_id='small-appliances:1000042'
165+
).get_result()
166+
167+
doc = Document.from_dict(result)
168+
# id is used to access document id in Document class
169+
print(doc.id)
170+
# prints:
171+
# small-appliances:1000042
172+
```
173+
174+
After:
175+
```python
176+
result = service.get_document(
177+
db='products',
178+
doc_id='small-appliances:1000042'
179+
).get_result()
180+
181+
doc = Document.from_dict(result)
182+
# Now _id is used to access document id in Document class
183+
print(doc._id)
184+
# prints:
185+
# small-appliances:1000042
186+
```

0 commit comments

Comments
 (0)