Package: tinydb-baseClass
TinyDB baseI use TinyDB ... a lot and very often and i seem to be writing the same functions over and over. The idea behind the small project to provide a way for devs to easily create a data modal around tinydb without the need to build a base class.How to installthe easiest way is to install through your dependency managerpip install tinyDbBaseor if usingpipenv install tinyDbBase.How do i use this thingFactoryThis allows you to interact with Tinydb and both the root object and a table in the same object.fromtinydb_baseimportFactorydb=Factory('myData.json','myTable')db.db# equliervent to `TinyDB`.db.tbl# equliervent to `TinyDB.Table`.db.close()# closes the database file.DatabaseBaseBreaking Changethe idea is to import the base class and then derives into your own class. the idea is that you can simply create your data layer by creating a class that will work out what needs and just work.fromtinydb_baseimportDatabaseBaseclassMyTable(DatabaseBase):def__init__(self,file='ds.json',table=__name__,requiredKeys='title:str,myContent:str'):super().__init__(file=file,table=table,requiredKeys=requiredKeys)MyTable().create({'title':'foobar','myContent':'this is a string'})This will enable to accesses the base class, you can add your own custom functions. the will use the namespaces of the file within the project, but you can override this easily at an instance. another cool feature is that required keys this is key that must be present in every row which can also be changed.you can now define the basic data type used for data in a table in therequiredKeysyou can now define types, it will not atempt to convert the value, but it will ensure that the data is a concestant type.methodsMethod NameattrDescriptioncreatedictthis adds a new row to the database.createMultiplelist of dictsthis added multiple rowsreadAllreturns a list objreadByIdidthis gets a row by the idremoveByIdidthis removes a row by idclearthis removes all data from the tableexiststag, valuechecks if a a value with header existsnow_tsreturns a unix time as a floatDatabaseBaseSercurethis works exactly the same way asDatabaseBasebut by providing a salt you can encrypt using Fernet, it is suggested that you define your own saltfromtinydb_base.cryptographyimportDatabaseBaseSercureclassDiary(DatabaseBaseSercure):def__init__(self,file='ds.json',table=__name__,requiredKeys='title,content',salt='salt'):super().__init__(file=file,table=table,requiredKeys=requiredKeys,salt=salt)obj=Diary(salt='thisisasalt')Method NameattrDescriptioncreatedictthis adds a new row to the database.createMultiplelist of dictsthis added multiple rowsreadAllreturns a list objreadByIdidthis gets a row by the idremoveByIdidthis removes a row by idclearthis removes all data from the tableUserSomething that alot of systems need to is work with user componants, this is a
simple class that enables Users to be created, it is a class that is inherted
fromDatabaseBase, but has speail methods that pertain to Usersfromtinydb_baseimportUserusrTable=User()# will work with a user as a table,>>>usrTable.makeUser('me','mypassword')1>>>usrTable.authUser('me','!mypassword')False>>>usrTable.authUser('me','mypassword')TrueMethod NameattrDescriptionmakeUserusername, passwordcreates a user in the systemtestUseruserId, passwordtests a password angest a user idauthUserusername, passwordtest a username and passwordGetSetthis is a very simple interface that sets and gets values based on a tag. this can be used for things like settings. if the tag dose did not exist the class will raisetinydb_base.exceptions.RowNotFound_Exceptionfromtinydb_base.getSetimportGetSetclassSettings(GetSet):def__init__(self,file:str='ds.json',table:str=__name__):super().__init__()Settings().set('foo','bar')Settings().get('foo')defualting keysif you need to have keys in your data file, you can make sure that they exist.fromtinydb_base.getSetimportGetSetclassSettings(GetSet):def__init__(self,salt,pw,file='ds.json',table=__name__):super().__init__(salt,pw,file=file,table=table)self.defaultRows({'foo':'bar'})NEWTime Out SettingThis new feature of get set allows a user to set values to timeout and be removed from the data base, this is manged though a argument that is passed though the set method.fromtinydb_base.getSetimportGetSet,futureTimeStampclassSettings(GetSet):def__init__(self,file:str='ds.json',table:str=__name__):super().__init__()obj=Settings().set('api-key','thisisaapikey, futureTimeStamp(hour=1))The above code will add a row to the databse that will that only be in the db for one hour.checking is done on a per meathod call basis.GetSetSercureWorks the same asGetSetbut both the tags and the values encrypted.fromtinydb_base.getSetSercureimportGetSetSercureclassPasswordCode(GetSetSercure):def__init__(self,salt,pw,file='ds.json',table=__name__):super().__init__(salt,pw,file=file,table=table)obj=PasswordCode('salty','notasercurepassword').set('foo','bar')exporting table to Filethese functions export a list of documents into JSON or Ymal, the functions take a list ofdocumentsand the export path. this will create a file that has all the data and a human-readable time date stamp.fromtinydb_base.exporterimportjsonExportfromtinydb_base.exporterimportymalExportfromtinydb_baseimportDatabaseBasea=DatabaseBase()forindexinrange(0,10):a.create({'title':index})b=jsonExport(a.readAll(),'jsonData.json')b=ymalExport(a.readAll(),'ymalData.ymal')