Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 56795a1

Browse files
authored
Merge pull request #320 from cloudant/317-update-document-cm-docs
Update Document CM usage in docs/getting_started.rst
2 parents 07f3ad9 + 95d67d9 commit 56795a1

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

docs/getting_started.rst

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,27 +342,27 @@ Context managers
342342
Now that we've gone through the basics, let's take a look at how to simplify
343343
the process of connection, database acquisition, and document management
344344
through the use of Python *with* blocks and this library's context managers.
345-
Handling your business using *with* blocks saves you from having to connect and
345+
346+
Handling your business using *with* blocks saves you from having to connect and
346347
disconnect your client as well as saves you from having to perform a lot of
347348
fetch and save operations as the context managers handle these operations for
348-
you. This example uses the ``cloudant`` context helper to illustrate the
349+
you.
350+
351+
This example uses the ``cloudant`` context helper to illustrate the
349352
process but identical functionality exists for CouchDB through the ``couchdb``
350353
and ``couchdb_admin_party`` context helpers.
351354

352355
.. code-block:: python
353356
354-
# cloudant context helper
355357
from cloudant import cloudant
356358
357-
# couchdb context helper
359+
# ...or use CouchDB variant
358360
# from cloudant import couchdb
359361
360-
from cloudant.document import Document
361-
362362
# Perform a connect upon entry and a disconnect upon exit of the block
363363
with cloudant(USERNAME, PASSWORD, account=ACCOUNT_NAME) as client:
364364
365-
# CouchDB variant
365+
# ...or use CouchDB variant
366366
# with couchdb(USERNAME, PASSWORD, url=COUCHDB_URL) as client:
367367
368368
# Perform client tasks...
@@ -378,9 +378,22 @@ and ``couchdb_admin_party`` context helpers.
378378
# You can open an existing database
379379
del my_database
380380
my_database = client['my_database']
381-
381+
382+
The following example uses the ``Document`` context manager. Here we make
383+
multiple updates to a single document. Note that we don't save to the server
384+
after each update. We only save once to the server upon exiting the ``Document``
385+
context manager.
386+
387+
.. code-block:: python
388+
389+
from cloudant import cloudant
390+
from cloudant.document import Document
391+
392+
with cloudant(USERNAME, PASSWORD, account=ACCOUNT_NAME) as client:
393+
394+
my_database = client.create_database('my_database')
395+
382396
# Performs a fetch upon entry and a save upon exit of this block
383-
# Use this context manager to create or update a Document
384397
with Document(my_database, 'julia30') as doc:
385398
doc['name'] = 'Julia'
386399
doc['age'] = 30
@@ -394,6 +407,22 @@ and ``couchdb_admin_party`` context helpers.
394407
395408
print('Databases: {0}'.format(client.all_dbs()))
396409
410+
Always use the ``_deleted`` document property to delete a document from within
411+
a ``Document`` context manager. For example:
412+
413+
.. code-block:: python
414+
415+
with Document(my_database, 'julia30') as doc:
416+
doc['_deleted'] = True
417+
418+
*You can also delete non underscore prefixed document keys to reduce the size of the request.*
419+
420+
.. warning:: Don't use the ``doc.delete()`` method inside your ``Document``
421+
context manager. This method immediately deletes the document on
422+
the server and clears the local document dictionary. A new, empty
423+
document is still saved to the server upon exiting the context
424+
manager.
425+
397426
****************
398427
Endpoint access
399428
****************

0 commit comments

Comments
 (0)