# CouchDB2
CouchDB v2.x Python 3 interface in a single module.
Also a command line tool; [see below](#command-line-tool).
Most, but not all, features of this module work with CouchDB version < 2.0.
## Installation
```
$ pip install couchdb2
```
This module relies on `requests`: http://docs.python-requests.org/en/master/
## Example code
```python
import couchdb2
server = couchdb2.Server() # Arguments required according to local setup
db = server.create('test')
doc1 = {'_id': 'myid', 'name': 'mydoc', 'level': 4}
db.put(doc1)
doc = db['myid']
assert doc == doc1
doc2 = {'name': 'another', 'level': 0}
db.put(doc2)
print(doc2)
# {'_id': '66b5...', '_rev': '1-f3ac...', 'name': 'another', 'level': 0}
db.put_design('mydesign',
{"views":
{"name": {"map": "function (doc) {emit(doc.name, null);}"}
}
})
result = db.view('mydesign', 'name', key='another', include_docs=True)
assert len(result) == 1
print(result[0].doc) # Same printout as above, using OrderedDict
db.destroy()
```
## Server
```python
server = Server(href='http://localhost:5984/', username=None, password=None, use_session=True)
```
Connection to the CouchDB server.
If `use_session` is true, then an authenticated session is used
transparently. Otherwise, username and password is sent with each request.
### version
```python
server.version
```
Property attribute providing the version of the CouchDB server software.
### user_context
```python
server.user_context
```
Property attribute providing the user context of the connection.
### \_\_str\_\_
```python
str(server)
```
Return a simple string representation of the server interface.
### \_\_len\_\_
```python
len(server)
```
Return the number of user-defined databases.
### \_\_iter\_\_
```python
for db in server: ...
```
Return an iterator over all user-defined databases on the server.
### \_\_getitem\_\_
```python
db = server[name]
```
Get the named database.
### \_\_contains\_\_
```python
if name in server: ...
```
Does the named database exist?
### \_\_call\_\_
```python
data = server()
```
Return meta information about the server.
### up
```python
if server.up(): ...
```
Is the server up and running, ready to respond to requests?
CouchDB version >= 2.0.
### get
```python
db = server.get(name, check=True)
```
Get the named database. If `check` is true, then raise NotFoundError
if the the database does not exist.
### create
```python
db = server.create(name)
```
Create the named database.
### get_config
```python
data = server.get_config(nodename='_local')
```
Get the named node's configuration.
### get_active_tasks
```python
data = server.get_active_tasks()
```
Return a list of running tasks.
### get_cluster_setup
```python
data = server.get_cluster_setup(config)
```
Return the status of the node or cluster.
CouchDB version >= 2.0.
### set_cluster_setup
```python
server.cluster_setup(doc)
```
Configure a node as a single node, as part of a cluster, or finalize a cluster.
CouchDB version >= 2.0.
### get_membership
```python
data = server.get_membership()
```
Return data about the nodes that are part of the cluster.
CouchDB version >= 2.0.
### set_replicate
```python
data = server.set_replicate(doc)
```
Request, configure, or stop, a replication operation.
### get_scheduler_jobs
```python
data = server.get_scheduler_jobs(limit=None, skip=None)
```
Get a list of replication jobs.
CouchDB version >= 2.0.
### get_scheduler_docs
```python
data = server.get_scheduler_docs(limit=None, skip=None,
replicator_db=None, docid=None)
```
Get information about replication document(s).
CouchDB version >= 2.0.
### get_node_stats
```python
data = server.get_node_stats(nodename='_local')
```
Return statistics for the running server.
CouchDB version >= 2.0.
### get_node_system
```python
data = server.get_node_system(nodename='_local')
```
Return various system-level statistics for the running server.
CouchDB version >= 2.0.
## Database
```python
db = Database(server, name, check=True)
```
Interface to a named CouchDB database.
If `check` is true, then raise NotFoundError if the the database does not exist.
### \_\_str\_\_
```python
str(db)
```
Return the name of the CouchDB database.
### \_\_len\_\_
```python
len(db)
```
Return the number of documents in the database.
### \_\_contains\_\_
```python
if id in db: ...
```
Does a document with the given id exist in the database?
### \_\_iter\_\_
```python
for doc in db: ...
```
Return an iterator over all documents in the database.
### \_\_getitem\_\_
```python
doc = db[id]
```
Return the document with the given id.
### exists
```python
if db.exists(): ...
```
Does the database exist?
### check
```python
db.check()
```
Raises NotFoundError if the database does not exist.
### create
```python
db.create()
```
Create the database.
### destroy
```python
db.destroy()
```
Delete the database and all its contents.
### get_info
```python
data = db.get_info()
```
Return a dictionary with information about the database.
### get_security
```python
data = db.get_security()
```
Return a dictionary with security information for the database.
### set_security
```python
db.set_security(doc)
```
Set the security information for the database.
### compact
```python
db.compact(finish=False, callback=None)
```
Compact the CouchDB database by rewriting the disk database file
and removing old revisions of documents.
If `finish` is True, then return only when compaction is done.
In addition, if defined, the function `callback(seconds)` is called
every second until compaction is done.
### compact_design
```python
db.compact_design(designname)
```
Compact the view indexes associated with the named design document.
### view_cleanup
```python
db.view_cleanup()
```
Remove unnecessary view index files due to changed views in
design documents of the database.
### get
```python
doc = db.get(id, rev=None, revs_info=False, default=None)
```
Return the document with the given id, or the `default` value if not found.
### put
```python
db.put(doc)
```
Insert or update the document.
If the document is already in the database, the `_rev` item must
be present in the document; its value will be updated.
If the document does not contain an item `_id`, one will be added
having a UUID4 value. The `_rev` item will also be added.
### update
```python
db.update(docs)
```
Perform a bulk update or insertion of the given documents using a
single HTTP request.
The return value of this method is a list containing a tuple for every
element in the `docs` sequence. Each tuple is of the form
`(success, docid, rev_or_exc)`, where `success` is a boolean
indicating whether the update succeeded, `docid` is the ID of the
document, and `rev_or_exc` is either the new document revision, or
an exception instance (e.g. `ResourceConflict`) if the update failed.
If an object in the documents list is not a dictionary, this method
looks for an `items()` method that can be used to convert the object
to a dictionary. Effectively this means you can also use this method
with `mapping.Document` objects.
`docs` : a sequence of dictionaries or `Document` objects, or
objects providing a `items()` method that can be used to convert
them to a dictionary.
Returns an iterable (list) over the resulting documents.
### delete
```python
db.delete(doc)
```
Delete the document.
### purge
```python
db.purge(docs)
```
Perform purging (complete removing) of the given documents.
Uses a single HTTP request to purge all given documents. Purged
documents do not leave any meta-data in the storage and are not
replicated.
### get_designs
```python
data = db.get_designs()
```
Return the design documents for the database.
CouchDB version >= 2.2.
### get_design
```python
data = db.get_design(designname)
```
Get the named design document.
### put_design
```python
db.put_design(designname, doc, rebuild=True)
```
Insert or update the
PyPI 官网下载 | CouchDB2-1.8.2.tar.gz
版权申诉
103 浏览量
2022-01-10
04:36:56
上传
评论
收藏 22KB GZ 举报
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
最新资源
- flowable-designer-5.22.0.zip
- threadmanager.cpp
- 腾讯云小程序 - 一站式开发与部署平台
- 基于JSP+Java+Servlet采用MVC模式开发的购物网站+源码(毕业设计&课程设计&项目开发)
- fastgestures安装包,模拟mac的触控板收拾,两指代表右击, 三指拖拽
- 基于组态王的升降式横移立体车库控制系统+源码(毕业设计&课程设计&项目开发)
- 基于python+Django和协同过滤算法的电影推荐系统+源码(毕业设计&课程设计&项目开发)
- 环境配置 vscode+jupyter
- 项目全部代码,还包含使用到的图片
- 项目全部代码,还包含使用到的图片
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈