Welcome to PyCouchDB’s documentation!¶
Indices and tables¶
PyCouchDB Client¶
>>> from pycouchdb import Client
>>> cli = Client('http://admin:admin@localhost')
Create database and get access , update
>>> cli.create('userdb')
>>> users = cli.get_db('userdb')
>>> usr = {'name': 'john', 'lastname':'doe'}
>>> _id, _rev = users.add(usr)
>>> updated_usr = {'name': 'john', 'lastname':'doe', 'email': 'john.doe@localhost'}
>>> _id, _rev = users.update(_id, updated_usr)
>>> users.delete(_id)
>>> cli.delete('usersdb')
- class pycouchdb.client.Client(url: str, connection_engine: Callable[[str], pycouchdb.connections.Connection] = <class 'pycouchdb.connections.urllibcon.UrllibConn'>)¶
Main class to connect to Database
- create(db_name: str)¶
Creates a new database. The database name {db} must be composed by following next rules: Name must begin with a lowercase letter (a-z) Lowercase characters (a-z) Digits (0-9) Any of the characters _, $, (, ), +, -, and /.
- Parameters
name (str) – The name of new Db.
- Returns
status
- Return type
dict
- Raises
ServerError –
- delete(db_name: str)¶
Deletes the specified database, and all the documents and attachments contained within it.
- Parameters
db_name (str) – Database name
- Returns
status
- Return type
dict
- Raises
ServerError –
- get_db(name: str)¶
Return database instance
- Parameters
name (str) – Database name
- Returns
instance of pycouchdb.db.Database:
- Return type
class
- list_database_names() List[str]¶
List of all databases names
- Returns
databases names
- Return type
list
PyCouchDB Database¶
- class pycouchdb.db.Database(name: str, connection: pycouchdb.connections.Connection)¶
- add(doc: Dict[Any, Any]) Tuple[str, str]¶
Creates a new document in database
- Parameters
doc (dict) – Document ID.
- Returns
document id, revision
- Return type
tuple
- Raises
DatabaseError –
- add_batch(doc: Dict[Any, Any])¶
write documents to the database at a higher rate by using the batch option. This collects document writes together in memory (on a per-user basis) before they are committed to disk. This increases the risk of the documents not being stored in the event of a failure, since the documents are not written to disk immediately.
- delete(doc_id: str, rev: str = '') Tuple[str, str]¶
Marks the specified document as deleted
- Parameters
doc_id (str) – Document ID
- Returns
document id rev
- Return type
tuple
- Raises
DatabaseError –
- delete_index(design: str, name: str = '') Dict[str, bool]¶
Delete index :param design: Design Document ID starting with _design/ :type design: str :param name: Name of index :type name: str
- Returns
for example {“ok”: true}
- Return type
dict
- Raises
DatabaseError –
- doc_info(doc_id: str) Dict[str, Any]¶
Minimal amount of information about the specified document.
- Parameters
docid (str) – Document ID.
- Returns
Dictionary with keys rev - revision, size - size of document , date
- Return type
dict
- Raises
DatabaseError –
- get(doc_id: str, attachments: bool = False, att_encoding_info: bool = False, atts_since: List[str] = [], conflicts: bool = False, deleted_conflicts: bool = False, latest: bool = False, rev: str = '', revs: bool = False) Dict[str, Any]¶
Get document by id
- Parameters
doc_id (str) – Document ID
attachments (bool) – Includes attachments bodies in response.Default is false
att_encoding_info (bool) – Includes encoding
- Returns
Document
- Raises
DatabaseError –
- get_all_docs() Iterator[Dict[str, Any]]¶
Returns iterator for all documents in database
- Yields
dict – Document
- get_indexes() Dict[str, Any]¶
Get a list of all indexes in the database. In addition to the information available through this API, indexes are also stored in design documents <index-functions>. Design documents are regular documents that have an ID starting with _design/. Design documents can be retrieved and modified in the same way as any other document, although this is not necessary when using Mango.
- Returns
- total_rows (int) – Number of indexes
indexes (list) – Array of index definitions
- Return type
dict
- Raises
DatabaseError –
- get_many(ids: List[Dict[str, str]]) List[Dict[Any, Any]]¶
Get document list
- Parameters
ids (list) – list of dict {‘id’: ‘someid’}
- Returns
Document list
- Return type
list
- Raises
DatabaseError –
- list_documents() List[Dict[str, Any]]¶
List all documents names in database
- Returns
Document names
- Return type
list
- purge(docinfo: Dict[str, List[str]])¶
A database purge permanently removes the references to documents in the database. Normal deletion of a document within CouchDB does not remove the document from the database, instead, the document is marked as _deleted=true (and a new revision is created). This is to ensure that deleted documents can be replicated to other databases as having been deleted. This also means that you can check the status of a document and identify that the document has been deleted by its absence. The purge request must include the document IDs, and for each document ID, one or more revisions that must be purged. Documents can be previously deleted, but it is not necessary. Revisions must be leaf revisions.
- set_index(index: pycouchdb.query.IndexQuery) Dict[str, str]¶
Create a new index on a database
- Parameters
index (IndexQuery) – see IndexQuery documentation for details
- Raises
DatabaseError –
- Returns
dict with keys result with status of creating index, id of index , and index name
- Return type
Dict[str,str]
- update(doc_id: str, doc: Dict[str, Any], rev: str = '') Tuple[str, str]¶
Update document in database
- Parameters
doc_id (str) – Document ID.
doc (dict) – Document
rev (str) – Document’s revision if updating an existing document.
- Returns
document id, revision
- Return type
tuple
- Raises
DatabaseError –
PyCouchDB Document¶
- class pycouchdb.doc.Document(_dict: Optional[Dict[Any, Any]] = None, **kwargs: str)¶
Document class
- property db: pycouchdb.db.Database¶
Database instance
- property id: str¶
Document id
- property json: str¶
serialized to json string
- load_from_database(db: pycouchdb.db.Database, doc_id: str, doc_rev: str = '')¶
Load document from given database instance and set db as current
- pop(k[, d]) v, remove specified key and return the corresponding value.¶
If key is not found, d is returned if given, otherwise KeyError is raised.
- popitem() (k, v), remove and return some (key, value) pair¶
as a 2-tuple; but raise KeyError if D is empty.
- property rev: str¶
Document revision
- store()¶
Store document to databse if databse instance is set else raise DocumentError
- store_to_databse(db: pycouchdb.db.Database)¶
Store document to given database instace
PyCouchDB Server¶
- class pycouchdb.server.Server(url: str = 'http://localhost:5984')¶
The CouchDB server interface provides the basic interface to a CouchDB server for obtaining CouchDB information and getting and setting configuration information.
- active_tasks() List[Dict[str, Any]]¶
List of running tasks, including the task type, name, status and process ID. The result is a JSON array of the currently running tasks, with each task being described with a single object. Depending on operation type set of response object fields might be different.
- Returns
- changes_done (number) – Processed changes
database (string) – Source database pid (string) – Process ID progress (number) – Current percentage progress started_on (number) – Task start time as unix timestamp status (string) – Task status message task (string) – Task name total_changes (number) – Total changes to process type (string) – Operation Type updated_on (number) – Unix timestamp of last operation update
- Return type
List[Dict[str,Any]]
- dbs_info(*keys: str) List[Any]¶
Returns information of a list of the specified databases in the CouchDB instance
- Parameters
keys (list) – Array of database names to be requested
- Returns
list of datebase info
- Return type
list
- Raises
ServerError –
- is_up()¶
Confirms that the server is up, running, and ready to respond to requests.
- list_database_names() List[str]¶
Returns a list of all the databases in the CouchDB instance.
- membership()¶
Displays the nodes that are part of the cluster as cluster_nodes. The field all_nodes displays all nodes this node knows about, including the ones that are part of the cluster.
- Returns
- Return type
dict