如何解决转换TypeOrm实体以响应友好的Typescript DTO
我想将大约200个typeOrm实体引用到我的Typescript React代码中。
这是一个monorepo项目,例如:
-|-后 |-共享 |-前
我的实体是这样的:
import {Column,Entity,JoinColumn,ManyToOne,OneToOne} from "typeorm";
import {childRelationOptions,parentEagerRelationOptions} from "../typeorm-helper/src";
import {Nationality} from "./cold-data/nationality.entity";
import {Situation} from "./cold-data/situation.entity";
import {Document} from "./document.entity";
import {
IsDate,IsMobilePhone,IsNotEmpty,IsNotEmptyObject,IsOptional,IsString,Matches,ValidateIf,ValidateNested
} from "class-validator";
import {ExportFor} from "../export/src";
import {UserRoleEnum} from "./User-role.enum";
import {VersionedBaseWithId} from "./versioned-base-with-id.entity";
import {CandidateFile} from "./candidate-file.entity";
import {DocumentTypeEnum} from "./cold-data/document-type.entity";
import {alwaysValidate,validateForSave,validateForSubmit} from "../class-validator-test-helper/src";
import {Empty,Fake,getEmpty,getFake} from "../context";
/**
* Contains all candidate personal data
*/
import * as faker from "faker";
@Entity()
export class Profile extends VersionedBaseWithId {
@OneToOne(() => CandidateFile,(e: CandidateFile) => e.profile,childRelationOptions)
@JoinColumn()
public candidateFile: CandidateFile
// noinspection JSUnusedLocalSymbols
@Fake((context) => {
const d: Document = context.japd || getFake(new Document(),context)
d.type.id = DocumentTypeEnum.JAPD
return d
})
@Empty((context) => {
const d: Document = getEmpty(new Document(),context)
d.type.id = DocumentTypeEnum.JAPD
return d
})
@ExportFor([UserRoleEnum.SECRETARY])
@ValidateIf(o => o.nationality?.name === "Française",validateForSubmit)
@ValidateNested(alwaysValidate)
@IsNotEmptyObject(validateForSubmit)
@IsOptional(validateForSave)
@ManyToOne(type => Document,parentEagerRelationOptions)
public JAPDDocument: Document
@Fake(faker.random.boolean)
@ExportFor([UserRoleEnum.SECRETARY])
@IsOptional(validateForSave)
@IsNotEmpty(validateForSubmit)
@Column({nullable: true})
funded: boolean
@Fake(faker.random.boolean)
@ExportFor([UserRoleEnum.SECRETARY])
@IsOptional(validateForSave)
@IsNotEmpty(validateForSubmit)
@Column({nullable: true})
thirdPartyTime: boolean
@Fake(faker.address.streetAddress)
@ExportFor([UserRoleEnum.SECRETARY])
@IsOptional(validateForSave)
@IsNotEmpty(validateForSubmit)
@IsString(alwaysValidate)
@Column({nullable: true})
address1: string
@Fake(faker.address.streetAddress)
@ExportFor([UserRoleEnum.SECRETARY])
@IsOptional(alwaysValidate)
@IsString({always: true})
@Column({nullable: true})
address2: string
@Fake(faker.address.zipCode,'#####')
@ExportFor([UserRoleEnum.SECRETARY])
@Matches(/^[0-9\- a-zA-Z]+|[0-9]{0}$/,{always: true})
@IsOptional(validateForSave)
@Column({nullable: true})
zipCode: string
@Fake(faker.address.city)
@ExportFor([UserRoleEnum.SECRETARY])
@IsOptional(validateForSave)
@IsString(alwaysValidate)
@Column({nullable: true})
city: string
@Fake(faker.phone.phoneNumber,'065######')
@ExportFor([UserRoleEnum.SECRETARY])
@IsOptional(validateForSave)
@IsNotEmpty(validateForSubmit)
@IsOptional(validateForSave)
@IsMobilePhone(undefined,undefined,{always: true})
@Column({nullable: true})
phone: string
@Fake(faker.date.past,10,new Date(2001,1))
@ExportFor([UserRoleEnum.SECRETARY])
@IsDate({always: true})
@IsOptional(validateForSave)
@IsNotEmpty(validateForSubmit)
@IsOptional(validateForSave)
@Column({nullable: true})
birthDate: Date
@Fake(faker.address.city)
@ExportFor([UserRoleEnum.SECRETARY])
@IsString({always: true})
@IsOptional(validateForSave)
@IsNotEmpty(validateForSubmit)
@IsOptional(alwaysValidate)
@Column({nullable: true})
birthPlace: string
@Fake((context) => {
return context.nationality
})
@ExportFor([UserRoleEnum.SECRETARY])
@ValidateNested()
@IsNotEmptyObject(validateForSubmit)
@IsOptional(validateForSave)
@ManyToOne(() => Nationality,{cascade: false})
nationality: Nationality
@Fake((context) => {
return context.situation
})
@ExportFor([UserRoleEnum.SECRETARY])
@ValidateNested(alwaysValidate)
@IsNotEmptyObject(validateForSubmit)
@IsOptional(validateForSave)
@ManyToOne(() => Situation,{cascade: false})
situation: Situation
@Fake((context) => {
const d: Document = context.avatarDocument || getFake(new Document(),context)
d.type.id = DocumentTypeEnum.AVATAR
return d
})
@Empty((context) => {
const d: Document = getEmpty(new Document(),context)
d.type.id = DocumentTypeEnum.AVATAR
return d
})
@ExportFor([UserRoleEnum.SECRETARY])
@ValidateNested(alwaysValidate)
@IsNotEmptyObject(validateForSubmit)
@IsOptional(validateForSave)
@ManyToOne(() => Document,parentEagerRelationOptions)
avatarDocument: Document
@Fake((context) => {
const d: Document = context.cv || getFake(new Document(),context)
d.type.id = DocumentTypeEnum.CV
return d
})
@Empty((context) => {
const d: Document = getEmpty(new Document(),context)
d.type.id = DocumentTypeEnum.CV
return d
})
@ExportFor([UserRoleEnum.SECRETARY])
@ValidateNested(alwaysValidate)
@IsNotEmptyObject(validateForSubmit)
@IsOptional(validateForSave)
@ManyToOne(() => Document,parentEagerRelationOptions)
cvDocument: Document
// noinspection JSUnusedLocalSymbols
@Fake((context) => {
const d: Document = context.thirdPartyTimeDocument || getFake(new Document(),context)
d.type.id = DocumentTypeEnum.THIRD_PARTY_TIME
return d
})
@Empty((context) => {
const d: Document = getEmpty(new Document(),context)
d.type.id = DocumentTypeEnum.THIRD_PARTY_TIME
return d
})
@ManyToOne(type => Document,parentEagerRelationOptions)
@ExportFor([UserRoleEnum.SECRETARY])
@ValidateIf((o: Profile) => o.thirdPartyTime,validateForSubmit)
@ValidateNested(alwaysValidate)
@IsNotEmptyObject(validateForSubmit)
@IsOptional(validateForSave)
thirdPartyTimeDocument: Document
@Fake((context) => {
const d: Document = context.idCardDocument || getFake(new Document(),context)
d.type.id = DocumentTypeEnum.ID_CARD
return d
})
@Empty((context) => {
const d: Document = getEmpty(new Document(),context)
d.type.id = DocumentTypeEnum.ID_CARD
return d
})
@ExportFor([UserRoleEnum.SECRETARY])
@ValidateNested(alwaysValidate)
@IsNotEmptyObject(validateForSubmit)
@IsOptional(validateForSave)
@ManyToOne(() => Document,parentEagerRelationOptions)
idCardDocument: Document
constructor() {
super();
}
}
我想为每个实体生成一个DTO,目标可能是拥有类似.d.ts文件的内容:
import { Nationality } from "./cold-data/nationality.entity";
import { Situation } from "./cold-data/situation.entity";
import { Document } from "./document.entity";
import { VersionedBaseWithId } from "./versioned-base-with-id.entity";
import { CandidateFile } from "./candidate-file.entity";
export declare class Profile extends VersionedBaseWithId {
candidateFile: CandidateFile;
JAPDDocument: Document;
funded: boolean;
thirdPartyTime: boolean;
address1: string;
address2: string;
zipCode: string;
city: string;
phone: string;
birthDate: Date;
birthPlace: string;
nationality: Nationality;
situation: Situation;
avatarDocument: Document;
cvDocument: Document;
thirdPartyTimeDocument: Document;
idCardDocument: Document;
constructor();
}
如何与React应用共享没有装饰器的代码?
解决方法
我尝试过:
"generate:dto": "yarn workspace back build && rm -rf dto && find ../back/dist/ -name '*.entity.d.ts' | cpio -pdm . | mv back/dist dto",
它生成了一个包含所有d.ts文件的文件夹
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。