Package: tinymongo
PurposeA simple wrapper to make a drop in replacement for mongodb out oftinydb.  This module is an
attempt to add an interface familiar to those currently using pymongo.StatusUnit testing is currently being worked on and functionality is being
added to the library.  Current coverage is 93%.  Current builds tested
on Python versions 2.7 and 3.3+.InstallationThe latest stable release can be installed viapip install tinymongo.The library is currently under rapid development and a more recent version
may be desired.In this case, simply clone this repository, navigate
to the root project directory, andpip install-e.or usepip install-egit+https://github.com/schapman1974/tinymongo.git#egg=tinymongoThis
is a pure python distribution and - thus - should require no external
compilers or tools besides those contained within Python itself.ExamplesThe quick start is shown below.  For a more detailed look at tinymongo,
take a look at demo.py within the repository.fromtinymongoimportTinyMongoClient# you can include a folder name or absolute path# as a parameter if not it will default to "tinydb"connection=TinyMongoClient()# either creates a new database file or accesses an existing one named `my_tiny_database`db=connection.my_tiny_database# either creates a new collection or accesses an existing one named `users`collection=db.users# insert data adds a new record returns _idrecord_id=collection.insert_one({"username":"admin","password":"admin","module":"somemodule"}).inserted_iduser_info=collection.find_one({"_id":record_id})# returns the record inserted# you can also use it directlydb.users.insert_one({"username":"admin"})# returns a list of all users of 'module'users=db.users.find({'module':'module'})#update data returns True if successful and False if unsuccessfulupd=db.users.update_one({"username":"admin"},{"$set":{"module":"someothermodule"}})# Sorting users by its username DESC# omitting `filter` returns all recordsdb.users.find(sort={'username':-1})# Pagination of the results# Getting the first 20 recordsdb.users.find(sort={'username':-1},skip=0,limit=20)# Getting next 20 recordsdb.users.find(sort={'username':-1},skip=20,limit=20)# Getting the total of recordsdb.users.count()Custom Storages and SerializersHINT: Learn more about TinyDB storages and Serializers indocumentationCustom StoragesYou have to subclassTinyMongoClientand provide custom storages like
CachingMiddleware or other available TinyDB Extension.Caching MiddlewarefromtinymongoimportTinyMongoClientfromtinydb.storagesimportJSONStoragefromtinydb.middlewaresimportCachingMiddlewareclassCachedClient(TinyMongoClient):"""This client has cache"""@propertydef_storage(self):returnCachingMiddleware(JSONStorage)connection=CachedClient('/path/to/folder')HINT: You can nest middlewares:FirstMiddleware(SecondMiddleware(JSONStorage))SerializersTo convert your data to a format that is writable to disk TinyDB uses the Python JSON module by default. It’s great when only simple data types are involved but it cannot handle more complex data types like custom classes.To support serialization of complex types you can write
your own serializers using thetinydb-serializationextension.First you need to install itpip installtinydb-serializationHandling datetime objectsYou can create a serializer for the pythondatetimeusing
the following snippet:fromdatetimeimportdatetimefromtinydb_serializationimportSerializerclassDatetimeSerializer(Serializer):OBJ_CLASS=datetimedef__init__(self,format='%Y-%m-%dT%H:%M:%S',*args,**kwargs):super(DatetimeSerializer,self).__init__(*args,**kwargs)self._format=formatdefencode(self,obj):returnobj.strftime(self._format)defdecode(self,s):returndatetime.strptime(s,self._format)NOTE: this serializer is available intinymongo.serializers.DateTimeSerializerNow you have to subclassTinyMongoClientand provide customs storage.fromtinymongoimportTinyMongoClientfromtinymongo.serializersimportDateTimeSerializerfromtinydb_serializationimportSerializationMiddlewareclassCustomClient(TinyMongoClient):@propertydef_storage(self):serialization=SerializationMiddleware()serialization.register_serializer(DateTimeSerializer(),'TinyDate')# register other custom serializersreturnserializationconnection=CustomClient('/path/to/folder')Flask-AdminThis extension can work with Flask-Admin which gives a web based administrative
panel to your TinyDB. Flask-Admin has features like filtering, search, web forms to
perform CRUD (Create, Read, Update, Delete) of the TinyDB records.You can find the example of Flask-Admin with TinyMongo inFlask-Admin Examples RepositoryNOTE: To use Flask-Admin you need to register a DateTimeSerialization as showed in the previous topic.ContributionsContributions are welcome!  Currently, the most valuable contributions
would be:adding test casesadding functionality consistent with pymongodocumentationidentifying bugs and issuesFuture DevelopmentI will also be adding support for gridFS by storing the files somehow and indexing them in a db like mongo currently doesMore to come……LicenseMIT License