-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa4c0fa
commit a0db24c
Showing
16 changed files
with
580 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import type { Model, Schema } from "@mrleebo/prisma-ast"; | ||
import { strings } from "@angular-devkit/core"; | ||
import { mkFile } from "./utils/mkFile"; | ||
|
||
const controllerTemplate = ` | ||
import { Controller } from '@nestjs/common'; | ||
import { {_@modelNameCapitalize@_}Service } from './{_@modelName@_}.service'; | ||
import { {_@modelNameCapitalize@_}IdExistDto, {_@modelNameCapitalize@_}CreateDto, {_@modelNameCapitalize@_}UpdateDto, Pagination{_@modelNameCapitalize@_}Dto } from './{_@modelName@_}.dtos'; | ||
import { ApiResponse, ApiTags, ApiOperation, ApiExtraModels } from '@nestjs/swagger'; | ||
import { HttpCode, Query, Body, Post, Get } from '@nestjs/common'; | ||
import { {_@modelNameCapitalize@_}FindAllResponse, {_@modelNameCapitalize@_}FindOneResponse, {_@modelNameCapitalize@_}CreateResponse, {_@modelNameCapitalize@_}UpdateResponse, {_@modelNameCapitalize@_}DeleteResponse, {_@modelNameCapitalize@_}ListResponse } from './{_@modelName@_}.types'; | ||
@ApiTags('{_@modelNameCapitalize@_}') | ||
@Controller('{_@modelName@_}') | ||
@ApiExtraModels({_@modelNameCapitalize@_}FindAllResponse, {_@modelNameCapitalize@_}FindOneResponse, {_@modelNameCapitalize@_}CreateResponse, {_@modelNameCapitalize@_}UpdateResponse, {_@modelNameCapitalize@_}DeleteResponse, {_@modelNameCapitalize@_}ListResponse) | ||
export class {_@modelNameCapitalize@_}Controller { | ||
constructor(private readonly {_@modelName@_}Service: {_@modelNameCapitalize@_}Service) {} | ||
@Get('all') | ||
@HttpCode(200) | ||
@ApiOperation({ summary: '获取所有{_@modelName@_}' }) | ||
@ApiResponse({ | ||
status: 200, | ||
type: {_@modelNameCapitalize@_}FindAllResponse, | ||
}) | ||
findAll() { | ||
return this.{_@modelName@_}Service.findAll(); | ||
} | ||
@Get('detail') | ||
@HttpCode(200) | ||
@ApiOperation({ summary: '获取{_@modelName@_}详情' }) | ||
@ApiResponse({ | ||
status: 200, | ||
type: {_@modelNameCapitalize@_}FindOneResponse, | ||
}) | ||
findOne(@Query() dto: {_@modelNameCapitalize@_}IdExistDto) { | ||
return this.{_@modelName@_}Service.findOne(dto); | ||
} | ||
@Post('create') | ||
@HttpCode(200) | ||
@ApiOperation({ summary: '创建{_@modelName@_}' }) | ||
@ApiResponse({ | ||
status: 200, | ||
type: {_@modelNameCapitalize@_}CreateResponse, | ||
}) | ||
create(@Body() dto: {_@modelNameCapitalize@_}CreateDto) { | ||
return this.{_@modelName@_}Service.create(dto); | ||
} | ||
@Post('update') | ||
@HttpCode(200) | ||
@ApiOperation({ summary: '更新{_@modelName@_}' }) | ||
@ApiResponse({ | ||
status: 200, | ||
type: {_@modelNameCapitalize@_}UpdateResponse, | ||
}) | ||
update(@Body() dto: {_@modelNameCapitalize@_}UpdateDto) { | ||
return this.{_@modelName@_}Service.update(dto); | ||
} | ||
@Post('delete') | ||
@HttpCode(200) | ||
@ApiOperation({ summary: '删除{_@modelName@_}' }) | ||
@ApiResponse({ | ||
status: 200, | ||
type: {_@modelNameCapitalize@_}DeleteResponse, | ||
}) | ||
delete(@Body() dto: {_@modelNameCapitalize@_}IdExistDto) { | ||
return this.{_@modelName@_}Service.delete(dto); | ||
} | ||
@Get('list') | ||
@ApiOperation({ summary: '分页获取{_@modelName@_}列表' }) | ||
@ApiResponse({ | ||
status: 200, | ||
type: {_@modelNameCapitalize@_}ListResponse, | ||
}) | ||
@HttpCode(200) | ||
async list(@Query() dto: Pagination{_@modelNameCapitalize@_}Dto) { | ||
return await this.{_@modelName@_}Service.list(dto) | ||
} | ||
// for CORS | ||
// @Options('create') | ||
// createOp() {} | ||
// @Options('update') | ||
// updateOp() {} | ||
// @Options('delete') | ||
// deleteOp() {} | ||
} | ||
`; | ||
|
||
export function generateNestController(prisma: Schema) { | ||
const entityList = prisma.list | ||
.filter((item) => item.type === "model") | ||
.map((item) => { | ||
const entity = item as Model; | ||
const modelNameCamelize = strings.camelize(entity.name); | ||
const modelNameCapitalize = strings.capitalize(modelNameCamelize); | ||
return { | ||
name: modelNameCamelize, | ||
content: controllerTemplate | ||
.replaceAll(/{_@modelName@_}/g, modelNameCamelize) | ||
.replaceAll(/{_@modelNameCapitalize@_}/g, modelNameCapitalize), | ||
}; | ||
}); | ||
return entityList; | ||
} | ||
|
||
export function generateNestControllerFile( | ||
prisma: Schema, | ||
outputPath: string, | ||
dryRun: boolean, | ||
) { | ||
const entityList = generateNestController(prisma); | ||
for (const entity of entityList) { | ||
mkFile( | ||
`${outputPath}/${entity.name}`, | ||
`${entity.name}.controller.ts`, | ||
entity.content, | ||
dryRun, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { strings } from "@angular-devkit/core"; | ||
import type { Model, Schema } from "@mrleebo/prisma-ast"; | ||
import { propertyMap } from "./utils/propertyMap"; | ||
import { mkFile } from "./utils/mkFile"; | ||
import { getRelation } from "./utils/getRelation"; | ||
import { importFile } from "./utils/import"; | ||
|
||
const dtoTemplate = ` | ||
import { ApiProperty, IntersectionType, OmitType, PartialType } from '@nestjs/swagger'; | ||
import { IsNotEmpty, IsString, IsNumber, IsDate, IsBoolean, IsArray, IsObject, IsOptional } from 'class-validator'; | ||
{_@imports@_} | ||
export class Pagination{_@modelNameCapitalize@_}Dto { | ||
@ApiProperty({ description: '分页大小' }) | ||
pageSize: number; | ||
@ApiProperty({ description: '当前页' }) | ||
page: number; | ||
} | ||
export class {_@modelNameCapitalize@_}IdExistDto { | ||
@ApiProperty({ description: '{_@modelName@_}ID' }) | ||
@IsNotEmpty({ message: '{_@modelName@_}ID不能为空' }) | ||
// @IsUUID('4', { message: '{_@modelName@_}ID类型错误, 正确类型为uuid' }) | ||
@IsString({ message: '{_@modelName@_}ID类型错误, 正确类型为string' }) | ||
// @IsExistInDataBase({_@modelNameCapitalize@_},'id') | ||
id: string; | ||
} | ||
export class {_@modelNameCapitalize@_}CreateDto { | ||
{_@CreateDtoFields@_} | ||
} | ||
export class {_@modelNameCapitalize@_}UpdateDto extends IntersectionType({_@modelNameCapitalize@_}IdExistDto, OmitType(PartialType({_@modelNameCapitalize@_}CreateDto), [])) {} | ||
`; | ||
|
||
export function generateNestDto(model: Schema) { | ||
const fields = model.list | ||
.filter((field) => field.type === "model") | ||
.map((v) => { | ||
const field = v as Model; | ||
const modelNameCamelize = strings.camelize(field.name); | ||
const modelNameCapitalize = strings.capitalize(modelNameCamelize); | ||
const imports = getRelation(field) | ||
.map((v) => importFile(v, true)) | ||
.join("\n"); | ||
const createdDtoField = v.properties | ||
.filter((v) => v.type === "field") | ||
.map((v) => { | ||
return propertyMap(v); | ||
}) | ||
.join("\n"); | ||
return { | ||
name: `${modelNameCamelize}`, | ||
content: dtoTemplate | ||
.replaceAll("{_@modelName@_}", modelNameCamelize) | ||
.replaceAll("{_@modelNameCapitalize@_}", modelNameCapitalize) | ||
.replaceAll("{_@CreateDtoFields@_}", createdDtoField) | ||
.replaceAll("{_@imports@_}", imports), | ||
}; | ||
}); | ||
return fields; | ||
} | ||
|
||
export function generateNestDtoFile( | ||
prisma: Schema, | ||
outputPath: string, | ||
dryRun: boolean, | ||
) { | ||
const entityList = generateNestDto(prisma); | ||
for (const entity of entityList) { | ||
mkFile( | ||
`${outputPath}/${entity.name}`, | ||
`${entity.name}.dtos.ts`, | ||
entity.content, | ||
dryRun, | ||
); | ||
} | ||
} |
Oops, something went wrong.