couchdb3 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
couchdb3/__init__.py ADDED
@@ -0,0 +1,169 @@
1
+ __doc__ = """# CouchDB3
2
+
3
+ *CouchDB3* is a wrapper around the CouchDB API. For more detailed information, please refer to
4
+ [the documentation](https://n-vlahovic.github.io/couchdb3/).
5
+
6
+ ## Disclaimer
7
+
8
+ Big parts of the documentation (and thus docstrings) have been copied from CouchDB's API's great
9
+ [official documentation](https://docs.couchdb.org/en/main/api/index.html).
10
+
11
+
12
+ ## Requirements
13
+
14
+ - Python version `>= 3.10`
15
+ - CouchDB version `3.x`
16
+
17
+ ## Installation
18
+ Installing via PyPi
19
+ ```bash
20
+ pip install couchdb3
21
+ ```
22
+
23
+ Installing via Github
24
+ ```bash
25
+ python -m pip install git+https://github.com/n-Vlahovic/couchdb3.git
26
+ ```
27
+
28
+ Installing from source
29
+ ```bash
30
+ git clone https://github.com/n-Vlahovic/couchdb3
31
+ python -m pip install -e couchdb3
32
+ ```
33
+
34
+ ## Quickstart
35
+
36
+ ### Connecting to a database server
37
+
38
+ ```python
39
+ import couchdb3
40
+
41
+ client = couchdb3.Server(
42
+ "http://user:password@127.0.0.1:5984"
43
+ )
44
+
45
+ # Checking if the server is up
46
+ print(client.up())
47
+ # True
48
+ ```
49
+
50
+ user and password can also be passed into the Server constructor as keyword parameters, e.g.
51
+
52
+ ```python
53
+ client = couchdb3.Server(
54
+ "127.0.0.1:5984", # Scheme omitted - will assume http protocol
55
+ user="user",
56
+ password="password"
57
+ )
58
+ ```
59
+
60
+ Both approaches are equivalent, i.e. in both cases the instance's `scheme,host,port,user,password` will be identical.
61
+
62
+ Further, clients can be used with context managers:
63
+ ```python
64
+ with couchdb3.Server("http://user:password@127.0.0.1:5984") as client:
65
+ # Do stuff
66
+ ...
67
+ ```
68
+
69
+ ### Getting or creating a database
70
+ ```python
71
+ dbname = "mydb"
72
+ db = client.get(dbname) if dbname in client else client.create(dbname)
73
+ print(db)
74
+ # Database: mydb
75
+ ```
76
+
77
+ ### Creating a document
78
+ ```python
79
+ mydoc = {
80
+ "_id": "mydoc-id",
81
+ "name": "Hello",
82
+ "type": "World"
83
+ }
84
+ print(db.save(mydoc))
85
+ # ('mydoc-id', True, '1-24fa3b3fd2691da9649dd6abe3cafc7e')
86
+ ```
87
+ Note: `Database.save` requires the document to have an id (i.e. a key `_id`),
88
+ `Database.create` does not.
89
+
90
+ ### Updating a document
91
+ To update an existing document, retrieving the revision is paramount.
92
+ In the example below, `dbdoc` contains the key `_rev` and the builtin `dict.update` function is used to update the
93
+ document before saving it.
94
+ ```python
95
+ mydoc = {
96
+ "_id": "mydoc-id",
97
+ "name": "Hello World",
98
+ "type": "Hello World"
99
+ }
100
+ dbdoc = db.get(mydoc["_id"])
101
+ dbdoc.update(mydoc)
102
+ print(db.save(dbdoc))
103
+ # ('mydoc-id', True, '2-374aa8f0236b9120242ca64935e2e8f1')
104
+ ```
105
+ Alternatively, one can use `Database.rev` to fetch the latest revision and overwrite the document
106
+ ```python
107
+ mydoc = {
108
+ "_id": "mydoc-id",
109
+ "_rev": db.rev("mydoc-id"),
110
+ "name": "Hello World",
111
+ "type": "Hello World"
112
+ }
113
+ print(db.save(mydoc))
114
+ # ('mydoc-id', True, '3-d56b14b7ffb87960b51d03269990a30d')
115
+ ```
116
+
117
+ ### Deleting a document
118
+ To delete a document, the `docid` and `rev` are needed
119
+ ```python
120
+ docid = "mydoc-id"
121
+ print(db.delete(docid=docid, rev=db.rev(docid))) # Fetch the revision on the go
122
+ # True
123
+ ```
124
+
125
+ ### Working with partitions
126
+ For a partitioned database, the `couchdb3.database.Partition` class offers a wrapper around partitions (acting similarly
127
+ to collections in Mongo).
128
+
129
+ ```python
130
+ from couchdb3 import Server, Database, Partition
131
+
132
+
133
+ client: Server = Server(...)
134
+ db: Database = client["some-db"]
135
+ partition: Partition = db.get_partition("partition_id")
136
+ ```
137
+
138
+ Partition instances append the partition's ID the document IDs (`partition-id:doc-id`) for a simpler user interaction,
139
+ e.g.
140
+ ```python
141
+ doc_id = "test-id"
142
+ print(doc_id in partition) # no need to append the partition's ID
143
+ rev = partition.rev(doc_id)
144
+ partition.save({
145
+ "_id": doc_id, # no need to append the partition's ID
146
+ "_rev": rev,
147
+ ...
148
+ })
149
+ ```
150
+
151
+ The partition ID will only be appended provided document IDs do not start with `partition-id`, e.g. the following will
152
+ work and be equivalent to the previous example
153
+ ```python
154
+ doc_id = "partition_id:test-id"
155
+ print(doc_id in partition)
156
+ rev = partition.rev(doc_id)
157
+ partition.save({
158
+ "_id": doc_id,
159
+ "_rev": rev,
160
+ ...
161
+ })
162
+ ```
163
+ """
164
+ from . import exceptions
165
+ from . import utils
166
+ from .database import Database, Partition
167
+ from .document import Document
168
+ from .server import Server
169
+ from .view import ViewResult, ViewRow