NoSQLModel
Defination
nosqlmodel 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/CHANGELOG
Help Us
We 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 in https://github.com/vinta/awesome-python/pull/1448 if you like (:
Tutorial
Declaration and Configuration
nosqlmodel declaration is simple class declaration.
Dont forget to extend BaseNosqlModel and Meta declaration
WARNING: All declared fields must return false in if check. Possible values: False , 0 , None , "" , [], {} or save_to_cache(compress=False)
must take compress=False. Otherwise it will cause a problem in stripping False values in compressed save.
from nosqlmodel.basenosql import BaseNoSqlModel
from nosqlmodel.connectors import dynamoconnector
from nosqlmodel.connectors import redisconnector
from nosqlmodel.connectors.redisconnector import RedisConnector
redisconnector.RHOST = REDIS_HOST
redisconnector.RPORT = REDIS_PORT
dynamoconnector.D_AWS_ACCESS_KEY_ID = DYNAMO_AWS_ACCESS_KEY_ID
dynamoconnector.D_AWS_SECRET_ACCESS_KEY = DYNAMO_AWS_SECRET_ACCESS_KEY
dynamoconnector.D_REGION_NAME = DYNAMO_REGION_NAME
class TestCar(BaseNoSqlModel):
def __init__(self):
super().__init__()
self.plate_number = ""
self.top_speed = 0
self.z_to_h = 0
class Meta:
connector = RedisConnector(0)
indexkey = "plate_number"
DB Operations
tc = TestCar()
tc.delete_table()
tc.create_table()
tc.flush()
dbsize = tc.dbsize()
Note: you can override create_table, delete_table in class declaration
CRUD
You can simply make CRUD operations in objects. There is no seperate insert or update operation. All is upsert executed by save_to_cache
tc = TestCar()
tc.plate_number = "35 PLATE 35"
tc.save_to_cache()
tc = TestCar()
tc.get_by_id("666 PLATE 666")
tc.get_or_create_by_id("666 PLATE 666")
tc.top_speed = 666
tc.save_to_cache()
tc.delete()
satans_car = TestCar()
stans_car.get_by_id("666 PLATE 666")
angel_car = TestCar()
angel_car.from_dict(satans_car.to_dict())
angel_car.plate_number = "7 PLATE 7"
angel_car.save_to_cache()
Bulk Operations
tc = TestCar()
keys = tc.get_keys()
updict = {
"1 TEST 35": tcl35.to_dict(),
"1 TEST 36": tcl36.to_dict(),
"1 TEST 37": tcl37.to_dict()
}
TestCar.save_multi(updict)
dict_of_dicts = tc.get_multi(["666 PLATE 666","666 PLATE 667"])
car_list = tc.get_all_as_objectlist()
list_of_dicts = tc.get_all_as_list()
car_dict = tc.get_all_as_objectdict()
dict_of_dicts = tc.get_all_as_dict()
Tagging
Tagging 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.
taglist = TestCar.get_tags()
itemkeylist = TestCar.get_tag_keys("Satan")
itemlist = TestCar.get_by_tag("Satan")
tc = TestCar()
tc.get_by_id("666 PLATE 666")
tc.add_tags_to_item(["Satan","Super car", "Red"])
tc.remove_item_from_tag("Super car")
tc.save_to_cache(["Favorite"])
Backup And Restore
There is also backup tools to keep things safe. In the below scenario we want to backup all the table,
and restore back again.
exportlist = TestCar.get_all_as_list()
export_text = TestCar.export_to_json_text(exportdict, compress_data=True)
TestCar.export_to_json_file("export.json", exportlist)
TestCar.export_to_json_zip("export.zip", exportlist)
TestCar.import_from_json_zip("export.zip")
TestCar.import_from_json_file("export.json")
TestCar.import_from_json_text("export_text")