Skip to content

Commit

Permalink
✨ feat: add generateEntityRelationSeparateClass
Browse files Browse the repository at this point in the history
  • Loading branch information
gylove1994 committed Oct 11, 2024
1 parent 8552a3f commit 1d302e1
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/generateEntity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { strings } from "@angular-devkit/core";
import { classify } from "@angular-devkit/core/src/utils/strings";
import type { Model, Schema } from "@mrleebo/prisma-ast";
import chalk from "chalk";
import { getRelation } from "./utils/getRelation";
import {
importApiProperty,
Expand Down Expand Up @@ -30,13 +29,26 @@ export function generateEntity(model: Model) {
.join("");
const content = `${imports}\nexport class ${strings.classify(
model.name,
)} {\n${propertiesContent}}\n`;
)} {\n${propertiesContent}}\n\n${generateEntityRelationSeparateClass(model)}`;
return {
name: `${strings.camelize(model.name)}Entity.ts`,
content,
};
}

export function generateEntityRelationSeparateClass(model: Model) {
const relationsName = getRelation(model);
const relations = model.properties
.filter(
(v: any) => v.type === "field" && relationsName.includes(v.fieldType),
)
.map(
(v: any) =>
`export class ${strings.classify(`${model.name}Relation${v.fieldType}`)}{\n${swaggerMap(v) + propertyMap(v)}}\n`,
);
return relations.join("\n");
}

export function generateEntityFile(
prisma: Schema,
outputPath: string,
Expand Down Expand Up @@ -104,7 +116,7 @@ export function generatePickEntity(model: Model) {

// 添加新的函数,使用泛型来确保返回类型的正确性
const pickTypeFunction = `
export function ${entityName}PickType<T extends keyof ${entityName}>(keys: T[]) {
export function ${entityName}PickType<T extends keyof ${entityName}>(keys: T[]):any {
const sortedKeys = keys.sort().join('_');
switch (sortedKeys) {
${combinations
Expand Down

0 comments on commit 1d302e1

Please sign in to comment.