Package: lightorm
LightORM 💡🧺🗄️It is what the name means. Light yet Do Stuffs (Almost) .
Yet another,superlightweightMySQLORM (Object-Relational Mapping)in Python.💾 How to Installpip install lightormIf you find this project interesting, you can always star it. :D📷 A Background for LightORMTheDall-Eprompt waslight yagami from death note is writing  code as darth vader yakuza style🔌 Connecting and QueryingLightORMis designed to reduce the overhead of doing everything and focus on
doing  things in a very light way. First connect with the database,importosfromdotenvimportload_dotenvload_dotenv()fromlightormimportDatabase,Field,_ANDtable_name='person_table'db=Database(db_name=os.environ.get('DB_NAME'),user=os.environ.get('DB_USER'),password=os.environ.get('DB_PASSWORD'),host=os.environ.get('DB_HOST'),port=int(os.environ.get('DB_PORT')))Here, thedatabaseconfigs are read from.envfile. And the db is instantiated.💉🗄️ Insert a RecordInserting a record is quiet simple.person={'name':'John Doe','age':23,'address':'LA','hobby':'writing'}row_id=db.table(table_name).insert(**person)If successful, theinsert(**person)will return therow-id🗃️🗄️ Insert Multiple RecordsInserting multiple record is simply feeding theinsert_manymethod with the
list of the dictionary of the records to be inserted.persons=[{'name':'John Doe','age':23,'address':'LA','hobby':'writing'},{'name':'Jane Doe','age':27,'address':'Kentucky','hobby':'sleeping'}]record_inserted=db.table(table_name).insert_many(rows=persons)Upon successful insertion,insert_manyreturns the number of rows inserted.🗄️🧩 Getting Data Back(SELECT)For getting all data back, simplyusers=db.table(table_name).select().execute()or simply with emptywhereclause [ not suggested, but it will work]users=db.table(table_name).select().where().execute()Note, there is an extra methodexecute, required for the operation.🥣🗄️ Filteringlightormis tested with several filtering, and it is simply chaining
filtering clauses. Let's see🥣🗄️ Filtering users byageandhobbyfromlightormimportField,_AND......users=db.table(table_name).select().where([Field('age').eq(33),_AND,Field('hobby').eq('programming')]).execute()🥣🗄️ Filteringuserswhereageis less than 33users=db.table(table_name).select().where([Field('age').lt(35)]).execute()print('users:',users)🥣🗄️ Filtering users whereadressis in `['Dhaka','Khulna']users=db.table(table_name).select().where([Field('address').find_in(['Dhaka','Khulna'])]).execute()print('users:',users)🚥🗄️ Updating the Recordsupdate()method receiverskey-valdict for fields to be changed. Simply,v_set={'age':65,'hobby':'sleeping'}user_count=db.table(table_name).update(**v_set).where([Field('address').eq('Dhaka')]).execute()print('Affected Row:',user_count)v_setis thedictthat is the followed bySETvalue insqlquery.
After successful query, it returns rows affected.✏️🗄️ Deleting Recordsdelete()works just like theselect()method. It returnsbooleanTrueif
is the query is successfully executed.delete_flag=self.db.table(self.table_name).delete().where([Field('hobby').eq('sleeping')]).execute()print('Delete-Flag:',delete_flag)📜 Almost Full Exampleimportosimportrandomimportunittestfromdotenvimportload_dotenvfromlightormimportDatabase,Field,_ANDload_dotenv()classTestTinyOrm(unittest.TestCase):table_name=os.environ.get('TABLE_NAME')db=Database(db_name=os.environ.get('DB_NAME'),user=os.environ.get('DB_USER'),password=os.environ.get('DB_PASSWORD'),host=os.environ.get('DB_HOST'),port=int(os.environ.get('DB_PORT')))first_name=['John','Jane','Jason','Guido','Martin','Rob']last_name=['Doe','Dee','Mraz','Van Russom','Fowler','Pike']addresses=['Dhaka','LA','Kentucky','Madrid','Khulna','Sylhet']hobbies=['singing','art',' gaming','programming','writing','sleeping']defget_name(self):name='{}{}'.format(random.choice(self.first_name),random.choice(self.last_name))returnnamedefget_age(self):returnrandom.choice([iforiinrange(25,60)])defget_address(self):returnrandom.choice(self.addresses)defget_hobby(self):returnrandom.choice(self.hobbies)deftest_insert(self):person={'name':self.get_name(),'age':self.get_age(),'address':self.get_address(),'hobby':self.get_hobby()}row_id=self.db.table(self.table_name).insert(**person)print('row-id:',row_id)deftest_insert_many(self):persons=[]foriinrange(1,50):person={'name':self.get_name(),'age':self.get_age(),'address':self.get_address(),'hobby':self.get_hobby()}persons.append(person)count=self.db.table(self.table_name).insert_many(rows=persons)print('recored created:',count)deftest_get_users(self):users=self.db.table(self.table_name).select().where().execute()print('users:',users)deftest_get_user_by_age_and_hobby(self):users=self.db.table(self.table_name).select().where([Field('age').eq(33),_AND,Field('hobby').eq('art')]).execute()print('users:',users)deftest_get_users_where_age_lt_33(self):users=self.db.table(self.table_name).select().where([Field('age').lt(35)]).execute()print('users:',users)deftest_get_users_where_age_is_in_list_33(self):users=self.db.table(self.table_name).select().where([Field('age').find_in([33])]).execute()print('users:',users)deftest_get_user_where_address_is_in_dhaka_or_sylhet(self):users=self.db.table(self.table_name).select().where([Field('address').find_in(['Dhaka','Khulna'])]).execute()print('users:',users)deftest_update_users_age_to_50_if_address_is_dhaka(self):v_set={'age':65,'hobby':'sleeping'}user_count=self.db.table(self.table_name).update(**v_set).where([Field('address').eq('Dhaka')]).execute()print('Affected Row:',user_count)deftest_delete_users_where_hobby_eq_art(self):delete_flag=self.db.table(self.table_name).delete().where([Field('hobby').eq('sleeping')]).execute()print('Delete-Flag:',delete_flag)deftest_find_not_in(self):users=self.db.table(self.table_name).select().where([Field('age').find_not_in([49,39,28])]).execute()print('users:',len(users))deftest_paginated_query(self):size=10users=self.db.table(self.table_name).select().where([Field('age').eq(65)]).paginate(0,size).execute()print('len(users):',len(users))print('users:',users)if__name__=='__main__':unittest.main()🔮 Upcoming FeaturesRawSQLexecution.Adding Pagination and SortingAdding proper Logging and debugging messages.AddingAggregate Functionfunction in the ORM.🧚 InspirationPeewee,SQLalchemydjangoORMand all the other ORMs out there, makingDeveloperslife easier.Change logs0.0.6Addingfind_not_in()method.0.0.7Addingpaginate()method.