Package: rick-db
rick_db - Simple SQL database layerrick_db is a simple SQL database layer for Python3. It includes connection management, Object Mapper, Query Builder,
and a Repository pattern implementation. It isnotan ORM, and it's not meant to replace one.FeaturesObject Mapper;Fluent Sql Query builder;High level connectors for PostgreSQL, SqlLite3;Pluggable SQL query profiler;Simple migration manager for SQL files;Usage scenariosrick_db was built to cater to a schema-first approach: Database schema is built and managed directly with SQL DDL commands,
and the application layer has no responsibility on the structure of the database.Installation$ pip3 install rick-dbDocumentationProject documentation can be found on theDocumentationwebsite.TL;DR; exampleShowcasing the Connection, DTO, Repository and Query Builder objects:fromrick_dbimportfieldmapper,Repositoryfromrick_db.conn.pgimportPgConnectionfromrick_db.sqlimportSelect,Literal@fieldmapper(tablename="publisher",pk="id_publisher")classPublisher:id="id_publisher"name="name"@fieldmapper(tablename="book",pk="id_book")classBook:id="id_book"title="title"total_pages="total_pages"rating="rating"isbn="isbn"published="published_date"fk_publisher="fk_publisher"@fieldmapper(tablename="author",pk="id_author")classAuthor:id="id_author"first_name="first_name"middle_name="middle_name"last_name="last_name"@fieldmapper(tablename="book_author",pk="id_book_author")classBookAuthor:id="id_book_author"fk_book="fk_book"fk_author="fk_author"classAuthorRepository(Repository):def__init__(self,db):super().__init__(db,Author)defcalc_avg_rating(self,id_author:int):"""Calculate average rating for a given author:param id_author: author id:return: average rating, if any"""# generated query:# SELECT avg(rating) AS "rating" FROM "book" INNER JOIN "book_author" ON# "book"."id_book"="book_author"."fk_book" WHERE ("fk_author" = %s)qry=(Select(self._dialect).from_(Book,{Literal("avg({})".format(Book.rating)):"rating"}).join(BookAuthor,BookAuthor.fk_book,Book,Book.id).where(BookAuthor.fk_author,"=",id_author))# retrieve result as list of type Book (to get the rating field)rset=self.fetch(qry,cls=Book)iflen(rset)>0:returnrset.pop(0).ratingreturn0defbooks(self,id_author:int)->list[Book]:"""Retrieve all books for the given author:return: list[Book]"""qry=(Select(self._dialect).from_(Book).join(BookAuthor,BookAuthor.fk_book,Book,Book.id).where(BookAuthor.fk_author,"=",id_author))returnself.fetch(qry,cls=Book)defdump_author_rating(repo:AuthorRepository):forauthorinrepo.fetch_all():# calculate averagerating=repo.calc_avg_rating(author.id)# print book listprint("Books by{firstname}{lastname}:".format(firstname=author.first_name,lastname=author.last_name))forbookinrepo.books(author.id):print(book.title)# print average ratingprint("Average rating for{firstname}{lastname}is{rating}".format(firstname=author.first_name,lastname=author.last_name,rating=rating))if__name__=="__main__":db_cfg={"dbname":"rickdb-bookstore","user":"rickdb_user","password":"rickdb_pass","host":"localhost","port":5432,"sslmode":"require",}conn=PgConnection(**db_cfg)repo=AuthorRepository(conn)dump_author_rating(repo)Running testsTo run the tests, you should have both tox and tox-docker, as well as a local docker daemon. Make sure the current user has
access to the docker daemon.$pip3install-rrequirements-dev.txt$tox