Package: model-dml
Model DMLExtends your SQLAlchemy models with DML mixins.Model DMLFeaturesHow to grow with model_dmlWhy not use model_dmlHow not to use model_dmlWhat kind of change may be neededGet model_dml for your projectRequirementsInstallationUse model_dml in your projectModify your sqlalchemy BaseCompose your model with what you needUse the DML methodsFeaturesFour methods forDMLoperations:select,insert,update,delete.Add those methods to models through mixin classes.Support returning selected columns for all methods.How to grow with model_dmlWhy not use model_dmlNo typed return values. It is theoretically possible to add annotation that will infer
the return type of the methods such as select(id, name) would return a namedtuple with
id asintand name asstr. If you want to add this feature, you should read the
method code and implement it yourself. Withoutmodel_dmlabstraction, the type
inference works better.How not to use model_dmlThis package is not meant to be used systematically as an installed package. You may
want to copy a specific function that you need in your project instead of installing the
whole package.What kind of change may be neededLet's take theselectmethod. It is implemented to return a sequence of rows. If you
want to order the rows by a column, you need to change the SQL statement construction.Find the source code of select method returning rowsdef_select_result(cls,where,columns,limit):stmt=sqlalchemy.select(*columns).where(*where).limit(limit)withcls.session_maker().begin()assession:returnsession.execute(stmt).all()Copy implementation in your code# Your codestmt=sqlalchemy.select(*columns).where(*where).limit(limit)withcls.session_maker().begin()assession:returnsession.execute(stmt).all()# Your codeUpdate implementation accordingly to your needs! Here session_maker should be the session factory of your project. Please
seesession_basics - using a sessionmaker.# Your codestmt=sqlalchemy.select(User.id,User.name).limit(10).order_by(User.name)withsession_maker().begin()assession:results=session.execute(stmt).all()# Your codeBravo :DGet model_dml for your projectRequirementsPython 3.8+Installationpipinstallmodel_dmlUse model_dml in your projectModify your sqlalchemy BaseTo usemodel_dml, you need to create a custom base class for your models with
asession_makermethod.What is a sessionmaker:Using a sessionmakersessionmaker documentation and exampleclassBase(sqlalchemy.orm.DeclarativeBase):@classmethoddefsession_maker(cls)->sqlalchemy.orm.sessionmaker:return<Namespace>.SessionThis method exist to allow DML methods to access thesessionmakerwithout creating a
new reference
that would also need to be monkey patched. By returning thesessionmakerfrom the
namespace, only<Namespace>.Sessionneeds to be monkey patched.Compose your model with what you needclassUser(base,model_dml.Insert,model_dml.Update,model_dml.Delete):__tablename__="users"id:sqlalchemy.orm.Mapped[int]=sqlalchemy.orm.mapped_column(primary_key=True)name:sqlalchemy.orm.Mapped[str]=sqlalchemy.orm.mapped_column(sqlalchemy.String(30))You can usemodel_dml.DMLwhich is a helper class that composes all the mixins
together.classUser(base,model_dml.DML):__tablename__="users"id:sqlalchemy.orm.Mapped[int]=sqlalchemy.orm.mapped_column(primary_key=True)name:sqlalchemy.orm.Mapped[str]=sqlalchemy.orm.mapped_column(sqlalchemy.String(30))Use the DML methodsUser.insert(dict(name="John"))User.insert({'name':"Jane"})user=User.insert({'name':"Jack"},returning=[User.id,User.name])