Package: ruson
Ruson-ODMRuson-ODM is an asynchronous object-document mapper for Python 3.11+. It is designed to be a lightweight and easy-to-use ODM for MongoDB.InstallationRuson-ODM is available to be installed from PyPi with:pipinstallrusonUsageUsing Ruson-ODM is easy and requires only two setup steps: setup and inheritance. After these two steps are complete, you can start using Ruson-ODM to query, insert, update and deleted documents from the database.Inheriting RusonDocRusonDocis the interface that your classes will use to interact with the ODM. To use it, you must create a class that inherits from it.importasynciofromruson.core.configimportConfigfromruson.core.instanceimportRusonfromruson.core.ruson_docimportRusonDocclassUser(RusonDoc):email:strNote that subclasses can override super class attributes like the "id" field.SetupTo use the ruson ODM, you need to first create aConfiginstance with the necessary parameters for your database connection (connection_nameis optional). With the config object created, you can then setup aRusonconnection using the singletonRuson.asyncdefsetup_ruson():config=Config(database_uri="mongodb://localhost:27017",database_name="test",connection_name="default")awaitRuson.create_connection(config)Querying the databaseOnce theRusonconnection is setup, you can start querying the database. Your classes that inherited from RusonDoc can now use thefind,find_oneandfind_manymethods to query the database.By default,Rusonwill not format your data. You can use theformatterparameter to pass a callable to format the response from the database.asyncdefformatter(value:dict):# This function does not need to be async, it is just an# example to show that async functions are also supportedreturnvalue["email"]asyncdeffind():email="test@example.com"# Return iterator over matched usersusers=awaitUser(email=email).find(many=True)# Consumes iterator and transform into a listusers_list=awaitusers.tolist()print("Find method")print(f"Users found with email: '{email}'",len(users_list))print("Users",list(users_list[0].items()))print()asyncdeffind_one():user_email=awaitUser.find_one({"email":"test@example.com"},formatter=formatter)print("Find one method")print("User email",user_email)print()asyncdeffind_many():users_emails=awaitUser.find_many(formatter=formatter)print("Find many method")# Iteration asynchronously through the resultsasyncforemailinusers_emails:print(email)print()Inserting into the databaseTo insert a document into the database, you can use theinsert_onemethod either with an instance or a class.asyncdefinsert_one():print("Insert one method")# Insert using class methoduser=User(email="test@example.com")result=awaitUser.insert_one(user)print("Inserted id",result.inserted_id)# Insert using instance methodanother=User(email="another@example.com")result=awaitanother.insert()print("Inserted id",result.inserted_id)print()To insert multiple documents into the database, you can use theinsert_manymethod either with a class.asyncdefinsert_many():users=[User(email="test1@example.com"),User(email="test2@example.com"),User(email="test3@example.com"),User(email="test4@example.com"),]result=awaitUser.insert_many(users)print("Insert many method")print("Inserted ids",list(map(str,result.inserted_ids)))print()Updating records in the databaseTo update a record in the database, you can use theupdate_onemethod. This method takes two parameters, the filter and the update. The filter is used to find the document to update and the update is the data to update the document with. It can be used either with the class method or the instance method.asyncdefupdate_one():result=awaitUser.update_one({"$set":{"email":"updated@example.com"}},{"email":"test1@example.com"})print("Update one method")print("Matched count",result.matched_count)print("Modified count",result.modified_count)print()Deleting records from the databaseTo delete a record from the database, you can use thedelete_onemethod. This method takes a filter as a parameter and can be used either with the class method or the instance method.asyncdefdelete():# Delete only the first match using "many=False"result=awaitUser(email="test4@example.com").delete(many=False)print("Delete method")print("Deleted count",result.deleted_count)print()asyncdefdelete_one():# Delete class methodresult=awaitUser.delete_one({"email":"test3@example.com"})print("Delete one method")print("Deleted count",result.deleted_count)print()It is also possible to delete multiple records from the database using thedelete_manymethod. This method takes a filter as a parameter and can be used either with the class method or the instance methoddeletecombined with the flagmany=True.asyncdefdelete_many():# Delete many using class methodresult=awaitUser.delete_many({"email":{"$regex":"^test"}})print("Delete many method")print("Deleted count",result.deleted_count)print()To run the examplesasyncdefmain():awaitsetup_ruson()awaitinsert_one()awaitinsert_many()awaitfind()awaitfind_one()awaitfind_many()awaitupdate_one()awaitdelete()awaitdelete_one()awaitdelete_many()if__name__=="__main__":asyncio.run(main())Other supported methodsRusonget_clientget_configcreate_sessionClientdatabasedefault_databaselist_databasescreate_sessionshutdownDatabasecollectionlist_collectionsdropCollectionupsertcount_documentsdistinctcreate_indexlist_indexesdrop_indexesdrop