我正在开发一个用于从外部数据库获取所有所需数据的项目。很快数据库将不再可访问,并将被端点取代。
假设我有这样的结构
project_root/
├── sql/
│ ├── query1.sql
│ ├── query2.sql
│ └── ...
├── main.py
└── ...
其中main有调用不同查询模块的函数。我应该如何命名新结构?像这样吗?
project_root/
├── requests/
│ ├── request1.py
│ ├── request2.py
│ └── ...
├── main.py
└── ...
假设您的项目中有
users
和 items
。
这里我按实体(routes
、repositories
、controllers
等)对项目进行分组,但也可以按域(users
、items
)进行分组,这也很好。
project_root/
├── routes/ # Contains your endpoints, should not contain any business logic.
│ ├── user.py
│ ├── item.py
│ └── ...
├── clients/ # Contains db client, s3 client, any client to interact with other services etc.
│ ├── db_client.py
│ └── ...
├── models/ # Contains db models that should reflect the database structure. Commonly ORM is used e.g. SQLAlchemy. The result of your queries should be converted to the models.
│ ├── user.py
│ ├── item.py
│ └── ...
├── schemas/ # Contains pydantic models: schemas / DTOs
│ ├── user.py
│ ├── item.py
│ └── ...
├── repositories/ # The only place containing database queries. Ideally models should be returned from repository methods.
│ ├── user.py
│ ├── item.py
│ └── ...
├── controllers/ # Contains business logic: take something from client, repository, do something, prepare API answer.
│ ├── user.py
│ ├── item.py
│ └── ...
├── main.py
└── ...
模型和模式之间的差异以及我提到的实体示例,您可以在我之前的答案中找到。