Package: nosqlmodel
NoSQLModelDefinationnosqlmodel is a first non-relational NoSql ORM framework. Easy way to create models with a nosql backend.Our Motto is simple :Speed, Speed Speed!So there is unneccesarry relations, object convertions, heavy queries are out of our scope!Currently Redis and Dynamodb supported.https://gitlab.com/mozgurbayhan/nosqlmodel/blob/master/CHANGELOGHelp UsWe need contrbutors to add MemCache and MongoDB backends!Feel free to send us pull requests about them or any contributions about our N-ORM.We will grow together.PS: Please vote up our project inhttps://github.com/vinta/awesome-python/pull/1448if you like (:TutorialDeclaration and Configurationnosqlmodel declaration is simple class declaration.Dont forget to extendBaseNosqlModelandMetadeclarationWARNING:All declared fields must return false in if check. Possible values:False,0,None,"",[],{}orsave_to_cache(compress=False)must take compress=False. Otherwise it will cause a problem in stripping False values in compressed save.fromnosqlmodel.basenosqlimportBaseNoSqlModelfromnosqlmodel.connectorsimportdynamoconnectorfromnosqlmodel.connectorsimportredisconnectorfromnosqlmodel.connectors.redisconnectorimportRedisConnector# Configurationredisconnector.RHOST=REDIS_HOSTredisconnector.RPORT=REDIS_PORTdynamoconnector.D_AWS_ACCESS_KEY_ID=DYNAMO_AWS_ACCESS_KEY_IDdynamoconnector.D_AWS_SECRET_ACCESS_KEY=DYNAMO_AWS_SECRET_ACCESS_KEYdynamoconnector.D_REGION_NAME=DYNAMO_REGION_NAME# DeclarationclassTestCar(BaseNoSqlModel):def__init__(self):super().__init__()self.plate_number=""self.top_speed=0self.z_to_h=0classMeta:# connector = DynamoConnector("Test") # Or you can us a redis connector too:connector=RedisConnector(0)# 0 is dbnum# or you can set table source to a different redis db# connector = RedisConnector(0,"192.168.1.1",6666)# think that as a primary key, index key is one of the keys which declared in classindexkey="plate_number"DB Operationstc=TestCar()tc.delete_table()# You can delete db--dynamotc.create_table()# You can create db--dynamotc.flush()# Flushes related dbdbsize=tc.dbsize()# Returns item count in dbNote:you can override create_table, delete_table in class declarationCRUDYou can simply make CRUD operations in objects. There is no seperate insert or update operation. All isupsertexecuted bysave_to_cache# Createtc=TestCar()# There is no copy of object in db yettc.plate_number="35 PLATE 35"tc.save_to_cache()# now saved in db# Readtc=TestCar()tc.get_by_id("666 PLATE 666")# Satan brings us the car with the idkey= 666 PLATE 666# In below satan brings us the car with the idkey= 666 PLATE 666 if not exists, creates a new one in the memory# If it cant find in hell creates a new one in memory ( :  Still doesnt exists in db!!!tc.get_or_create_by_id("666 PLATE 666")# Updatetc.top_speed=666tc.save_to_cache()# Put it in the db again!# Deletetc.delete()# i dont want it anymore!# Populate#You can populate your cars from another dict:satans_car=TestCar()stans_car.get_by_id("666 PLATE 666")angel_car=TestCar()angel_car.from_dict(satans_car.to_dict())# we clone stan's car and all its attributesangel_car.plate_number="7 PLATE 7"# we turn it into new carangel_car.save_to_cache()# And save it to db as a new car!Bulk Operationstc=TestCar()keys=tc.get_keys()# returns all keys in db# Assume we have 3 test carsupdict={"1 TEST 35":tcl35.to_dict(),"1 TEST 36":tcl36.to_dict(),"1 TEST 37":tcl37.to_dict()}TestCar.save_multi(updict)# bulk insertdict_of_dicts=tc.get_multi(["666 PLATE 666","666 PLATE 667"])# returns selected cars in db as as dictionary dictcar_list=tc.get_all_as_objectlist()# returns all cars in db # Be careful memory usage could be a problem in big sizes !list_of_dicts=tc.get_all_as_list()# returns all cars in db  as dictionary list, More speeder, more effective memory usage!car_dict=tc.get_all_as_objectdict()# returns all cars in db as dict instead of list # Be careful memory usage could be a problem in big sizesdict_of_dicts=tc.get_all_as_dict()# returns all cars in db  as dictionary of dictionaries, More speeder, more effective memory usage!TaggingTagging is supported even your backend doesnt supports it too!!But beware while using tags. All is in your responsibility.For example you have to remove an object from tag if you removed object because tags and objects are not directly releated.# returns all tag names in dbtaglist=TestCar.get_tags()# returns all items's keys tagged by satanitemkeylist=TestCar.get_tag_keys("Satan")# returns all items tagged by Satanitemlist=TestCar.get_by_tag("Satan")tc=TestCar()tc.get_by_id("666 PLATE 666")# adds "Satan","Super car", "Red" tags to itemtc.add_tags_to_item(["Satan","Super car","Red"])# we added "Super car" by mistake and now will remove it from objecttc.remove_item_from_tag("Super car")# oh we forget to add tag "Favorite" now adding it to the item while savetc.save_to_cache(["Favorite"])Backup And RestoreThere is also backup tools to keep things safe. In the below scenario we want to backup all the table,
and restore back again.# ################ EXPORTexportlist=TestCar.get_all_as_list()# exports the exportlist into compressed, None fields cleaned, new line markers removes jsontextexport_text=TestCar.export_to_json_text(exportdict,compress_data=True)# or we can export it into a json fileTestCar.export_to_json_file("export.json",exportlist)# or we can export it into a zip fileTestCar.export_to_json_zip("export.zip",exportlist)# ################ IMPORTTestCar.import_from_json_zip("export.zip")# orTestCar.import_from_json_file("export.json")# orTestCar.import_from_json_text("export_text")