Package: psql-to-models
PSQL TO MODELSUse regex patterns to match PostgreSQL schemas and output SQLAlchemy and Pydantic Models.Designed for FastAPI.InstallRequires Python 3.10.gitclonegit@github.com:AlbertoV5/psql-to-models.gitcdpsql-to-modelsInstall in editable mode.pipinstall-e.Usagepython-mpsql-to-models-i./example/schema.sql-a./example/models_alchemy.py-p./example/models_pydantic.pyBefore.schema.sqlAfter.models_alchemy.pyschema.sql
models_pydantic.pyResults ExampleSQL InputDROPTABLEIFEXISTSDATETIMEEVENTSCASCADE;CREATETABLEDATETIMEEVENTS(ROW_IDINTNOTNULL,SUBJECT_IDINTNOTNULL,HADM_IDINT,ICUSTAY_IDINT,ITEMIDINTNOTNULL,CHARTTIMETIMESTAMP(0)NOTNULL,STORETIMETIMESTAMP(0)NOTNULL,CGIDINTNOTNULL,VALUETIMESTAMP(0),VALUEUOMVARCHAR(50)NOTNULL,WARNINGSMALLINT,ERRORSMALLINT,RESULTSTATUSVARCHAR(50),STOPPEDVARCHAR(50),CONSTRAINTdatetime_rowid_pkPRIMARYKEY(ROW_ID));DROPTABLEIFEXISTSDIAGNOSES_ICDCASCADE;CREATETABLEDIAGNOSES_ICD(ROW_IDINTNOTNULL,SUBJECT_IDINTNOTNULL,HADM_IDINTNOTNULL,SEQ_NUMINT,ICD9_CODEVARCHAR(10),CONSTRAINTdiagnosesicd_rowid_pkPRIMARYKEY(ROW_ID));SQLALchemy OutputclassDatetimeevents(Base):__tablename__="datetimeevents"row_id=Column(Integer,nullable=False,primary_key=True)subject_id=Column(Integer,nullable=False)hadm_id=Column(Integer)icustay_id=Column(Integer)itemid=Column(Integer,nullable=False)charttime=Column(TIMESTAMP(0),nullable=False)storetime=Column(TIMESTAMP(0),nullable=False)cgid=Column(Integer,nullable=False)value=Column(TIMESTAMP(0))valueuom=Column(String(50),nullable=False)warning=Column(SmallInteger)error=Column(SmallInteger)resultstatus=Column(String(50))stopped=Column(String(50))classDiagnoses_icd(Base):__tablename__="diagnoses_icd"row_id=Column(Integer,nullable=False,primary_key=True)subject_id=Column(Integer,nullable=False)hadm_id=Column(Integer,nullable=False)seq_num=Column(Integer)icd9_code=Column(String(10))Pydantic OutputclassDatetimeevents(BaseModel):row_id:intsubject_id:inthadm_id:int|Noneicustay_id:int|Noneitemid:intcharttime:datetimestoretime:datetimecgid:intvalue:datetime|Nonevalueuom:strwarning:int|Noneerror:int|Noneresultstatus:str|Nonestopped:str|NoneclassConfig:orm_mode=TrueclassDiagnoses_icd(BaseModel):row_id:intsubject_id:inthadm_id:intseq_num:int|Noneicd9_code:str|NoneclassConfig:orm_mode=TrueSupported QueriesCREATETABLE*NOTNULLCONSTRAINTUNIQUECONSTRAINTPRIMARYKEYConstantsMake sure to edit the header constants under __ main __.pyALCHEMY_HEADER='''"""SQLAlchemy Models"""from sqlalchemy import Column, Integer, String, CHAR, TIMESTAMP, SmallIntegerfrom sqlalchemy.dialects.postgresql import DOUBLE_PRECISIONfrom db.setup import Base'''You can always extend the supported types by editing the TYPE_LOOKUP dict in the types.py file.TYPE_LOOKUP:dict[str,tuple[str,str]]={"INT":("Integer","int"),"SMALLINT":("SmallInteger","int"),"VARCHAR":("String","str"),"TIMESTAMP":("TIMESTAMP","datetime"),"DOUBLE":("DOUBLE_PRECISION","float"),"CHAR":("CHAR","str"),"TEXT":("String","str"),}"""Values are tuples of SQLAlchemy Model Type and Pydantic/Python Type."""NotesThis utility is meant to be modified to match every case that's why the installation is in editable mode.The __ main __ .py file contains all the necessary logic and header configs.The types.py file contains a lookup table for the postgresql -> models type lookup.The header assumes a path for the SQLAlchemy Base so make sure to change it to match yours, etc.PlansA more robust tool can be created which uses .toml files (or whatever) for configuration instead of python files so there is no need for editable installation.The applications are Postgresql schemas with FastAPI but the tool can be generalized even further to support different types for other RDMS and frameworks.I'll add support for more queries as I find them in my day-to-day work but feel free to contribute!Changelog0.1.2 - added ForeignKey Support0.1.1 - added REAL -> Float support0.1.0 - initial release