Package: smartninja-mongo
SmartNinja MongoA simplewrapperfor MongoDB (pymongo) and TinyDB (tinymongo). It also provides a lightweightobject-document mapper(ODM).Use it if you don't want to (or can't) install MongoDB locally.InstallationInstall the package via pip:pip install smartninja-mongoOr add it in yourrequirements.txtand runpip install -r requirements.txt.DependenciesSmartNinja Mongo automatically installs the following dependencies:tinymongopymongotinydbtinydb_serialization(used to easily convert datetime objects)How it worksYou write your code as you would using thepymongopackage. Except that you get theMongoClientclass from thesmartninja_mongolibrary (see examples below).SmartNinja Mongoautomaticallyfigures out whether you're the program is running onlocalhostor on aproductionserver.If it's onlocalhost, TinyDB will be used instead of MongoDB via thetinymongopackage (except if you set theCUSTOM_MONGO_SERVERenv var).If the app runs onHerokuor onAzure, thesmartninja_mongopackage automatically recognizes this and returns thepymongoclient class instead of the one fromtinymongo.If your program runs insome other production environment, just set theCUSTOM_MONGO_SERVERenv var (add any value to it that comes back asTrue, like for example"1") andpymongoclient will be used.UsageSmartNinja Mongo's only task is to get you the right Mongo client:fromsmartninja_mongo.connectionimportMongoClientclient=MongoClient('mongodb://ds012345.mlab.com:56789/')If your Python program runs on Heroku (for example), you'll get back aMongoClientfrom thepymongolibrary which has a connection to the production MongoDB database (in this case the mLab's Mongo database).But if your app runs on localhost, a TinyDB instance will be created (you'll see alocalhost.dbfolder created, put it in.gitignore).Even if TinyDB is used on localhost instead of the MongoDB, you can write your code in the MongoDB way (orpymongoway).See the following examples:db=client.my_databasecollection=db.usersuser_id=collection.insert_one({"first_name":"Matej","last_name":"Ramuta","year_born":1987,"created":datetime.datetime.now()}).inserted_iduser_info=collection.find_one({"_id":user_id})print(user_info)More usage examplesFor more usage examples see PyMongo docs:https://api.mongodb.com/python/current/.BewareThere might be some incompatibilities between TinyMongo and PyMongo. Make sure to also check theTinyMongo docsto identify the problem if weird behavior occurs.ODMMongoDB accepts data as dictionaries and also returns data as dicts. If you'd like to use model classes in your project, you can use a base model from the SmartNinja Mongo library:fromsmartninja_mongo.odmimportModelclassUser(Model):def__init__(self,first_name,**kwargs):self.first_name=first_namesuper().__init__(**kwargs)Make sure to add**kwargsas a parameter and callsuper()at the end of the__init__method.Alternatively you can skip__init__completelyclassUser(Model):passThe benefit of using SmartNinja Mongo ODMModelThe main benefit is that you get a method called:convert_dict_to_object():user_info=collection.find_one({"_id":user_id})user_obj=User.convert_dict_to_object(data_dict=user_info)print(user_obj.first_name)ThisModelclass provides a very lightweight and simpleobject-document mapping(ODM).ContributionsContributions via pull requests are warmly welcome!TODOtestsCI