touchdb

Downloads

touchdb is a simple document based nosql database built entirely on python. This save the data input which is in python dictionary into a JSON data in a .db file. There are only limited number of functionalities that are implemented in the engine but they are more than sufficient to handle normal database queries.

Find Me

Installation

Easy to install in your projects using pip.
pip install touchdb

Usage

>>> from touchdb import Database
>>> db = Database.Connector('./record.db', True)
>>> print(db.getByAttribute(record_type='sickness', patient_id='usr1'))

List of functions

  1. load(location, auto_dump) : Force the database to load the database from the database file into the memory.
  2. dump() : Dump the data in the memory into the database file.
  3. insert(**kwargs) : Insert the python dictionary data into the database. key - optional parameter which provide the key for the value to be inserted. value is python dict to be inserted into the database.
  4. get(key) : Get the data from the database using the key.
  5. getAll() : Get all the data in the database as JSON format(python dictionary).
  6. getByAttribute(**kwargs) : Attribute of the record will be checked with the provided values and return the list of mapping data in the database.
  7. exists(key) : Check if a key exists in the database.
  8. remove(key) : Remove a particular data mapping to the key in the database.
  9. removeByAttribute(**kwargs) : Remove all data records in the database which correlate to the attribute value.
  10. totalkeys(key=None) : If the parameter key is not provided return the total number of keys in the database, but if the key is provided return the number of data records 11. inside that particular dictionary.
  11. add(key, value:dict) : Add a new key-value pair to the existing data record with key as provided.
  12. extend(datas: dict) : Extend the database with the provided dictionary data.
  13. deldb() : Delete the database from both memory and file.


Sample Data

A sample data record file is given below for testing purpose.

{
    "894a9a5d-46e8-11ed-b600-b526d12bcc50": {
        "patient_id": "usr1",
        "patient_name": "James",
        "record_type": "sickness",
        "sens_level": 4,
        "data": "<THIS IS THE ACTUAL DATA TO BE STORED IN THE DATABASE>"
    },
    "69840102-479e-11ed-97ec-b526d12bcc50": {
        "patient_id": "usr6",
        "patient_name": "Kabilan",
        "record_type": "sickness",
        "sens_level": 3,
        "data": "THIS IS A DUMMY SICKNESS DATA FOR NEW USER"
    },
    "4e4f3d76-47b0-11ed-8752-b526d12bcc50": {
        "patient_id": "usr7",
        "patient_name": "Levi Ackerman",
        "record_type": "drug_prescription",
        "sens_level": 3,
        "data": "THIS IS A DUMMY DRUG PRESCRIPTION DATA RECORD FOR TESTING"
    }
}


A few example code

from touchdb import Database

db = Database.Connector('./record.db', True)
db.insert(value={'test_id':"test123"})
print(db.get(key='cf9a67ed-4bf6-11ed-9284-b72b6fa7f086'))
print(db.getAll())
print(db.getByAttribute(record_type='sickness'))
print(db.exists(key='cf9a67ed-4bf6-11ed-9284-b72b6fa7f086'))
print(db.remove(key='cf9a67ed-4bf6-11ed-9284-b72b6fa7f086'))
print(db.removeByAttribute(record_type='sickness'))
print(db.totalkeys())
print(db.totalkeys(key='69840102-479e-11ed-97ec-b526d12bcc50'))
print(db.add('4e4f3d76-47b0-11ed-8752-b526d12bcc50', {'address':'Eldian Kingdom', 'Kill count':'inf+'}))