Walkthrough
Make sure you have installed kenobiDB via pip or from source
if you are following along.
Setting up the database
Import kenobiDB
>>> from kenobi import KenobiDB
Create instance of KenobiDB class
>>> db = KenobiDB('database.yaml')
If you would like kenobiDB to write changes to the disk as soon as they happen
you can set auto_save to True:
>>> db = KenobiDB('database.yaml', auto_save=True)
Inserting documents
Insert one document at a time
>>> db.insert({'name': 'user1', 'groups': ['user']})
True
You can also insert many documents at once
>>> db.insert_many([{'name': 'user2', 'groups': ['admin', 'user']},
{'name': 'user3', 'groups': ['sudo', 'user']}])
True
Searching documents
Get a list of all documents in the database
>>> db.all()
[{'name': 'user1', 'groups': ['user']},
{'name': 'user2', 'groups': ['admin', 'user']},
{'name': 'user3', 'groups': ['sudo', 'user']}]
Search for documents
>>> db.search('name', 'user1')
[{'name': 'user1', 'groups': ['user']}]
Use nested any/all searches as well:
>>> db.find_any('groups', ['admin', 'sudo'])
[{'name': 'user2', 'groups': ['admin', 'user']},
{'name': 'user3', 'groups': ['sudo', 'user']}]
>>> db.find_all('groups', ['admin', 'user'])
[{'name': 'user2', 'groups': ['admin', 'user']}]
As you can see, 'any' tests if there is at least one document matching
the search while 'all' ensures all documents match the search.
Updating a document
Update a with the matching key, value pair
>>> db.update('name', 'user1', {'groups': ['user', sudo', 'admin']})
True
Lets check to see the updated document
>>> db.all()
[{'name': 'user1', 'groups': ['user', 'sudo', 'admin']},
{'name': 'user2', 'groups': ['admin', 'user']},
{'name': 'user3', 'groups': ['sudo', 'user']}]
Saving the database
Since auto_save was not to True when this instance of KenobiDB was created,
you have to save the database to the 'database.yaml' file
>>> db.save_db()
True
Removing documents
Remove one document:
>>> db.remove('name', 'user1')
[{'name': 'user1', 'groups': ['user', 'sudo', 'admin']}]
You can see the document is gone from our database:
>>> db.all()
[{'name': 'user2', 'groups': ['admin', 'user']},
{'name': 'user3', 'groups': ['sudo', 'user']}]
And of course you can delete the entire database and start with a clean slate
>>> db.purge()
True
All gone:
>>> db.all()
[]
Suggestions
If you have any suggestions or need to report a bug please file an issue on GitHub!
Written by Harrison Erd
Art by Jessica Finson Roth