Package: danio
DanioDanio is a ORM for python asyncio world.It is designed to make getting easy and clearly.It builds on python's dataclass and encode'sdatabasesFeatureskeep OOM in mind, custom your Field and Model behavior easilytype hints any where, no more need to memorize words your field names any morebase CRUD operation, transactions, lock and so onsignals like before save, after save and so oncomplex operation like bulk create, upsert, create or update and so onassist model schema migrationsupport MySQL/PostgreSQL/SQLitehints generationinstallpip install danioDocumentsDanio DocumentGlancedb=danio.Database("mysql://root:letmein@server:3306/test",maxsize=3,charset="utf8mb4",use_unicode=True,connect_timeout=60,)@dataclasses.dataclassclassUser(danio.Model):# auto generated by danio:# --------------------Danio Hints--------------------# TABLE NAME: user# TABLE IS MIGRATED!ID:typing.ClassVar[danio.Field]# "id" serial PRIMARY KEY NOT NULLNAME:typing.ClassVar[danio.Field]# "name" varchar(255)  NOT NULLAGE:typing.ClassVar[danio.Field]# "age" int  NOT NULLCREATED_AT:typing.ClassVar[danio.Field]# "created_at" timestamp without time zone  NOT NULLUPDATED_AT:typing.ClassVar[danio.Field]# "updated_at" timestamp without time zone  NOT NULLGENDER:typing.ClassVar[danio.Field]# "gender" int  NOT NULL# --------------------Danio Hints--------------------classGender(enum.Enum):MALE=0FEMALE=1OTHER=2id:typing.Annotated[int,danio.IntField(primary=True,type="serial")]=0name:typing.Annotated[str,danio.CharField(comment="User name")]=""age:typing.Annotated[int,danio.IntField]=0created_at:typing.Annotated[datetime.datetime,danio.DateTimeField(type="timestamp without time zone",comment="when created"),]=dataclasses.field(default_factory=datetime.datetime.now)updated_at:typing.Annotated[datetime.datetime,danio.DateTimeField(type="timestamp without time zone",comment="when updated"),]=dataclasses.field(default_factory=datetime.datetime.now)gender:typing.Annotated[Gender,danio.IntField(enum=Gender)]=Gender.MALEasyncdefbefore_create(self,validate=True):awaitsuper().before_create(validate=True)asyncdefbefore_update(self,validate=True):self.updated_at=datetime.datetime.now()awaitsuper().before_update(validate=True)asyncdefvalidate(self):awaitsuper().validate()ifnotself.name:raisedanio.ValidateException("Empty name!")@classmethoddefget_database(cls,operation:danio.Operation,table:str,*args,**kwargs)->danio.Database:returndb# base CRUDuser=awaitUser(name="batman").save()user=awaitUser.where(User.NAME=="batman").fetch_one()user.gender=User.Gender.MALEawaituser.save()awaituser.delete()# sql chainawaitUser.where(User.NAME!="").limit(10).fetch_all()# multi where conditionawaitUser.where(User.ID!=1,User.NAME!="").fetch_all()awaitUser.where(User.ID!=1).where(User.NAME!="").fetch_all()awaitUser.where(User.ID<=10,User.ID>=20,is_and=False).fetch_all()# complicated expressionawaitUser.where(User.ID==1).update(age=(User.AGE+1)/(User.AGE/12)-2)awaitUser.where((User.AGE+1)==3).fetch_all()# complicated sql operationawaitUser.where(User.ID==u.id).update(age=User.AGE.case(User.AGE>10,1,default=18).case(User.AGE<=0,10))created,updated=awaitUserProfile.upsert([dict(id=1,name="upsert"),],update_fields=["name"],)# bulk operationawaitUser.bulk_create([User(name=f"user_{i}")foriinrange(10)])awaitUser.bulk_update(awaitUser.fetch_all())awaitUser.bulk_delete(awaitUser.fetch_all())# shortcutuser,created=awaitUser(id=1,name="created?").get_or_create(key_fields=(User.ID,))user,created,updated=awaitUser(id=2,name="updated?").create_or_update(key_fields=(User.ID,))