Package: fastapi-crud-code-generator
Fastapi CRUD Template GeneratorIntroducing FastAPI CRUD Template GeneratorFastAPI CRUD Template Generator is a tool that helps developers quickly scaffold FastAPI projects with CRUD operations for their SQLAlchemy databases. It can provide a starting point for your project and can save time and effort by providing pre-written code of the CRUD operations and validation model, and a pre-defined project structureIt. The generated code can be customized to add business logic and extend the functionality of the FastAPI project. The tool supports the following APIs:Get oneGet manyInsert oneInsert manyUpdate oneUpdate manyPatch onePatch manyDelete oneDelete manyFastAPI CRUD Generator is the tool for building efficient and scalable APIs with FastAPI and SQLAlchemy. Try it now and start building your next great project.DEMOFeaturesCRUD router code generation from SQLAlchemy declarative class definitionsAPI validation model code generationSupport for SQLAlchemy 1.4Pagination support for APIsFlexible API request handlingQuick Start with in-memory DB (or see the other (example)InstallTo get started with FastAPI CRUD Template Generator, you will need to install the package using pip:pipinstallfastapi-crud-code-generatorPrepare your Sqlalchemy schemaNext, you will need to prepare your SQLAlchemy schema by defining your database tables as declarative classes. You can use thesqlacodegentool to automatically generate these classes from your database.from sqlalchemy import *
from sqlalchemy.orm import declarative_base

Base = declarative_base()
metadata = Base.metadata

class SampleTable(Base):
    __tablename__ = 'test_build_myself_memory'
    __table_args__ = (
        UniqueConstraint('primary_key', 'int4_value', 'float4_value'),
    )
    primary_key = Column(Integer, primary_key=True, autoincrement=True)
    bool_value = Column(Boolean, nullable=False, default=False)
    bytea_value = Column(LargeBinary)
    char_value = Column(CHAR(10, collation='NOCASE'))
    date_value = Column(Date)
    float4_value = Column(Float, nullable=False)
    float8_value = Column(Float(53), nullable=False, default=10.10)
    int2_value = Column(SmallInteger, nullable=False)
    int4_value = Column(Integer, nullable=False)
    int8_value = Column(BigInteger, default=99)
    text_value = Column(Text)
    varchar_value = Column(String)

class SampleTableTwo(Base):
    __tablename__ = 'test_build_myself_memory_two'
    primary_key = Column(Integer, primary_key=True, autoincrement=True)
    bool_value = Column(Boolean, nullable=False, default=False)
    bytea_value = Column(LargeBinary)Generate templateOnce you have your SQLAlchemy schema defined, you can use thecrud_router_buildermethod to generate CRUD router code for your API. This method takes the declarative class definitions as input and generates code that defines the endpoints and validation models for each CRUD operation.Here is an example of how to use thecrud_router_buildermethod to generate CRUD router code:fromfastapi_quickcrud_codegen.db_modelimportDbModelfromfastapi_quickcrud_codegen.misc.typeimportCrudMethodsfromfastapi_quickcrud_codegenimportcrud_router_buildermodel_list=[DbModel(db_model=SampleTable,prefix="/my_first_api",tags=["sample api"],exclude_columns=['bytea_value']),DbModel(db_model=SampleTableTwo,prefix="/my_second_api",tags=["sample api"],exclude_columns=['bytea_value'],crud_methods=[CrudMethods.FIND_ONE])]crud_router_builder(db_model_list=model_list,# is_async=True,# database_url="sqlite+aiosqlite://",is_async=False,database_url="sqlite://")crud_router_builder argsdb_model_list[Required[List[DbModel]]]Model list of dict for code generateList[]DbModeldb_model[Required[DeclarativeMeta]]prefix[Required[str]]prefix for Fastapi's end pointtags[Required[List[str]]]list of tag for Fastapi's end pointexclude_columns[Optional[List[str]]]set the columns that not to be operated but the columns should nullable or set the default value)crud_methods[Opional[List[CrudMethods]]]Create the following apis for that model, but default: [CrudMethods.FIND_MANY, CrudMethods.FIND_ONE, CrudMethods.CREATE_MANY, CrudMethods.PATCH_ONE, CrudMethods.PATCH_MANY, CrudMethods.PATCH_ONE, CrudMethods.UPDATE_MANY, CrudMethods.UPDATE_ONE, CrudMethods.DELETE_MANY, CrudMethods.DELETE_ONE]- CrudMethods.FIND_ONE
- CrudMethods.FIND_MANY
- CrudMethods.UPDATE_ONE
- CrudMethods.UPDATE_MANY
- CrudMethods.PATCH_ONE
- CrudMethods.PATCH_MANY
- CrudMethods.CREATE_ONE
- CrudMethods.CREATE_MANY
- CrudMethods.DELETE_ONE
- CrudMethods.DELETE_MANYis_async[Required]True for async; False for syncdatabase_url[Optional (str)]A database URL. The URL is passed directly to SQLAlchemy's create_engine() method so please refer to SQLAlchemy's documentation for instructions on how to construct a proper URL.Known limitations❌ Please use composite unique constraints instead of multiple unique constraints❌ Composite primary key is not supported❌ Sqlalchemy table type model schema is not supportedDesign:The model generation part and api router part refer to my anotherproject; The code generation part is using JinjaHow to contribute more apis?It will be super excited if you have any idea with api router/ model template. then you can follow the step as below to try to contribute.Model generationPrepare Jinja template tofastapi-crud-project-generator/src/fastapi_quickcrud_codegen/model/templatePrepare model code generation method fromfastapi_quickcrud_codegen.utils.schema_builder.ApiParameterSchemaBuilderGenerate the code fromfastapi_quickcrud_codegen/model/model_builder.pyCRUD Method GenerationAfter the model generation, you can try to build your own api by your own modelModifyfastapi_quickcrud_codegen.utils.sqlalchemy_to_pydantic.sqlalchemy_to_pydantic,fastapi_quickcrud_codegen.misc.type.CrudMethodsandfastapi_quickcrud_codegen.misc.crud_model.CRUDModelto let project supports your apiPrepare your api router Jinja template fromsrc/fastapi_quickcrud_codegen/model/template/routeGenerate the code fromfastapi_quickcrud_codegen/model/crud_builder.pyUpdate the api_register fromsrc/fastapi_quickcrud_codegen/crud_generator.pyuse Sqlalchemy's sql.expression instead custom statement when building sql statement for your apiRoad mapCurrent State: StableWill do:Support foreign tree in find apiSupport foreign tree in insert apiSupport foreign tree in update apiSupport foreign tree in delete apiGood to have:Support Sqlalchemy Table type model