mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 05:02:49 +00:00
Update code generator plugin naming to CodeGen prefix (#1243)
This commit is contained in:
+8
-8
@@ -597,15 +597,15 @@ async def import_table(
|
||||
raise cappa.Exit('代码生成仅在开发环境可用', code=1)
|
||||
|
||||
try:
|
||||
from backend.plugin.code_generator.schema.gen import ImportParam
|
||||
from backend.plugin.code_generator.service.gen_service import gen_service
|
||||
from backend.plugin.code_generator.schema.code_gen import ImportParam
|
||||
from backend.plugin.code_generator.service.code_gen_service import code_gen_service
|
||||
except ImportError:
|
||||
raise cappa.Exit('代码生成插件用法导入失败,请联系系统管理员', code=1)
|
||||
|
||||
try:
|
||||
obj = ImportParam(app=app, table_schema=table_schema, table_name=table_name)
|
||||
async with async_db_session.begin() as db:
|
||||
await gen_service.import_business_and_model(db=db, obj=obj)
|
||||
await code_gen_service.import_business_and_model(db=db, obj=obj)
|
||||
console.tip('代码生成业务和模型列导入成功')
|
||||
console.log('\n快试试 [bold cyan]fba codegen[/bold cyan] 生成代码吧~')
|
||||
except Exception as e:
|
||||
@@ -618,15 +618,15 @@ async def generate(*, preview: bool = False) -> None:
|
||||
raise cappa.Exit('代码生成仅在开发环境可用', code=1)
|
||||
|
||||
try:
|
||||
from backend.plugin.code_generator.service.business_service import gen_business_service
|
||||
from backend.plugin.code_generator.service.gen_service import gen_service
|
||||
from backend.plugin.code_generator.service.business_service import code_gen_business_service
|
||||
from backend.plugin.code_generator.service.code_gen_service import code_gen_service
|
||||
except ImportError:
|
||||
raise cappa.Exit('代码生成插件用法导入失败,请联系系统管理员', code=1)
|
||||
|
||||
try:
|
||||
ids = []
|
||||
async with async_db_session() as db:
|
||||
results = await gen_business_service.get_all(db=db)
|
||||
results = await code_gen_business_service.get_all(db=db)
|
||||
|
||||
if not results:
|
||||
raise cappa.Exit('[red]暂无可用的代码生成业务!请先通过 import 命令导入![/]')
|
||||
@@ -651,7 +651,7 @@ async def generate(*, preview: bool = False) -> None:
|
||||
|
||||
# 预览
|
||||
async with async_db_session() as db:
|
||||
preview_data = await gen_service.preview(db=db, pk=business)
|
||||
preview_data = await code_gen_service.preview(db=db, pk=business)
|
||||
|
||||
console.print('\n[bold yellow]将要生成以下文件:[/]')
|
||||
file_table = Table(show_header=True, header_style='bold cyan')
|
||||
@@ -675,7 +675,7 @@ async def generate(*, preview: bool = False) -> None:
|
||||
|
||||
if ok.lower() == 'y':
|
||||
async with async_db_session.begin() as db:
|
||||
gen_path = await gen_service.generate(db=db, pk=business)
|
||||
gen_path = await code_gen_service.generate(db=db, pk=business)
|
||||
|
||||
console.print()
|
||||
console.tip('代码已生成完成')
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
CODE_GENERATOR_DOWNLOAD_ZIP_FILENAME = 'fba_generator'
|
||||
```
|
||||
|
||||
在 `backend/core/conf.py` 中添加以下内容:
|
||||
当前项目的 `backend/core/conf.py` 已包含以下字段:
|
||||
|
||||
```python
|
||||
##################################################
|
||||
@@ -28,6 +28,10 @@ CODE_GENERATOR_DOWNLOAD_ZIP_FILENAME = 'fba_generator'
|
||||
CODE_GENERATOR_DOWNLOAD_ZIP_FILENAME: str
|
||||
```
|
||||
|
||||
## 配置项说明
|
||||
|
||||
- `CODE_GENERATOR_DOWNLOAD_ZIP_FILENAME`:控制代码生成结果下载压缩包的文件名
|
||||
|
||||
## 使用方式
|
||||
|
||||
1. 安装并启用插件后,重启后端服务
|
||||
|
||||
@@ -3,10 +3,10 @@ from fastapi import APIRouter
|
||||
from backend.core.conf import settings
|
||||
from backend.plugin.code_generator.api.v1.business import router as business_router
|
||||
from backend.plugin.code_generator.api.v1.column import router as column_router
|
||||
from backend.plugin.code_generator.api.v1.gen import router as gen_router
|
||||
from backend.plugin.code_generator.api.v1.code_gen import router as code_gen_router
|
||||
|
||||
v1 = APIRouter(prefix=f'{settings.FASTAPI_API_V1_PATH}/code-generation', tags=['代码生成'])
|
||||
|
||||
v1.include_router(business_router, prefix='/businesses')
|
||||
v1.include_router(column_router, prefix='/columns')
|
||||
v1.include_router(gen_router, prefix='/generations')
|
||||
v1.include_router(code_gen_router, prefix='/generations')
|
||||
|
||||
@@ -9,20 +9,20 @@ from backend.common.security.permission import RequestPermission
|
||||
from backend.common.security.rbac import DependsRBAC
|
||||
from backend.database.db import CurrentSession, CurrentSessionTransaction
|
||||
from backend.plugin.code_generator.schema.business import (
|
||||
CreateGenBusinessParam,
|
||||
GetGenBusinessDetail,
|
||||
UpdateGenBusinessParam,
|
||||
CreateCodeGenBusinessParam,
|
||||
GetCodeGenBusinessDetail,
|
||||
UpdateCodeGenBusinessParam,
|
||||
)
|
||||
from backend.plugin.code_generator.schema.column import GetGenColumnDetail
|
||||
from backend.plugin.code_generator.service.business_service import gen_business_service
|
||||
from backend.plugin.code_generator.service.column_service import gen_column_service
|
||||
from backend.plugin.code_generator.schema.column import GetCodeGenColumnDetail
|
||||
from backend.plugin.code_generator.service.business_service import code_gen_business_service
|
||||
from backend.plugin.code_generator.service.column_service import code_gen_column_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get('/all', summary='获取所有代码生成业务', dependencies=[DependsJwtAuth])
|
||||
async def get_all_businesses(db: CurrentSession) -> ResponseSchemaModel[list[GetGenBusinessDetail]]:
|
||||
data = await gen_business_service.get_all(db=db)
|
||||
async def get_all_businesses(db: CurrentSession) -> ResponseSchemaModel[list[GetCodeGenBusinessDetail]]:
|
||||
data = await code_gen_business_service.get_all(db=db)
|
||||
return response_base.success(data=data)
|
||||
|
||||
|
||||
@@ -30,8 +30,8 @@ async def get_all_businesses(db: CurrentSession) -> ResponseSchemaModel[list[Get
|
||||
async def get_business(
|
||||
db: CurrentSession,
|
||||
pk: Annotated[int, Path(description='业务 ID')],
|
||||
) -> ResponseSchemaModel[GetGenBusinessDetail]:
|
||||
data = await gen_business_service.get(db=db, pk=pk)
|
||||
) -> ResponseSchemaModel[GetCodeGenBusinessDetail]:
|
||||
data = await code_gen_business_service.get(db=db, pk=pk)
|
||||
return response_base.success(data=data)
|
||||
|
||||
|
||||
@@ -46,8 +46,8 @@ async def get_business(
|
||||
async def get_businesses_paginated(
|
||||
db: CurrentSession,
|
||||
table_name: Annotated[str | None, Query(description='代码生成业务表名称')] = None,
|
||||
) -> ResponseSchemaModel[PageData[GetGenBusinessDetail]]:
|
||||
page_data = await gen_business_service.get_list(db=db, table_name=table_name)
|
||||
) -> ResponseSchemaModel[PageData[GetCodeGenBusinessDetail]]:
|
||||
page_data = await code_gen_business_service.get_list(db=db, table_name=table_name)
|
||||
return response_base.success(data=page_data)
|
||||
|
||||
|
||||
@@ -55,8 +55,8 @@ async def get_businesses_paginated(
|
||||
async def get_business_all_columns(
|
||||
db: CurrentSession,
|
||||
pk: Annotated[int, Path(description='业务 ID')],
|
||||
) -> ResponseSchemaModel[list[GetGenColumnDetail]]:
|
||||
data = await gen_column_service.get_columns(db=db, business_id=pk)
|
||||
) -> ResponseSchemaModel[list[GetCodeGenColumnDetail]]:
|
||||
data = await code_gen_column_service.get_columns(db=db, business_id=pk)
|
||||
return response_base.success(data=data)
|
||||
|
||||
|
||||
@@ -68,8 +68,8 @@ async def get_business_all_columns(
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
async def create_business(db: CurrentSessionTransaction, obj: CreateGenBusinessParam) -> ResponseModel:
|
||||
await gen_business_service.create(db=db, obj=obj)
|
||||
async def create_business(db: CurrentSessionTransaction, obj: CreateCodeGenBusinessParam) -> ResponseModel:
|
||||
await code_gen_business_service.create(db=db, obj=obj)
|
||||
return response_base.success()
|
||||
|
||||
|
||||
@@ -84,9 +84,9 @@ async def create_business(db: CurrentSessionTransaction, obj: CreateGenBusinessP
|
||||
async def update_business(
|
||||
db: CurrentSessionTransaction,
|
||||
pk: Annotated[int, Path(description='业务 ID')],
|
||||
obj: UpdateGenBusinessParam,
|
||||
obj: UpdateCodeGenBusinessParam,
|
||||
) -> ResponseModel:
|
||||
count = await gen_business_service.update(db=db, pk=pk, obj=obj)
|
||||
count = await code_gen_business_service.update(db=db, pk=pk, obj=obj)
|
||||
if count > 0:
|
||||
return response_base.success()
|
||||
return response_base.fail()
|
||||
@@ -103,7 +103,7 @@ async def update_business(
|
||||
async def delete_business(
|
||||
db: CurrentSessionTransaction, pk: Annotated[int, Path(description='业务 ID')]
|
||||
) -> ResponseModel:
|
||||
count = await gen_business_service.delete(db=db, pk=pk)
|
||||
count = await code_gen_business_service.delete(db=db, pk=pk)
|
||||
if count > 0:
|
||||
return response_base.success()
|
||||
return response_base.fail()
|
||||
|
||||
+8
-8
@@ -9,8 +9,8 @@ from backend.common.security.permission import RequestPermission
|
||||
from backend.common.security.rbac import DependsRBAC
|
||||
from backend.core.conf import settings
|
||||
from backend.database.db import CurrentSession, CurrentSessionTransaction
|
||||
from backend.plugin.code_generator.schema.gen import ImportParam
|
||||
from backend.plugin.code_generator.service.gen_service import gen_service
|
||||
from backend.plugin.code_generator.schema.code_gen import ImportParam
|
||||
from backend.plugin.code_generator.service.code_gen_service import code_gen_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -20,7 +20,7 @@ async def get_all_tables(
|
||||
db: CurrentSession,
|
||||
table_schema: Annotated[str, Query(description='数据库名')] = 'fba',
|
||||
) -> ResponseSchemaModel[list[dict[str, str | None]]]:
|
||||
data = await gen_service.get_tables(db=db, table_schema=table_schema)
|
||||
data = await code_gen_service.get_tables(db=db, table_schema=table_schema)
|
||||
return response_base.success(data=data)
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ async def get_all_tables(
|
||||
],
|
||||
)
|
||||
async def import_table(db: CurrentSessionTransaction, obj: ImportParam) -> ResponseModel:
|
||||
await gen_service.import_business_and_model(db=db, obj=obj)
|
||||
await code_gen_service.import_business_and_model(db=db, obj=obj)
|
||||
return response_base.success()
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ async def import_table(db: CurrentSessionTransaction, obj: ImportParam) -> Respo
|
||||
async def preview_code(
|
||||
db: CurrentSession, pk: Annotated[int, Path(description='业务 ID')]
|
||||
) -> ResponseSchemaModel[dict[str, bytes]]:
|
||||
data = await gen_service.preview(db=db, pk=pk)
|
||||
data = await code_gen_service.preview(db=db, pk=pk)
|
||||
return response_base.success(data=data)
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ async def preview_code(
|
||||
async def get_generate_paths(
|
||||
db: CurrentSession, pk: Annotated[int, Path(description='业务 ID')]
|
||||
) -> ResponseSchemaModel[list[str]]:
|
||||
data = await gen_service.get_generate_path(db=db, pk=pk)
|
||||
data = await code_gen_service.get_generate_path(db=db, pk=pk)
|
||||
return response_base.success(data=data)
|
||||
|
||||
|
||||
@@ -63,13 +63,13 @@ async def get_generate_paths(
|
||||
],
|
||||
)
|
||||
async def generate_code(db: CurrentSession, pk: Annotated[int, Path(description='业务 ID')]) -> ResponseModel:
|
||||
await gen_service.generate(db=db, pk=pk)
|
||||
await code_gen_service.generate(db=db, pk=pk)
|
||||
return response_base.success()
|
||||
|
||||
|
||||
@router.get('/{pk}', summary='下载代码', dependencies=[DependsJwtAuth])
|
||||
async def download_code(db: CurrentSession, pk: Annotated[int, Path(description='业务 ID')]): # ruff:ignore[missing-return-type-undocumented-public-function]
|
||||
bio = await gen_service.download(db=db, pk=pk)
|
||||
bio = await code_gen_service.download(db=db, pk=pk)
|
||||
return StreamingResponse(
|
||||
bio,
|
||||
media_type='application/x-zip-compressed',
|
||||
@@ -8,26 +8,26 @@ from backend.common.security.permission import RequestPermission
|
||||
from backend.common.security.rbac import DependsRBAC
|
||||
from backend.database.db import CurrentSession, CurrentSessionTransaction
|
||||
from backend.plugin.code_generator.schema.column import (
|
||||
CreateGenColumnParam,
|
||||
GetGenColumnDetail,
|
||||
UpdateGenColumnParam,
|
||||
CreateCodeGenColumnParam,
|
||||
GetCodeGenColumnDetail,
|
||||
UpdateCodeGenColumnParam,
|
||||
)
|
||||
from backend.plugin.code_generator.service.column_service import gen_column_service
|
||||
from backend.plugin.code_generator.service.column_service import code_gen_column_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get('/types', summary='获取代码生成模型列类型', dependencies=[DependsJwtAuth])
|
||||
async def get_column_types() -> ResponseSchemaModel[list[str]]:
|
||||
column_types = await gen_column_service.get_types()
|
||||
column_types = await code_gen_column_service.get_types()
|
||||
return response_base.success(data=column_types)
|
||||
|
||||
|
||||
@router.get('/{pk}', summary='获取代码生成模型列详情', dependencies=[DependsJwtAuth])
|
||||
async def get_column(
|
||||
db: CurrentSession, pk: Annotated[int, Path(description='模型列 ID')]
|
||||
) -> ResponseSchemaModel[GetGenColumnDetail]:
|
||||
data = await gen_column_service.get(db=db, pk=pk)
|
||||
) -> ResponseSchemaModel[GetCodeGenColumnDetail]:
|
||||
data = await code_gen_column_service.get(db=db, pk=pk)
|
||||
return response_base.success(data=data)
|
||||
|
||||
|
||||
@@ -39,8 +39,8 @@ async def get_column(
|
||||
DependsRBAC,
|
||||
],
|
||||
)
|
||||
async def create_column(db: CurrentSessionTransaction, obj: CreateGenColumnParam) -> ResponseModel:
|
||||
await gen_column_service.create(db=db, obj=obj)
|
||||
async def create_column(db: CurrentSessionTransaction, obj: CreateCodeGenColumnParam) -> ResponseModel:
|
||||
await code_gen_column_service.create(db=db, obj=obj)
|
||||
return response_base.success()
|
||||
|
||||
|
||||
@@ -53,9 +53,9 @@ async def create_column(db: CurrentSessionTransaction, obj: CreateGenColumnParam
|
||||
],
|
||||
)
|
||||
async def update_column(
|
||||
db: CurrentSessionTransaction, pk: Annotated[int, Path(description='模型列 ID')], obj: UpdateGenColumnParam
|
||||
db: CurrentSessionTransaction, pk: Annotated[int, Path(description='模型列 ID')], obj: UpdateCodeGenColumnParam
|
||||
) -> ResponseModel:
|
||||
count = await gen_column_service.update(db=db, pk=pk, obj=obj)
|
||||
count = await code_gen_column_service.update(db=db, pk=pk, obj=obj)
|
||||
if count > 0:
|
||||
return response_base.success()
|
||||
return response_base.fail()
|
||||
@@ -72,7 +72,7 @@ async def update_column(
|
||||
async def delete_column(
|
||||
db: CurrentSessionTransaction, pk: Annotated[int, Path(description='模型列 ID')]
|
||||
) -> ResponseModel:
|
||||
count = await gen_column_service.delete(db=db, pk=pk)
|
||||
count = await code_gen_column_service.delete(db=db, pk=pk)
|
||||
if count > 0:
|
||||
return response_base.success()
|
||||
return response_base.fail()
|
||||
|
||||
@@ -4,15 +4,15 @@ from sqlalchemy import Select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy_crud_plus import CRUDPlus
|
||||
|
||||
from backend.plugin.code_generator.model import GenBusiness
|
||||
from backend.plugin.code_generator.schema.business import CreateGenBusinessParam, UpdateGenBusinessParam
|
||||
from backend.plugin.code_generator.model import CodeGenBusiness
|
||||
from backend.plugin.code_generator.schema.business import CreateCodeGenBusinessParam, UpdateCodeGenBusinessParam
|
||||
from backend.utils.timezone import timezone
|
||||
|
||||
|
||||
class CRUDGenBusiness(CRUDPlus[GenBusiness]):
|
||||
class CRUDCodeGenBusiness(CRUDPlus[CodeGenBusiness]):
|
||||
"""代码生成业务 CRUD 类"""
|
||||
|
||||
async def get(self, db: AsyncSession, pk: int) -> GenBusiness | None:
|
||||
async def get(self, db: AsyncSession, pk: int) -> CodeGenBusiness | None:
|
||||
"""
|
||||
获取代码生成业务
|
||||
|
||||
@@ -22,7 +22,7 @@ class CRUDGenBusiness(CRUDPlus[GenBusiness]):
|
||||
"""
|
||||
return await self.select_model(db, pk, deleted=0)
|
||||
|
||||
async def get_by_name(self, db: AsyncSession, name: str) -> GenBusiness | None:
|
||||
async def get_by_name(self, db: AsyncSession, name: str) -> CodeGenBusiness | None:
|
||||
"""
|
||||
通过 name 获取代码生成业务
|
||||
|
||||
@@ -32,7 +32,7 @@ class CRUDGenBusiness(CRUDPlus[GenBusiness]):
|
||||
"""
|
||||
return await self.select_model_by_column(db, table_name=name, deleted=0)
|
||||
|
||||
async def get_all(self, db: AsyncSession) -> Sequence[GenBusiness]:
|
||||
async def get_all(self, db: AsyncSession) -> Sequence[CodeGenBusiness]:
|
||||
"""
|
||||
获取所有代码生成业务
|
||||
|
||||
@@ -55,7 +55,7 @@ class CRUDGenBusiness(CRUDPlus[GenBusiness]):
|
||||
|
||||
return await self.select_order('id', 'desc', **filters)
|
||||
|
||||
async def create(self, db: AsyncSession, obj: CreateGenBusinessParam) -> None:
|
||||
async def create(self, db: AsyncSession, obj: CreateCodeGenBusinessParam) -> None:
|
||||
"""
|
||||
创建代码生成业务
|
||||
|
||||
@@ -65,7 +65,7 @@ class CRUDGenBusiness(CRUDPlus[GenBusiness]):
|
||||
"""
|
||||
await self.create_model(db, obj)
|
||||
|
||||
async def update(self, db: AsyncSession, pk: int, obj: UpdateGenBusinessParam) -> int:
|
||||
async def update(self, db: AsyncSession, pk: int, obj: UpdateCodeGenBusinessParam) -> int:
|
||||
"""
|
||||
更新代码生成业务
|
||||
|
||||
@@ -96,4 +96,4 @@ class CRUDGenBusiness(CRUDPlus[GenBusiness]):
|
||||
)
|
||||
|
||||
|
||||
gen_business_dao: CRUDGenBusiness = CRUDGenBusiness(GenBusiness)
|
||||
code_gen_business_dao: CRUDCodeGenBusiness = CRUDCodeGenBusiness(CodeGenBusiness)
|
||||
|
||||
+2
-2
@@ -7,7 +7,7 @@ from backend.common.enums import DataBaseType
|
||||
from backend.core.conf import settings
|
||||
|
||||
|
||||
class CRUDGen:
|
||||
class CRUDCodeGen:
|
||||
"""代码生成 CRUD 类"""
|
||||
|
||||
@staticmethod
|
||||
@@ -194,4 +194,4 @@ class CRUDGen:
|
||||
return result.mappings().all()
|
||||
|
||||
|
||||
gen_dao: CRUDGen = CRUDGen()
|
||||
code_gen_dao: CRUDCodeGen = CRUDCodeGen()
|
||||
@@ -3,18 +3,18 @@ from collections.abc import Sequence
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy_crud_plus import CRUDPlus
|
||||
|
||||
from backend.plugin.code_generator.model import GenColumn
|
||||
from backend.plugin.code_generator.model import CodeGenColumn
|
||||
from backend.plugin.code_generator.schema.column import (
|
||||
CreateGenColumnInternalParam,
|
||||
CreateGenColumnParam,
|
||||
UpdateGenColumnParam,
|
||||
CreateCodeGenColumnInternalParam,
|
||||
CreateCodeGenColumnParam,
|
||||
UpdateCodeGenColumnParam,
|
||||
)
|
||||
|
||||
|
||||
class CRUDGenColumn(CRUDPlus[GenColumn]):
|
||||
class CRUDCodeGenColumn(CRUDPlus[CodeGenColumn]):
|
||||
"""代码生成模型列 CRUD 类"""
|
||||
|
||||
async def get(self, db: AsyncSession, pk: int) -> GenColumn | None:
|
||||
async def get(self, db: AsyncSession, pk: int) -> CodeGenColumn | None:
|
||||
"""
|
||||
获取代码生成模型列
|
||||
|
||||
@@ -24,7 +24,7 @@ class CRUDGenColumn(CRUDPlus[GenColumn]):
|
||||
"""
|
||||
return await self.select_model(db, pk)
|
||||
|
||||
async def get_all_by_business(self, db: AsyncSession, business_id: int) -> Sequence[GenColumn]:
|
||||
async def get_all_by_business(self, db: AsyncSession, business_id: int) -> Sequence[CodeGenColumn]:
|
||||
"""
|
||||
获取所有代码生成模型列
|
||||
|
||||
@@ -32,9 +32,9 @@ class CRUDGenColumn(CRUDPlus[GenColumn]):
|
||||
:param business_id: 业务 ID
|
||||
:return:
|
||||
"""
|
||||
return await self.select_models_order(db, sort_columns='sort', gen_business_id=business_id)
|
||||
return await self.select_models_order(db, sort_columns='sort', code_gen_business_id=business_id)
|
||||
|
||||
async def create(self, db: AsyncSession, obj: CreateGenColumnParam, pd_type: str | None) -> None:
|
||||
async def create(self, db: AsyncSession, obj: CreateCodeGenColumnParam, pd_type: str | None) -> None:
|
||||
"""
|
||||
创建代码生成模型列
|
||||
|
||||
@@ -45,7 +45,7 @@ class CRUDGenColumn(CRUDPlus[GenColumn]):
|
||||
"""
|
||||
await self.create_model(db, obj, pd_type=pd_type)
|
||||
|
||||
async def bulk_create(self, db: AsyncSession, objs: list[CreateGenColumnInternalParam]) -> None:
|
||||
async def bulk_create(self, db: AsyncSession, objs: list[CreateCodeGenColumnInternalParam]) -> None:
|
||||
"""
|
||||
批量创建代码生成模型列
|
||||
|
||||
@@ -55,7 +55,7 @@ class CRUDGenColumn(CRUDPlus[GenColumn]):
|
||||
"""
|
||||
await self.create_models(db, objs)
|
||||
|
||||
async def update(self, db: AsyncSession, pk: int, obj: UpdateGenColumnParam, pd_type: str | None) -> int:
|
||||
async def update(self, db: AsyncSession, pk: int, obj: UpdateCodeGenColumnParam, pd_type: str | None) -> int:
|
||||
"""
|
||||
更新代码生成模型列
|
||||
|
||||
@@ -78,4 +78,4 @@ class CRUDGenColumn(CRUDPlus[GenColumn]):
|
||||
return await self.delete_model(db, pk)
|
||||
|
||||
|
||||
gen_column_dao: CRUDGenColumn = CRUDGenColumn(GenColumn)
|
||||
code_gen_column_dao: CRUDCodeGenColumn = CRUDCodeGenColumn(CodeGenColumn)
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
from backend.plugin.code_generator.model.business import GenBusiness as GenBusiness
|
||||
from backend.plugin.code_generator.model.column import GenColumn as GenColumn
|
||||
from backend.plugin.code_generator.model.business import CodeGenBusiness as CodeGenBusiness
|
||||
from backend.plugin.code_generator.model.column import CodeGenColumn as CodeGenColumn
|
||||
|
||||
@@ -5,12 +5,12 @@ from sqlalchemy.orm import Mapped, mapped_column
|
||||
from backend.common.model import Base, UniversalText, id_key
|
||||
|
||||
|
||||
class GenBusiness(Base):
|
||||
class CodeGenBusiness(Base):
|
||||
"""代码生成业务表"""
|
||||
|
||||
__tablename__ = 'gen_business'
|
||||
__tablename__ = 'code_gen_business'
|
||||
__table_args__ = (
|
||||
sa.UniqueConstraint('table_name', 'deleted', name='uk_gen_business_table_name_deleted'),
|
||||
sa.UniqueConstraint('table_name', 'deleted', name='uk_code_gen_business_table_name_deleted'),
|
||||
{'comment': '代码生成业务表'},
|
||||
)
|
||||
|
||||
|
||||
@@ -5,12 +5,12 @@ from sqlalchemy.orm import Mapped, mapped_column
|
||||
from backend.common.model import DataClassBase, UniversalText, id_key
|
||||
|
||||
|
||||
class GenColumn(DataClassBase):
|
||||
class CodeGenColumn(DataClassBase):
|
||||
"""代码生成模型列表"""
|
||||
|
||||
__tablename__ = 'gen_column'
|
||||
__tablename__ = 'code_gen_column'
|
||||
__table_args__ = (
|
||||
sa.UniqueConstraint('gen_business_id', 'name', name='uk_gen_column_business_id_name'),
|
||||
sa.UniqueConstraint('code_gen_business_id', 'name', name='uk_code_gen_column_business_id_name'),
|
||||
{'comment': '代码生成模型列表'},
|
||||
)
|
||||
|
||||
@@ -26,4 +26,4 @@ class GenColumn(DataClassBase):
|
||||
is_nullable: Mapped[bool] = mapped_column(default=False, comment='是否可为空')
|
||||
|
||||
# 逻辑外键
|
||||
gen_business_id: Mapped[int] = mapped_column(sa.BigInteger, default=0, comment='代码生成业务ID')
|
||||
code_gen_business_id: Mapped[int] = mapped_column(sa.BigInteger, default=0, comment='代码生成业务ID')
|
||||
|
||||
@@ -7,7 +7,7 @@ from backend.common.schema import SchemaBase
|
||||
from backend.utils.pattern_validate import is_english_identifier
|
||||
|
||||
|
||||
class GenBusinessSchemaBase(SchemaBase):
|
||||
class CodeGenBusinessSchemaBase(SchemaBase):
|
||||
"""代码生成业务基础模型"""
|
||||
|
||||
app_name: str = Field(description='应用名称(英文)')
|
||||
@@ -32,15 +32,15 @@ class GenBusinessSchemaBase(SchemaBase):
|
||||
return v
|
||||
|
||||
|
||||
class CreateGenBusinessParam(GenBusinessSchemaBase):
|
||||
class CreateCodeGenBusinessParam(CodeGenBusinessSchemaBase):
|
||||
"""创建代码生成业务参数"""
|
||||
|
||||
|
||||
class UpdateGenBusinessParam(GenBusinessSchemaBase):
|
||||
class UpdateCodeGenBusinessParam(CodeGenBusinessSchemaBase):
|
||||
"""更新代码生成业务参数"""
|
||||
|
||||
|
||||
class GetGenBusinessDetail(GenBusinessSchemaBase):
|
||||
class GetCodeGenBusinessDetail(CodeGenBusinessSchemaBase):
|
||||
"""获取代码生成业务详情"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
@@ -4,7 +4,7 @@ from backend.common.schema import SchemaBase
|
||||
from backend.plugin.code_generator.utils.type_conversion import sql_type_to_sqlalchemy
|
||||
|
||||
|
||||
class GenColumnSchemaBase(SchemaBase):
|
||||
class CodeGenColumnSchemaBase(SchemaBase):
|
||||
"""代码生成模型基础模型"""
|
||||
|
||||
name: str = Field(description='列名称')
|
||||
@@ -15,7 +15,7 @@ class GenColumnSchemaBase(SchemaBase):
|
||||
length: int = Field(description='列长度')
|
||||
is_pk: bool = Field(False, description='是否主键')
|
||||
is_nullable: bool = Field(False, description='是否可为空')
|
||||
gen_business_id: int = Field(description='代码生成业务ID')
|
||||
code_gen_business_id: int = Field(description='代码生成业务ID')
|
||||
|
||||
@field_validator('type')
|
||||
@classmethod
|
||||
@@ -24,21 +24,21 @@ class GenColumnSchemaBase(SchemaBase):
|
||||
return sql_type_to_sqlalchemy(v)
|
||||
|
||||
|
||||
class CreateGenColumnParam(GenColumnSchemaBase):
|
||||
class CreateCodeGenColumnParam(CodeGenColumnSchemaBase):
|
||||
"""创建代码生成模型列参数"""
|
||||
|
||||
|
||||
class CreateGenColumnInternalParam(CreateGenColumnParam):
|
||||
class CreateCodeGenColumnInternalParam(CreateCodeGenColumnParam):
|
||||
"""创建代码生成模型列内部参数"""
|
||||
|
||||
pd_type: str | None = Field(None, description='列类型对应的 pydantic 类型')
|
||||
|
||||
|
||||
class UpdateGenColumnParam(GenColumnSchemaBase):
|
||||
class UpdateCodeGenColumnParam(CodeGenColumnSchemaBase):
|
||||
"""更新代码生成模型列参数"""
|
||||
|
||||
|
||||
class GetGenColumnDetail(GenColumnSchemaBase):
|
||||
class GetCodeGenColumnDetail(CodeGenColumnSchemaBase):
|
||||
"""获取代码生成模型列详情"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
@@ -5,16 +5,16 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from backend.common.exception import errors
|
||||
from backend.common.pagination import paging_data
|
||||
from backend.plugin.code_generator.crud.crud_business import gen_business_dao
|
||||
from backend.plugin.code_generator.model import GenBusiness
|
||||
from backend.plugin.code_generator.schema.business import CreateGenBusinessParam, UpdateGenBusinessParam
|
||||
from backend.plugin.code_generator.crud.crud_business import code_gen_business_dao
|
||||
from backend.plugin.code_generator.model import CodeGenBusiness
|
||||
from backend.plugin.code_generator.schema.business import CreateCodeGenBusinessParam, UpdateCodeGenBusinessParam
|
||||
|
||||
|
||||
class GenBusinessService:
|
||||
class CodeGenBusinessService:
|
||||
"""代码生成业务服务类"""
|
||||
|
||||
@staticmethod
|
||||
async def get(*, db: AsyncSession, pk: int) -> GenBusiness:
|
||||
async def get(*, db: AsyncSession, pk: int) -> CodeGenBusiness:
|
||||
"""
|
||||
获取指定 ID 的业务
|
||||
|
||||
@@ -23,13 +23,13 @@ class GenBusinessService:
|
||||
:return:
|
||||
"""
|
||||
|
||||
business = await gen_business_dao.get(db, pk)
|
||||
business = await code_gen_business_dao.get(db, pk)
|
||||
if not business:
|
||||
raise errors.NotFoundError(msg='代码生成业务不存在')
|
||||
return business
|
||||
|
||||
@staticmethod
|
||||
async def get_all(*, db: AsyncSession) -> Sequence[GenBusiness]:
|
||||
async def get_all(*, db: AsyncSession) -> Sequence[CodeGenBusiness]:
|
||||
"""
|
||||
获取所有业务
|
||||
|
||||
@@ -37,7 +37,7 @@ class GenBusinessService:
|
||||
:return:
|
||||
"""
|
||||
|
||||
return await gen_business_dao.get_all(db)
|
||||
return await code_gen_business_dao.get_all(db)
|
||||
|
||||
@staticmethod
|
||||
async def get_list(*, db: AsyncSession, table_name: str) -> dict[str, Any]:
|
||||
@@ -48,11 +48,11 @@ class GenBusinessService:
|
||||
:param table_name: 业务表名
|
||||
:return:
|
||||
"""
|
||||
business_select = await gen_business_dao.get_select(table_name=table_name)
|
||||
business_select = await code_gen_business_dao.get_select(table_name=table_name)
|
||||
return await paging_data(db, business_select)
|
||||
|
||||
@staticmethod
|
||||
async def create(*, db: AsyncSession, obj: CreateGenBusinessParam) -> None:
|
||||
async def create(*, db: AsyncSession, obj: CreateCodeGenBusinessParam) -> None:
|
||||
"""
|
||||
创建业务
|
||||
|
||||
@@ -61,13 +61,13 @@ class GenBusinessService:
|
||||
:return:
|
||||
"""
|
||||
|
||||
business = await gen_business_dao.get_by_name(db, obj.table_name)
|
||||
business = await code_gen_business_dao.get_by_name(db, obj.table_name)
|
||||
if business:
|
||||
raise errors.ConflictError(msg='代码生成业务已存在')
|
||||
await gen_business_dao.create(db, obj)
|
||||
await code_gen_business_dao.create(db, obj)
|
||||
|
||||
@staticmethod
|
||||
async def update(*, db: AsyncSession, pk: int, obj: UpdateGenBusinessParam) -> int:
|
||||
async def update(*, db: AsyncSession, pk: int, obj: UpdateCodeGenBusinessParam) -> int:
|
||||
"""
|
||||
更新业务
|
||||
|
||||
@@ -77,12 +77,12 @@ class GenBusinessService:
|
||||
:return:
|
||||
"""
|
||||
|
||||
business = await gen_business_dao.get(db, pk)
|
||||
business = await code_gen_business_dao.get(db, pk)
|
||||
if not business:
|
||||
raise errors.NotFoundError(msg='代码生成业务不存在')
|
||||
if business.table_name != obj.table_name and await gen_business_dao.get_by_name(db, obj.table_name):
|
||||
if business.table_name != obj.table_name and await code_gen_business_dao.get_by_name(db, obj.table_name):
|
||||
raise errors.ConflictError(msg='代码生成业务已存在')
|
||||
return await gen_business_dao.update(db, pk, obj)
|
||||
return await code_gen_business_dao.update(db, pk, obj)
|
||||
|
||||
@staticmethod
|
||||
async def delete(*, db: AsyncSession, pk: int) -> int:
|
||||
@@ -94,10 +94,10 @@ class GenBusinessService:
|
||||
:return:
|
||||
"""
|
||||
|
||||
business = await gen_business_dao.get(db, pk)
|
||||
business = await code_gen_business_dao.get(db, pk)
|
||||
if not business:
|
||||
raise errors.NotFoundError(msg='代码生成业务不存在')
|
||||
return await gen_business_dao.delete(db, pk)
|
||||
return await code_gen_business_dao.delete(db, pk)
|
||||
|
||||
|
||||
gen_business_service: GenBusinessService = GenBusinessService()
|
||||
code_gen_business_service: CodeGenBusinessService = CodeGenBusinessService()
|
||||
|
||||
+27
-27
@@ -17,21 +17,21 @@ from starlette.concurrency import run_in_threadpool
|
||||
from backend.common.exception import errors
|
||||
from backend.core.conf import settings
|
||||
from backend.core.path_conf import BASE_PATH
|
||||
from backend.plugin.code_generator.crud.crud_business import gen_business_dao
|
||||
from backend.plugin.code_generator.crud.crud_column import gen_column_dao
|
||||
from backend.plugin.code_generator.crud.crud_gen import gen_dao
|
||||
from backend.plugin.code_generator.model import GenBusiness
|
||||
from backend.plugin.code_generator.schema.business import CreateGenBusinessParam
|
||||
from backend.plugin.code_generator.schema.column import CreateGenColumnInternalParam
|
||||
from backend.plugin.code_generator.schema.gen import ImportParam
|
||||
from backend.plugin.code_generator.service.column_service import gen_column_service
|
||||
from backend.plugin.code_generator.crud.crud_business import code_gen_business_dao
|
||||
from backend.plugin.code_generator.crud.crud_code_gen import code_gen_dao
|
||||
from backend.plugin.code_generator.crud.crud_column import code_gen_column_dao
|
||||
from backend.plugin.code_generator.model import CodeGenBusiness
|
||||
from backend.plugin.code_generator.schema.business import CreateCodeGenBusinessParam
|
||||
from backend.plugin.code_generator.schema.column import CreateCodeGenColumnInternalParam
|
||||
from backend.plugin.code_generator.schema.code_gen import ImportParam
|
||||
from backend.plugin.code_generator.service.column_service import code_gen_column_service
|
||||
from backend.plugin.code_generator.utils.format_code import format_python_code
|
||||
from backend.plugin.code_generator.utils.gen_template import gen_template
|
||||
from backend.plugin.code_generator.utils.type_conversion import sql_type_to_pydantic
|
||||
from backend.utils.locks import acquire_distributed_reload_lock
|
||||
|
||||
|
||||
class GenService:
|
||||
class CodeGenService:
|
||||
"""代码生成服务类"""
|
||||
|
||||
@staticmethod
|
||||
@@ -43,7 +43,7 @@ class GenService:
|
||||
:param table_schema: 数据库 schema 名称
|
||||
:return:
|
||||
"""
|
||||
return await gen_dao.get_all_tables(db, table_schema)
|
||||
return await code_gen_dao.get_all_tables(db, table_schema)
|
||||
|
||||
@staticmethod
|
||||
async def import_business_and_model(*, db: AsyncSession, obj: ImportParam) -> None:
|
||||
@@ -57,11 +57,11 @@ class GenService:
|
||||
if settings.ENVIRONMENT != 'dev':
|
||||
raise errors.ForbiddenError(msg='禁止在非开发环境下导入代码生成业务')
|
||||
|
||||
table_info = await gen_dao.get_table(db, obj.table_schema, obj.table_name)
|
||||
table_info = await code_gen_dao.get_table(db, obj.table_schema, obj.table_name)
|
||||
if not table_info:
|
||||
raise errors.NotFoundError(msg='数据库表不存在')
|
||||
|
||||
business_info = await gen_business_dao.get_by_name(db, obj.table_name)
|
||||
business_info = await code_gen_business_dao.get_by_name(db, obj.table_name)
|
||||
if business_info:
|
||||
raise errors.ConflictError(msg='已存在相同数据库表业务')
|
||||
|
||||
@@ -71,8 +71,8 @@ class GenService:
|
||||
if table_info['table_comment'][-1] == '表'
|
||||
else table_info['table_comment'] or table_name.split('_')[-1]
|
||||
)
|
||||
new_business = GenBusiness(
|
||||
**CreateGenBusinessParam(
|
||||
new_business = CodeGenBusiness(
|
||||
**CreateCodeGenBusinessParam(
|
||||
app_name=obj.app,
|
||||
table_name=table_name,
|
||||
doc_comment=doc_comment,
|
||||
@@ -86,13 +86,13 @@ class GenService:
|
||||
db.add(new_business)
|
||||
await db.flush()
|
||||
|
||||
column_info = await gen_dao.get_all_columns(db, obj.table_schema, table_name)
|
||||
gen_columns = []
|
||||
column_info = await code_gen_dao.get_all_columns(db, obj.table_schema, table_name)
|
||||
code_gen_columns = []
|
||||
for column in column_info:
|
||||
column_type = column['column_type'].split('(')[0].upper()
|
||||
pd_type = sql_type_to_pydantic(column_type)
|
||||
gen_columns.append(
|
||||
CreateGenColumnInternalParam(
|
||||
code_gen_columns.append(
|
||||
CreateCodeGenColumnInternalParam(
|
||||
name=column['column_name'],
|
||||
comment=column['column_comment'],
|
||||
type=column_type,
|
||||
@@ -102,14 +102,14 @@ class GenService:
|
||||
else 0,
|
||||
is_pk=column['is_pk'],
|
||||
is_nullable=column['is_nullable'],
|
||||
gen_business_id=new_business.id,
|
||||
code_gen_business_id=new_business.id,
|
||||
pd_type=pd_type,
|
||||
),
|
||||
)
|
||||
await gen_column_dao.bulk_create(db, gen_columns)
|
||||
await code_gen_column_dao.bulk_create(db, code_gen_columns)
|
||||
|
||||
@staticmethod
|
||||
async def _render_tpl_code(*, db: AsyncSession, business: GenBusiness) -> dict[str, str]:
|
||||
async def _render_tpl_code(*, db: AsyncSession, business: CodeGenBusiness) -> dict[str, str]:
|
||||
"""
|
||||
渲染模板代码
|
||||
|
||||
@@ -117,7 +117,7 @@ class GenService:
|
||||
:param business: 业务对象
|
||||
:return:
|
||||
"""
|
||||
gen_models = await gen_column_service.get_columns(db=db, business_id=business.id)
|
||||
gen_models = await code_gen_column_service.get_columns(db=db, business_id=business.id)
|
||||
if not gen_models:
|
||||
raise errors.NotFoundError(msg='代码生成模型表为空')
|
||||
|
||||
@@ -176,7 +176,7 @@ class GenService:
|
||||
:param pk: 业务 ID
|
||||
:return:
|
||||
"""
|
||||
business = await gen_business_dao.get(db, pk)
|
||||
business = await code_gen_business_dao.get(db, pk)
|
||||
if not business:
|
||||
raise errors.NotFoundError(msg='业务不存在')
|
||||
|
||||
@@ -206,7 +206,7 @@ class GenService:
|
||||
:param pk: 业务 ID
|
||||
:return:
|
||||
"""
|
||||
business = await gen_business_dao.get(db, pk)
|
||||
business = await code_gen_business_dao.get(db, pk)
|
||||
if not business:
|
||||
raise errors.NotFoundError(msg='业务不存在')
|
||||
|
||||
@@ -232,7 +232,7 @@ class GenService:
|
||||
if settings.ENVIRONMENT != 'dev':
|
||||
raise errors.ForbiddenError(msg='禁止在非开发环境下生成代码')
|
||||
|
||||
business = await gen_business_dao.get(db, pk)
|
||||
business = await code_gen_business_dao.get(db, pk)
|
||||
if not business:
|
||||
raise errors.NotFoundError(msg='业务不存在')
|
||||
|
||||
@@ -274,7 +274,7 @@ class GenService:
|
||||
:param pk: 业务 ID
|
||||
:return:
|
||||
"""
|
||||
business = await gen_business_dao.get(db, pk)
|
||||
business = await code_gen_business_dao.get(db, pk)
|
||||
if not business:
|
||||
raise errors.NotFoundError(msg='业务不存在')
|
||||
|
||||
@@ -297,4 +297,4 @@ class GenService:
|
||||
return bio
|
||||
|
||||
|
||||
gen_service: GenService = GenService()
|
||||
code_gen_service: CodeGenService = CodeGenService()
|
||||
@@ -5,19 +5,19 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from backend.common.enums import DataBaseType
|
||||
from backend.common.exception import errors
|
||||
from backend.core.conf import settings
|
||||
from backend.plugin.code_generator.crud.crud_business import gen_business_dao
|
||||
from backend.plugin.code_generator.crud.crud_column import gen_column_dao
|
||||
from backend.plugin.code_generator.crud.crud_business import code_gen_business_dao
|
||||
from backend.plugin.code_generator.crud.crud_column import code_gen_column_dao
|
||||
from backend.plugin.code_generator.enums import GenMySQLColumnType, GenPostgreSQLColumnType
|
||||
from backend.plugin.code_generator.model import GenColumn
|
||||
from backend.plugin.code_generator.schema.column import CreateGenColumnParam, UpdateGenColumnParam
|
||||
from backend.plugin.code_generator.model import CodeGenColumn
|
||||
from backend.plugin.code_generator.schema.column import CreateCodeGenColumnParam, UpdateCodeGenColumnParam
|
||||
from backend.plugin.code_generator.utils.type_conversion import sql_type_to_pydantic
|
||||
|
||||
|
||||
class GenColumnService:
|
||||
class CodeGenColumnService:
|
||||
"""代码生成模型列服务类"""
|
||||
|
||||
@staticmethod
|
||||
async def get(*, db: AsyncSession, pk: int) -> GenColumn:
|
||||
async def get(*, db: AsyncSession, pk: int) -> CodeGenColumn:
|
||||
"""
|
||||
获取指定 ID 的模型列
|
||||
|
||||
@@ -26,10 +26,10 @@ class GenColumnService:
|
||||
:return:
|
||||
"""
|
||||
|
||||
column = await gen_column_dao.get(db, pk)
|
||||
column = await code_gen_column_dao.get(db, pk)
|
||||
if not column:
|
||||
raise errors.NotFoundError(msg='代码生成模型列不存在')
|
||||
if not await gen_business_dao.get(db, column.gen_business_id):
|
||||
if not await code_gen_business_dao.get(db, column.code_gen_business_id):
|
||||
raise errors.NotFoundError(msg='代码生成业务不存在')
|
||||
return column
|
||||
|
||||
@@ -44,7 +44,7 @@ class GenColumnService:
|
||||
return types
|
||||
|
||||
@staticmethod
|
||||
async def get_columns(*, db: AsyncSession, business_id: int) -> Sequence[GenColumn]:
|
||||
async def get_columns(*, db: AsyncSession, business_id: int) -> Sequence[CodeGenColumn]:
|
||||
"""
|
||||
获取指定业务的所有模型列
|
||||
|
||||
@@ -53,12 +53,12 @@ class GenColumnService:
|
||||
:return:
|
||||
"""
|
||||
|
||||
if not await gen_business_dao.get(db, business_id):
|
||||
if not await code_gen_business_dao.get(db, business_id):
|
||||
raise errors.NotFoundError(msg='代码生成业务不存在')
|
||||
return await gen_column_dao.get_all_by_business(db, business_id)
|
||||
return await code_gen_column_dao.get_all_by_business(db, business_id)
|
||||
|
||||
@staticmethod
|
||||
async def create(*, db: AsyncSession, obj: CreateGenColumnParam) -> None:
|
||||
async def create(*, db: AsyncSession, obj: CreateCodeGenColumnParam) -> None:
|
||||
"""
|
||||
创建模型列
|
||||
|
||||
@@ -67,18 +67,18 @@ class GenColumnService:
|
||||
:return:
|
||||
"""
|
||||
|
||||
if not await gen_business_dao.get(db, obj.gen_business_id):
|
||||
if not await code_gen_business_dao.get(db, obj.code_gen_business_id):
|
||||
raise errors.NotFoundError(msg='代码生成业务不存在')
|
||||
|
||||
gen_columns = await gen_column_dao.get_all_by_business(db, obj.gen_business_id)
|
||||
if obj.name in [gen_column.name for gen_column in gen_columns]:
|
||||
code_gen_columns = await code_gen_column_dao.get_all_by_business(db, obj.code_gen_business_id)
|
||||
if obj.name in [code_gen_column.name for code_gen_column in code_gen_columns]:
|
||||
raise errors.ForbiddenError(msg='模型列已存在')
|
||||
|
||||
pd_type = sql_type_to_pydantic(obj.type)
|
||||
await gen_column_dao.create(db, obj, pd_type=pd_type)
|
||||
await code_gen_column_dao.create(db, obj, pd_type=pd_type)
|
||||
|
||||
@staticmethod
|
||||
async def update(*, db: AsyncSession, pk: int, obj: UpdateGenColumnParam) -> int:
|
||||
async def update(*, db: AsyncSession, pk: int, obj: UpdateCodeGenColumnParam) -> int:
|
||||
"""
|
||||
更新模型列
|
||||
|
||||
@@ -88,20 +88,20 @@ class GenColumnService:
|
||||
:return:
|
||||
"""
|
||||
|
||||
column = await gen_column_dao.get(db, pk)
|
||||
column = await code_gen_column_dao.get(db, pk)
|
||||
if not column:
|
||||
raise errors.NotFoundError(msg='代码生成模型列不存在')
|
||||
if not await gen_business_dao.get(db, column.gen_business_id):
|
||||
if not await code_gen_business_dao.get(db, column.code_gen_business_id):
|
||||
raise errors.NotFoundError(msg='代码生成业务不存在')
|
||||
if not await gen_business_dao.get(db, obj.gen_business_id):
|
||||
if not await code_gen_business_dao.get(db, obj.code_gen_business_id):
|
||||
raise errors.NotFoundError(msg='代码生成业务不存在')
|
||||
if obj.name != column.name:
|
||||
gen_columns = await gen_column_dao.get_all_by_business(db, obj.gen_business_id)
|
||||
if obj.name in [gen_column.name for gen_column in gen_columns]:
|
||||
code_gen_columns = await code_gen_column_dao.get_all_by_business(db, obj.code_gen_business_id)
|
||||
if obj.name in [code_gen_column.name for code_gen_column in code_gen_columns]:
|
||||
raise errors.ConflictError(msg='模型列名已存在')
|
||||
|
||||
pd_type = sql_type_to_pydantic(obj.type)
|
||||
return await gen_column_dao.update(db, pk, obj, pd_type=pd_type)
|
||||
return await code_gen_column_dao.update(db, pk, obj, pd_type=pd_type)
|
||||
|
||||
@staticmethod
|
||||
async def delete(*, db: AsyncSession, pk: int) -> int:
|
||||
@@ -113,12 +113,12 @@ class GenColumnService:
|
||||
:return:
|
||||
"""
|
||||
|
||||
column = await gen_column_dao.get(db, pk)
|
||||
column = await code_gen_column_dao.get(db, pk)
|
||||
if not column:
|
||||
raise errors.NotFoundError(msg='代码生成模型列不存在')
|
||||
if not await gen_business_dao.get(db, column.gen_business_id):
|
||||
if not await code_gen_business_dao.get(db, column.code_gen_business_id):
|
||||
raise errors.NotFoundError(msg='代码生成业务不存在')
|
||||
return await gen_column_dao.delete(db, pk)
|
||||
return await code_gen_column_dao.delete(db, pk)
|
||||
|
||||
|
||||
gen_column_service: GenColumnService = GenColumnService()
|
||||
code_gen_column_service: CodeGenColumnService = CodeGenColumnService()
|
||||
|
||||
@@ -2,5 +2,5 @@ delete from sys_menu where name in ('AddGenCodeBusiness', 'EditGenCodeBusiness',
|
||||
|
||||
delete from sys_menu where name = 'PluginCodeGenerator';
|
||||
|
||||
drop table if exists gen_column;
|
||||
drop table if exists gen_business;
|
||||
drop table if exists code_gen_column;
|
||||
drop table if exists code_gen_business;
|
||||
|
||||
@@ -2,5 +2,5 @@ delete from sys_menu where name in ('AddGenCodeBusiness', 'EditGenCodeBusiness',
|
||||
|
||||
delete from sys_menu where name = 'PluginCodeGenerator';
|
||||
|
||||
drop table if exists gen_column;
|
||||
drop table if exists gen_business;
|
||||
drop table if exists code_gen_column;
|
||||
drop table if exists code_gen_business;
|
||||
|
||||
@@ -14,10 +14,10 @@ values
|
||||
('导入', 'ImportGenCode', null, 0, null, 2, null, 'codegen:table:import', 1, 0, 1, '', null, @codegen_menu_id, now(), null),
|
||||
('写入', 'WriteGenCode', null, 0, null, 2, null, 'codegen:local:write', 1, 0, 1, '', null, @codegen_menu_id, now(), null);
|
||||
|
||||
insert into gen_business (id, app_name, table_name, doc_comment, table_comment, class_name, schema_name, filename, datetime_mixin, api_version, gen_path, remark, created_time, updated_time)
|
||||
insert into code_gen_business (id, app_name, table_name, doc_comment, table_comment, class_name, schema_name, filename, datetime_mixin, api_version, gen_path, remark, created_time, updated_time)
|
||||
values (1, 'test', 'sys_opera_log', '操作日志表', '操作日志表', 'SysOperaLog', 'SysOperaLog', 'sys_opera_log', true, 'v1', null, null, '2025-12-15 15:30:33', null);
|
||||
|
||||
insert into gen_column (id, name, comment, type, pd_type, `default`, sort, `length`, is_pk, is_nullable, gen_business_id)
|
||||
insert into code_gen_column (id, name, comment, type, pd_type, `default`, sort, `length`, is_pk, is_nullable, code_gen_business_id)
|
||||
values
|
||||
(1, 'trace_id', '请求跟踪 ID', 'String', 'str', null, 2, 32, false, false, 1),
|
||||
(2, 'username', '用户名', 'String', 'str', null, 3, 64, false, true, 1),
|
||||
|
||||
@@ -12,10 +12,10 @@ values
|
||||
(2049629108257816587, '导入', 'ImportGenCode', null, 0, null, 2, null, 'codegen:table:import', 1, 0, 1, '', null, 2049629108257816580, now(), null),
|
||||
(2049629108257816588, '写入', 'WriteGenCode', null, 0, null, 2, null, 'codegen:local:write', 1, 0, 1, '', null, 2049629108257816580, now(), null);
|
||||
|
||||
insert into gen_business (id, app_name, table_name, doc_comment, table_comment, class_name, schema_name, filename, datetime_mixin, api_version, gen_path, remark, created_time, updated_time)
|
||||
insert into code_gen_business (id, app_name, table_name, doc_comment, table_comment, class_name, schema_name, filename, datetime_mixin, api_version, gen_path, remark, created_time, updated_time)
|
||||
values (2112248797819043840, 'test', 'sys_opera_log', '操作日志表', '操作日志表', 'SysOperaLog', 'SysOperaLog', 'sys_opera_log', true, 'v1', null, null, '2025-12-15 15:30:33', null);
|
||||
|
||||
insert into gen_column (id, name, comment, type, pd_type, `default`, sort, `length`, is_pk, is_nullable, gen_business_id)
|
||||
insert into code_gen_column (id, name, comment, type, pd_type, `default`, sort, `length`, is_pk, is_nullable, code_gen_business_id)
|
||||
values
|
||||
(2112248797881958400, 'trace_id', '请求跟踪 ID', 'String', 'str', null, 2, 32, false, false, 2112248797819043840),
|
||||
(2112248797944872960, 'username', '用户名', 'String', 'str', null, 3, 64, false, true, 2112248797819043840),
|
||||
|
||||
@@ -2,7 +2,7 @@ delete from sys_menu where name in ('AddGenCodeBusiness', 'EditGenCodeBusiness',
|
||||
|
||||
delete from sys_menu where name = 'PluginCodeGenerator';
|
||||
|
||||
drop table if exists gen_column;
|
||||
drop table if exists gen_business;
|
||||
drop table if exists code_gen_column;
|
||||
drop table if exists code_gen_business;
|
||||
|
||||
select setval(pg_get_serial_sequence('sys_menu', 'id'), coalesce(max(id), 0) + 1, true) from sys_menu;
|
||||
|
||||
@@ -2,5 +2,5 @@ delete from sys_menu where name in ('AddGenCodeBusiness', 'EditGenCodeBusiness',
|
||||
|
||||
delete from sys_menu where name = 'PluginCodeGenerator';
|
||||
|
||||
drop table if exists gen_column;
|
||||
drop table if exists gen_business;
|
||||
drop table if exists code_gen_column;
|
||||
drop table if exists code_gen_business;
|
||||
|
||||
@@ -20,10 +20,10 @@ end $$;
|
||||
|
||||
select setval(pg_get_serial_sequence('sys_menu', 'id'), coalesce(max(id), 0) + 1, true) from sys_menu;
|
||||
|
||||
insert into gen_business (id, app_name, table_name, doc_comment, table_comment, class_name, schema_name, filename, datetime_mixin, api_version, gen_path, remark, created_time, updated_time)
|
||||
insert into code_gen_business (id, app_name, table_name, doc_comment, table_comment, class_name, schema_name, filename, datetime_mixin, api_version, gen_path, remark, created_time, updated_time)
|
||||
values (1, 'test', 'sys_opera_log', '操作日志表', '操作日志表', 'SysOperaLog', 'SysOperaLog', 'sys_opera_log', true, 'v1', null, null, '2025-12-15 15:30:33', null);
|
||||
|
||||
insert into gen_column (id, name, comment, type, pd_type, "default", sort, "length", is_pk, is_nullable, gen_business_id)
|
||||
insert into code_gen_column (id, name, comment, type, pd_type, "default", sort, "length", is_pk, is_nullable, code_gen_business_id)
|
||||
values
|
||||
(1, 'trace_id', '请求跟踪 ID', 'String', 'str', null, 2, 32, false, false, 1),
|
||||
(2, 'username', '用户名', 'String', 'str', null, 3, 64, false, true, 1),
|
||||
@@ -45,5 +45,5 @@ values
|
||||
(18, 'cost_time', '请求耗时(ms)', 'String', 'str', null, 19, 0, false, false, 1),
|
||||
(19, 'opera_time', '操作时间', 'String', 'str', null, 20, 0, false, false, 1);
|
||||
|
||||
select setval(pg_get_serial_sequence('gen_business', 'id'),coalesce(max(id), 0) + 1, true) from gen_business;
|
||||
select setval(pg_get_serial_sequence('gen_column', 'id'),coalesce(max(id), 0) + 1, true) from gen_column;
|
||||
select setval(pg_get_serial_sequence('code_gen_business', 'id'),coalesce(max(id), 0) + 1, true) from code_gen_business;
|
||||
select setval(pg_get_serial_sequence('code_gen_column', 'id'),coalesce(max(id), 0) + 1, true) from code_gen_column;
|
||||
|
||||
@@ -12,10 +12,10 @@ values
|
||||
(2049629108257816587, '导入', 'ImportGenCode', null, 0, null, 2, null, 'codegen:table:import', 1, 0, 1, '', null, 2049629108257816580, now(), null),
|
||||
(2049629108257816588, '写入', 'WriteGenCode', null, 0, null, 2, null, 'codegen:local:write', 1, 0, 1, '', null, 2049629108257816580, now(), null);
|
||||
|
||||
insert into gen_business (id, app_name, table_name, doc_comment, table_comment, class_name, schema_name, filename, datetime_mixin, api_version, gen_path, remark, created_time, updated_time)
|
||||
insert into code_gen_business (id, app_name, table_name, doc_comment, table_comment, class_name, schema_name, filename, datetime_mixin, api_version, gen_path, remark, created_time, updated_time)
|
||||
values (2112248797819043840, 'test', 'sys_opera_log', '操作日志表', '操作日志表', 'SysOperaLog', 'SysOperaLog', 'sys_opera_log', true, 'v1', null, null, '2025-12-15 15:30:33', null);
|
||||
|
||||
insert into gen_column (id, name, comment, type, pd_type, "default", sort, "length", is_pk, is_nullable, gen_business_id)
|
||||
insert into code_gen_column (id, name, comment, type, pd_type, "default", sort, "length", is_pk, is_nullable, code_gen_business_id)
|
||||
values
|
||||
(2112248797881958400, 'trace_id', '请求跟踪 ID', 'String', 'str', null, 2, 32, false, false, 2112248797819043840),
|
||||
(2112248797944872960, 'username', '用户名', 'String', 'str', null, 3, 64, false, true, 2112248797819043840),
|
||||
|
||||
@@ -49,8 +49,8 @@ class Get{{ schema_name }}Detail({{ schema_name }}SchemaBase):
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
id: int = Field(description='主键 ID')
|
||||
{% if datetime_mixin %}
|
||||
created_time: datetime
|
||||
updated_time: datetime | None = None
|
||||
created_time: datetime = Field(description='创建时间')
|
||||
updated_time: datetime | None = Field(None, description='更新时间')
|
||||
{% endif %}
|
||||
|
||||
@@ -10,6 +10,8 @@ from backend.common.pagination import paging_data
|
||||
|
||||
|
||||
class {{ class_name }}Service:
|
||||
"""{{ doc_comment }}服务类"""
|
||||
|
||||
@staticmethod
|
||||
async def get(*, db: AsyncSession, pk: int) -> {{ class_name }}:
|
||||
"""
|
||||
@@ -25,7 +27,7 @@ class {{ class_name }}Service:
|
||||
return {{ table_name }}
|
||||
|
||||
@staticmethod
|
||||
async def get_list(db: AsyncSession) -> dict[str, Any]:
|
||||
async def get_list(*, db: AsyncSession) -> dict[str, Any]:
|
||||
"""
|
||||
获取{{ doc_comment }}列表
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ from pydantic.alias_generators import to_pascal
|
||||
|
||||
from backend.common.enums import PrimaryKeyType
|
||||
from backend.core.conf import settings
|
||||
from backend.plugin.code_generator.model import GenBusiness, GenColumn
|
||||
from backend.plugin.code_generator.model import CodeGenBusiness, CodeGenColumn
|
||||
from backend.plugin.code_generator.path_conf import JINJA2_TEMPLATE_DIR
|
||||
from backend.plugin.code_generator.utils.type_conversion import sql_type_to_sqlalchemy_name
|
||||
from backend.utils.snowflake import snowflake
|
||||
@@ -31,17 +31,17 @@ class GenTemplate:
|
||||
获取 Jinja2 模板对象
|
||||
|
||||
:param jinja_file: Jinja2 模板文件路径
|
||||
:return: Template 对象
|
||||
:return:
|
||||
"""
|
||||
return self.env.get_template(jinja_file)
|
||||
|
||||
@staticmethod
|
||||
def get_template_path_mapping(business: GenBusiness) -> dict[str, str]:
|
||||
def get_template_path_mapping(business: CodeGenBusiness) -> dict[str, str]:
|
||||
"""
|
||||
获取模板文件到生成文件的路径映射
|
||||
|
||||
:param business: 代码生成业务对象
|
||||
:return: {模板路径: 生成文件路径}
|
||||
:return:
|
||||
"""
|
||||
app_name = business.app_name
|
||||
filename = business.filename
|
||||
@@ -59,12 +59,12 @@ class GenTemplate:
|
||||
f'sql/postgresql/init{pk_suffix}.jinja': f'{app_name}/sql/postgresql/init{pk_suffix}.sql',
|
||||
}
|
||||
|
||||
def get_init_files(self, business: GenBusiness) -> dict[str, str]:
|
||||
def get_init_files(self, business: CodeGenBusiness) -> dict[str, str]:
|
||||
"""
|
||||
获取需要生成的 __init__.py 文件及其内容
|
||||
|
||||
:param business: 业务对象
|
||||
:return: {相对路径: 文件内容}
|
||||
:return:
|
||||
"""
|
||||
app_name = business.app_name
|
||||
table_name = business.table_name
|
||||
@@ -84,7 +84,9 @@ class GenTemplate:
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def get_vars(business: GenBusiness, models: Sequence[GenColumn]) -> dict[str, str | Sequence[GenColumn]]:
|
||||
def get_vars(
|
||||
business: CodeGenBusiness, models: Sequence[CodeGenColumn]
|
||||
) -> dict[str, str | Sequence[CodeGenColumn]]:
|
||||
"""
|
||||
获取模板变量
|
||||
|
||||
|
||||
Reference in New Issue
Block a user