nosqlapi document¶
In this package we find abstract classes and ODM classes concerning the document database types.
client module¶
The client module contains the specific classes for document databases and they are based on the core classes.
Client module for document NOSQL database.
- class nosqlapi.docdb.client.DocBatch(batch, session=None)[source]¶
Bases:
nosqlapi.common.core.Batch
,abc.ABC
Document NOSQL database Batch class
- class nosqlapi.docdb.client.DocConnection(*args, **kwargs)[source]¶
Bases:
nosqlapi.common.core.Connection
,abc.ABC
Document NOSQL database Connection class
- __init__(*args, **kwargs)[source]¶
Instantiate Connection object
- Parameters
host – Name of host that contains database
user – Username for connect to the host
password – Password for connect to the host
database – Name of database
port – Tcp port
bind_address – Hostname or an IP address for multiple network interfaces
read_timeout – Timeout for reading from the connection in seconds
write_timeout – Timeout for writing from the connection in seconds
ssl – Ssl connection established
ssl_ca – Ssl CA file specified
ssl_cert – Ssl certificate file specified
tls – Tls connection established
ssl_key – Ssl private key file specified
ssl_verify_cert – Verify certificate file
max_allowed_packet – Max size of packet sent to server in bytes
- class nosqlapi.docdb.client.DocResponse(data, code=None, header=None, error=None)[source]¶
Bases:
nosqlapi.common.core.Response
,abc.ABC
Document NOSQL database Response class
- __weakref__¶
list of weak references to the object (if defined)
- class nosqlapi.docdb.client.DocSelector(*args, **kwargs)[source]¶
Bases:
nosqlapi.common.core.Selector
,abc.ABC
Document NOSQL database Selector class
- class nosqlapi.docdb.client.DocSession(connection, database=None)[source]¶
Bases:
nosqlapi.common.core.Session
,abc.ABC
Document NOSQL database Session class
client example¶
This is an example of a library for connecting to a mongodb server.
# mydocdb.py
import nosqlapi
# this module is my library of NOSQL document database, like MongoDB database
class Connection(nosqlapi.docdb.DocConnection):
def __init__(host='localhost', port=27017, database=None, username=None, password=None, ssl=None, tls=None,
cert=None, ca_cert=None, ca_bundle=None): ...
def close(self): ...
def connect(self, *args, **kwargs): ...
def create_database(self, database): ...
def has_database(self, database): ...
def databases(self): ...
def delete_database(self): ...
def show_database(self, database): ...
def copy_database(self, source, destination, force=False): ...
class Session(nosqlapi.docdb.DocSession):
# define here all methods
pass
conn = Connection('mymongo.local', username='admin', password='pa$$w0rd')
print(conn.databases()) # {"databases": [{"name": "admin", "sizeOnDisk": 978944, "empty": False}], "totalSize": 1835008, "ok": 1}
sess = conn.connect() # Session object
...
odm module¶
The odm module contains the specific object for document databases.
ODM module for document NOSQL database.
- class nosqlapi.docdb.odm.Collection(name, *docs)[source]¶
Bases:
object
Represents collection of documents
- __init__(name, *docs)[source]¶
Collection object
- Parameters
name – Name of collection
docs – Documents like dict
- __weakref__¶
list of weak references to the object (if defined)
- append(doc)[source]¶
Append document to collection
- Parameters
doc – Documents like dict
- Returns
None
- property docs¶
Documents of collection
- class nosqlapi.docdb.odm.Database(name, exists=False)[source]¶
Bases:
nosqlapi.kvdb.odm.Keyspace
Represents database
- class nosqlapi.docdb.odm.Document(value=None, oid=None, **values)[source]¶
Bases:
object
Represents document
- __init__(value=None, oid=None, **values)[source]¶
Document object
- Parameters
value – Body of document like dict
oid – String id (default uuid1 string)
values – Additional values of body
- __weakref__¶
list of weak references to the object (if defined)
- property body¶
Elements of document
- property id¶
Document unique id
- class nosqlapi.docdb.odm.Index(name, data)[source]¶
Bases:
object
Represents document index
- __init__(name, data)[source]¶
Index object
- Parameters
name – Name of index
data – Data of index like dict
- __weakref__¶
list of weak references to the object (if defined)
- nosqlapi.docdb.odm.document(func)[source]¶
Decorator function to transform dictionary object to Document object
- Parameters
func – function to decorate
- Returns
Document object
odm example¶
These objects represent the respective document in databases.
import nosqlapi
import mydocdb
# Create database
db = nosqlapi.docdb.odm.Database('test')
# Create documents
doc1 = nosqlapi.docdb.Document(oid=1)
doc2 = nosqlapi.docdb.Document(oid=2)
# Add documents to database
db.append(doc1)
db.append(doc2)
# Create Document object from document decorator
@nosqlapi.docdb.document
def user(name, age, salary):
return {'name': name, 'age': age, 'salary': salary}
# Create database with docs
mydocdb.conn.create_database(db)
# Add more doc
mydocdb.sess.insert(user('Matteo Guadrini', 36, 25000))