GithubHelp home page GithubHelp logo

fastapi-crud-template's Introduction

CRUD Framework for fastapi

You just design DB and API schema. image

from crud.crud_router import create_crud_router
from crud.mapper import CRUDEntitySchemaMapper
from user import User
from user.dtos import V1UserSchema, V1UserCreateDto, V1UserUpdateDto, V1UserDetailSchema
from user.service import UserService

router = create_crud_router(
    CRUDEntitySchemaMapper(User,
                           read_schema=V1UserDetailSchema,
                           list_schema=V1UserSchema,
                           create_schema=V1UserCreateDto,
                           update_schema=V1UserUpdateDto,
                           delete_api=False,
                           # schema_entity_keys_mapping=[("api_schema_id", "db_entity_id")]
                           ),
    UserService)

User Service

from crud.crud_service import CRUDService
from user.models import User


class UserService(CRUDService[User]):
    @property
    def entity(self) -> User.__class__:
        return User

User Model

from datetime import datetime

from sqlalchemy import Column, Integer, String, Engine, func, DateTime
from sqlalchemy.orm import declarative_base

UserDB = declarative_base()


class User(UserDB):
    __tablename__ = "user"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(50), nullable=True)
    email = Column(String(320), nullable=True)
    password = Column(String(128), nullable=True)
    created_at: datetime = Column(DateTime, default=func.now(), server_default=func.now())
    updated_at: datetime = Column(String(50), default=func.now(), server_default=func.now(), onupdate=func.now())

    def __str__(self):
        return f"User(id={self.id}, name={self.name}, email={self.email}, password={self.password}, created_at={self.created_at}, updated_at={self.updated_at})"

V1 Schema

from typing import Optional

from pydantic import BaseModel

from shared.models import TimestampModel


class V1UserSchema(BaseModel):
    id: int
    name: Optional[str]
    email: Optional[str]


class V1UserDetailSchema(TimestampModel):
    id: int
    name: Optional[str]
    email: Optional[str]


class V1UserCreateDto(BaseModel):
    name: Optional[str]
    email: Optional[str]
    password: Optional[str]


class V1UserUpdateDto(BaseModel):
    name: Optional[str]
    email: Optional[str]
    password: Optional[str]

fastapi-crud-template's People

Contributors

terryjoo avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

basket-labs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.